-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathInOutController.java
More file actions
148 lines (118 loc) · 4.47 KB
/
Copy pathInOutController.java
File metadata and controls
148 lines (118 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package org.cellocad.api;
import org.cellocad.MIT.dnacompiler.Util;
import org.json.simple.JSONObject;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
@RestController
public class InOutController extends BaseController {
@RequestMapping(value = "/in_out", method = RequestMethod.GET, produces = "application/json")
public
@ResponseBody
JSONObject getResultFiles(
@RequestHeader("Authorization") String basic,
@RequestParam Map<String, String> params
) {
if (!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String keyword = "";
String extension = "";
if (params.containsKey("keyword")) {
keyword = params.get("keyword");
}
if (params.containsKey("extension")) {
extension = params.get("extension");
}
String filePath = _resultPath + "/" + username;
File f = new File(filePath);
if (f.exists()) {
ArrayList<String> fileNames = new ArrayList<>();
File files[] = f.listFiles();
for (int i = 0; i < files.length; ++i) {
if (files[i].isDirectory()) {
continue;
}
String fileName = files[i].getName();
if (fileName.contains(keyword) && fileName.endsWith(extension)) {
fileNames.add(fileName);
}
}
String[] fileArray = fileNames.toArray(new String[fileNames.size()]);
JSONObject response = new JSONObject();
response.put("files", fileArray);
return response;
} else {
return null;
}
}
@RequestMapping(value = "/in_out/{filename:.+}", method = RequestMethod.GET, produces = "text/plain")
public
@ResponseBody
String getResultFile(
@RequestHeader("Authorization") String basic,
@PathVariable("filename") String filename
) {
if (!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String filePath = _resultPath + "/" + username + "/" + filename;
String fileContents = "";
try {
fileContents = readFile(filePath, Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
return fileContents;
}
private static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
@RequestMapping(value = "/in_out/{filename:.+}", method = RequestMethod.POST)
public
@ResponseBody
String writeFile(
@RequestHeader("Authorization") String basic,
@PathVariable("filename") String filename,
@RequestParam String filetext
) {
if (!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String filePath = _resultPath + "/" + username + "/" + filename;
Util.fileWriter(filePath, filetext, false);
JSONObject response = new JSONObject();
response.put("message", "wrote file " + filename);
return response.toJSONString();
}
@RequestMapping(value = "/in_out/{filename:.+}", method = RequestMethod.DELETE)
public
@ResponseBody
String deleteFile(
@RequestHeader("Authorization") String basic,
@PathVariable("filename") String filename
) {
if (!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String filepath = _resultPath + "/" + username + "/" + filename;
File f = new File(filepath);
if (!f.exists()) {
throw new CelloNotFoundException("file " + filename + " not found");
}
Util.deleteFile(new File(filepath));
JSONObject response = new JSONObject();
response.put("message", "deleted file " + filename);
return response.toJSONString();
}
}