1 | package web.servlets;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.List;
|
---|
6 | import javax.servlet.RequestDispatcher;
|
---|
7 | import javax.servlet.ServletConfig;
|
---|
8 | import javax.servlet.ServletException;
|
---|
9 | import javax.servlet.http.HttpServlet;
|
---|
10 | import javax.servlet.http.HttpServletRequest;
|
---|
11 | import javax.servlet.http.HttpServletResponse;
|
---|
12 | import org.apache.commons.fileupload.FileItem;
|
---|
13 | import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
---|
14 | import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
---|
15 | import monogram.plugin.PluginConfiguration;
|
---|
16 | import util.FileUtil;
|
---|
17 | import monogram.plugin.PluginManager;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @author University of Waikato - Te Whare WÄnanga o Waikato
|
---|
21 | * @version 1.0
|
---|
22 | * @since 2014-11-20
|
---|
23 | */
|
---|
24 | public class FileUpload extends HttpServlet {
|
---|
25 |
|
---|
26 | private File tmpdir;
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public void init(ServletConfig config) throws ServletException {
|
---|
30 | super.init(config);
|
---|
31 | tmpdir = new File((String) config.getServletContext().getAttribute("tmpdir"));
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
---|
35 | throws ServletException, IOException {
|
---|
36 | //do nothing
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
41 | throws ServletException, IOException {
|
---|
42 | processRequest(request, response);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Handles the HTTP <code>POST</code> method.
|
---|
47 | * @param request servlet request
|
---|
48 | * @param response servlet response
|
---|
49 | * @throws ServletException if a servlet-specific error occurs
|
---|
50 | * @throws IOException if an I/O error occurs
|
---|
51 | */
|
---|
52 | @Override
|
---|
53 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
54 | throws ServletException, IOException {
|
---|
55 | // set DI to false as this is not Directinput.
|
---|
56 | DirectInput.DI = false;
|
---|
57 | final Properties properties = handleRequest(request);
|
---|
58 | final String address = "/jsp" + (properties.getLanguage().equals("en") ? "/en" : "/mi");
|
---|
59 | final PluginConfiguration configuration = configure(properties);
|
---|
60 | final PluginManager pluginManager = new PluginManager(tmpdir);
|
---|
61 |
|
---|
62 | if (configuration.getFile().length() == 0) {
|
---|
63 | configuration.getFile().delete();
|
---|
64 | request.setAttribute("errorCode", "FILE_NOT_FOUND_ERROR");
|
---|
65 | forward(address + "/error.jsp", request, response);
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | File restoredFile = null;
|
---|
70 | try {
|
---|
71 | restoredFile = pluginManager.run(configuration);
|
---|
72 | request.setAttribute("file", restoredFile);
|
---|
73 | request.setAttribute("fileType", configuration.getFileType());
|
---|
74 | request.setAttribute("charsetEncoding", "utf-8");
|
---|
75 | request.setAttribute("filename", properties.getFilename());
|
---|
76 | request.setAttribute("preserveMacrons", properties.getPreserveExistingMacrons());
|
---|
77 | request.setAttribute("options", properties.getOptions());
|
---|
78 | forward(address + "/main.jsp", request, response);
|
---|
79 | } catch (UnsupportedOperationException e) {
|
---|
80 | FileUtil.deleteFile(restoredFile);
|
---|
81 | request.setAttribute("errorCode", "FILE_TYPE_NOT_SUPPORTED_ERROR");
|
---|
82 | forward(address + "/error.jsp", request, response);
|
---|
83 | } catch (Exception e) {
|
---|
84 | FileUtil.deleteFile(restoredFile);
|
---|
85 | request.setAttribute("errorCode", "UNEXPECTED_ERROR");
|
---|
86 | forward(address + "/error.jsp", request, response);
|
---|
87 | } finally {
|
---|
88 | FileUtil.deleteFile(configuration.getFile());
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private void forward(String path, HttpServletRequest request, HttpServletResponse response)
|
---|
93 | throws ServletException, IOException {
|
---|
94 | final RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
|
---|
95 | dispatcher.forward(request, response);
|
---|
96 | }
|
---|
97 |
|
---|
98 | private Properties handleRequest(HttpServletRequest request) {
|
---|
99 | Properties requestData = new Properties();
|
---|
100 | DiskFileItemFactory factory = new DiskFileItemFactory();
|
---|
101 | factory.setSizeThreshold(10 * 1024 * 1024);
|
---|
102 | ServletFileUpload upload = new ServletFileUpload(factory);
|
---|
103 | try {
|
---|
104 | System.out.println("looking at request parameters...");
|
---|
105 | List<FileItem> items = upload.parseRequest(request);
|
---|
106 |
|
---|
107 | for (FileItem item : items) {
|
---|
108 | if (item.isFormField()) {
|
---|
109 | String fieldName = item.getFieldName();
|
---|
110 | String fieldValue = item.getString();
|
---|
111 | if (fieldName.equals("charsetEncoding")) {
|
---|
112 | requestData.setCharsetEncoding(fieldValue);
|
---|
113 | } else if (fieldName.equals("fileType")) {
|
---|
114 | requestData.setFileType(fieldValue);
|
---|
115 | } else if (fieldName.equals("preserveExistingMacrons")) {
|
---|
116 | requestData.setPreserveExistingMacrons(fieldValue);
|
---|
117 | } else if (fieldName.equals("lang")) {
|
---|
118 | requestData.setLanguage(fieldValue);
|
---|
119 | } else if (fieldName.equals("options")) {
|
---|
120 | requestData.setOptions(fieldValue);
|
---|
121 | }
|
---|
122 | } else {
|
---|
123 | String fileType = FileUtil.guessFileType(new File(item.getName()));
|
---|
124 | File file = File.createTempFile("mi-tmp-", fileType, tmpdir);
|
---|
125 | item.write(file);
|
---|
126 | requestData.setFile(file);
|
---|
127 | requestData.setFilename(item.getName());
|
---|
128 | }
|
---|
129 | }
|
---|
130 | } catch (Exception e) {
|
---|
131 | e.printStackTrace();
|
---|
132 | }
|
---|
133 | return requestData;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private PluginConfiguration configure(Properties properties) {
|
---|
137 | final File file = properties.getFile();
|
---|
138 | final String fileType = properties.getFileType().equals("(detect automatically)") ? FileUtil.guessFileType(file) : properties.getFileType();
|
---|
139 | final String charsetEncoding = properties.getCharsetEncoding().equals("(detect automatically)") ? "utf8" : properties.getCharsetEncoding();
|
---|
140 | final String preserveExistingMacrons = properties.getPreserveExistingMacrons();
|
---|
141 |
|
---|
142 | final PluginConfiguration configuration = new PluginConfiguration();
|
---|
143 | configuration.setFile(file);
|
---|
144 | configuration.setFileType(fileType);
|
---|
145 | configuration.setCharsetEncoding(charsetEncoding);
|
---|
146 | configuration.setPreserveExistingMacrons(Boolean.getBoolean(preserveExistingMacrons));
|
---|
147 | return configuration;
|
---|
148 | }
|
---|
149 |
|
---|
150 | private class Properties {
|
---|
151 |
|
---|
152 | private File file;
|
---|
153 | private String filename;
|
---|
154 | private String fileType;
|
---|
155 | private String charsetEncoding;
|
---|
156 | private String preserveExistingMacrons;
|
---|
157 | private String language;
|
---|
158 | private String options;
|
---|
159 |
|
---|
160 | public File getFile() {
|
---|
161 | return file;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public String getFilename() {
|
---|
165 | return filename;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public String getFileType() {
|
---|
169 | return fileType;
|
---|
170 | }
|
---|
171 |
|
---|
172 | public String getCharsetEncoding() {
|
---|
173 | return charsetEncoding;
|
---|
174 | }
|
---|
175 |
|
---|
176 | public String getPreserveExistingMacrons() {
|
---|
177 | return preserveExistingMacrons;
|
---|
178 | }
|
---|
179 |
|
---|
180 | public String getLanguage() {
|
---|
181 | return language;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public String getOptions() {
|
---|
185 | return options;
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void setFile(File file) {
|
---|
189 | this.file = file;
|
---|
190 | }
|
---|
191 |
|
---|
192 | public void setFilename(String filename) {
|
---|
193 | this.filename = filename;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void setFileType(String fileType) {
|
---|
197 | this.fileType = fileType;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void setCharsetEncoding(String charsetEncoding) {
|
---|
201 | this.charsetEncoding = charsetEncoding;
|
---|
202 | }
|
---|
203 |
|
---|
204 | public void setPreserveExistingMacrons(String preserveExistingMacrons) {
|
---|
205 | this.preserveExistingMacrons = preserveExistingMacrons;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public void setLanguage(String language) {
|
---|
209 | this.language = language;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void setOptions(String options) {
|
---|
213 | this.options = options;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|