private void loadKeys() throws Exception { InputStream in = new FileInputStream(prvFile); byte[] prvBytes = StreamUtils.getBytes(in); IOUtils.closeQuietly(in); in = new FileInputStream(pubFile); byte[] pubBytes = StreamUtils.getBytes(in); IOUtils.closeQuietly(in); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(prvBytes); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); privateKey = kf.generatePrivate(privateKeySpec); publicKey = kf.generatePublic(publicKeySpec); }
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { log.debug("Start doPost"); try { // Inputstream final InputStream is = request.getInputStream(); final byte[] buffer = StreamUtils.getBytes(is); String filename; String croppedname; if (request.getParameter("croppedname") != null) { croppedname = request.getParameter("croppedname"); } else { croppedname = "cropped"; } if ((request.getParameter("filename") != null) && (request.getParameter("filetype") != null)) { filename = croppedname + request.getParameter("filename") + "." + request.getParameter("filetype"); } else { filename = "image.png"; } log.info("Filename: " + filename); ResponseUtils.streamToOut(filename, buffer, response, request.getSession().getServletContext(), true); } catch (Exception ex) { log.warn("Failure reading the request"); log.warn(ex.getMessage(), ex); } }
@Override public void addData(CrawleableUri uri, InputStream stream) { try { addData(uri, StreamUtils.getBytes(stream)); } catch (IOException e) { LOGGER.error("Error while reading data from stream. The data won't be stored.", e); } }
@Override public void addData(CrawleableUri uri, InputStream stream) { OutputStream outputStream = getStream(uri); if (outputStream != null) { try { StreamUtils.copy(stream, outputStream); } catch (Exception e) { LOGGER.error("Exception while writing unstructed data to file.", e); } } }
public byte[] getBytesForFileName(String filename) throws Exception { if (Utils.isEmpty(filename)) { throw new IllegalArgumentException("File name cannot be null"); } byte[] fileContent = null; ByteArrayInputStream bis = new ByteArrayInputStream(getUnpackagedZipFileAsBytes()); ZipInputStream zis = new ZipInputStream(bis); try { for (;;) { ZipEntry ze = zis.getNextEntry(); if (ze == null) { break; } fileContent = StreamUtils.getBytes(zis); String name = ze.getName(); if (ze.isDirectory()) { continue; } if (filename.endsWith(name)) { if (logger.isDebugEnabled()) { logger.debug("Found '" + name + "' body size [" + fileContent.length + "]"); } return fileContent; } } } finally { zis.close(); } return fileContent; }
private void createTempJSPFile(ActionInvocation invocation) throws IOException { try { if (!jspFileRecreated) { InputStream is = getClass().getResourceAsStream("/com/googlecode/s2hibernate/struts2/plugin/pages/management.jsp"); byte[] bytes = StreamUtils.getBytes(is); String jspPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/"); jspPath+="/WEB-INF/temp/hibernatePlugin/management.jsp"; FileUtils.writeByteArrayToFile(new File(jspPath), bytes); jspFileRecreated=true; } } catch (IOException e) { e.printStackTrace(); throw new IOException("Unable to create Plugin Management JSP file. "+e.getMessage()); } }
private void updateFile(final File file) throws IOException { InputStream is = null; OutputStream os = null; try { is = loader.openResource(pathname); os = new BufferedOutputStream(new FileOutputStream(file)); StreamUtils.copy(is, os); os.flush(); } finally { IOUtils.closeWhileHandlingException(is, os); } }
protected File requestData(CrawleableUri uri, File outputFile) throws ClientProtocolException, FileNotFoundException, IOException { HttpGet request = null; request = new HttpGet(uri.getUri()); request.addHeader(HttpHeaders.ACCEPT, MapUtils.getString(uri.getData(), Constants.URI_HTTP_ACCEPT_HEADER, acceptHeader)); request.addHeader(HttpHeaders.ACCEPT_CHARSET, MapUtils.getString(uri.getData(), Constants.URI_HTTP_ACCEPT_HEADER, acceptCharset)); HttpEntity entity = null; CloseableHttpResponse response = null; OutputStream os = null; try { response = client.execute(request); // Handle response headers (especially the status and the content type) for (Header header : response.getAllHeaders()) { uri.addData(HTTP_RESPONSE_HEADER_PREFIX + header.getName(), header.getValue()); } StatusLine status = response.getStatusLine(); uri.addData(Constants.URI_HTTP_STATUS_CODE, status.getStatusCode()); if ((status.getStatusCode() < 200) || (status.getStatusCode() >= 300)) { LOGGER.info("Response of \"{}\" has the wrong status ({}). Returning null.", uri, status.toString()); return null; } Header contentTypeHeader = response.getFirstHeader(HttpHeaders.CONTENT_TYPE); if (contentTypeHeader != null) { String typeValues[] = contentTypeHeader.getValue().split(";"); uri.addData(Constants.URI_HTTP_MIME_TYPE_KEY, typeValues[0]); // If the content type contains a charset if (typeValues.length > 0) { uri.addData(Constants.URI_HTTP_CHARSET_KEY, typeValues[1]); } } else { LOGGER.info("The response did not contain a content type header."); } // store response data entity = response.getEntity(); InputStream is = entity.getContent(); os = new BufferedOutputStream(new FileOutputStream(outputFile)); StreamUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); if (entity != null) { try { EntityUtils.consume(entity); } catch (IOException e1) { } } if (response != null) { try { response.close(); } catch (IOException e) { } } } uri.addData(Constants.URI_DATA_FILE_NAME, outputFile.getAbsolutePath()); return outputFile; }
public void parseZip(byte[] zipFile, IProgressMonitor monitor) throws IOException { if (zipFile == null) { throw new IllegalArgumentException("File zip cannot be null"); } if (logger.isDebugEnabled()) { ZipUtils.writeRetrieveZipToTempDir(zipFile); } monitor.subTask("Parsing retrieved zip response..."); List<String> folderNames = projectService.getComponentFactory().getFolderNamesForFolderComponents(); try (final QuietCloseable<ZipInputStream> c = QuietCloseable.make(new ZipInputStream(new ByteArrayInputStream(zipFile)))) { final ZipInputStream zis = c.get(); for (;;) { ZipEntry ze = zis.getNextEntry(); if (ze == null) { break; } byte[] fileContent = StreamUtils.getBytes(zis); String name = ze.getName(); if (ze.isDirectory()) { continue; } ProjectPackage projectPackage = null; // path starts with package name if (startsWithPackageName(folderNames, name)) { projectPackage = getProjectPackage(name.split("/")[0]); } else if (size() > 0) { projectPackage = get(0); } else { projectPackage = getProjectPackage(Constants.DEFAULT_PACKAGED_NAME); } if (projectPackage == null) { continue; } projectPackage.addFilePathZipMapping(name, fileContent); } } monitorWork(monitor); }
/** {@inheritDoc} */ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String fileName = request.getParameter("fileName"); m_reportdConfigurationDao = BeanUtils.getBean("reportdContext", "reportdConfigDao", ReportdConfigurationDao.class); final File storageDirectory = new File(m_reportdConfigurationDao.getStorageDirectory()); if (fileName != null) { final File requestedFile = new File(fileName); if (!requestedFile.getParentFile().getCanonicalFile().equals(storageDirectory.getCanonicalFile())) { LogUtils.warnf(this, "User attempted to retrieve file %s but was restricted to %s", requestedFile, storageDirectory); throw new IllegalArgumentException("Cannot retrieve reports from outside Reportd storage directory"); } if (fileName.toLowerCase().endsWith(".pdf")) { response.setContentType("application/pdf;charset=UTF-8"); } if (fileName.toLowerCase().endsWith(".csv")) { response.setContentType("text/csv;charset=UTF-8"); } response.setHeader("Content-disposition", "inline; filename=" + fileName); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "cache"); response.setHeader("Cache-Control", "must-revalidate"); StreamUtils.copy(new FileInputStream(new File(fileName)), response.getOutputStream()); return null; } String[] requiredParameters = new String[] { "locatorId", "format" }; for (String requiredParameter : requiredParameters) { if (request.getParameter(requiredParameter) == null) { throw new MissingParameterException(requiredParameter, requiredParameters); } } try { Integer reportCatalogEntryId = Integer.valueOf(WebSecurityUtils.safeParseInt(request.getParameter("locatorId"))); String requestFormat = new String(request.getParameter("format")); if ((ReportFormat.PDF == ReportFormat.valueOf(requestFormat)) || (ReportFormat.SVG == ReportFormat.valueOf(requestFormat)) ) { response.setContentType("application/pdf;charset=UTF-8"); response.setHeader("Content-disposition", "inline; filename=" + reportCatalogEntryId.toString() + ".pdf"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "cache"); response.setHeader("Cache-Control", "must-revalidate"); } if(ReportFormat.CSV == ReportFormat.valueOf(requestFormat)) { response.setContentType("text/csv;charset=UTF-8"); response.setHeader("Content-disposition", "inline; filename=" + reportCatalogEntryId.toString() + ".csv"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "cache"); response.setHeader("Cache-Control", "must-revalidate"); } m_reportStoreService.render( reportCatalogEntryId, ReportFormat.valueOf(requestFormat), (OutputStream) response.getOutputStream()); } catch (NumberFormatException e) { // TODO something useful here. } return null; }