mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-09 05:16:21 +10:00
去除tomcat,不再使用第三方容器,而是直接采用jdk自带的HttpServer来做http服务
This commit is contained in:
@@ -3,6 +3,7 @@ package com.mars.common.base.config;
|
||||
import com.mars.common.base.config.model.CrossDomainConfig;
|
||||
import com.mars.common.base.config.model.FileUploadConfig;
|
||||
import com.mars.common.base.config.model.JedisConfig;
|
||||
import com.mars.common.base.config.model.ThreadPoolConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -29,6 +30,14 @@ public abstract class MarsConfig {
|
||||
return 86400;
|
||||
}
|
||||
|
||||
/**
|
||||
* 线程池配置
|
||||
* @return 线程池配置
|
||||
*/
|
||||
public ThreadPoolConfig getThreadPoolConfig(){
|
||||
return new ThreadPoolConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件大小配置
|
||||
* @return 上传文件大小配置
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.mars.common.base.config.model;
|
||||
|
||||
/**
|
||||
* 线程池配置
|
||||
*/
|
||||
public class ThreadPoolConfig {
|
||||
|
||||
/**
|
||||
* TCP最大连接池
|
||||
*/
|
||||
private int backLog = 50;
|
||||
|
||||
/**
|
||||
* 最大线程数
|
||||
*/
|
||||
private int maxPoolSize = 2000;
|
||||
|
||||
/**
|
||||
* 核心线程数
|
||||
*/
|
||||
private int corePoolSize = 5;
|
||||
|
||||
/**
|
||||
* 最大等待时长,默认20秒
|
||||
*/
|
||||
private int keepAliveTime = 20;
|
||||
|
||||
public int getBackLog() {
|
||||
if(backLog < 1){
|
||||
return 50;
|
||||
}
|
||||
return backLog;
|
||||
}
|
||||
|
||||
public void setBackLog(int backLog) {
|
||||
this.backLog = backLog;
|
||||
}
|
||||
|
||||
public int getMaxPoolSize() {
|
||||
if(maxPoolSize < 10){
|
||||
return 10;
|
||||
}
|
||||
return maxPoolSize;
|
||||
}
|
||||
|
||||
public void setMaxPoolSize(int maxPoolSize) {
|
||||
this.maxPoolSize = maxPoolSize;
|
||||
}
|
||||
|
||||
public int getCorePoolSize() {
|
||||
if(corePoolSize < 1){
|
||||
return 1;
|
||||
}
|
||||
return corePoolSize;
|
||||
}
|
||||
|
||||
public void setCorePoolSize(int corePoolSize) {
|
||||
this.corePoolSize = corePoolSize;
|
||||
}
|
||||
|
||||
public int getKeepAliveTime() {
|
||||
if (keepAliveTime < 1) {
|
||||
return 10;
|
||||
}
|
||||
return keepAliveTime;
|
||||
}
|
||||
|
||||
public void setKeepAliveTime(int keepAliveTime) {
|
||||
this.keepAliveTime = keepAliveTime;
|
||||
}
|
||||
}
|
||||
@@ -91,9 +91,6 @@ public class MarsAddressUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (candidateAddress != null) {
|
||||
return candidateAddress;
|
||||
}
|
||||
return null;
|
||||
return candidateAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.mars.mvc.resolve;
|
||||
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.common.constant.MarsSpace;
|
||||
import com.mars.mvc.resolve.access.PathAccess;
|
||||
import com.mars.tomcat.execute.access.PathAccess;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.HttpMarsResponse;
|
||||
import com.mars.server.util.RequestUtil;
|
||||
@@ -47,18 +47,14 @@ public class ResolveRequest {
|
||||
|
||||
try {
|
||||
Map<String, MarsMappingModel> maps = getMarsApis();
|
||||
|
||||
String uri = getRequestPath(request);
|
||||
if(PathAccess.hasAccess(uri)){
|
||||
return "ok";
|
||||
}
|
||||
return executeMars.execute(maps.get(uri),request.getMethod(),request,response);
|
||||
} catch (Exception e) {
|
||||
log.error("解释请求的时候报错",e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从uri中提取 请求连接的最末端,用来匹配控制层映射
|
||||
* @param request qingqiu
|
||||
|
||||
@@ -11,14 +11,6 @@
|
||||
<artifactId>mars-server</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-servlet-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.mars.server.server.request;
|
||||
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -15,9 +14,9 @@ import java.util.*;
|
||||
public class HttpMarsRequest {
|
||||
|
||||
/**
|
||||
* tomcat原生request
|
||||
* java原生request
|
||||
*/
|
||||
private HttpServletRequest httpRequest;
|
||||
private HttpExchange httpExchange;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
@@ -31,10 +30,10 @@ public class HttpMarsRequest {
|
||||
|
||||
/**
|
||||
* 构造函数,框架自己用的,程序员用不到,用了也没意义
|
||||
* @param httpRequest
|
||||
* @param httpExchange
|
||||
*/
|
||||
public HttpMarsRequest(HttpServletRequest httpRequest) {
|
||||
this.httpRequest = httpRequest;
|
||||
public HttpMarsRequest(HttpExchange httpExchange) {
|
||||
this.httpExchange = httpExchange;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +60,7 @@ public class HttpMarsRequest {
|
||||
* @return 请求方法
|
||||
*/
|
||||
public String getMethod() {
|
||||
return httpRequest.getMethod();
|
||||
return httpExchange.getRequestMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +68,7 @@ public class HttpMarsRequest {
|
||||
* @return 请求方法
|
||||
*/
|
||||
public String getUrl() {
|
||||
return httpRequest.getRequestURL().toString();
|
||||
return httpExchange.getRequestURI().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,8 +76,8 @@ public class HttpMarsRequest {
|
||||
* @param key 键
|
||||
* @return 头数据
|
||||
*/
|
||||
public Object getHeader(String key) {
|
||||
return httpRequest.getHeader(key);
|
||||
public List<String> getHeader(String key) {
|
||||
return httpExchange.getRequestHeaders().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,32 +86,19 @@ public class HttpMarsRequest {
|
||||
*/
|
||||
public Map<String, Object> getParameters() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
if(ServletFileUpload.isMultipartContent(httpRequest)){
|
||||
if(marsParams == null){
|
||||
return params;
|
||||
if(marsParams == null){
|
||||
return params;
|
||||
}
|
||||
for(String key : marsParams.keySet()){
|
||||
List<String> paramsList = marsParams.get(key);
|
||||
if(paramsList == null || paramsList.size() < 1){
|
||||
continue;
|
||||
}
|
||||
for(String key : marsParams.keySet()){
|
||||
List<String> paramsList = marsParams.get(key);
|
||||
if(paramsList == null || paramsList.size() < 1){
|
||||
continue;
|
||||
}
|
||||
String[] paramsListToArray = paramsListToArray(paramsList);
|
||||
if(paramsListToArray != null && paramsListToArray.length == 1){
|
||||
params.put(key,paramsListToArray[0]);
|
||||
} else {
|
||||
params.put(key,paramsListToArray);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Map<String,String[]> parameterMap = httpRequest.getParameterMap();
|
||||
for(String key : parameterMap.keySet()){
|
||||
String[] paramsListToArray = parameterMap.get(key);
|
||||
if(paramsListToArray != null && paramsListToArray.length == 1){
|
||||
params.put(key,paramsListToArray[0]);
|
||||
} else {
|
||||
params.put(key,paramsListToArray);
|
||||
}
|
||||
|
||||
String[] paramsListToArray = paramsListToArray(paramsList);
|
||||
if(paramsListToArray != null && paramsListToArray.length == 1){
|
||||
params.put(key,paramsListToArray[0]);
|
||||
} else {
|
||||
params.put(key,paramsListToArray);
|
||||
}
|
||||
}
|
||||
return params;
|
||||
@@ -124,16 +110,11 @@ public class HttpMarsRequest {
|
||||
* @return 请求参数
|
||||
*/
|
||||
public String getParameter(String key) {
|
||||
if(ServletFileUpload.isMultipartContent(httpRequest)){
|
||||
if(marsParams != null){
|
||||
List<String> marsParam = marsParams.get(key);
|
||||
if(marsParam == null){
|
||||
return null;
|
||||
}
|
||||
return marsParam.get(0);
|
||||
if(marsParams != null){
|
||||
List<String> value = marsParams.get(key);
|
||||
if(value != null && value.size() > 0){
|
||||
return value.get(0);
|
||||
}
|
||||
} else {
|
||||
return httpRequest.getParameter(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -144,13 +125,9 @@ public class HttpMarsRequest {
|
||||
* @return 请求参数
|
||||
*/
|
||||
public String[] getParameterValues(String key) {
|
||||
if(ServletFileUpload.isMultipartContent(httpRequest)){
|
||||
if(marsParams != null){
|
||||
List<String> paramsList = marsParams.get(key);
|
||||
return paramsListToArray(paramsList);
|
||||
}
|
||||
} else {
|
||||
return httpRequest.getParameterValues(key);
|
||||
if(marsParams != null) {
|
||||
List<String> paramsList = marsParams.get(key);
|
||||
return paramsListToArray(paramsList);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -180,24 +157,16 @@ public class HttpMarsRequest {
|
||||
* 获取tomcat原生request
|
||||
* @return 原生请求对象
|
||||
*/
|
||||
public HttpServletRequest getHttpServletRequest() {
|
||||
return httpRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Session
|
||||
* @return jwt
|
||||
*/
|
||||
public HttpSession getSession(){
|
||||
return httpRequest.getSession();
|
||||
public HttpExchange getHttpExchange() {
|
||||
return httpExchange;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端IP
|
||||
* @return ip
|
||||
* 获取客户端InetSocketAddress
|
||||
* @return inetSocketAddress
|
||||
*/
|
||||
public String getRemoteHost() {
|
||||
return httpRequest.getRemoteHost();
|
||||
public InetSocketAddress getInetSocketAddress() {
|
||||
return httpExchange.getLocalAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,13 +3,11 @@ package com.mars.server.server.request;
|
||||
import com.mars.common.base.config.MarsConfig;
|
||||
import com.mars.common.base.config.model.CrossDomainConfig;
|
||||
import com.mars.common.util.MarsConfiguration;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -26,9 +24,9 @@ public class HttpMarsResponse {
|
||||
private Logger logger = LoggerFactory.getLogger(HttpMarsResponse.class);
|
||||
|
||||
/**
|
||||
* netty原生通道
|
||||
* java原生通道
|
||||
*/
|
||||
private HttpServletResponse response;
|
||||
private HttpExchange httpExchange;
|
||||
|
||||
/**
|
||||
* 响应头
|
||||
@@ -39,19 +37,19 @@ public class HttpMarsResponse {
|
||||
/**
|
||||
* 构造函数,框架自己用的,程序员用不到,用了也没意义
|
||||
*
|
||||
* @param response netty原生通道
|
||||
* @param httpExchange java原生通道
|
||||
*/
|
||||
public HttpMarsResponse(HttpServletResponse response) {
|
||||
this.response = response;
|
||||
public HttpMarsResponse(HttpExchange httpExchange) {
|
||||
this.httpExchange = httpExchange;
|
||||
this.header = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取tomcat原生response
|
||||
* @return netty原生通道
|
||||
* 获取java原生httpExchange
|
||||
* @return java原生通道
|
||||
*/
|
||||
public HttpServletResponse geHttpServletResponse() {
|
||||
return response;
|
||||
public HttpExchange geHttpServletResponse() {
|
||||
return httpExchange;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,21 +68,29 @@ public class HttpMarsResponse {
|
||||
* @param context 消息
|
||||
*/
|
||||
public void send(String context) {
|
||||
PrintWriter out = null;
|
||||
OutputStream out = null;
|
||||
try {
|
||||
crossDomain();
|
||||
loadHeader();
|
||||
|
||||
response.setContentType("text/json;charset=UTF-8");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
out = response.getWriter();
|
||||
out.println(context);
|
||||
/* 设置响应头,必须在sendResponseHeaders方法之前设置 */
|
||||
httpExchange.getResponseHeaders().add("Content-Type:", "text/json;charset=utf-8");
|
||||
|
||||
/* 设置响应码和响应体长度,必须在getResponseBody方法之前调用 */
|
||||
byte[] responseContentByte = context.getBytes("utf-8");
|
||||
httpExchange.sendResponseHeaders(200, responseContentByte.length);
|
||||
|
||||
out = httpExchange.getResponseBody();
|
||||
out.write(responseContentByte);
|
||||
} catch (Exception e){
|
||||
logger.error("响应数据异常",e);
|
||||
} finally {
|
||||
if (out != null){
|
||||
out.flush();
|
||||
out.close();
|
||||
try{
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e){
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,17 +101,22 @@ public class HttpMarsResponse {
|
||||
* @param inputStream
|
||||
*/
|
||||
public void downLoad(String fileName, InputStream inputStream) {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
if(fileName == null || inputStream == null){
|
||||
logger.error("downLoad方法的传参不可以为空");
|
||||
return;
|
||||
}
|
||||
crossDomain();
|
||||
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName,"UTF-8"));
|
||||
httpExchange.getResponseHeaders().add("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName,"UTF-8"));
|
||||
|
||||
int len=0;
|
||||
byte[] buffer = new byte[1024];
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
|
||||
//设置响应码和响应体长度,必须在getResponseBody方法之前调用!
|
||||
httpExchange.sendResponseHeaders(200, inputStream.available());
|
||||
|
||||
out = httpExchange.getResponseBody();
|
||||
while((len=inputStream.read(buffer))!=-1){
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
@@ -116,6 +127,10 @@ public class HttpMarsResponse {
|
||||
if(inputStream != null){
|
||||
inputStream.close();
|
||||
}
|
||||
if(out != null){
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
} catch (Exception e){
|
||||
}
|
||||
}
|
||||
@@ -127,7 +142,7 @@ public class HttpMarsResponse {
|
||||
private void loadHeader(){
|
||||
if (header != null && !header.isEmpty()) {
|
||||
for (String key : header.keySet()) {
|
||||
response.setHeader(key, header.get(key));
|
||||
httpExchange.getResponseHeaders().set(key, header.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,10 +153,10 @@ public class HttpMarsResponse {
|
||||
private void crossDomain() {
|
||||
MarsConfig marsConfig = MarsConfiguration.getConfig();
|
||||
CrossDomainConfig crossDomainConfig = marsConfig.crossDomainConfig();
|
||||
response.setHeader("Access-Control-Allow-Origin", crossDomainConfig.getOrigin());
|
||||
response.setHeader("Access-Control-Allow-Methods", crossDomainConfig.getMethods());
|
||||
response.setHeader("Access-Control-Max-Age", crossDomainConfig.getMaxAge());
|
||||
response.setHeader("Access-Control-Allow-Headers", crossDomainConfig.getHeaders());
|
||||
response.setHeader("Access-Control-Allow-Credentials", crossDomainConfig.getCredentials());
|
||||
httpExchange.getResponseHeaders().set("Access-Control-Allow-Origin", crossDomainConfig.getOrigin());
|
||||
httpExchange.getResponseHeaders().set("Access-Control-Allow-Methods", crossDomainConfig.getMethods());
|
||||
httpExchange.getResponseHeaders().set("Access-Control-Max-Age", crossDomainConfig.getMaxAge());
|
||||
httpExchange.getResponseHeaders().set("Access-Control-Allow-Headers", crossDomainConfig.getHeaders());
|
||||
httpExchange.getResponseHeaders().set("Access-Control-Allow-Credentials", crossDomainConfig.getCredentials());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package com.mars.tomcat.execute;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* HttpMarsRequest工厂
|
||||
*/
|
||||
public class HttpMarsRequestFactory {
|
||||
|
||||
/**
|
||||
* 从httpExchange中提取出所有的参数,并放置到HttpMarsRequest中
|
||||
* @param httpExchange 请求
|
||||
* @param marsRequest mars请求
|
||||
* @return 加工后的mars请求
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public static HttpMarsRequest getHttpMarsRequest(HttpExchange httpExchange, HttpMarsRequest marsRequest) throws Exception{
|
||||
if(httpExchange != null){
|
||||
Map<String, MarsFileUpLoad> files = new HashMap<>();
|
||||
Map<String,List<String>> marsParams = new HashMap<>();
|
||||
|
||||
if (httpExchange.getRequestMethod().equals("GET")) {
|
||||
/* 从get请求中获取参数 */
|
||||
String paramStr = httpExchange.getRequestURI().getQuery();
|
||||
marsParams = urlencoded(paramStr,marsParams,false);
|
||||
} else {
|
||||
/* 非GET请求读请求体 */
|
||||
InputStream inputStream = httpExchange.getRequestBody();
|
||||
if(inputStream == null){
|
||||
return marsRequest;
|
||||
}
|
||||
|
||||
/* 根据提交方式,分别处理参数 */
|
||||
String contentType = getContentType(httpExchange);
|
||||
if(contentType.startsWith("application/x-www-form-urlencoded")){
|
||||
/* 正常的表单提交 */
|
||||
String paramStr = getParamStr(inputStream);
|
||||
marsParams = urlencoded(paramStr,marsParams,true);
|
||||
} else if(contentType.startsWith("multipart/form-data")){
|
||||
/* formData提交,可以用于文件上传 */
|
||||
Map<String,Object> result = formData(inputStream,marsParams,files);
|
||||
files = (Map<String, MarsFileUpLoad>)result.get("files");
|
||||
marsParams = (Map<String,List<String>>)result.get("marsParams");
|
||||
} else if(contentType.startsWith("application/json")){
|
||||
/* RAW提交(json) */
|
||||
marsParams = raw(inputStream,marsParams);
|
||||
}
|
||||
}
|
||||
/* 将提取出来的参数,放置到HttpMarsRequest中 */
|
||||
marsRequest.setFiles(files);
|
||||
marsRequest.setParams(marsParams);
|
||||
}
|
||||
return marsRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取提交方式
|
||||
* @param httpExchange 请求对象
|
||||
* @return 提交方式
|
||||
*/
|
||||
private static String getContentType(HttpExchange httpExchange){
|
||||
List<String> ctList = httpExchange.getRequestHeaders().get("Content-type");
|
||||
if(ctList == null || ctList.size() < 1){
|
||||
return null;
|
||||
}
|
||||
return ctList.get(0).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从输入流里面读取所有的数据
|
||||
* @param inputStream 输入流
|
||||
* @return 数据
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static String getParamStr(InputStream inputStream) throws Exception {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
||||
String line = null;
|
||||
StringBuffer paramsStr = new StringBuffer();
|
||||
while ((line = br.readLine()) != null) {
|
||||
paramsStr.append(line);
|
||||
}
|
||||
return paramsStr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 表单提交处理
|
||||
* @param paramStr 数据
|
||||
* @param marsParams httpMarsRequest的参数对象
|
||||
* @param hasDecode 是否需要解码 true是,false不是
|
||||
* @return httpMarsRequest的参数对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static Map<String,List<String>> urlencoded(String paramStr,Map<String,List<String>> marsParams, boolean hasDecode) throws Exception {
|
||||
if(paramStr != null){
|
||||
String[] paramsArray = paramStr.split("&");
|
||||
if(paramsArray == null || paramsArray.length < 1){
|
||||
return marsParams;
|
||||
}
|
||||
List<String> values = null;
|
||||
for(String paramItem : paramsArray){
|
||||
String[] param = paramItem.split("=");
|
||||
if(param == null || param.length < 2){
|
||||
continue;
|
||||
}
|
||||
|
||||
values = marsParams.get(param[0]);
|
||||
if(values == null){
|
||||
values = new ArrayList<>();
|
||||
}
|
||||
|
||||
String value = param[1];
|
||||
if(hasDecode){
|
||||
value = URLDecoder.decode(value, "UTF-8");
|
||||
}
|
||||
values.add(value);
|
||||
marsParams.put(param[0],values);
|
||||
}
|
||||
}
|
||||
return marsParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* RAW提交处理
|
||||
* @param inputStream 输入流
|
||||
* @param marsParams httpMarsRequest的参数对象
|
||||
* @return httpMarsRequest的参数对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static Map<String,List<String>> raw(InputStream inputStream,Map<String,List<String>> marsParams) throws Exception {
|
||||
String paramStr = getParamStr(inputStream);
|
||||
if(paramStr == null || paramStr.trim().equals("")){
|
||||
return marsParams;
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(paramStr);
|
||||
List<String> values = null;
|
||||
for(String key : jsonObject.keySet()){
|
||||
values = marsParams.get(key);
|
||||
if(values == null){
|
||||
values = new ArrayList<>();
|
||||
}
|
||||
Object val = jsonObject.get(key);
|
||||
if(val != null){
|
||||
if(val instanceof JSONArray) {
|
||||
JSONArray jsonArray = (JSONArray)val;
|
||||
for(int i = 0; i<jsonArray.size(); i++){
|
||||
values.add(jsonArray.getString(i));
|
||||
}
|
||||
} else {
|
||||
values.add(val.toString());
|
||||
}
|
||||
}
|
||||
marsParams.put(key,values);
|
||||
}
|
||||
return marsParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* formData提交处理
|
||||
* @param inputStream 输入流
|
||||
* @param marsParams httpMarsRequest的参数对象
|
||||
* @param files httpMarsRequest的文件参数对象
|
||||
* @return httpMarsRequest的参数对象 和 httpMarsRequest的文件参数对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static Map<String,Object> formData(InputStream inputStream, Map<String,List<String>> marsParams, Map<String, MarsFileUpLoad> files) throws Exception {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,15 @@ import com.mars.common.util.MesUtil;
|
||||
import com.mars.common.util.StringUtil;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.HttpMarsResponse;
|
||||
import com.mars.server.util.RequestUtil;
|
||||
import com.mars.tomcat.execute.access.PathAccess;
|
||||
import com.mars.tomcat.par.factory.ParamAndResultFactory;
|
||||
import com.mars.tomcat.util.FileItemUtil;
|
||||
import com.mars.tomcat.util.FileUpLoad;
|
||||
import org.apache.tomcat.util.http.fileupload.FileItem;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 处理请求的线程
|
||||
@@ -28,21 +25,12 @@ public class RequestExecute {
|
||||
private Logger log = LoggerFactory.getLogger(RequestExecute.class);
|
||||
|
||||
/**
|
||||
* tomcat的request对象
|
||||
* 原生HttpExchange
|
||||
*/
|
||||
private HttpServletRequest httpRequest;
|
||||
private HttpExchange httpExchange;
|
||||
|
||||
/**
|
||||
* tomcat的response对象
|
||||
*/
|
||||
private HttpServletResponse httpResponse;
|
||||
|
||||
public void setHttpRequest(HttpServletRequest httpRequest) {
|
||||
this.httpRequest = httpRequest;
|
||||
}
|
||||
|
||||
public void setHttpResponse(HttpServletResponse response) {
|
||||
this.httpResponse = response;
|
||||
public void setHttpExchange(HttpExchange httpExchange) {
|
||||
this.httpExchange = httpExchange;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,22 +39,25 @@ public class RequestExecute {
|
||||
public void execute() {
|
||||
|
||||
/* 组装httpRequest对象 */
|
||||
HttpMarsRequest request = new HttpMarsRequest(httpRequest);
|
||||
HttpMarsRequest request = new HttpMarsRequest(httpExchange);
|
||||
|
||||
/* 组装httpResponse对象 */
|
||||
HttpMarsResponse response = new HttpMarsResponse(httpResponse);
|
||||
HttpMarsResponse response = new HttpMarsResponse(httpExchange);
|
||||
|
||||
try {
|
||||
/* 从请求中获取数据 */
|
||||
List<FileItem> fileItemList = FileUpLoad.getFileItem(httpRequest);
|
||||
/* 请求的数据中分出表单数据和文件流 */
|
||||
request = FileItemUtil.getHttpMarsRequest(fileItemList, request);
|
||||
Object result = "ok";
|
||||
/* 如果请求路径合法,则继续往下执行 */
|
||||
String uri = RequestUtil.getUriName(request);
|
||||
if(!PathAccess.hasAccess(uri)){
|
||||
/* 获取请求的数据,并填充表单 */
|
||||
request = HttpMarsRequestFactory.getHttpMarsRequest(httpExchange, request);
|
||||
|
||||
/* 通过反射执行核心servlet */
|
||||
Class<?> cls = CoreServletClass.getCls();
|
||||
Object object = cls.getDeclaredConstructor().newInstance();
|
||||
Method helloMethod = cls.getDeclaredMethod("doRequest", new Class[]{HttpMarsRequest.class, HttpMarsResponse.class});
|
||||
Object result = helloMethod.invoke(object, new Object[]{request, response});
|
||||
/* 通过反射执行核心servlet */
|
||||
Class<?> cls = CoreServletClass.getCls();
|
||||
Object object = cls.getDeclaredConstructor().newInstance();
|
||||
Method helloMethod = cls.getDeclaredMethod("doRequest", new Class[]{HttpMarsRequest.class, HttpMarsResponse.class});
|
||||
result = helloMethod.invoke(object, new Object[]{request, response});
|
||||
}
|
||||
|
||||
/* 响应 */
|
||||
ParamAndResultFactory.getBaseParamAndResult().result(response, result);
|
||||
|
||||
+2
-1
@@ -1,4 +1,4 @@
|
||||
package com.mars.mvc.resolve.access;
|
||||
package com.mars.tomcat.execute.access;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -17,6 +17,7 @@ public class PathAccess {
|
||||
if(urls == null){
|
||||
urls = new HashMap<>();
|
||||
urls.put("favicon.ico","no");
|
||||
urls.put("/favicon.ico","no");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package com.mars.tomcat.server;
|
||||
|
||||
import com.mars.common.base.config.MarsConfig;
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.common.constant.MarsSpace;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import com.mars.common.util.MarsConfiguration;
|
||||
import com.mars.tomcat.server.threadpool.ThreadPool;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* tomcat服务
|
||||
* @author yuye
|
||||
@@ -20,24 +25,26 @@ public class MarsServer {
|
||||
* 启动tomcat服务
|
||||
* @param portNumber
|
||||
*/
|
||||
public static void start(final int portNumber) {
|
||||
public static void start(int portNumber) {
|
||||
try {
|
||||
Tomcat tomcat = new Tomcat();
|
||||
tomcat.setPort(portNumber);
|
||||
tomcat.setBaseDir(".");
|
||||
|
||||
Context context = tomcat.addContext("/", null);
|
||||
Tomcat.addServlet(context, "dispatcher", new MarsServerHandler());
|
||||
context.addServletMappingDecoded("/*", "dispatcher");
|
||||
// 获取最大并发数
|
||||
MarsConfig marsConfig = MarsConfiguration.getConfig();
|
||||
int backLog = marsConfig.getThreadPoolConfig().getBackLog();
|
||||
|
||||
tomcat.init();
|
||||
tomcat.start();
|
||||
// 创建服务
|
||||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(portNumber),backLog);
|
||||
httpServer.createContext("/", new MarsServerHandler());
|
||||
|
||||
//设置服务器的线程池对象
|
||||
httpServer.setExecutor(ThreadPool.getThreadPoolExecutor());
|
||||
|
||||
/* 标识tomcat是否已经启动 */
|
||||
MarsSpace.getEasySpace().setAttr(MarsConstant.HAS_NETTY_START,"yes");
|
||||
log.info("启动成功");
|
||||
|
||||
tomcat.getServer().await();
|
||||
//启动服务器
|
||||
httpServer.start();
|
||||
} catch (Exception e) {
|
||||
log.error("启动tomcat报错",e);
|
||||
}
|
||||
|
||||
@@ -2,56 +2,34 @@ package com.mars.tomcat.server;
|
||||
|
||||
import com.mars.tomcat.execute.RequestExecute;
|
||||
import com.mars.tomcat.util.ResponseUtil;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 接收tomcat服务
|
||||
* 接收请求
|
||||
* @author yuye
|
||||
*
|
||||
*/
|
||||
@MultipartConfig
|
||||
public class MarsServerHandler extends HttpServlet {
|
||||
public class MarsServerHandler implements HttpHandler {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(MarsServerHandler.class);
|
||||
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
try {
|
||||
req.setCharacterEncoding("UTF-8");
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
|
||||
RequestExecute requestExecute = new RequestExecute();
|
||||
requestExecute.setHttpRequest(req);
|
||||
requestExecute.setHttpResponse(resp);
|
||||
requestExecute.setHttpExchange(httpExchange);
|
||||
requestExecute.execute();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理请求失败!", e);
|
||||
ResponseUtil.sendServerError(resp,"处理请求发生错误"+e.getMessage());
|
||||
ResponseUtil.sendServerError(httpExchange,"处理请求发生错误"+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req,resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req,resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req,resp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.mars.tomcat.server.threadpool;
|
||||
|
||||
import com.mars.common.base.config.MarsConfig;
|
||||
import com.mars.common.base.config.model.ThreadPoolConfig;
|
||||
import com.mars.common.util.MarsConfiguration;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 执行请求的线程池
|
||||
*/
|
||||
public class ThreadPool {
|
||||
|
||||
/**
|
||||
* 线程池
|
||||
*/
|
||||
private static ThreadPoolExecutor threadPoolExecutor;
|
||||
|
||||
/**
|
||||
* 获取线程池
|
||||
* @return
|
||||
*/
|
||||
public static ThreadPoolExecutor getThreadPoolExecutor(){
|
||||
if(threadPoolExecutor == null){
|
||||
/* 获取线程池的配置 */
|
||||
MarsConfig marsConfig = MarsConfiguration.getConfig();
|
||||
ThreadPoolConfig threadPoolConfig = marsConfig.getThreadPoolConfig();
|
||||
int maxPoolSize = threadPoolConfig.getMaxPoolSize();
|
||||
int corePoolSize = threadPoolConfig.getCorePoolSize();
|
||||
int keepAliveTime = threadPoolConfig.getKeepAliveTime();
|
||||
|
||||
/* 创建线程池 */
|
||||
threadPoolExecutor = new ThreadPoolExecutor(
|
||||
corePoolSize,
|
||||
maxPoolSize,
|
||||
keepAliveTime,
|
||||
TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<>(maxPoolSize-corePoolSize));
|
||||
}
|
||||
return threadPoolExecutor;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.mars.tomcat.util;
|
||||
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
import org.apache.tomcat.util.http.fileupload.FileItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 如果是文件上传的提交,处理表单字段和文件流
|
||||
*/
|
||||
public class FileItemUtil {
|
||||
|
||||
/**
|
||||
* 如果是文件上传的提交,处理表单字段和文件流
|
||||
* @param fileItemList 文件列表
|
||||
* @param marsRequest mars请求
|
||||
* @return 加工后的mars请求
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public static HttpMarsRequest getHttpMarsRequest(List<FileItem> fileItemList, HttpMarsRequest marsRequest) throws Exception{
|
||||
if(fileItemList != null){
|
||||
Map<String, MarsFileUpLoad> files = new HashMap<>();
|
||||
Map<String,List<String>> marsParams = new HashMap<>();
|
||||
for(FileItem item : fileItemList){
|
||||
if(item.isFormField()){
|
||||
String name = item.getFieldName();
|
||||
String value = item.getString("UTF-8");
|
||||
List<String> params = marsParams.get(name);
|
||||
if(params == null){
|
||||
params = new ArrayList<>();
|
||||
}
|
||||
params.add(value);
|
||||
marsParams.put(name,params);
|
||||
} else {
|
||||
MarsFileUpLoad marsFileUpLoad = new MarsFileUpLoad();
|
||||
marsFileUpLoad.setName(item.getFieldName());
|
||||
marsFileUpLoad.setInputStream(item.getInputStream());
|
||||
marsFileUpLoad.setFileName(item.getName());
|
||||
files.put(marsFileUpLoad.getName(),marsFileUpLoad);
|
||||
}
|
||||
}
|
||||
marsRequest.setFiles(files);
|
||||
marsRequest.setParams(marsParams);
|
||||
}
|
||||
return marsRequest;
|
||||
}
|
||||
}
|
||||
@@ -2,38 +2,9 @@ package com.mars.tomcat.util;
|
||||
|
||||
import com.mars.common.base.config.model.FileUploadConfig;
|
||||
import com.mars.common.util.MarsConfiguration;
|
||||
import org.apache.tomcat.util.http.fileupload.FileItem;
|
||||
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
|
||||
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
public class FileUpLoad {
|
||||
|
||||
/**
|
||||
* 获取文件列表
|
||||
* @param request 请求
|
||||
* @return 返回
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public static List<FileItem> getFileItem(HttpServletRequest request) throws Exception {
|
||||
if(!ServletFileUpload.isMultipartContent(request)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
FileItemFactory factory = new DiskFileItemFactory();
|
||||
ServletFileUpload fileUpload = new ServletFileUpload(factory);
|
||||
|
||||
FileUploadConfig fileUploadConfig = MarsConfiguration.getConfig().fileUploadConfig();
|
||||
|
||||
fileUpload.setFileSizeMax(fileUploadConfig.getFileSizeMax());
|
||||
fileUpload.setSizeMax(fileUploadConfig.getSizeMax());
|
||||
|
||||
List<FileItem> fileItemList = fileUpload.parseRequest(new ServletRequestContext(request));
|
||||
|
||||
return fileItemList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@ package com.mars.tomcat.util;
|
||||
|
||||
import com.mars.common.util.MesUtil;
|
||||
import com.mars.server.server.request.HttpMarsResponse;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
/**
|
||||
* 响应工具类
|
||||
@@ -12,10 +11,10 @@ public class ResponseUtil {
|
||||
|
||||
/**
|
||||
* 出错响应
|
||||
* @param response
|
||||
* @param httpExchange
|
||||
*/
|
||||
public static void sendServerError(HttpServletResponse response, String ex){
|
||||
HttpMarsResponse marsResponse = new HttpMarsResponse(response);
|
||||
public static void sendServerError(HttpExchange httpExchange, String ex){
|
||||
HttpMarsResponse marsResponse = new HttpMarsResponse(httpExchange);
|
||||
marsResponse.send(MesUtil.getMes(500,ex).toJSONString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,23 +48,6 @@
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
<version>8.5.55</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-servlet-api</artifactId>
|
||||
<version>8.5.55</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user