mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-08 21:06:40 +10:00
去除tomcat,不再使用第三方容器,而是直接采用jdk自带的HttpServer来做http服务
This commit is contained in:
@@ -3,6 +3,7 @@ package com.mars.iserver.execute;
|
||||
import com.mars.common.ncfg.mvc.CoreServletClass;
|
||||
import com.mars.common.util.MesUtil;
|
||||
import com.mars.common.util.StringUtil;
|
||||
import com.mars.iserver.par.HttpMarsRequestFactory;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.HttpMarsResponse;
|
||||
import com.mars.server.util.RequestUtil;
|
||||
@@ -52,7 +53,7 @@ public class RequestExecute {
|
||||
/* 获取请求的数据,并填充表单 */
|
||||
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});
|
||||
|
||||
+12
-8
@@ -1,7 +1,8 @@
|
||||
package com.mars.iserver.execute;
|
||||
package com.mars.iserver.par;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.mars.iserver.par.formdata.ParsingFormData;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -20,6 +21,8 @@ import java.util.Map;
|
||||
*/
|
||||
public class HttpMarsRequestFactory {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 从httpExchange中提取出所有的参数,并放置到HttpMarsRequest中
|
||||
* @param httpExchange 请求
|
||||
@@ -31,7 +34,7 @@ public class HttpMarsRequestFactory {
|
||||
Map<String, MarsFileUpLoad> files = new HashMap<>();
|
||||
Map<String, List<String>> marsParams = new HashMap<>();
|
||||
|
||||
if (httpExchange.getRequestMethod().equals("GET")) {
|
||||
if (httpExchange.getRequestMethod().toUpperCase().equals("GET")) {
|
||||
/* 从get请求中获取参数 */
|
||||
String paramStr = httpExchange.getRequestURI().getQuery();
|
||||
marsParams = urlencoded(paramStr, marsParams, false);
|
||||
@@ -50,9 +53,9 @@ public class HttpMarsRequestFactory {
|
||||
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");
|
||||
Map<String, Object> result = formData(inputStream, marsParams, files, contentType);
|
||||
files = (Map<String, MarsFileUpLoad>) result.get(ParsingFormData.FILES_KEY);
|
||||
marsParams = (Map<String, List<String>>) result.get(ParsingFormData.PARAMS_KEY);
|
||||
} else if (contentType.startsWith("application/json")) {
|
||||
/* RAW提交(json) */
|
||||
marsParams = raw(inputStream, marsParams);
|
||||
@@ -75,7 +78,7 @@ public class HttpMarsRequestFactory {
|
||||
if(ctList == null || ctList.size() < 1){
|
||||
return null;
|
||||
}
|
||||
return ctList.get(0).toLowerCase();
|
||||
return ctList.get(0).trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,10 +176,11 @@ public class HttpMarsRequestFactory {
|
||||
* @param inputStream 输入流
|
||||
* @param marsParams httpMarsRequest的参数对象
|
||||
* @param files httpMarsRequest的文件参数对象
|
||||
* @param contentType 内容类型
|
||||
* @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;
|
||||
private static Map<String,Object> formData(InputStream inputStream, Map<String,List<String>> marsParams, Map<String, MarsFileUpLoad> files, String contentType) throws Exception {
|
||||
return ParsingFormData.parsing(inputStream,marsParams,files,contentType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.mars.iserver.par.formdata;
|
||||
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 解析FormData
|
||||
*/
|
||||
public class ParsingFormData {
|
||||
|
||||
public static final String PARAMS_KEY = "paramsKey";
|
||||
public static final String FILES_KEY = "filesKey";
|
||||
|
||||
/**
|
||||
* 解析
|
||||
* @param inputStream 输入流
|
||||
* @param marsParams 参数
|
||||
* @param files 文件
|
||||
* @param contentType 内容类型
|
||||
* @return 参数和文件
|
||||
*/
|
||||
public static Map<String,Object> parsing(InputStream inputStream, Map<String, List<String>> marsParams, Map<String, MarsFileUpLoad> files, String contentType) throws Exception {
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
|
||||
byte[] boundary = boundary(contentType).getBytes("UTF-8");
|
||||
byte[] endBoundary = endBoundary(contentType).getBytes("UTF-8");
|
||||
|
||||
|
||||
|
||||
result.put(PARAMS_KEY,"");
|
||||
result.put(FILES_KEY,"");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分隔符
|
||||
* @param contentType 内容类型
|
||||
* @return 分隔符
|
||||
*/
|
||||
private static String boundary(String contentType){
|
||||
String bo = contentType.substring(contentType.indexOf(";"));
|
||||
return "--"+(bo.substring(bo.indexOf("=")+1).trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束时的分隔符
|
||||
* @param contentType 内容类型
|
||||
* @return 结束时的分隔符
|
||||
*/
|
||||
private static String endBoundary(String contentType){
|
||||
return boundary(contentType)+"--";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user