Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/main/java/com/laker/postman/model/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Data;

import java.nio.file.Path;

// Workspace模型类
@Data
public class Workspace {
Expand All @@ -10,7 +12,7 @@ public class Workspace {
private String description; // 工作空间描述
private WorkspaceType type; // LOCAL/GIT
private GitRepoSource gitRepoSource; // 仓库来源:INITIALIZED(本地初始化)/ CLONED(远程克隆)
private String path; // 本地路径
private Path path; // 本地路径
private String gitRemoteUrl; // Git远程仓库地址
private String currentBranch; // 当前分支名称
private String remoteBranch; // 当前跟踪的远程分支名称
Expand All @@ -23,4 +25,5 @@ public class Workspace {
private GitAuthType gitAuthType; // 认证类型
private String sshPrivateKeyPath; // SSH 私钥文件路径
private String sshPassphrase; // SSH 私钥密码(可选)

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -93,7 +94,7 @@ private JScrollPane getTreeScrollPane() {
rootTreeNode = new DefaultMutableTreeNode(ROOT);
treeModel = new DefaultTreeModel(rootTreeNode);
Workspace currentWorkspace = WorkspaceService.getInstance().getCurrentWorkspace();
String filePath = SystemUtil.getCollectionPathForWorkspace(currentWorkspace);
Path filePath = SystemUtil.getCollectionPathForWorkspace(currentWorkspace);
// 初始化持久化工具
persistence = new RequestsPersistence(filePath, rootTreeNode, treeModel);
// 创建树组件
Expand Down Expand Up @@ -806,7 +807,7 @@ public void locateAndSelectRequest(String requestId) {
/**
* 切换到指定工作区的请求集合文件,并刷新树UI
*/
public void switchWorkspaceAndRefreshUI(String collectionFilePath) {
public void switchWorkspaceAndRefreshUI(Path collectionFilePath) {
if (persistence != null) {
persistence.setDataFilePath(collectionFilePath);
}
Expand Down Expand Up @@ -950,7 +951,7 @@ private void performCollectionMove(DefaultMutableTreeNode collectionNode, Worksp
DefaultMutableTreeNode copiedNode = deepCopyGroupNode(collectionNode);

// 2. 获取目标工作区的集合文件路径
String targetCollectionPath = SystemUtil.getCollectionPathForWorkspace(targetWorkspace);
Path targetCollectionPath = SystemUtil.getCollectionPathForWorkspace(targetWorkspace);

// 3. 创建目标工作区的持久化工具
DefaultMutableTreeNode targetRootNode = new DefaultMutableTreeNode(ROOT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -704,7 +705,7 @@ private void syncComboBoxOrder() {
/**
* 切换到指定工作区的环境数据文件,并刷新UI
*/
public void switchWorkspaceAndRefreshUI(String envFilePath) {
public void switchWorkspaceAndRefreshUI(Path envFilePath) {
EnvironmentService.setDataFilePath(envFilePath);
this.refreshUI();
// 同步刷新顶部环境下拉框
Expand Down Expand Up @@ -856,10 +857,10 @@ private void performEnvironmentMove(Environment environment, Workspace targetWor
}

// 2. 获取目标工作区的环境文件路径
String targetEnvPath = SystemUtil.getEnvPathForWorkspace(targetWorkspace);
Path targetEnvPath = SystemUtil.getEnvPathForWorkspace(targetWorkspace);

// 3. 临时切换到目标工作区的环境服务
String originalDataFilePath = EnvironmentService.getDataFilePath();
Path originalDataFilePath = EnvironmentService.getDataFilePath();
try {
// 切换到目标工作区
EnvironmentService.setDataFilePath(targetEnvPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void addFileMenu() {

private void openLogDirectory() {
try {
Desktop.getDesktop().open(new File(SystemUtil.LOG_DIR));
Desktop.getDesktop().open(SystemUtil.LOG_DIR.toFile());
} catch (IOException ex) {
log.error("Failed to open log directory", ex);
JOptionPane.showMessageDialog(null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;

public class SettingManager {
private static final String CONFIG_FILE = SystemUtil.getUserHomeEasyPostmanPath() + "easy_postman_settings.properties";
private static final Path CONFIG_FILE = SystemUtil.EASY_POSTMAN_HOME.resolve("easy_postman_settings.properties");
private static final Properties props = new Properties();

// 私有构造函数,防止实例化
Expand All @@ -23,7 +24,7 @@ private SettingManager() {
}

public static void load() {
File file = new File(CONFIG_FILE);
File file = CONFIG_FILE.toFile();
if (file.exists()) {
try (FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
Expand All @@ -34,7 +35,7 @@ public static void load() {
}

public static void save() {
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE)) {
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE.toFile())) {
props.store(fos, "EasyPostman Settings");
} catch (IOException e) {
// ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* 创建工作区对话框
Expand Down Expand Up @@ -120,16 +123,16 @@ private void initComponents() {
* 设置默认的工作区路径
*/
private void setDefaultWorkspacePath() {
String defaultWorkspaceDir = SystemUtil.getUserHomeEasyPostmanPath() + WORKSPACES;
pathField.setText(defaultWorkspaceDir);
Path defaultWorkspaceDir = SystemUtil.EASY_POSTMAN_HOME.resolve(WORKSPACES);
pathField.setText(defaultWorkspaceDir.toString());
}

/**
* 根据工作区名称生成符合路径规则的目录名
*/
private String generatePathFromName(String name) {
private Path generatePathFromName(String name) {
if (name == null || name.trim().isEmpty()) {
return "";
return null;
}

// 清理名称,生成合法的目录名
Expand All @@ -143,7 +146,7 @@ private String generatePathFromName(String name) {
cleanName = RandomUtil.randomString(10); // 如果清理后为空,使用随机字符串
}

return SystemUtil.getUserHomeEasyPostmanPath() + WORKSPACES + File.separator + cleanName;
return SystemUtil.EASY_POSTMAN_HOME.resolve(WORKSPACES).resolve(cleanName);
}

@Override
Expand Down Expand Up @@ -256,8 +259,8 @@ public void changedUpdate(DocumentEvent e) {
*/
private void updatePathFromName() {
String name = nameField.getText();
String generatedPath = generatePathFromName(name);
pathField.setText(generatedPath);
Path generatedPath = generatePathFromName(name);
pathField.setText(generatedPath == null ? "":generatedPath.toString());
}

private JPanel createBasicInfoPanel() {
Expand Down Expand Up @@ -426,17 +429,18 @@ private void browseForPath(ActionEvent e) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogTitle(I18nUtil.getMessage(MessageKeys.WORKSPACE_SELECT_PATH));

String currentPath = pathField.getText().trim();
if (currentPath.isEmpty()) {
currentPath = SystemUtil.getUserHomeEasyPostmanPath() + WORKSPACES;
String currentPathStr = pathField.getText().trim();
Path currentPath = SystemUtil.EASY_POSTMAN_HOME.resolve(WORKSPACES);
if (!currentPathStr.isEmpty()) {
currentPath = Paths.get(currentPathStr);
}

// 确保父目录存在
File parentDir = new File(currentPath).getParentFile();
if (parentDir != null && parentDir.exists()) {
chooser.setCurrentDirectory(parentDir);
Path parentDir = currentPath.getParent();
if (parentDir != null && Files.exists(parentDir)) {
chooser.setCurrentDirectory(parentDir.toFile());
} else {
chooser.setCurrentDirectory(new File(SystemUtil.getUserHomeEasyPostmanPath()));
chooser.setCurrentDirectory(SystemUtil.EASY_POSTMAN_HOME.toFile());
}

int result = chooser.showOpenDialog(this);
Expand Down Expand Up @@ -558,7 +562,7 @@ private Workspace buildWorkspace() {
Workspace ws = new Workspace();
ws.setName(nameField.getText().trim());
ws.setDescription(descriptionArea.getText().trim());
ws.setPath(pathField.getText().trim());
ws.setPath(Paths.get(pathField.getText().trim()));

if (localTypeRadio.isSelected()) {
ws.setType(WorkspaceType.LOCAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public WorkspaceDetailPanel(Workspace workspace) {
gbc.gridx = 1;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel pathLabel = new JLabel(workspace.getPath());
JLabel pathLabel = new JLabel(workspace.getPath().toString());
infoSection.add(pathLabel, gbc);

// 描述
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -23,7 +24,7 @@
*/
@Slf4j
public class ClientCertificateService {
private static final String CERT_CONFIG_FILE = SystemUtil.getUserHomeEasyPostmanPath() + "client_certificates.json";
private static final Path CERT_CONFIG_FILE = SystemUtil.EASY_POSTMAN_HOME.resolve("client_certificates.json");
private static final List<ClientCertificate> certificates = new CopyOnWriteArrayList<>();

private ClientCertificateService() {
Expand All @@ -38,7 +39,7 @@ private ClientCertificateService() {
* 从文件加载证书配置
*/
public static void load() {
File file = new File(CERT_CONFIG_FILE);
File file = CERT_CONFIG_FILE.toFile();
if (!file.exists()) {
log.info("Client certificate config file not found, creating new one");
return;
Expand All @@ -63,7 +64,7 @@ public static void load() {
*/
public static void save() {
try {
File file = new File(CERT_CONFIG_FILE);
File file = CERT_CONFIG_FILE.toFile();
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
boolean created = parent.mkdirs();
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/laker/postman/service/EnvironmentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -42,7 +43,7 @@ private EnvironmentService() {
private static final ThreadLocal<Map<String, String>> temporaryVariables = ThreadLocal.withInitial(ConcurrentHashMap::new);

// 当前数据文件路径
private static String currentDataFilePath;
private static Path currentDataFilePath;

static {
loadEnvironments();
Expand All @@ -68,7 +69,7 @@ public static void clearTemporaryVariables() {
/**
* 获取当前数据文件路径
*/
public static String getDataFilePath() {
public static Path getDataFilePath() {
if (currentDataFilePath != null) {
return currentDataFilePath;
}
Expand All @@ -80,17 +81,17 @@ public static String getDataFilePath() {
/**
* 切换环境变量数据文件路径,并重新加载
*/
public static void setDataFilePath(String path) {
if (path == null || path.isBlank()) return;
public static void setDataFilePath(Path path) {
if (path == null) return;
currentDataFilePath = path;
loadEnvironmentsFromPath(path);
}

/**
* 从指定路径加载环境变量
*/
private static void loadEnvironmentsFromPath(String filePath) {
File file = new File(filePath);
private static void loadEnvironmentsFromPath(Path filePath) {
File file = filePath.toFile();
if (!file.exists()) {
log.info("环境变量文件不存在: {}, 将创建默认环境", filePath);
createDefaultEnvironments();
Expand Down Expand Up @@ -129,7 +130,7 @@ public static void loadEnvironments() {
loadEnvironmentsFromPath(currentDataFilePath);
} else {
Workspace currentWorkspace = WorkspaceService.getInstance().getCurrentWorkspace();
String filePath = SystemUtil.getEnvPathForWorkspace(currentWorkspace);
Path filePath = SystemUtil.getEnvPathForWorkspace(currentWorkspace);
loadEnvironmentsFromPath(filePath);
}
}
Expand Down Expand Up @@ -165,15 +166,15 @@ private static void createDefaultEnvironments() {
*/
public static void saveEnvironments() {
try {
String filePath;
Path filePath;
if (currentDataFilePath != null) {
filePath = currentDataFilePath;
} else {
Workspace currentWorkspace = WorkspaceService.getInstance().getCurrentWorkspace();
filePath = SystemUtil.getEnvPathForWorkspace(currentWorkspace);
}

File file = new File(filePath);
File file = filePath.toFile();
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
@Slf4j
public class HistoryPersistenceManager {
private static final String HISTORY_FILE = SystemUtil.getUserHomeEasyPostmanPath() + "request_history.json";
private static final Path HISTORY_FILE = SystemUtil.EASY_POSTMAN_HOME.resolve("request_history.json");

private static HistoryPersistenceManager instance;
private final List<RequestHistoryItem> historyItems = new CopyOnWriteArrayList<>();
Expand All @@ -45,7 +45,7 @@ public static synchronized HistoryPersistenceManager getInstance() {

private void ensureHistoryDirExists() {
try {
Path historyDir = Paths.get(SystemUtil.getUserHomeEasyPostmanPath());
Path historyDir = SystemUtil.EASY_POSTMAN_HOME;
if (!Files.exists(historyDir)) {
Files.createDirectories(historyDir);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public void saveHistory() {

// 写入文件
String jsonString = JSONUtil.toJsonPrettyStr(jsonArray);
Files.writeString(Paths.get(HISTORY_FILE), jsonString, StandardCharsets.UTF_8);
Files.writeString(HISTORY_FILE, jsonString, StandardCharsets.UTF_8);
} catch (IOException e) {
log.error("Failed to save history: {}", e.getMessage());
}
Expand All @@ -123,13 +123,13 @@ private void saveHistoryAsync() {
* 加载历史记录
*/
private void loadHistory() {
File file = new File(HISTORY_FILE);
File file = HISTORY_FILE.toFile();
if (!file.exists()) {
return;
}

try {
String jsonString = Files.readString(Paths.get(HISTORY_FILE), StandardCharsets.UTF_8);
String jsonString = Files.readString(HISTORY_FILE, StandardCharsets.UTF_8);
if (jsonString.trim().isEmpty()) {
return;
}
Expand Down
Loading
Loading