source: other-projects/the-macronizer/trunk/src/java/web/servlets/FileUpload.java@ 31962

Last change on this file since 31962 was 31962, checked in by rmw36, 7 years ago

Macronizer changes: 1. adding an ethical disclaimer to the main web page (temporarily still in English for the Maori jsp file too, but Te Taka will be translating this.) 2. Adding more logging to the macronizer. This required setting up a log4j.properties file (and adding its jar file copied from GS3), and modifying the existing java code DirectInput and FileUpload to make calls to logger. Modifications to build.xml to copy the new template log4j.properties.in file into web/WEB-INF/classes where the log4j.props needs to live. 3. Corrected the svn mime-type property on build.xml so that it's no longer mistaken for a binary file

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