整理格式

This commit is contained in:
peng
2021-10-29 10:52:57 +08:00
parent b74033acf1
commit d674b43a16
20 changed files with 86 additions and 98 deletions

View File

@@ -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) {

View File

@@ -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();

View File

@@ -14,7 +14,7 @@ public class Configuration {
private int intLen;
private List<ServerConfig>serverConfigs;
private List<ServerConfig> serverConfigs;
public Configuration() {
}

View File

@@ -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);
}

View File

@@ -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());
}

View File

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

View File

@@ -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);
}

View File

@@ -14,12 +14,12 @@ public class ServerConfig {
/**
* 请求url集合
*/
private List<ServerUrl>serverUrls;
private List<ServerUrl> serverUrls;
public ServerConfig() {
}
public ServerConfig(String serverName, int port, List<ServerUrl>serverUrls) {
public ServerConfig(String serverName, int port, List<ServerUrl> serverUrls) {
this.serverName = serverName;
this.port = port;
this.serverUrls = serverUrls;

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
/**
* 添加server
*
* @author peng
* @date 2021/10/28
*/

View File

@@ -29,6 +29,7 @@ import java.util.List;
/**
* 添加路径
*
* @author peng
* @date 2021/10/27
*/
@@ -61,7 +62,7 @@ public class AddUrlHandler implements EventHandler<ActionEvent> {
urlField.setPrefWidth(80);
Label methodLabel = LabelFactory.getLabel("请求方式:");
ChoiceBox<String>choiceBox = new ChoiceBox<>();
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.setItems(FXCollections.observableArrayList("POST", "GET"));
choiceBox.setValue("POST");
urlField.setPrefWidth(80);
@@ -103,7 +104,7 @@ public class AddUrlHandler implements EventHandler<ActionEvent> {
String name = nameField.getText();
List<ServerUrl> 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<ActionEvent> {
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);

View File

@@ -11,6 +11,7 @@ import java.io.File;
/**
* 导出配置文件
*
* @author peng
* @date 2021/10/28
*/
@@ -31,14 +32,14 @@ public class ExportConfigHandler implements EventHandler<ActionEvent> {
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);
}
}

View File

@@ -13,6 +13,7 @@ import java.io.File;
/**
* 导入配置文件
*
* @author peng
* @date 2021/10/28
*/
@@ -37,7 +38,7 @@ public class ImportConfigHandler implements EventHandler<ActionEvent> {
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);

View File

@@ -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<ActionEvent> {
@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);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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();
}

View File

@@ -6,17 +6,17 @@ import javafx.scene.control.ListView;
public class ListViewHelper {
public static void addAndRefresh(ServerUrl serverUrl, ListView<ServerUrl>serverUrls) {
public static void addAndRefresh(ServerUrl serverUrl, ListView<ServerUrl> serverUrls) {
serverUrls.getItems().add(serverUrl);
refresh(serverUrls);
}
public static void deleteAndRefresh(ServerUrl serverUrl, ListView<ServerUrl>serverUrls) {
public static void deleteAndRefresh(ServerUrl serverUrl, ListView<ServerUrl> serverUrls) {
serverUrls.getItems().remove(serverUrl);
refresh(serverUrls);
}
public static void refresh(ListView<ServerUrl>serverUrls) {
public static void refresh(ListView<ServerUrl> serverUrls) {
ObservableList<ServerUrl> items = serverUrls.getItems();
serverUrls.setItems(null);
serverUrls.setItems(items);

View File

@@ -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<String, ServerThread> threadMap = new HashMap<>();
private List<ListView<ServerUrl>> 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<MouseEvent>() {
@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<ListView<ServerUrl>, ListCell<ServerUrl>>() {
@Override
public ListCell<ServerUrl> call(ListView<ServerUrl> param) {
ListCell<ServerUrl> listCell =new ListCell<ServerUrl>() {
ListCell<ServerUrl> listCell = new ListCell<ServerUrl>() {
@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<ServerConfig> 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);
}
}

View File

@@ -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": []
}
]
}