List of usage examples for org.apache.commons.io FileUtils openInputStream
public static FileInputStream openInputStream(File file) throws IOException
new FileInputStream(file)
. From source file:org.silverpeas.core.web.http.FileResponse.java
/** * Fills fully the output response./*from w ww .java 2 s.com*/ * @param path the path of the file. * @param output the output stream to write into. */ void fullOutputStream(final Path path, final OutputStream output) { try (final InputStream stream = FileUtils.openInputStream(path.toFile())) { IOUtils.copy(stream, output); } catch (IOException e) { throw new WebApplicationException(e, Response.Status.NOT_FOUND); } }
From source file:org.silverpeas.core.web.http.RequestParameterDecoderTest.java
@BeforeEach public void setup() throws ParseException { httpRequestMock = mock(HttpRequest.class); when(httpRequestMock.getParameter(anyString())).then(invocation -> { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { switch (parameterName) { case "aString": return "<a>aStringValue</a>"; case "aStringToUnescape": return "<a>aStringValueToUnescape</a>"; case "anUri": return "/an/uri/"; case "anOffsetDateTime": return "2009-01-02T23:54:26Z"; case "anInteger": return "1"; case "anIntegerFromAnnotation": return "2"; case "aLongFromAnnotation": return "20"; case "aLong": return "10"; case "aBoolean": return "true"; case "aBooleanFromAnnotation": return "false"; case "anEnum": return EnumWithoutCreationAnnotation.VALUE_A.name(); }/*from w w w .j a v a2 s .c o m*/ } return null; }); when(httpRequestMock.getParameterAsRequestFile(anyString())).then(invocation -> { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aRequestFile")) { FileItem fileItem = mock(FileItem.class); when(fileItem.getName()).thenReturn("fileName"); when(fileItem.getSize()).thenReturn(26L); when(fileItem.getContentType()).thenReturn(MediaType.TEXT_PLAIN); when(fileItem.getInputStream()).thenReturn(FileUtils.openInputStream(getImageResource())); return new RequestFile(fileItem); } } return null; }); when(httpRequestMock.getParameterAsDate(anyString())).then(invocation -> { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aDate")) { return TODAY; } } return null; }); when(httpRequestMock.getParameterValues(anyString())).then((Answer<String[]>) invocation -> { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("strings")) { return new String[] { "string_1", "string_2", "string_2" }; } else if (parameterName.equals("integers")) { return new String[] { "1", "2", "2" }; } } return null; }); }
From source file:org.silverpeas.migration.jcr.service.repository.DocumentRepository.java
/** * Get the content.//w w w. j ava 2 s .c o m * * @param session the current JCR session. * @param pk the document which content is to be added. * @param lang the content language. * @return the attachment binary content. * @throws RepositoryException * @throws IOException */ public InputStream getContent(Session session, SimpleDocumentPK pk, String lang) throws RepositoryException, IOException { Node docNode = session.getNodeByIdentifier(pk.getId()); String language = lang; if (!StringUtil.isDefined(language)) { language = ConverterUtil.defaultLanguage; } SimpleDocument document = converter.fillDocument(docNode, language); return new BufferedInputStream(FileUtils.openInputStream(new File(document.getAttachmentPath()))); }
From source file:org.silverpeas.servlet.RequestParameterDecoderTest.java
@Before public void setup() throws ParseException { httpRequestMock = mock(HttpRequest.class); when(httpRequestMock.getParameter(anyString())).then(new Answer<Object>() { @Override//from w ww . j a v a2 s . c om public Object answer(final InvocationOnMock invocation) throws Throwable { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aString")) { return "aStringValue"; } } return null; } }); when(httpRequestMock.getParameterAsRequestFile(anyString())).then(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aRequestFile")) { FileItem fileItem = mock(FileItem.class); when(fileItem.getName()).thenReturn("fileName"); when(fileItem.getSize()).thenReturn(26L); when(fileItem.getContentType()).thenReturn(MediaType.TEXT_PLAIN); when(fileItem.getInputStream()).thenReturn(FileUtils.openInputStream(getImageResource())); return new RequestFile(fileItem); } } return null; } }); when(httpRequestMock.getParameterAsInteger(anyString())).then(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("anInteger")) { return 1; } else if (parameterName.equals("anIntegerFromAnnotation")) { return 2; } } return null; } }); when(httpRequestMock.getParameterAsLong(anyString())).then(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aLongFromAnnotation")) { return 20L; } else if (parameterName.equals("aLong")) { return 10L; } } return null; } }); when(httpRequestMock.getParameterAsBoolean(anyString())).then(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aBoolean")) { return true; } else if (parameterName.equals("aBooleanFromAnnotation")) { return false; } } return null; } }); when(httpRequestMock.getParameterAsDate(anyString())).then(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { String parameterName = (String) invocation.getArguments()[0]; if (StringUtil.isDefined(parameterName)) { if (parameterName.equals("aDate")) { return TODAY; } } return null; } }); }
From source file:org.silverpeas.util.PdfUtil.java
/** * Add a image under or over content on each page of a PDF file. * @param pdfSource the source pdf file, this content is not modified by this method * @param image the image file//from ww w. ja v a2 s. c o m * @param pdfDestination the destination pdf file, with the image under or over content * @param isBackground indicates if image is addes under or over the content of the pdf source * file */ private static void addImageOnEachPage(File pdfSource, File image, File pdfDestination, final boolean isBackground) { if (pdfSource == null || !pdfSource.isFile()) { throw new RuntimeException("The pdf source file doesn't exist"); } else if (!FileUtil.isPdf(pdfSource.getPath())) { throw new RuntimeException("The source is not a pdf file"); } else if (pdfDestination == null) { throw new RuntimeException("The pdf destination file is unknown"); } FileInputStream pdfSourceIS = null; FileOutputStream pdfDestinationIS = null; try { pdfSourceIS = FileUtils.openInputStream(pdfSource); pdfDestinationIS = FileUtils.openOutputStream(pdfDestination); addImageOnEachPage(pdfSourceIS, image, pdfDestinationIS, isBackground); } catch (IOException e) { throw new RuntimeException("Pdf source file cannot be opened or pdf destination file cannot be created", e); } finally { IOUtils.closeQuietly(pdfSourceIS); IOUtils.closeQuietly(pdfDestinationIS); } }
From source file:org.silverpeas.web.sharing.servlets.GetLinkFileServlet.java
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RestRequest rest = new RestRequest(request, "myFile"); String keyFile = rest.getElementValue(PARAM_KEYFILE); Ticket ticket = SharingServiceProvider.getSharingTicketService().getTicket(keyFile); if (ticket != null && ticket.isValid()) { // recherche des infos sur le fichier... String filePath = null;/*from w ww . j av a2 s.c om*/ String fileType = null; String fileName = null; long fileSize = 0; SimpleDocument document = null; if (ticket instanceof SimpleFileTicket) { document = ((SimpleFileTicket) ticket).getResource().getAccessedObject(); } else if (ticket instanceof VersionFileTicket) { document = ((VersionFileTicket) ticket).getResource().getAccessedObject(); } if (document != null) { filePath = document.getAttachmentPath(); fileType = document.getContentType(); fileName = document.getFilename(); fileSize = document.getSize(); } BufferedInputStream input = null; OutputStream out = response.getOutputStream(); try { File realFile = new File(filePath); response.setContentType(fileType); response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\""); response.setHeader("Content-Length", String.valueOf(fileSize)); input = new BufferedInputStream(FileUtils.openInputStream(realFile)); IOUtils.copy(input, out); DownloadDetail download = new DownloadDetail(ticket, new Date(), request.getRemoteAddr()); SharingServiceProvider.getSharingTicketService().addDownload(download); return; } catch (Exception ignored) { } finally { if (input != null) { IOUtils.closeQuietly(input); } IOUtils.closeQuietly(out); } } getServletContext().getRequestDispatcher("/sharing/jsp/invalidTicket.jsp").forward(request, response); }
From source file:org.slc.sli.ingestion.landingzone.IngestionFileEntry.java
public InputStream getFileStream() throws IOException { File parentFile = new File(getParentZipFileOrDirectory()); if (parentFile.isDirectory()) { return new BufferedInputStream(FileUtils.openInputStream(new File(parentFile, getFileName()))); } else {//from w ww . j a v a 2 s .c o m return ZipFileUtil.getInputStreamForFile(parentFile, getFileName()); } }
From source file:org.slc.sli.ingestion.landingzone.LocalFileSystemLandingZone.java
/** * @return md5Hex string for the given File object *//*from w ww . ja v a 2s .c o m*/ @Override public String getMd5Hex(File file) throws IOException { FileInputStream s = FileUtils.openInputStream(file); try { return DigestUtils.md5Hex(s); } finally { IOUtils.closeQuietly(s); } }
From source file:org.sonar.api.rules.XMLRuleParser.java
public List<Rule> parse(File file) { Reader reader = null;//from w w w.j a v a2s . co m try { reader = new InputStreamReader(FileUtils.openInputStream(file), CharEncoding.UTF_8); return parse(reader); } catch (IOException e) { throw new SonarException("Fail to load the file: " + file, e); } finally { Closeables.closeQuietly(reader); } }
From source file:org.sonar.batch.internal.PluginsManager.java
private void unzip(File file, String name) throws Exception { File toDir = new File(workDir, name); ZipUtils.unzip(file, toDir);/*w w w. j av a2s. c o m*/ File manifestFile = new File(toDir, "META-INF/MANIFEST.MF"); InputStream manifestStream = FileUtils.openInputStream(manifestFile); manifests.put(manifestFile, new Manifest(manifestStream)); IOUtils.closeQuietly(manifestStream); }