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