Skip to content

feat(common): Add HttpTool #2142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
386 changes: 386 additions & 0 deletions common/src/main/java/com/tlcsdm/jfxcommon/debug/HttpTool.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private void addPort(String portString, List<String> portsList) {
}
}

public void setTableColumnMapValueFactory(TableColumn tableColumn, String name, boolean isEdit,
private void setTableColumnMapValueFactory(TableColumn tableColumn, String name, boolean isEdit,
Runnable onEditCommitHandle) {
tableColumn.setCellValueFactory(new MapValueFactory(name));
tableColumn.setCellFactory(TextFieldTableCell.<Map<String, String>>forTableColumn());
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
requires org.apache.commons.text;
requires java.net.http;
requires org.apache.commons.configuration2;
requires org.fxmisc.flowless;

exports com.tlcsdm.jfxcommon;
exports com.tlcsdm.jfxcommon.provider to com.tlcsdm.frame;
Expand Down
176 changes: 176 additions & 0 deletions common/src/main/resources/com/tlcsdm/jfxcommon/fxml/httpTool.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright (c) 2024 unknowIfGuestInDream.
~ All rights reserved.
~
~ Redistribution and use in source and binary forms, with or without
~ modification, are permitted provided that the following conditions are met:
~ * Redistributions of source code must retain the above copyright
~ notice, this list of conditions and the following disclaimer.
~ * Redistributions in binary form must reproduce the above copyright
~ notice, this list of conditions and the following disclaimer in the
~ documentation and/or other materials provided with the distribution.
~ * Neither the name of unknowIfGuestInDream, any associated website, nor the
~ names of its contributors may be used to endorse or promote products
~ derived from this software without specific prior written permission.
~
~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
~ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
~ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
~ DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
~ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
~ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
~ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
~ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
~ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
~ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->


<?import com.tlcsdm.core.javafx.richtext.InformationArea?>
<?import com.tlcsdm.core.javafx.richtext.JsonCodeArea?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.flowless.VirtualizedScrollPane?>
<AnchorPane prefHeight="520.0" prefWidth="854.0" xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.tlcsdm.jfxcommon.debug.HttpTool">
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
<top>
<HBox alignment="CENTER" spacing="5.0" BorderPane.alignment="CENTER">
<children>
<Label text="Url:"/>
<TextField fx:id="urlTextField" promptText="请输入Url"
text="https://api.github.com" HBox.hgrow="ALWAYS"/>
<Label text="Method:"/>
<ChoiceBox fx:id="methodChoiceBox" prefWidth="150.0"/>
<Button fx:id="sendButton" mnemonicParsing="false" onAction="#sendAction" text="Send"/>
<Button fx:id="toBrowerButton" mnemonicParsing="false" onAction="#toBrowerAction"
text="在浏览器中打开"/>
</children>
</HBox>
</top>
<left>
<VBox spacing="5.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" spacing="5.0">
<children>
<CheckBox fx:id="paramsDataCheckBox" mnemonicParsing="false" selected="true"
text="请求参数"/>
<Button fx:id="addParamsDataButton" mnemonicParsing="false"
onAction="#addParamsDataAction" text="添加"/>
<CheckBox fx:id="paramsDataIsStringCheckBox" mnemonicParsing="false" text="字符串"/>
</children>
</HBox>
<StackPane>
<children>
<TextArea fx:id="paramsDataTextArea" prefHeight="200.0" prefWidth="200.0"
visible="false"/>
<TableView fx:id="paramsDataTableView" editable="true">
<columns>
<TableColumn fx:id="paramsDataNameTableColumn" prefWidth="59.0" text="参数名"/>
<TableColumn fx:id="paramsDataValueTableColumn" prefWidth="73.0" text="参数值"/>
<TableColumn fx:id="paramsDataRemarkTableColumn" prefWidth="75.0" text="备注"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
</children>
</StackPane>
<HBox alignment="CENTER_LEFT" spacing="5.0">
<children>
<CheckBox fx:id="paramsHeaderCheckBox" mnemonicParsing="false" selected="true"
text="请求Header"/>
<Button fx:id="addParamsHeaderButton" mnemonicParsing="false"
onAction="#addParamsHeaderAction" text="添加"/>
</children>
</HBox>
<TableView fx:id="paramsHeaderTableView" editable="true">
<columns>
<TableColumn fx:id="paramsHeaderNameTableColumn" prefWidth="75.0" text="参数名"/>
<TableColumn fx:id="paramsHeaderValueTableColumn" prefWidth="75.0" text="参数值"/>
<TableColumn fx:id="paramsHeaderRemarkTableColumn" prefWidth="75.0" text="备注"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
<HBox alignment="CENTER_LEFT" spacing="5.0">
<children>
<CheckBox fx:id="paramsCookieCheckBox" mnemonicParsing="false" selected="true"
text="请求Cookie"/>
<Button fx:id="addParamsCookieButton" mnemonicParsing="false"
onAction="#addParamsCookieAction" text="添加"/>
</children>
</HBox>
<TableView fx:id="paramsCookieTableView" editable="true">
<columns>
<TableColumn fx:id="paramsCookieNameTableColumn" prefWidth="75.0" text="参数名"/>
<TableColumn fx:id="paramsCookieValueTableColumn" prefWidth="75.0" text="参数值"/>
<TableColumn fx:id="paramsCookieRemarkTableColumn" prefWidth="75.0" text="备注"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
</children>
</VBox>
</left>
<center>
<TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE"
BorderPane.alignment="CENTER">
<tabs>
<Tab text="响应内容">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<VirtualizedScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<JsonCodeArea fx:id="ResponseBodyTextArea"/>
</content>
</VirtualizedScrollPane>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="响应头部">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<VirtualizedScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<InformationArea fx:id="ResponseHeaderTextArea"/>
</content>
</VirtualizedScrollPane>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
<BorderPane.margin>
<Insets left="10.0" top="10.0"/>
</BorderPane.margin>
</TabPane>
</center>
</BorderPane>
</children>
</AnchorPane>
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private File findToolExecutable(String toolName, String relativePath) {
return file;
}

StaticLog.warn("Not found " + toolName + ".");
StaticLog.debug("Not found " + toolName + ".");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static URL getUrlFromName(String name) {
case "convert" -> ImageViewHelper.class.getResource("/com/tlcsdm/core/static/icon/convert.png");
case "url" -> ImageViewHelper.class.getResource("/com/tlcsdm/core/static/icon/url.png");
case "debug" -> ImageViewHelper.class.getResource("/com/tlcsdm/core/static/icon/debug.png");
case "http" -> ImageViewHelper.class.getResource("/com/tlcsdm/core/static/icon/http.png");
default -> throw new IllegalStateException("Unexpected value: " + name);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ private StyleSpans<Collection<String>> computeHighlighting(String text) {
return spansBuilder.create();
}

public void setText(String text) {
replaceText(0, getLength(), text);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2025 unknowIfGuestInDream.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.tlcsdm.core.javafx.richtext;

import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;

import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* SON Code Area
*
* @author unknowIfGuestInDream
*/
public class JsonCodeArea extends CodeArea {

//匹配 JSON 的 key
private static final String KEY_PATTERN = "\"(\\w+)\"(?=\\s*:)";
//匹配 JSON 的 value(支持字符串、数字、布尔值和 null)
private static final String VALUE_PATTERN = ":\\s*(\"(?:\\\\.|[^\\\\\"])*\"|\\d+|true|false|null)";
//匹配 JSON 的结构符号(如大括号、中括号、逗号等)
private static final String BRACE_PATTERN = "\\{|\\}";
private static final String BRACKET_PATTERN = "\\[|\\]";
private static final String COMMA_PATTERN = ",";
private static final String COLON_PATTERN = ":";

private static final Pattern PATTERN = Pattern.compile(
"(?<KEY>" + KEY_PATTERN + ")" +
"|(?<VALUE>" + VALUE_PATTERN + ")" +
"|(?<BRACE>" + BRACE_PATTERN + ")" +
"|(?<BRACKET>" + BRACKET_PATTERN + ")" +
"|(?<COMMA>" + COMMA_PATTERN + ")" +
"|(?<COLON>" + COLON_PATTERN + ")");

public JsonCodeArea() {
super();
getStyleClass().add("text-json-area");
getStylesheets().add(
getClass().getResource("/com/tlcsdm/core/static/javafx/richtext/json-keywords.css").toExternalForm());

Check warning on line 69 in core/src/main/java/com/tlcsdm/core/javafx/richtext/JsonCodeArea.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `toExternalForm` may produce `NullPointerException`
this.setParagraphGraphicFactory(LineNumberFactory.get(this));
this.textProperty().addListener((obs, oldText, newText) -> {
this.setStyleSpans(0, computeHighlighting(newText));
});
}

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
while (matcher.find()) {
String styleClass = matcher.group("KEY") != null ? "key"
: matcher.group("VALUE") != null ? "value"
: matcher.group("BRACE") != null ? "brace"
: matcher.group("BRACKET") != null ? "bracket"
: matcher.group("COMMA") != null ? "comma"
: matcher.group("COLON") != null ? "colon" : null;
assert styleClass != null;
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}

public void setText(String text) {
replaceText(0, getLength(), text);
}
}
Loading
Loading