-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathUCFController.java
More file actions
190 lines (147 loc) · 5.89 KB
/
Copy pathUCFController.java
File metadata and controls
190 lines (147 loc) · 5.89 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.cellocad.api;
import org.cellocad.MIT.dnacompiler.Args;
import org.cellocad.MIT.dnacompiler.UCF;
import org.cellocad.MIT.dnacompiler.Util;
import org.cellocad.adaptors.ucfadaptor.UCFReader;
import org.cellocad.adaptors.ucfadaptor.UCFValidator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
@RestController
public class UCFController extends BaseController {
@RequestMapping(value="/ucf",method= RequestMethod.GET, produces = "application/json")
public @ResponseBody
JSONObject getUCFFiles(
@RequestHeader("Authorization") String basic
) {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String keyword = "UCF";
String extension = "json";
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="/ucf/{filename:.+}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
JSONObject getUCF(
@RequestHeader("Authorization") String basic,
@PathVariable("filename") String filename,
@RequestParam String owner
) throws IOException, ParseException {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String filePath = "";
if(owner.equals("default")) {
filePath = _srcPath + "/resources/UCF/" + filename;
}
else if(!username.equals(owner)) {
throw new CelloUnauthorizedException("owner is not the authenticated user.");
}
else {
filePath = _resultPath + "/" + username + "/" + filename;
}
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
JSONObject response = new JSONObject();
response.put("ucf", jsonArray);
return response;
}
@RequestMapping(value="/ucf/{filename:.+}",method= RequestMethod.POST)
public @ResponseBody
String postUCF(
@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="/ucf/{filename:.+}/validate",method= RequestMethod.GET, produces = "application/json")
public @ResponseBody
JSONObject validateUploadedUCF(
@RequestHeader("Authorization") String basic,
@PathVariable String filename
) throws IOException {
if(!auth.login(basic)) {
throw new CelloUnauthorizedException("invalid username/password");
}
String username = auth.getUsername(basic);
String filePath = _resultPath + "/" + username + "/" + filename;
UCFReader ucf_reader = new UCFReader();
UCF ucf = ucf_reader.readAllCollections(filePath);
UCFValidator ucf_validator = new UCFValidator();
JSONObject ucf_validation_map = ucf_validator.validateAllUCFCollections(ucf, new Args());
boolean is_ucf_valid = (boolean) ucf_validation_map.get("is_valid");
JSONObject toReturn = new JSONObject();
if(is_ucf_valid) {
try {
toReturn.put("status", "VALID");
toReturn.put("filepath", filename);
}catch(Exception e) {
e.printStackTrace();
}
}
else {
try {
toReturn.put("status", "INVALID");
toReturn.put("filepath", filename);
}catch(Exception e) {
e.printStackTrace();
}
}
return toReturn;
}
@RequestMapping(value="/ucf/{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;
Util.deleteFile(new File(filepath));
JSONObject toReturn = new JSONObject();
toReturn.put("message", "deleted file " + filename);
return toReturn.toJSONString();
}
}