99 lines
3.4 KiB
Java
99 lines
3.4 KiB
Java
package com.sftp.manager.controller;
|
|
|
|
import com.sftp.manager.dto.ApiResponse;
|
|
import com.sftp.manager.dto.ConnectionRequest;
|
|
import com.sftp.manager.model.Connection;
|
|
import com.sftp.manager.service.ConnectionService;
|
|
import com.sftp.manager.service.SessionManager;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/connection")
|
|
@CrossOrigin(origins = "*")
|
|
public class ConnectionController {
|
|
|
|
@Autowired
|
|
private ConnectionService connectionService;
|
|
|
|
@Autowired
|
|
private SessionManager sessionManager;
|
|
|
|
@PostMapping("/connect")
|
|
public ApiResponse<String> connect(@RequestBody ConnectionRequest request) {
|
|
try {
|
|
String sessionId = connectionService.connect(request);
|
|
return ApiResponse.success("连接成功", sessionId);
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("连接失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@PostMapping("/disconnect")
|
|
public ApiResponse<Void> disconnect(@RequestBody Map<String, String> request) {
|
|
try {
|
|
String sessionId = request.get("sessionId");
|
|
connectionService.disconnect(sessionId);
|
|
return ApiResponse.success("断开成功", null);
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("断开失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@PostMapping("/save")
|
|
public ApiResponse<Connection> saveConnection(@RequestBody Connection connection) {
|
|
try {
|
|
Connection saved = connectionService.saveConnection(connection);
|
|
return ApiResponse.success("保存成功", saved);
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("保存失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@GetMapping("/list")
|
|
public ApiResponse<List<Connection>> listConnections() {
|
|
try {
|
|
List<Connection> connections = connectionService.listConnections();
|
|
return ApiResponse.success("查询成功", connections);
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("查询失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public ApiResponse<Connection> getConnection(@PathVariable Long id) {
|
|
try {
|
|
Connection connection = connectionService.getConnectionById(id);
|
|
if (connection != null) {
|
|
return ApiResponse.success("查询成功", connection);
|
|
} else {
|
|
return ApiResponse.error("连接不存在");
|
|
}
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("查询失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public ApiResponse<Void> deleteConnection(@PathVariable Long id) {
|
|
try {
|
|
connectionService.deleteConnection(id);
|
|
return ApiResponse.success("删除成功", null);
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("删除失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
@GetMapping("/active")
|
|
public ApiResponse<Map<String, Connection>> getActiveConnections() {
|
|
try {
|
|
return ApiResponse.success("查询成功", sessionManager.getAllActiveConnections());
|
|
} catch (Exception e) {
|
|
return ApiResponse.error("查询失败: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|