diff --git a/src/com/dayrain/ApplicationStarter.java b/src/com/dayrain/ApplicationStarter.java index 8a412d8..28ba054 100644 --- a/src/com/dayrain/ApplicationStarter.java +++ b/src/com/dayrain/ApplicationStarter.java @@ -4,9 +4,6 @@ import com.dayrain.views.HomePage; import javafx.application.Application; import javafx.stage.Stage; -import java.io.File; - - public class ApplicationStarter extends Application { public static void main(String[] args) { diff --git a/src/com/dayrain/component/ConfigHolder.java b/src/com/dayrain/component/ConfigHolder.java index ed4d2c2..0d94d11 100644 --- a/src/com/dayrain/component/ConfigHolder.java +++ b/src/com/dayrain/component/ConfigHolder.java @@ -1,14 +1,12 @@ package com.dayrain.component; import com.dayrain.utils.FileUtils; -import com.fasterxml.jackson.databind.util.BeanUtil; - -import java.awt.*; public class ConfigHolder { private static Configuration configuration; - private ConfigHolder(){} + private ConfigHolder() { + } public synchronized static Configuration init() { configuration = FileUtils.load(); diff --git a/src/com/dayrain/component/Configuration.java b/src/com/dayrain/component/Configuration.java index c6de0f9..b3bc209 100644 --- a/src/com/dayrain/component/Configuration.java +++ b/src/com/dayrain/component/Configuration.java @@ -14,7 +14,7 @@ public class Configuration { private int intLen; - private ListserverConfigs; + private List serverConfigs; public Configuration() { } diff --git a/src/com/dayrain/component/ConsoleLog.java b/src/com/dayrain/component/ConsoleLog.java index 2221da7..086e23d 100644 --- a/src/com/dayrain/component/ConsoleLog.java +++ b/src/com/dayrain/component/ConsoleLog.java @@ -18,11 +18,11 @@ public class ConsoleLog { public static void log(ServerUrl serverUrl, String params, String resp) { String log = logs.getOrDefault(serverUrl.getServerName(), null); - if(log == null || NO_REQUEST.equals(log)) { + if (log == null || NO_REQUEST.equals(log)) { log = ""; } - if(params == null || "".equals(params)) { + if (params == null || "".equals(params)) { params = "空"; } StringBuilder stringBuilder = new StringBuilder(); @@ -38,27 +38,18 @@ public class ConsoleLog { log += stringBuilder.toString(); logs.put(serverUrl.getServerName(), log); - if(serverUrl.getServerName().equals(logArea.getServerName())) { - if(NO_REQUEST.equals(logArea.getText())) { + if (serverUrl.getServerName().equals(logArea.getServerName())) { + if (NO_REQUEST.equals(logArea.getText())) { logArea.setText(log); - }else { + } else { logArea.appendText(log); } logArea.setScrollTop(Double.MAX_VALUE); } } - public static String getLog(String serverName) { - if(logs.containsKey(serverName)) { - return logs.get(serverName); - } - - logs.put(serverName, NO_REQUEST); - return NO_REQUEST; - } - public static void resetTextArea(String serverName) { - if(!logs.containsKey(serverName)) { + if (!logs.containsKey(serverName)) { logs.put(serverName, NO_REQUEST); } diff --git a/src/com/dayrain/component/RequestHandler.java b/src/com/dayrain/component/RequestHandler.java index 2b8cde0..22129c9 100644 --- a/src/com/dayrain/component/RequestHandler.java +++ b/src/com/dayrain/component/RequestHandler.java @@ -24,11 +24,11 @@ public class RequestHandler implements HttpHandler { public void handle(HttpExchange exchange) throws IOException { RequestType requestType = serverUrl.getRequestType(); String param = null; - if(RequestType.GET.equals(requestType)) { + if (RequestType.GET.equals(requestType)) { param = handleGetRequest(exchange); } - if(RequestType.POST.equals(requestType)) { + if (RequestType.POST.equals(requestType)) { param = handlePostRequest(exchange); } @@ -36,7 +36,7 @@ public class RequestHandler implements HttpHandler { ConsoleLog.log(serverUrl, param, resp); response(exchange, resp); - } + } private String handleGetRequest(HttpExchange exchange) { return exchange.getRequestURI().getQuery(); @@ -50,7 +50,7 @@ public class RequestHandler implements HttpHandler { private void response(HttpExchange exchange, String jsonBody) { try { exchange.sendResponseHeaders(200, jsonBody.length()); - exchange.setAttribute("Content-Type","application/json; charset=utf-8"); + exchange.setAttribute("Content-Type", "application/json; charset=utf-8"); OutputStream outputStream = exchange.getResponseBody(); outputStream.write(jsonBody.getBytes(StandardCharsets.UTF_8)); outputStream.close(); @@ -61,7 +61,7 @@ public class RequestHandler implements HttpHandler { private String replaceResp(String resp) { - if(resp == null || "".equals(resp)) { + if (resp == null || "".equals(resp)) { return resp; } @@ -82,14 +82,14 @@ public class RequestHandler implements HttpHandler { private String randomString(int len) { String res = UUID.randomUUID().toString(); - if(len > res.length()) { + if (len > res.length()) { len = res.length(); } return UUID.randomUUID().toString().substring(0, len); } private int randomInt(int len) { - int res = (int) Math.pow(10, len - 1 ); + int res = (int) Math.pow(10, len - 1); return res + (int) (Math.pow(10, len - 1) * Math.random()); } diff --git a/src/com/dayrain/component/RequestType.java b/src/com/dayrain/component/RequestType.java index c5071d2..3ec5c36 100644 --- a/src/com/dayrain/component/RequestType.java +++ b/src/com/dayrain/component/RequestType.java @@ -1,5 +1,5 @@ package com.dayrain.component; public enum RequestType { - GET,POST; + GET, POST; } diff --git a/src/com/dayrain/component/Server.java b/src/com/dayrain/component/Server.java index 9158e36..621c92a 100644 --- a/src/com/dayrain/component/Server.java +++ b/src/com/dayrain/component/Server.java @@ -8,6 +8,7 @@ import java.util.concurrent.Executors; /** * 服务 + * * @author peng * @date 2021/10/25 */ @@ -31,14 +32,14 @@ public class Server implements Runnable { addContext(serverUrl); } httpServer.start(); - System.out.println("【" +serverConfig.getServerName() + "】服务已开启..."); + System.out.println("【" + serverConfig.getServerName() + "】服务已开启..."); } catch (IOException e) { e.printStackTrace(); } } public void stop() { - if(httpServer != null) { + if (httpServer != null) { System.out.println("【" + serverConfig.getServerName() + "】服务已关闭..."); httpServer.stop(0); } diff --git a/src/com/dayrain/component/ServerConfig.java b/src/com/dayrain/component/ServerConfig.java index 93945e7..8cac664 100644 --- a/src/com/dayrain/component/ServerConfig.java +++ b/src/com/dayrain/component/ServerConfig.java @@ -14,12 +14,12 @@ public class ServerConfig { /** * 请求url集合 */ - private ListserverUrls; + private List serverUrls; public ServerConfig() { } - public ServerConfig(String serverName, int port, ListserverUrls) { + public ServerConfig(String serverName, int port, List serverUrls) { this.serverName = serverName; this.port = port; this.serverUrls = serverUrls; diff --git a/src/com/dayrain/handle/AddServerHandler.java b/src/com/dayrain/handle/AddServerHandler.java index ebbd975..c0759d3 100644 --- a/src/com/dayrain/handle/AddServerHandler.java +++ b/src/com/dayrain/handle/AddServerHandler.java @@ -22,6 +22,7 @@ import java.util.ArrayList; /** * 添加server + * * @author peng * @date 2021/10/28 */ diff --git a/src/com/dayrain/handle/AddUrlHandler.java b/src/com/dayrain/handle/AddUrlHandler.java index 4ba90ca..5c27fde 100644 --- a/src/com/dayrain/handle/AddUrlHandler.java +++ b/src/com/dayrain/handle/AddUrlHandler.java @@ -29,6 +29,7 @@ import java.util.List; /** * 添加路径 + * * @author peng * @date 2021/10/27 */ @@ -61,7 +62,7 @@ public class AddUrlHandler implements EventHandler { urlField.setPrefWidth(80); Label methodLabel = LabelFactory.getLabel("请求方式:"); - ChoiceBoxchoiceBox = new ChoiceBox<>(); + ChoiceBox choiceBox = new ChoiceBox<>(); choiceBox.setItems(FXCollections.observableArrayList("POST", "GET")); choiceBox.setValue("POST"); urlField.setPrefWidth(80); @@ -103,7 +104,7 @@ public class AddUrlHandler implements EventHandler { String name = nameField.getText(); List serverUrls = serverConfig.getServerUrls(); for (ServerUrl serverUrl : serverUrls) { - if(serverUrl.getUrlName().equals(name)) { + if (serverUrl.getUrlName().equals(name)) { return; } } @@ -114,7 +115,7 @@ public class AddUrlHandler implements EventHandler { ServerUrl serverUrl = new ServerUrl(serverConfig.getServerName(), name, url, type.equals(RequestType.POST.name()) ? RequestType.POST : RequestType.GET, resp); serverUrls.add(serverUrl); ServerThread serverThread = threadMap.getOrDefault(serverConfig.getServerName(), null); - if(serverThread != null) { + if (serverThread != null) { serverThread.addContext(serverUrl); } ListViewHelper.addAndRefresh(serverUrl, serverUrlListView); diff --git a/src/com/dayrain/handle/ExportConfigHandler.java b/src/com/dayrain/handle/ExportConfigHandler.java index d9d570c..18dc6ca 100644 --- a/src/com/dayrain/handle/ExportConfigHandler.java +++ b/src/com/dayrain/handle/ExportConfigHandler.java @@ -11,6 +11,7 @@ import java.io.File; /** * 导出配置文件 + * * @author peng * @date 2021/10/28 */ @@ -31,14 +32,14 @@ public class ExportConfigHandler implements EventHandler { stage.initOwner(primaryStage); FileChooser fileChooser = new FileChooser(); String projectName = configuration.getProjectName(); - if(projectName == null) { + if (projectName == null) { projectName = ""; } projectName += "server"; fileChooser.setTitle("导出配置"); fileChooser.setInitialFileName(projectName + ".json"); File file = fileChooser.showSaveDialog(stage); - if(file != null) { + if (file != null) { FileUtils.saveConfig(configuration, file); } } diff --git a/src/com/dayrain/handle/ImportConfigHandler.java b/src/com/dayrain/handle/ImportConfigHandler.java index 96e28d6..b30ebd8 100644 --- a/src/com/dayrain/handle/ImportConfigHandler.java +++ b/src/com/dayrain/handle/ImportConfigHandler.java @@ -13,6 +13,7 @@ import java.io.File; /** * 导入配置文件 + * * @author peng * @date 2021/10/28 */ @@ -37,7 +38,7 @@ public class ImportConfigHandler implements EventHandler { fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("只能导入json文件", "*.json")); File file = fileChooser.showOpenDialog(stage); - if(file != null) { + if (file != null) { Configuration loadConfig = FileUtils.load(file); ConfigHolder.replace(loadConfig); FileUtils.saveConfig(loadConfig); diff --git a/src/com/dayrain/handle/StartServerHandler.java b/src/com/dayrain/handle/StartServerHandler.java index 2e1127e..d79607f 100644 --- a/src/com/dayrain/handle/StartServerHandler.java +++ b/src/com/dayrain/handle/StartServerHandler.java @@ -10,8 +10,10 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import java.util.HashMap; + /** * 服务启动与关闭控制器 + * * @author peng * @date 2021/10/27 */ @@ -35,15 +37,15 @@ public class StartServerHandler implements EventHandler { @Override public void handle(ActionEvent event) { String serverName = serverConfig.getServerName(); - if(threadMap.containsKey(serverName)) { + if (threadMap.containsKey(serverName)) { ServerThread serverThread = threadMap.get(serverName); - if(serverThread != null) { + if (serverThread != null) { serverThread.stopServer(); } threadMap.remove(serverName); openButton.setText("开启服务"); statusCircle.setFill(Color.RED); - }else { + } else { ServerThread serverThread = new ServerThread(new Server(serverConfig)); serverThread.start(); threadMap.put(serverName, serverThread); diff --git a/src/com/dayrain/handle/UpdateRandomLenHandler.java b/src/com/dayrain/handle/UpdateRandomLenHandler.java index 5a0c930..230e009 100644 --- a/src/com/dayrain/handle/UpdateRandomLenHandler.java +++ b/src/com/dayrain/handle/UpdateRandomLenHandler.java @@ -2,7 +2,6 @@ package com.dayrain.handle; import com.dayrain.component.ConfigHolder; import com.dayrain.component.Configuration; -import com.dayrain.component.ServerConfig; import com.dayrain.style.ButtonFactory; import com.dayrain.style.LabelFactory; import javafx.event.ActionEvent; diff --git a/src/com/dayrain/style/ButtonFactory.java b/src/com/dayrain/style/ButtonFactory.java index 79fe9c3..61e8310 100644 --- a/src/com/dayrain/style/ButtonFactory.java +++ b/src/com/dayrain/style/ButtonFactory.java @@ -18,7 +18,7 @@ public class ButtonFactory { button.setText(text); button.setPrefWidth(100); button.setPrefHeight(50); - button.setFont(Font.font("Microsoft YaHei",15)); + button.setFont(Font.font("Microsoft YaHei", 15)); button.setTextFill(Color.WHITE); button.setBackground(new Background(bgf)); return button; diff --git a/src/com/dayrain/style/LabelFactory.java b/src/com/dayrain/style/LabelFactory.java index e56b0ae..5378883 100644 --- a/src/com/dayrain/style/LabelFactory.java +++ b/src/com/dayrain/style/LabelFactory.java @@ -7,7 +7,7 @@ public class LabelFactory { public static Label getLabel(String text) { Label label = new Label(text); - label.setFont(Font.font("Microsoft YaHei",18)); + label.setFont(Font.font("Microsoft YaHei", 18)); return label; } } diff --git a/src/com/dayrain/utils/FileUtils.java b/src/com/dayrain/utils/FileUtils.java index 93e3c46..ae92a58 100644 --- a/src/com/dayrain/utils/FileUtils.java +++ b/src/com/dayrain/utils/FileUtils.java @@ -1,8 +1,6 @@ package com.dayrain.utils; -import com.dayrain.ApplicationStarter; import com.dayrain.component.Configuration; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.BufferedReader; @@ -10,22 +8,20 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; -public class FileUtils{ +public class FileUtils { - public static void saveConfig(Configuration configuration){ + public static void saveConfig(Configuration configuration) { saveConfig(configuration, getConfigFile()); } - public static void saveConfig(Configuration configuration, File file){ + public static void saveConfig(Configuration configuration, File file) { BufferedWriter bufferedWriter = null; try { @@ -35,9 +31,9 @@ public class FileUtils{ bufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); - }finally { + } finally { try { - if(bufferedWriter != null) { + if (bufferedWriter != null) { bufferedWriter.close(); } } catch (IOException e) { @@ -63,9 +59,9 @@ public class FileUtils{ return "".equals(configStr.toString()) ? new Configuration(1200, 800, 8, 8) : new ObjectMapper().readValue(configStr.toString(), Configuration.class); } catch (IOException e) { e.printStackTrace(); - }finally { + } finally { try { - if(bufferedReader != null) { + if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { @@ -78,7 +74,7 @@ public class FileUtils{ public static String getFromInputStream(InputStream inputStream) { try { - byte[]buf = new byte[4096]; + byte[] buf = new byte[4096]; int len = 0; StringBuilder stringBuilder = new StringBuilder(); while ((len = inputStream.read(buf)) != -1) { @@ -95,10 +91,10 @@ public class FileUtils{ private static File getConfigFile() { String property = System.getProperty("user.dir"); File file = new File(property + File.separator + "config" + File.separator + "config.json"); - if(!file.exists()) { + if (!file.exists()) { File dir = new File(property + File.separator + "config"); - if(!dir.exists()) { + if (!dir.exists()) { dir.mkdirs(); } diff --git a/src/com/dayrain/utils/ListViewHelper.java b/src/com/dayrain/utils/ListViewHelper.java index 641ed27..b7347e8 100644 --- a/src/com/dayrain/utils/ListViewHelper.java +++ b/src/com/dayrain/utils/ListViewHelper.java @@ -6,17 +6,17 @@ import javafx.scene.control.ListView; public class ListViewHelper { - public static void addAndRefresh(ServerUrl serverUrl, ListViewserverUrls) { + public static void addAndRefresh(ServerUrl serverUrl, ListView serverUrls) { serverUrls.getItems().add(serverUrl); refresh(serverUrls); } - public static void deleteAndRefresh(ServerUrl serverUrl, ListViewserverUrls) { + public static void deleteAndRefresh(ServerUrl serverUrl, ListView serverUrls) { serverUrls.getItems().remove(serverUrl); refresh(serverUrls); } - public static void refresh(ListViewserverUrls) { + public static void refresh(ListView serverUrls) { ObservableList items = serverUrls.getItems(); serverUrls.setItems(null); serverUrls.setItems(items); diff --git a/src/com/dayrain/views/HomePage.java b/src/com/dayrain/views/HomePage.java index 60a2909..66803f0 100644 --- a/src/com/dayrain/views/HomePage.java +++ b/src/com/dayrain/views/HomePage.java @@ -1,7 +1,5 @@ package com.dayrain.views; -import com.dayrain.handle.UpdateRandomLenHandler; -import com.dayrain.style.ButtonFactory; import com.dayrain.component.ConfigHolder; import com.dayrain.component.Configuration; import com.dayrain.component.ConsoleLog; @@ -15,11 +13,12 @@ import com.dayrain.handle.DeleteUrlHandler; import com.dayrain.handle.ExportConfigHandler; import com.dayrain.handle.ImportConfigHandler; import com.dayrain.handle.StartServerHandler; +import com.dayrain.handle.UpdateRandomLenHandler; import com.dayrain.handle.UpdateServerConfigHandler; import com.dayrain.handle.UpdateUrlHandler; import com.dayrain.server.ServerThread; +import com.dayrain.style.ButtonFactory; import com.dayrain.style.LabelFactory; -import com.dayrain.utils.FileUtils; import javafx.beans.binding.Bindings; import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -47,10 +46,8 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.text.Font; import javafx.stage.Stage; -import javafx.stage.WindowEvent; import javafx.util.Callback; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -63,8 +60,6 @@ public class HomePage { private HashMap threadMap = new HashMap<>(); - private List> listViews = new ArrayList<>(); - private VBox serverContainer; private LogArea logArea; @@ -140,7 +135,7 @@ public class HomePage { editButton.setOnAction(new UpdateServerConfigHandler(serverConfig, primaryStage)); deleteButton.setOnAction(new DeleteServerHandler(serverConfig, configuration, this)); headBox.getChildren().addAll(openButton, editButton, deleteButton, addButton, statusCircle); - HBox.setMargin(statusCircle, new Insets(0,0,0,30)); + HBox.setMargin(statusCircle, new Insets(0, 0, 0, 30)); headBox.setSpacing(20d); headBox.setAlignment(Pos.CENTER); //添加url @@ -159,7 +154,7 @@ public class HomePage { titledPane.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent event) { - if(!serverConfig.getServerName().equals(logArea.getServerName())) { + if (!serverConfig.getServerName().equals(logArea.getServerName())) { ConsoleLog.resetTextArea(serverConfig.getServerName()); } } @@ -179,15 +174,15 @@ public class HomePage { serverListViews.setCellFactory(new Callback, ListCell>() { @Override public ListCell call(ListView param) { - ListCell listCell =new ListCell() { + ListCell listCell = new ListCell() { @Override protected void updateItem(ServerUrl item, boolean empty) { super.updateItem(item, empty); - if(empty || item == null) { + if (empty || item == null) { setText(null); setGraphic(null); - }else { + } else { BorderPane urlPane = new BorderPane(); HBox labelBox = new HBox(); @@ -202,7 +197,7 @@ public class HomePage { Button deleteButton = ButtonFactory.getButton("删除"); btnBox.setSpacing(15d); btnBox.getChildren().addAll(configButton, deleteButton); - HBox.setMargin(deleteButton, new Insets(0, 20,0,0)); + HBox.setMargin(deleteButton, new Insets(0, 20, 0, 0)); deleteButton.setOnAction(new DeleteUrlHandler(item, serverConfig, serverListViews, HomePage.this)); configButton.setOnAction(new UpdateUrlHandler(item, serverListViews, primaryStage)); @@ -227,7 +222,7 @@ public class HomePage { public void refreshServerContainer() { serverContainer.getChildren().removeAll(serverContainer.getChildren()); List serverConfigs = configuration.getServerConfigs(); - if(serverConfigs == null || serverConfigs.size() == 0) { + if (serverConfigs == null || serverConfigs.size() == 0) { serverConfigs = new ArrayList<>(); configuration.setServerConfigs(serverConfigs); } @@ -258,7 +253,7 @@ public class HomePage { public Background getBackGround() { BackgroundFill backgroundFill = new BackgroundFill(Color.GRAY, new CornerRadii(1), - new Insets(0.0,0.0,0.0,0.0)); + new Insets(0.0, 0.0, 0.0, 0.0)); return new Background(backgroundFill); } } diff --git a/src/resources/config.json b/src/resources/config.json index 02dffd5..df45a57 100644 --- a/src/resources/config.json +++ b/src/resources/config.json @@ -1,21 +1,26 @@ { - "projectName" : "HTTP SERVER 模拟器", - "width" : 1200, - "height" : 800, - "serverConfigs" : [ { - "serverName" : "WMS", - "port" : 8082, - "serverUrls" : [ { - "urlName" : "登录", - "url" : "/login", - "serverName" : "WMS", - "requestType" : "POST", - "responseBody" : "{\n\t\"success\": \"ok\"\n}", - "headerMap" : null - } ] - }, { - "serverName" : "WCS", - "port" : 8083, - "serverUrls" : [ ] - } ] + "projectName": "HTTP SERVER 模拟器", + "width": 1200, + "height": 800, + "serverConfigs": [ + { + "serverName": "WMS", + "port": 8082, + "serverUrls": [ + { + "urlName": "登录", + "url": "/login", + "serverName": "WMS", + "requestType": "POST", + "responseBody": "{\n\t\"success\": \"ok\"\n}", + "headerMap": null + } + ] + }, + { + "serverName": "WCS", + "port": 8083, + "serverUrls": [] + } + ] } \ No newline at end of file