- Timestamp:
- 2021-11-03T16:18:48+13:00 (2 years ago)
- Location:
- other-projects/the-macronizer/trunk/src/java/web/servlets
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/the-macronizer/trunk/src/java/web/servlets/DirectInput.java
r35719 r35720 85 85 86 86 final String fragment = request.getParameter("fragment"); 87 final Boolean showAdvancedOptions = request.getParameter("options") != null && request.getParameter("options").equals("true");88 final Boolean shouldPreserveMacrons = request.getParameter("preserveExistingMacrons") != null && request.getParameter("preserveExistingMacrons").equals("true");87 final Boolean showAdvancedOptions = Boolean.parseBoolean(request.getParameter("options")); 88 final Boolean shouldPreserveMacrons = Boolean.parseBoolean(request.getParameter("preserveExistingMacrons")); 89 89 final OutputType outputType = OutputType.parse(request.getParameter("o")); 90 90 91 91 // Note that the lang parameter might be null if the request hasn't been made from an internal JSP page 92 final String lang = request.getParameter("lang"); 93 final String forwardPath = lang != null ? "/jsp/" + lang : "/jsp/mi"; 92 final String forwardPath = request.getParameter("lang") != null 93 ? "/jsp/" + request.getParameter("lang") 94 : "/jsp/mi"; 94 95 95 96 try -
other-projects/the-macronizer/trunk/src/java/web/servlets/FileUpload.java
r35719 r35720 72 72 throws IllegalStateException, IOException, ServletException 73 73 { 74 final OutputType outputType = OutputType.parse(request.getParameter("o")); // TODO: This probably won't work with the multipart request75 74 List<FileItem> formItems = null; 75 Properties properties = null; 76 76 File restoredFile = null; 77 77 PluginConfiguration configuration = null; 78 Properties properties = null;79 String forwardPath = "/jsp/mi";80 78 81 79 try 82 80 { 83 properties = handleRequest(request);84 forwardPath = properties.getLanguage() != null ? "/jsp/" + properties.getLanguage() : forwardPath;81 formItems = parseFormItems(request); 82 properties = getRequestProperties(formItems); 85 83 } 86 84 catch (Exception ex) 87 85 { 88 setError(outputType, forwardPath, "INVALID_REQUEST", request, response);86 throw new ServletException("Expected a multipart/form-data request.", ex); 89 87 } 90 88 91 89 try 92 90 { 91 Boolean foundFile = getRequestFile(formItems, properties); 92 if (!foundFile) 93 { 94 setError(properties, "FILE_NOT_FOUND_ERROR", request, response, true); 95 return; 96 } 97 } 98 catch (Exception ex) 99 { 100 ex.printStackTrace(); 101 logger.error("Failed to get uploaded file", ex); 102 setError(properties, "UNEXPECTED_ERROR", request, response, false); 103 } 104 105 try 106 { 93 107 configuration = configure(properties); 94 108 … … 96 110 { 97 111 configuration.getFile().delete(); 98 setError( outputType, forwardPath, "FILE_NOT_FOUND_ERROR", request, response);112 setError(properties, "FILE_NOT_FOUND_ERROR", request, response, false); 99 113 return; 100 114 } … … 106 120 request.setAttribute("filename", properties.getFilename()); 107 121 request.setAttribute("preserveMacrons", properties.getPreserveExistingMacrons()); 108 request.setAttribute("options", properties.getOptions()); 109 forward(forwardPath + "/main.jsp", request, response); 122 request.setAttribute("options", properties.getShowAdvancedOptions()); 123 124 forward(properties.getJspForwardPath() + "/main.jsp", request, response); 110 125 } 111 126 catch (UnsupportedOperationException uoex) … … 113 128 FileUtil.deleteFile(restoredFile); 114 129 logger.error("Failed to restore macrons on a file", uoex); 115 setError( outputType, forwardPath, "FILE_TYPE_NOT_SUPPORTED_ERROR", request, response);130 setError(properties, "FILE_TYPE_NOT_SUPPORTED_ERROR", request, response, false); 116 131 } 117 132 catch (Exception ex) … … 120 135 ex.printStackTrace(); 121 136 logger.error("Failed to restore macrons on a file", ex); 122 setError( outputType, forwardPath, "UNEXPECTED_ERROR", request, response);137 setError(properties, "UNEXPECTED_ERROR", request, response, false); 123 138 } 124 139 finally … … 127 142 { 128 143 FileUtil.deleteFile(configuration.getFile()); 129 } 130 } 131 } 132 133 private void setError(OutputType outputType, String forwardPath, String errorCode, HttpServletRequest request, HttpServletResponse response) 144 // We don't clean up the file held by the properties object, 145 // as the configuration holds a reference to said file 146 } 147 } 148 } 149 150 private void setError( 151 Properties properties, 152 String errorCode, 153 HttpServletRequest request, 154 HttpServletResponse response, 155 Boolean isClientError) 134 156 throws IOException, ServletException 135 157 { 136 switch ( outputType) {158 switch (properties.getOutputType()) { 137 159 case Json: 138 160 // 500 Internal Server Error 139 response.sendError( 500, errorCode);161 response.sendError(isClientError ? 400 : 500, errorCode); 140 162 break; 141 163 default: 142 164 request.setAttribute("errorMessage", errorCode); 143 forward( forwardPath+ "/error.jsp", request, response);165 forward(properties.getJspForwardPath()+ "/error.jsp", request, response); 144 166 break; 145 167 } … … 161 183 } 162 184 163 private Properties handleRequest(HttpServletRequest request) 164 throws Exception, FileUploadException, IOException 165 { 166 Properties requestData = new Properties(); 185 /** 186 * Parses multipart items from a request. 187 * @param request The request 188 * @return The parsed multipart items. 189 * @throws FileUploadException 190 */ 191 private List<FileItem> parseFormItems(HttpServletRequest request) 192 throws FileUploadException 193 { 167 194 DiskFileItemFactory factory = new DiskFileItemFactory(); 168 195 factory.setSizeThreshold(10 * 1024 * 1024); … … 174 201 } 175 202 176 for (FileItem item : items) 203 return items; 204 } 205 206 /** 207 * Gets the properties of the request by checking the submitted form items. 208 * @param formItems The multipart form items. 209 * @return The properties. 210 */ 211 private Properties getRequestProperties(List<FileItem> formItems) 212 { 213 Properties properties = new Properties(); 214 215 for (FileItem item : formItems) 216 { 217 if (!item.isFormField()) 218 { 219 continue; 220 } 221 222 String fieldName = item.getFieldName(); 223 String fieldValue = item.getString(); 224 225 switch (fieldName) 226 { 227 case "charsetEncoding": 228 properties.setCharsetEncoding(fieldValue); 229 break; 230 case "fileType": 231 properties.setFileType(fieldValue); 232 break; 233 case "preserveExistingMacrons": 234 properties.setPreserveExistingMacrons(fieldValue); 235 break; 236 case "lang": 237 properties.setLanguage(fieldValue); 238 break; 239 case "options": 240 properties.setShowAdvancedOptions(fieldValue); 241 break; 242 case "o": 243 properties.setOutputType(fieldValue); 244 break; 245 } 246 } 247 248 return properties; 249 } 250 251 /** 252 * Gets the first file contained in the submitted form items. 253 * @param formItems The multipart form items. 254 * @return The file, or <code>null</code> if the request contained no files. 255 * @throws Exception 256 * @throws IOException 257 */ 258 private Boolean getRequestFile(List<FileItem> formItems, Properties properties) 259 throws Exception, IOException 260 { 261 for (FileItem item : formItems) 177 262 { 178 263 if (item.isFormField()) 179 264 { 180 String fieldName = item.getFieldName(); 181 String fieldValue = item.getString(); 182 if (fieldName.equals("charsetEncoding")) { 183 requestData.setCharsetEncoding(fieldValue); 184 } else if (fieldName.equals("fileType")) { 185 requestData.setFileType(fieldValue); 186 } else if (fieldName.equals("preserveExistingMacrons")) { 187 requestData.setPreserveExistingMacrons(fieldValue); 188 } else if (fieldName.equals("lang")) { 189 requestData.setLanguage(fieldValue); 190 } else if (fieldName.equals("options")) { 191 requestData.setOptions(fieldValue); 192 } 193 } 194 else 195 { 196 String fileType = FileUtil.guessFileType(new File(item.getName())); 197 File file = File.createTempFile(FileUtil.TMP_FILE_PREFIX, fileType, tmpdir); 198 item.write(file); 199 requestData.setFile(file); 200 requestData.setFilename(item.getName()); 201 } 202 } 203 204 return requestData; 265 continue; 266 } 267 268 String fileType = FileUtil.guessFileType(new File(item.getName())); 269 File file = File.createTempFile(FileUtil.TMP_FILE_PREFIX, fileType, tmpdir); 270 item.write(file); 271 272 properties.setFile(file); 273 properties.setFilename(item.getName()); 274 275 return true; 276 } 277 278 return false; 205 279 } 206 280 … … 233 307 final String fileType = properties.getFileType().equals("(detect automatically)") ? FileUtil.guessFileType(file) : properties.getFileType(); 234 308 final String charsetEncoding = properties.getCharsetEncoding().equals("(detect automatically)") ? "utf8" : properties.getCharsetEncoding(); 235 final String preserveExistingMacrons = properties.getPreserveExistingMacrons();236 309 237 310 final PluginConfiguration configuration = new PluginConfiguration(); … … 239 312 configuration.setFileType(fileType); 240 313 configuration.setCharsetEncoding(charsetEncoding); 241 configuration.setPreserveExistingMacrons( Boolean.getBoolean(preserveExistingMacrons));314 configuration.setPreserveExistingMacrons(properties.getPreserveExistingMacrons()); 242 315 243 316 return configuration; … … 250 323 private String fileType; 251 324 private String charsetEncoding; 252 private StringpreserveExistingMacrons;325 private Boolean preserveExistingMacrons; 253 326 private String language; 254 private String options; 327 private Boolean showAdvancedOptions; 328 private OutputType outputType; 255 329 256 330 public File getFile() { … … 270 344 } 271 345 272 public StringgetPreserveExistingMacrons() {346 public Boolean getPreserveExistingMacrons() { 273 347 return preserveExistingMacrons; 274 348 } 275 349 276 @SuppressWarnings("unused")277 350 public String getLanguage() { 278 351 return language; 279 352 } 280 353 281 public String getOptions() { 282 return options; 354 public Boolean getShowAdvancedOptions() { 355 return showAdvancedOptions; 356 } 357 358 public OutputType getOutputType() { 359 return outputType; 360 } 361 362 public String getJspForwardPath() { 363 return getLanguage() != null 364 ? "/jsp/" + getLanguage() 365 : "/jsp/mi"; 283 366 } 284 367 … … 300 383 301 384 public void setPreserveExistingMacrons(String preserveExistingMacrons) { 302 this.preserveExistingMacrons = preserveExistingMacrons;385 this.preserveExistingMacrons = Boolean.parseBoolean(preserveExistingMacrons); 303 386 } 304 387 … … 307 390 } 308 391 309 public void setOptions(String options) { 310 this.options = options; 392 public void setShowAdvancedOptions(String options) { 393 this.showAdvancedOptions = Boolean.parseBoolean(options); 394 } 395 396 public void setOutputType(String outputType) { 397 this.outputType = OutputType.parse(outputType); 311 398 } 312 399 }
Note:
See TracChangeset
for help on using the changeset viewer.