@PostMapping("file") public String upload(@RequestParam("file") MultipartFile file, MultipartRequest multipartRequest) { System.out.println(file); DefaultMultipartHttpServletRequest request = (DefaultMultipartHttpServletRequest) multipartRequest; String fileName = request.getParameter("fileName"); StringBuilder stringBuilder = new StringBuilder("fileName" + " : " + fileName); try { InputStream inputStream = file.getInputStream(); InputStreamReader in = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(in); bufferedReader.lines().forEach(stringBuilder::append); IOUtils.closeQuietly(inputStream, in, bufferedReader); } catch (IOException e) { e.printStackTrace(); } return stringBuilder.toString(); }
@RequestMapping(value = IMAGES, method = POST) public String addImage(@RequestParam("image") MultipartFile file, DefaultMultipartHttpServletRequest request, Locale locale) throws IOException { // NOTE: Checking user rights and CSRF-Token needs to be done here because HttpUserService gains // NOTE org.eclipse.jetty.server.Request which is not able to handle parameters at multipartrequests User currentUser = userService.getCurrentUser(false); if (!currentUser.isOm()) { throw new AccessDeniedException("Om rights required"); } userService.verifyCSRFToken(request); imageFinder.validateAndSaveFile(file); return redirectWithMessage(request.getHeader("Referer"), RequestMessage.EDITOR_UPLOAD_IMAGE, request); }
@RequestMapping(value = "/runReporting", method = RequestMethod.POST) @ResponseBody public void runReporting(final HttpServletRequest req, final HttpServletResponse resp) { logger.info("Start processing the run reporting web service"); String respStr = WebServiceUtil.EMPTY_STRING; try { if (req instanceof DefaultMultipartHttpServletRequest) { InputStream instream = null; final DefaultMultipartHttpServletRequest multiPartRequest = (DefaultMultipartHttpServletRequest) req; final MultiValueMap<String, MultipartFile> fileMap = multiPartRequest.getMultiFileMap(); for (final String fileName : fileMap.keySet()) { final MultipartFile multiPartFile = multiPartRequest.getFile(fileName); instream = multiPartFile.getInputStream(); final Source source = XMLUtil.createSourceFromStream(instream); final ReportingOptions option = (ReportingOptions) batchSchemaDao.getJAXB2Template().getJaxb2Marshaller() .unmarshal(source); final String installerPath = option.getInstallerPath(); if (installerPath == null || installerPath.isEmpty() || !installerPath.toLowerCase().contains("build.xml")) { respStr = "Improper input to server. Installer path not specified or it does not contain the build.xml path."; } else { logger.info("synchronizing the database"); reportingService.syncDatabase(installerPath); break; } } } else { respStr = "Improper input to server. Expected multipart request. Returning without processing the results."; } } catch (final XmlMappingException xmle) { respStr = "Error in mapping input XML in the desired format. Please send it in the specified format. Detailed exception is " + xmle; } catch (final Exception e) { respStr = "Internal Server error.Please check logs for further details." + e; } if (!respStr.isEmpty()) { try { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, respStr); } catch (final IOException ioe) { } } }
/** * To run Reporting. * @param req {@link HttpServletRequest} * @param resp {@link HttpServletResponse} */ @RequestMapping(value = "/runReporting", method = RequestMethod.POST) @ResponseBody public void runReporting(final HttpServletRequest req, final HttpServletResponse resp) { LOGGER.info("Start processing the run reporting web service"); String respStr = WebServiceUtil.EMPTY_STRING; try { if (req instanceof DefaultMultipartHttpServletRequest) { InputStream instream = null; final DefaultMultipartHttpServletRequest multiPartRequest = (DefaultMultipartHttpServletRequest) req; final MultiValueMap<String, MultipartFile> fileMap = multiPartRequest.getMultiFileMap(); for (final String fileName : fileMap.keySet()) { final MultipartFile multiPartFile = multiPartRequest.getFile(fileName); instream = multiPartFile.getInputStream(); final Source source = XMLUtil.createSourceFromStream(instream); final ReportingOptions option = (ReportingOptions) batchSchemaDao.getJAXB2Template().getJaxb2Marshaller() .unmarshal(source); final String installerPath = option.getInstallerPath(); if (installerPath == null || installerPath.isEmpty() || !installerPath.toLowerCase(Locale.getDefault()).contains(WebServiceUtil.BUILD_XML)) { respStr = "Improper input to server. Installer path not specified or it does not contain the build.xml path."; } else { LOGGER.info("synchronizing the database"); reportingService.syncDatabase(installerPath); break; } } } else { respStr = IMPROPER_INPUT_TO_SERVER; LOGGER.error(SERVER_ERROR_MSG + respStr); } } catch (final XmlMappingException xmle) { respStr = ERROR_IN_MAPPING_INPUT + xmle; LOGGER.error(SERVER_ERROR_MSG + respStr); } catch (final Exception e) { respStr = INTERNAL_SERVER_ERROR + e; LOGGER.error(SERVER_ERROR_MSG + respStr); } if (!respStr.isEmpty()) { try { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, respStr); LOGGER.error(SERVER_ERROR_MSG + respStr); } catch (final IOException ioe) { LOGGER.info(ERROR_WHILE_SENDING_ERROR_RESPONSE_TO_CLIENT + ioe, ioe); } } }
private String validateInputAndPerformExtraction(final HttpServletRequest req, final HttpServletResponse resp, final String respStr, final String workingDir, final DefaultMultipartHttpServletRequest multiPartRequest, final Set<String> fileNameSet, final String batchClassIdentifier, final String documentType, final String hocrFileName) throws IOException, FileNotFoundException, DCMAException { InputStream instream; OutputStream outStream; String respStrLocal = respStr; try { BatchClass batchClass = bcService.getBatchClassByIdentifier(batchClassIdentifier); if (batchClass == null) { respStrLocal = "Please enter valid batch class identifier"; } else { Set<String> loggedInUserRole = getUserRoles(req); if (!isBatchClassViewableToUser(batchClassIdentifier, loggedInUserRole, isSuperAdmin(req))) { respStrLocal = USER_NOT_AUTHORIZED_TO_VIEW_THE_BATCH_CLASS + batchClassIdentifier; LOGGER.error(SERVER_ERROR_MSG + respStrLocal); } else { BatchPlugin regularRegexPlugin = batchClassPPService.getPluginProperties(batchClassIdentifier, "REGULAR_REGEX_EXTRACTION"); if (regularRegexPlugin == null || regularRegexPlugin.getPropertiesSize() == 0) { respStrLocal = "Fuzzy DB plugin is not configured for batch class : " + batchClassIdentifier + " . Please select proper batch class"; LOGGER.error(SERVER_ERROR_MSG + respStrLocal); } for (final String fileName : fileNameSet) { if (fileName.equalsIgnoreCase(hocrFileName)) { LOGGER.info("hocr file name found : " + hocrFileName); final MultipartFile multiPartFile = multiPartRequest.getFile(fileName); instream = multiPartFile.getInputStream(); final File file = new File(workingDir + File.separator + fileName); outStream = new FileOutputStream(file); final byte[] buf = new byte[WebServiceUtil.bufferSize]; int len = instream.read(buf); while (len > 0) { outStream.write(buf, 0, len); len = instream.read(buf); } break; } } final File hocrFile = new File(workingDir + File.separator + hocrFileName); if (hocrFile.exists()) { respStrLocal = performRegexExtraction(resp, workingDir, hocrFile.getAbsolutePath(), batchClassIdentifier, documentType); } } } } catch (Exception exception) { LOGGER.error("Error occurred while extracting fields using regular regex extarction."); throw new DCMAException(exception.getMessage(), exception); } return respStrLocal; }