Revize 05ad5e0f
Přidáno uživatelem Patrik Harag před téměř 6 roky(ů)
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.offscreen.servlets; |
2 | 2 |
|
3 |
import cz.zcu.kiv.imiger.spi.IModule; |
|
3 | 4 |
import cz.zcu.kiv.offscreen.modularization.ModuleProvider; |
4 | 5 |
import cz.zcu.kiv.offscreen.storage.FileLoader; |
5 | 6 |
import cz.zcu.kiv.offscreen.user.DataAccessException; |
6 | 7 |
import cz.zcu.kiv.offscreen.user.dao.DiagramDAO; |
8 |
import org.apache.commons.fileupload.FileUploadException; |
|
7 | 9 |
import org.apache.commons.lang3.StringUtils; |
8 | 10 |
import org.apache.logging.log4j.LogManager; |
9 | 11 |
import org.apache.logging.log4j.Logger; |
... | ... | |
13 | 15 |
import javax.servlet.http.HttpServletRequest; |
14 | 16 |
import javax.servlet.http.HttpServletResponse; |
15 | 17 |
import java.io.IOException; |
18 |
import java.io.UnsupportedEncodingException; |
|
16 | 19 |
import java.util.ArrayList; |
17 | 20 |
import java.util.List; |
18 | 21 |
import java.util.Map; |
22 |
import java.util.regex.Matcher; |
|
23 |
import java.util.regex.Pattern; |
|
19 | 24 |
|
20 |
/** |
|
21 |
* |
|
22 |
* @author Jindra PavlĂková <jindra.pav2@seznam.cz> |
|
23 |
*/ |
|
24 | 25 |
public class UploadFiles extends BaseServlet { |
25 | 26 |
private static final Logger logger = LogManager.getLogger(); |
26 | 27 |
|
... | ... | |
59 | 60 |
@Override |
60 | 61 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
61 | 62 |
logger.debug("Processing request"); |
62 |
Map<String, String> data = new FileLoader().loadFile(request); |
|
63 |
try { |
|
64 |
FileLoader fileLoader = new FileLoader(request); |
|
65 |
Map<String, String> formFields = fileLoader.loadFormFields(); |
|
66 |
String fileType = formFields.get("fileFormat"); |
|
67 |
String fileName = formFields.get("filename"); |
|
63 | 68 |
|
64 |
if(!data.containsKey("file") || !data.containsKey("fileFormat")) { |
|
65 |
request.setAttribute("errorMessage", "<strong>Invalid request!</strong><br/>"); |
|
66 |
logger.debug("Invalid request"); |
|
67 |
doGet(request, response); |
|
69 |
if (fileType == null || fileName == null) { |
|
70 |
throw new IllegalArgumentException("Missing parameter"); |
|
71 |
} |
|
68 | 72 |
|
69 |
} else if (StringUtils.isBlank(data.get("file"))) { |
|
70 |
request.setAttribute("errorMessage", "<strong>Unsupported file</strong><br/>"); |
|
71 |
logger.debug("Empty diagram string"); |
|
72 |
doGet(request, response); |
|
73 |
} else { |
|
74 |
request.getSession().setAttribute("diagram_string", data.get("file")); |
|
75 |
request.getSession().setAttribute("diagram_type", data.get("fileFormat")); |
|
76 |
request.getSession().setAttribute("diagram_filename", data.get("filename")); |
|
73 |
Pattern pattern = getFileNamePatternForType(fileType); |
|
74 |
Matcher matcher = pattern.matcher(fileName); |
|
75 |
if (!matcher.matches()) { |
|
76 |
throw new IllegalArgumentException( |
|
77 |
"Filename '" + fileName + "' does not match file type pattern: " + pattern); |
|
78 |
} |
|
79 |
|
|
80 |
String fileContent = fileLoader.loadFileAsString(fileName); |
|
81 |
if (fileContent == null || StringUtils.isBlank(fileContent)) { |
|
82 |
throw new IllegalArgumentException("Empty file!"); |
|
83 |
} |
|
84 |
|
|
85 |
request.getSession().setAttribute("diagram_string", fileContent); |
|
86 |
request.getSession().setAttribute("diagram_type", fileType); |
|
87 |
request.getSession().setAttribute("diagram_filename", fileName); |
|
77 | 88 |
response.sendRedirect(getServletContext().getInitParameter("APP_HOME_URL") + "graph"); |
78 | 89 |
logger.debug("send redirect to /graph"); |
90 |
|
|
91 |
} catch (IllegalArgumentException | UnsupportedEncodingException e) { |
|
92 |
logger.debug("Invalid request"); |
|
93 |
request.setAttribute("errorMessage", "<strong>" + e.getMessage() + "</strong><br/>"); |
|
94 |
doGet(request, response); |
|
95 |
} catch (FileUploadException e) { |
|
96 |
logger.debug("Invalid request"); |
|
97 |
request.setAttribute("errorMessage", "<strong>File upload failed!</strong><br/>"); |
|
98 |
doGet(request, response); |
|
79 | 99 |
} |
80 | 100 |
logger.debug("Request processed"); |
81 | 101 |
} |
102 |
|
|
103 |
private Pattern getFileNamePatternForType(String fileType) { |
|
104 |
if (fileType.equals("raw")) { |
|
105 |
return Pattern.compile("(?s).+\\.json"); |
|
106 |
} else { |
|
107 |
IModule module = ModuleProvider.getInstance().getModules().get(fileType); |
|
108 |
if (module != null) { |
|
109 |
return module.getFileNamePattern(); |
|
110 |
} else { |
|
111 |
throw new IllegalArgumentException("No loader available for type: " + fileType); |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
82 | 116 |
} |
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/storage/FileLoader.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.offscreen.storage; |
2 | 2 |
|
3 | 3 |
import org.apache.commons.fileupload.FileItem; |
4 |
import org.apache.commons.fileupload.FileUploadException; |
|
4 | 5 |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
5 | 6 |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
6 |
import org.apache.logging.log4j.LogManager; |
|
7 |
import org.apache.logging.log4j.Logger; |
|
8 | 7 |
|
9 | 8 |
import javax.servlet.http.HttpServletRequest; |
9 |
import java.io.UnsupportedEncodingException; |
|
10 | 10 |
import java.util.HashMap; |
11 | 11 |
import java.util.List; |
12 | 12 |
import java.util.Map; |
... | ... | |
14 | 14 |
public class FileLoader { |
15 | 15 |
|
16 | 16 |
private static final int MAX_FILE_SIZE = 512000000; |
17 |
private static final String[] ACCEPTED_EXTENSIONS_FILE = {"json", "dot"}; |
|
18 | 17 |
|
19 |
private static final Logger logger = LogManager.getLogger(); |
|
18 |
private final List<FileItem> fileItems; |
|
19 |
|
|
20 |
public FileLoader(HttpServletRequest request) throws FileUploadException { |
|
21 |
DiskFileItemFactory factory = new DiskFileItemFactory(); |
|
22 |
ServletFileUpload upload = new ServletFileUpload(factory); |
|
23 |
upload.setSizeMax(MAX_FILE_SIZE); |
|
24 |
|
|
25 |
this.fileItems = upload.parseRequest(request); |
|
26 |
} |
|
20 | 27 |
|
21 | 28 |
/** |
22 | 29 |
* Get multipart data from request which was send and return it as a map where key is field name from form and |
... | ... | |
24 | 31 |
* |
25 | 32 |
* @return loaded multipart data in map or empty map. |
26 | 33 |
*/ |
27 |
public Map<String, String> loadFile(HttpServletRequest request){ |
|
28 |
|
|
29 |
DiskFileItemFactory factory = new DiskFileItemFactory(); |
|
30 |
|
|
31 |
ServletFileUpload upload = new ServletFileUpload(factory); |
|
32 |
upload.setSizeMax(MAX_FILE_SIZE); |
|
33 |
|
|
34 |
public Map<String, String> loadFormFields() throws FileUploadException { |
|
34 | 35 |
Map<String, String> resultMap = new HashMap<>(); |
35 |
|
|
36 |
try { |
|
37 |
|
|
38 |
List<FileItem> fileItems = upload.parseRequest(request); |
|
39 |
for (FileItem item : fileItems) { |
|
40 |
if (item.isFormField()) { |
|
41 |
resultMap.put(item.getFieldName(), item.getString()); |
|
42 |
} else { |
|
43 |
resultMap.put("filename", item.getName()); |
|
44 |
if (isAcceptedExtension(item.getName()) && item.getSize() > 0) { |
|
45 |
logger.debug(item.getName() + " - " + item.getContentType()); |
|
46 |
resultMap.put(item.getFieldName(), item.getString("UTF-8")); |
|
47 |
} else { |
|
48 |
resultMap.put(item.getFieldName(), ""); |
|
49 |
} |
|
50 |
} |
|
36 |
for (FileItem item : fileItems) { |
|
37 |
if (item.isFormField()) { |
|
38 |
resultMap.put(item.getFieldName(), item.getString()); |
|
39 |
} else { |
|
40 |
resultMap.put("filename", item.getName()); |
|
51 | 41 |
} |
52 |
|
|
53 |
} catch (Exception ex) { |
|
54 |
logger.error("Can not load file from request: ", ex); |
|
55 | 42 |
} |
56 | 43 |
return resultMap; |
57 | 44 |
} |
58 | 45 |
|
59 |
/** |
|
60 |
* Check if input string ends with allowed extension. |
|
61 |
* |
|
62 |
* @param componentName all name of file |
|
63 |
* @return true - if extension is allowed, false - otherwise. |
|
64 |
*/ |
|
65 |
private boolean isAcceptedExtension(String componentName) { |
|
66 |
boolean accepted = false; |
|
67 |
for(String extension : ACCEPTED_EXTENSIONS_FILE) { |
|
68 |
accepted = componentName.endsWith(extension); |
|
69 |
|
|
70 |
if(accepted) { |
|
71 |
break; |
|
46 |
public String loadFileAsString(String name) throws UnsupportedEncodingException { |
|
47 |
for (FileItem item : fileItems) { |
|
48 |
if (!item.isFormField() && item.getName().equals(name)) { |
|
49 |
return item.getString("UTF-8"); |
|
72 | 50 |
} |
73 | 51 |
} |
74 |
return accepted;
|
|
52 |
return null;
|
|
75 | 53 |
} |
54 |
|
|
76 | 55 |
} |
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/DOT.java | ||
---|---|---|
7 | 7 |
import cz.zcu.kiv.imiger.plugin.dot.loader.DigraphDOTLoader; |
8 | 8 |
import cz.zcu.kiv.imiger.spi.IModule; |
9 | 9 |
import cz.zcu.kiv.imiger.vo.Graph; |
10 |
import java.util.regex.Pattern; |
|
10 | 11 |
|
11 | 12 |
/** |
12 | 13 |
* IModule implementation for DOT converter. |
... | ... | |
23 | 24 |
return "DOT file"; |
24 | 25 |
} |
25 | 26 |
|
27 |
@Override |
|
28 |
public Pattern getFileNamePattern() { |
|
29 |
return Pattern.compile("(?s).+\\.(dot|gv)"); |
|
30 |
} |
|
31 |
|
|
26 | 32 |
/** |
27 | 33 |
* Retrieves DOT file which has to be converted to raw JSON that |
28 | 34 |
* IMiGEr support. |
... | ... | |
39 | 45 |
if(!graph.getVertices().isEmpty()) { |
40 | 46 |
Gson gson = new Gson(); |
41 | 47 |
return gson.toJson(graph, Graph.class); |
42 |
} |
|
43 |
|
|
44 |
else { |
|
48 |
} else { |
|
45 | 49 |
return ""; |
46 | 50 |
} |
47 | 51 |
} |
sources/imiger-module/src/main/java/cz/zcu/kiv/imiger/spi/IModule.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.imiger.spi; |
2 | 2 |
|
3 |
import java.util.regex.Pattern; |
|
4 |
|
|
3 | 5 |
/** |
4 | 6 |
* Service Provider Interface (SPI) that must be implemented by all modules. |
5 | 7 |
*/ |
... | ... | |
9 | 11 |
*/ |
10 | 12 |
String getModuleName(); |
11 | 13 |
|
14 |
/** |
|
15 |
* Returns diagram file name pattern. |
|
16 |
* |
|
17 |
* @return pattern |
|
18 |
*/ |
|
19 |
Pattern getFileNamePattern(); |
|
20 |
|
|
12 | 21 |
/** |
13 | 22 |
* Converts input string in any format to raw JSON processable by IMiGEr. |
14 | 23 |
* |
sources/imiger-plugin-template/src/main/java/cz/zcu/kiv/imiger/plugin/template/Converter.java | ||
---|---|---|
3 | 3 |
import com.google.gson.Gson; |
4 | 4 |
import cz.zcu.kiv.imiger.spi.IModule; |
5 | 5 |
import cz.zcu.kiv.imiger.vo.Graph; |
6 |
import java.util.regex.Pattern; |
|
6 | 7 |
|
7 | 8 |
public class Converter implements IModule { |
8 | 9 |
@Override |
... | ... | |
10 | 11 |
return "Module Template"; |
11 | 12 |
} |
12 | 13 |
|
14 |
@Override |
|
15 |
public Pattern getFileNamePattern() { |
|
16 |
return Pattern.compile("(?s).+\\.json"); |
|
17 |
} |
|
18 |
|
|
13 | 19 |
/** |
14 | 20 |
* Convert input file to RAW JSON and return it. |
15 | 21 |
*/ |
sources/imiger-spade-converter/src/main/java/cz/zcu/kiv/imiger/plugin/spade/Spade.java | ||
---|---|---|
5 | 5 |
import cz.zcu.kiv.imiger.plugin.spade.graph.GraphManager; |
6 | 6 |
import cz.zcu.kiv.imiger.plugin.spade.graph.loader.GraphJSONDataLoader; |
7 | 7 |
import cz.zcu.kiv.imiger.plugin.spade.graph.loader.JSONConfigLoader; |
8 |
import java.util.regex.Pattern; |
|
8 | 9 |
import net.sf.json.JSONObject; |
9 | 10 |
|
10 | 11 |
public class Spade implements IModule { |
... | ... | |
13 | 14 |
return "Spade JSON"; |
14 | 15 |
} |
15 | 16 |
|
17 |
@Override |
|
18 |
public Pattern getFileNamePattern() { |
|
19 |
return Pattern.compile("(?s).+\\.json"); |
|
20 |
} |
|
21 |
|
|
16 | 22 |
/** |
17 | 23 |
* Convert input spade JSON to RAW JSON. |
18 | 24 |
* |
Také k dispozici: Unified diff
Refactor file uploading (#7267)