Skip to content

Commit 3e02b19

Browse files
authored
feat(http): 新增HTTP文件导入功能,idea 支持的 http 文件请求格式 (#49)
* feat(http): 新增HTTP文件导入功能 - 实现了HttpFileParser类,用于解析.http文件格式 - 添加了对REST Client格式的完整支持,包括请求分隔符、HTTP方法、URL、请求头和请求体 - 支持多种HTTP方法(GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS) - 实现了Content-Type识别和不同body类型的处理(raw、urlencoded) - 支持Basic和Bearer认证方式的解析 - 添加了对SSE和WebSocket协议的自动识别 - 在左侧集合面板中增加了HTTP文件导入菜单项 - 实现了文件选择对话框,限制只显示.http文件 - 添加了完整的单元测试覆盖各种解析场景 - 增加了国际化消息键值和英文翻译 - 升级 lombok.version 到 1.18.42,支持高版本 jdk * chore(deps): 移除没有使用到的 hutool-http 依赖
1 parent 1c213cb commit 3e02b19

File tree

7 files changed

+961
-7
lines changed

7 files changed

+961
-7
lines changed

pom.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<okhttp.version>4.12.0</okhttp.version>
2626
<polyglot.version>24.2.1</polyglot.version>
2727
<org.eclipse.jgit.version>7.3.0.202506031305-r</org.eclipse.jgit.version>
28+
<lombok.version>1.18.42</lombok.version>
2829
</properties>
2930
<dependencies>
3031

@@ -58,11 +59,6 @@
5859
<artifactId>hutool-crypto</artifactId>
5960
<version>${hutool.version}</version>
6061
</dependency>
61-
<dependency>
62-
<groupId>cn.hutool</groupId>
63-
<artifactId>hutool-http</artifactId>
64-
<version>${hutool.version}</version>
65-
</dependency>
6662
<dependency>
6763
<groupId>cn.hutool</groupId>
6864
<artifactId>hutool-system</artifactId>
@@ -72,7 +68,7 @@
7268
<dependency>
7369
<groupId>org.projectlombok</groupId>
7470
<artifactId>lombok</artifactId>
75-
<version>1.18.34</version>
71+
<version>${lombok.version}</version>
7672
<scope>provided</scope>
7773
</dependency>
7874

src/main/java/com/laker/postman/panel/collections/left/LeftTopPanel.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.laker.postman.service.curl.CurlParser;
1818
import com.laker.postman.service.har.HarParser;
1919
import com.laker.postman.service.http.HttpUtil;
20+
import com.laker.postman.service.httpfile.HttpFileParser;
2021
import com.laker.postman.service.postman.PostmanCollectionParser;
2122
import com.laker.postman.util.I18nUtil;
2223
import com.laker.postman.util.MessageKeys;
@@ -149,12 +150,16 @@ private JPopupMenu getImportMenu() {
149150
JMenuItem importHarItem = new JMenuItem(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_HAR),
150151
new FlatSVGIcon("icons/har.svg", 20, 20));
151152
importHarItem.addActionListener(e -> importHarCollection());
153+
JMenuItem importHttpItem = new JMenuItem(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_HTTP),
154+
new FlatSVGIcon("icons/http.svg", 20, 20));
155+
importHttpItem.addActionListener(e -> importHttpFile());
152156
JMenuItem importCurlItem = new JMenuItem(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_CURL),
153157
new FlatSVGIcon("icons/curl.svg", 20, 20));
154158
importCurlItem.addActionListener(e -> importCurlToCollection(null));
155159
importMenu.add(importEasyToolsItem);
156160
importMenu.add(importPostmanItem);
157161
importMenu.add(importHarItem);
162+
importMenu.add(importHttpItem);
158163
importMenu.add(importCurlItem);
159164
return importMenu;
160165
}
@@ -254,6 +259,47 @@ private void importHarCollection() {
254259
}
255260
}
256261

262+
// 导入HTTP文件
263+
private void importHttpFile() {
264+
RequestCollectionsLeftPanel leftPanel = SingletonFactory.getInstance(RequestCollectionsLeftPanel.class);
265+
MainFrame mainFrame = SingletonFactory.getInstance(MainFrame.class);
266+
JFileChooser fileChooser = new JFileChooser();
267+
fileChooser.setDialogTitle(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_HTTP_DIALOG_TITLE));
268+
// 设置文件过滤器,只显示 .http 文件
269+
javax.swing.filechooser.FileFilter httpFilter = new javax.swing.filechooser.FileFilter() {
270+
@Override
271+
public boolean accept(File f) {
272+
return f.isDirectory() || f.getName().toLowerCase().endsWith(".http");
273+
}
274+
275+
@Override
276+
public String getDescription() {
277+
return "HTTP Files (*.http)";
278+
}
279+
};
280+
fileChooser.setFileFilter(httpFilter);
281+
int userSelection = fileChooser.showOpenDialog(mainFrame);
282+
if (userSelection == JFileChooser.APPROVE_OPTION) {
283+
File fileToOpen = fileChooser.getSelectedFile();
284+
try {
285+
String content = FileUtil.readString(fileToOpen, StandardCharsets.UTF_8);
286+
DefaultMutableTreeNode collectionNode = HttpFileParser.parseHttpFile(content);
287+
if (collectionNode != null) {
288+
leftPanel.getRootTreeNode().add(collectionNode);
289+
leftPanel.getTreeModel().reload();
290+
leftPanel.getPersistence().saveRequestGroups();
291+
leftPanel.getRequestTree().expandPath(new TreePath(collectionNode.getPath()));
292+
NotificationUtil.showSuccess(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_SUCCESS));
293+
} else {
294+
NotificationUtil.showError(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_HTTP_INVALID));
295+
}
296+
} catch (Exception ex) {
297+
log.error("Import HTTP file error", ex);
298+
NotificationUtil.showError(I18nUtil.getMessage(MessageKeys.COLLECTIONS_IMPORT_FAIL, ex.getMessage()));
299+
}
300+
}
301+
}
302+
257303
private void importCurlToCollection(String defaultCurl) {
258304
MainFrame mainFrame = SingletonFactory.getInstance(MainFrame.class);
259305
String curlText;

0 commit comments

Comments
 (0)