初步完成请求接收与回写

This commit is contained in:
peng
2021-10-25 20:02:39 +08:00
parent 43fc20ff97
commit e294131e95
9 changed files with 327 additions and 5 deletions

View File

@@ -1,12 +1,52 @@
package com.dayrain;
import com.dayrain.entity.Configuration;
import com.dayrain.entity.ConfigurationHolder;
import com.dayrain.entity.RequestType;
import com.dayrain.entity.Server;
import com.dayrain.entity.ServerConfig;
import com.dayrain.entity.ServerUrl;
import javafx.application.Application;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class ApplicationStarter extends Application {
public static void main(String[] args) {
launch(args);
ConfigurationHolder.reload(get());
Configuration configuration = ConfigurationHolder.get();
List<ServerConfig> serverConfigs = configuration.getServerConfigs();
for (ServerConfig serverConfig : serverConfigs) {
Server server = new Server(serverConfig);
Thread thread = new Thread(server);
thread.start();
}
// launch(args);
}
private static Configuration get() {
Configuration configuration = new Configuration();
configuration.setServerConfigs(new ArrayList<ServerConfig>(){
{
add(new ServerConfig("测试", 8080, new ArrayList<ServerUrl>(){
{
add(new ServerUrl("登录", "/login", RequestType.GET, "{\n" +
" \"username\": \"admin\",\n" +
" \"password\": \"123\"\n" +
"}"));
add(new ServerUrl("登录", "/logout", RequestType.GET, "{\n" +
" \"username\": \"admin\",\n" +
" \"password\": \"123\"\n" +
"}"));
}
}));
}
});
return configuration;
}
@Override

View File

@@ -1,4 +1,16 @@
package com.dayrain.entity;
import java.util.List;
public class Configuration {
private List<ServerConfig>serverConfigs;
public List<ServerConfig> getServerConfigs() {
return serverConfigs;
}
public void setServerConfigs(List<ServerConfig> serverConfigs) {
this.serverConfigs = serverConfigs;
}
}

View File

@@ -0,0 +1,18 @@
package com.dayrain.entity;
public class ConfigurationHolder {
private static Configuration configuration;
public static void init() {
configuration = new Configuration();
}
public static void reload(Configuration config) {
configuration = config;
}
public static Configuration get() {
return configuration;
}
}

View File

@@ -0,0 +1,88 @@
package com.dayrain.entity;
import com.dayrain.utils.JackSonUtils;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class RequestHandler implements HttpHandler {
private final ServerUrl serverUrl;
public RequestHandler(ServerUrl serverUrl) {
this.serverUrl = serverUrl;
}
@Override
public void handle(HttpExchange exchange) throws IOException {
if(RequestType.GET == serverUrl.getRequestType()) {
handleGetRequest(exchange);
}else if(RequestType.POST == serverUrl.getRequestType()) {
handlePostRequest(exchange);
}
response(exchange, serverUrl.getResponseBody());
}
private void handleGetRequest(HttpExchange exchange) {
String queryString = exchange.getRequestURI().getQuery();
System.out.println(queryString);
}
private void handlePostRequest(HttpExchange exchange) {
String requestBody = JackSonUtils.getFromInputStream(exchange.getRequestBody());
System.out.println(requestBody);
}
private void response(HttpExchange exchange, String jsonBody) {
setHeaders(exchange);
try {
exchange.sendResponseHeaders(200, jsonBody.length());
exchange.setAttribute("Content-Type","application/json; charset=utf-8");
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(jsonBody.getBytes(StandardCharsets.UTF_8));
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setHeaders(HttpExchange exchange) {
Map<String, String> headerMap = serverUrl.getHeaderMap();
if(headerMap != null && headerMap.size() > 0) {
Headers responseHeaders = exchange.getResponseHeaders();
for (String header : headerMap.keySet()) {
responseHeaders.add(header, headerMap.get(header));
}
}
}
public static Map<String,String> formData2Dic(String formData ) {
Map<String,String> result = new HashMap<>();
if(formData== null || formData.trim().length() == 0) {
return result;
}
final String[] items = formData.split("&");
Arrays.stream(items).forEach(item ->{
final String[] keyAndVal = item.split("=");
if( keyAndVal.length == 2) {
try{
final String key = URLDecoder.decode( keyAndVal[0],"utf8");
final String val = URLDecoder.decode( keyAndVal[1],"utf8");
result.put(key,val);
}catch (UnsupportedEncodingException ignored) {}
}
});
return result;
}
}

View File

@@ -0,0 +1,5 @@
package com.dayrain.entity;
public enum RequestType {
GET,POST;
}

View File

@@ -0,0 +1,35 @@
package com.dayrain.entity;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
/**
* 服务
* @author peng
* @date 2021/10/25
*/
public class Server implements Runnable {
private final ServerConfig serverConfig;
public Server(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
@Override
public void run() {
try {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(serverConfig.getPort()), 0);
httpServer.setExecutor(Executors.newCachedThreadPool());
for (ServerUrl serverUrl : serverConfig.getServerUrls()) {
httpServer.createContext(serverUrl.getUrl(), new RequestHandler(serverUrl));
}
httpServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -1,8 +1,26 @@
package com.dayrain.entity;
public class ServerConfig {
import java.util.List;
private int port = 8081;
public class ServerConfig {
/**
* 服务名
*/
private String serverName;
/**
* 端口
*/
private int port;
/**
* 请求url集合
*/
private List<ServerUrl>serverUrls;
public ServerConfig(String serverName, int port, List<ServerUrl>serverUrls) {
this.serverName = serverName;
this.port = port;
this.serverUrls = serverUrls;
}
public int getPort() {
return port;
@@ -12,7 +30,19 @@ public class ServerConfig {
this.port = port;
}
public ServerConfig(int port) {
this.port = port;
public String getServerName() {
return serverName;
}
public void setServerName(String serverName) {
this.serverName = serverName;
}
public List<ServerUrl> getServerUrls() {
return serverUrls;
}
public void setServerUrls(List<ServerUrl> serverUrls) {
this.serverUrls = serverUrls;
}
}

View File

@@ -0,0 +1,75 @@
package com.dayrain.entity;
import java.util.HashMap;
import java.util.Map;
public class ServerUrl {
/**
* 路径名
*/
private String urlName;
/**
* 路径url
*/
private String url;
/**
* 请求类型
*/
private RequestType requestType;
/**
* 响应体
*/
private String responseBody;
/**
* 请求头
*/
private Map<String, String> headerMap;
public ServerUrl(String urlName, String url, RequestType requestType, String requestBody) {
this.urlName = urlName;
this.url = url;
this.requestType = requestType;
this.responseBody = requestBody;
}
public String getUrlName() {
return urlName;
}
public void setUrlName(String urlName) {
this.urlName = urlName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public RequestType getRequestType() {
return requestType;
}
public void setRequestType(RequestType requestType) {
this.requestType = requestType;
}
public Map<String, String> getHeaderMap() {
return headerMap;
}
public void setHeaderMap(Map<String, String> headerMap) {
this.headerMap = headerMap;
}
public String getResponseBody() {
return responseBody;
}
public void setResponseBody(String responseBody) {
this.responseBody = responseBody;
}
}

View File

@@ -0,0 +1,19 @@
package com.dayrain.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
public class JackSonUtils {
public static String getFromInputStream(InputStream inputStream) {
try {
return new ObjectMapper().readValue(inputStream, String.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}