List of usage examples for javax.activation DataSource getInputStream
public InputStream getInputStream() throws IOException;
InputStream
representing the data and throws the appropriate exception if it can not do so. From source file:org.apache.axis.attachments.DimeBodyPart.java
protected long getDataSize(DataHandler dh) { long dataSize = -1L; try {/*from ww w. j a v a 2 s. c om*/ DataSource ds = dh.getDataSource(); //Do files our selfs since this is costly to read in. Ask the file system. // This is 90% of the use of attachments. if (ds instanceof javax.activation.FileDataSource) { javax.activation.FileDataSource fdh = (javax.activation.FileDataSource) ds; java.io.File df = fdh.getFile(); if (!df.exists()) { throw new RuntimeException(Messages.getMessage("noFile", df.getAbsolutePath())); } dataSize = df.length(); } else { dataSize = 0; java.io.InputStream in = ds.getInputStream(); byte[] readbuf = new byte[64 * 1024]; int bytesread; do { bytesread = in.read(readbuf); if (bytesread > 0) dataSize += bytesread; } while (bytesread > -1); if (in.markSupported()) { //Leave the stream open for future reading // and reset the stream pointer to the first byte in.reset(); } else { //FIXME: bug http://nagoya.apache.org/jira/secure/ViewIssue.jspa?key=AXIS-1126 //if we close this then how can we read the file? eh? in.close(); } } } catch (Exception e) { //TODO: why are exceptions swallowed here? log.error(Messages.getMessage("exception00"), e); } return dataSize; }
From source file:org.apache.axis.attachments.MimeUtils.java
/** * Determine the length for the individual part. * @param bp is the part to be searched. * @return the length in bytes./*from w w w . jav a 2 s .c o m*/ */ protected static long getContentLength(javax.mail.internet.MimeBodyPart bp) { long headerLength = -1L; long dataSize = -1L; try { headerLength = getHeaderLength(bp); javax.activation.DataHandler dh = bp.getDataHandler(); javax.activation.DataSource ds = dh.getDataSource(); // Do files our selfs since this is costly to read in. Ask the file system. // This is 90% of the use of attachments. if (ds instanceof javax.activation.FileDataSource) { javax.activation.FileDataSource fdh = (javax.activation.FileDataSource) ds; java.io.File df = fdh.getFile(); if (!df.exists()) { throw new RuntimeException(Messages.getMessage("noFile", df.getAbsolutePath())); } dataSize = df.length(); } else { dataSize = bp.getSize(); if (-1 == dataSize) { // Data size is not known so read it the hard way... dataSize = 0; java.io.InputStream in = ds.getInputStream(); byte[] readbuf = new byte[64 * 1024]; int bytesread; do { bytesread = in.read(readbuf); if (bytesread > 0) { dataSize += bytesread; } } while (bytesread > -1); in.close(); } } } catch (Exception e) { log.error(Messages.getMessage("exception00"), e); } return dataSize + headerLength; }
From source file:org.apache.axis2.format.ElementHelperTest.java
public void testGetTextAsStreamWithOMSourcedElement() throws Exception { OMFactory factory = OMAbstractFactory.getOMFactory(); DataSource ds = new RandomDataSource(445566, 32, 128, 20000000); QName qname = new QName("a"); Charset cs = Charset.forName("ascii"); OMSourcedElement element = new OMSourcedElementImpl(qname, factory, new WrappedTextNodeOMDataSourceFromDataSource(qname, ds, cs)); Reader in = ElementHelper.getTextAsStream(element, true); assertFalse(in instanceof StringReader); compareStreams(new InputStreamReader(ds.getInputStream(), cs), in); }
From source file:org.apache.axis2.format.ElementHelperTest.java
public void testGetTextAsStreamWithoutCaching() throws Exception { DataSource ds = new RandomDataSource(654321, 64, 128, 20000000); Vector<InputStream> v = new Vector<InputStream>(); v.add(new ByteArrayInputStream("<a>".getBytes("ascii"))); v.add(ds.getInputStream()); v.add(new ByteArrayInputStream("</a>".getBytes("ascii"))); XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE); XMLStreamReader reader = factory.createXMLStreamReader(new SequenceInputStream(v.elements()), "ascii"); OMElement element = new StAXOMBuilder(reader).getDocumentElement(); Reader in = ElementHelper.getTextAsStream(element, false); compareStreams(new InputStreamReader(ds.getInputStream(), "ascii"), in); }
From source file:org.apache.axis2.transport.testkit.axis2.LogAspect.java
@AfterReturning(pointcut = "call(javax.activation.DataSource org.apache.axis2.format.MessageFormatterEx.getDataSource(..))", returning = "dataSource") public void afterGetDataSource(DataSource dataSource) { try {/* w w w . jav a2 s . c o m*/ OutputStream out = LogManager.INSTANCE.createLog("formatter"); try { InputStream in = dataSource.getInputStream(); try { IOUtils.copy(in, out); } finally { in.close(); } } finally { out.close(); } } catch (Throwable ex) { log.error("Unable to dump message", ex); } }
From source file:org.apache.nifi.processors.email.ExtractEmailAttachments.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final ComponentLog logger = getLogger(); final FlowFile originalFlowFile = session.get(); if (originalFlowFile == null) { return;/*from w ww. j a v a 2 s . c o m*/ } final List<FlowFile> attachmentsList = new ArrayList<>(); final List<FlowFile> invalidFlowFilesList = new ArrayList<>(); final List<FlowFile> originalFlowFilesList = new ArrayList<>(); session.read(originalFlowFile, new InputStreamCallback() { @Override public void process(final InputStream rawIn) throws IOException { try (final InputStream in = new BufferedInputStream(rawIn)) { Properties props = new Properties(); Session mailSession = Session.getDefaultInstance(props, null); MimeMessage originalMessage = new MimeMessage(mailSession, in); MimeMessageParser parser = new MimeMessageParser(originalMessage).parse(); // RFC-2822 determines that a message must have a "From:" header // if a message lacks the field, it is flagged as invalid Address[] from = originalMessage.getFrom(); Date sentDate = originalMessage.getSentDate(); if (from == null || sentDate == null) { // Throws MessageException due to lack of minimum required headers throw new MessagingException("Message failed RFC2822 validation"); } originalFlowFilesList.add(originalFlowFile); if (parser.hasAttachments()) { final String originalFlowFileName = originalFlowFile .getAttribute(CoreAttributes.FILENAME.key()); try { for (final DataSource data : parser.getAttachmentList()) { FlowFile split = session.create(originalFlowFile); final Map<String, String> attributes = new HashMap<>(); if (StringUtils.isNotBlank(data.getName())) { attributes.put(CoreAttributes.FILENAME.key(), data.getName()); } if (StringUtils.isNotBlank(data.getContentType())) { attributes.put(CoreAttributes.MIME_TYPE.key(), data.getContentType()); } String parentUuid = originalFlowFile.getAttribute(CoreAttributes.UUID.key()); attributes.put(ATTACHMENT_ORIGINAL_UUID, parentUuid); attributes.put(ATTACHMENT_ORIGINAL_FILENAME, originalFlowFileName); split = session.append(split, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { IOUtils.copy(data.getInputStream(), out); } }); split = session.putAllAttributes(split, attributes); attachmentsList.add(split); } } catch (FlowFileHandlingException e) { // Something went wrong // Removing splits that may have been created session.remove(attachmentsList); // Removing the original flow from its list originalFlowFilesList.remove(originalFlowFile); logger.error( "Flowfile {} triggered error {} while processing message removing generated FlowFiles from sessions", new Object[] { originalFlowFile, e }); invalidFlowFilesList.add(originalFlowFile); } } } catch (Exception e) { // Another error hit... // Removing the original flow from its list originalFlowFilesList.remove(originalFlowFile); logger.error("Could not parse the flowfile {} as an email, treating as failure", new Object[] { originalFlowFile, e }); // Message is invalid or triggered an error during parsing invalidFlowFilesList.add(originalFlowFile); } } }); session.transfer(attachmentsList, REL_ATTACHMENTS); // As per above code, originalFlowfile may be routed to invalid or // original depending on RFC2822 compliance. session.transfer(invalidFlowFilesList, REL_FAILURE); session.transfer(originalFlowFilesList, REL_ORIGINAL); if (attachmentsList.size() > 10) { logger.info("Split {} into {} files", new Object[] { originalFlowFile, attachmentsList.size() }); } else if (attachmentsList.size() > 1) { logger.info("Split {} into {} files: {}", new Object[] { originalFlowFile, attachmentsList.size(), attachmentsList }); } }
From source file:org.apache.pdfbox.preflight.parser.PreflightParser.java
/** * Constructor. This one is slower than the file and the filename constructors, because * a temporary file will be created./*from ww w . j ava 2s . co m*/ * * @param dataSource the datasource * @throws IOException if there is a reading error. */ public PreflightParser(DataSource dataSource) throws IOException { // TODO move file handling outside of the parser super(new RandomAccessBufferedFileInputStream(dataSource.getInputStream())); this.setLenient(false); this.dataSource = dataSource; }
From source file:org.apache.wink.itest.standard.JAXRSDataSourceTest.java
/** * Verify that we can send a DataSource and receive a DataSource. The 'POST' * method on the resource we are calling is a simple echo. *//*from ww w.j a v a 2 s . c om*/ public void testPOSTDataSource() throws Exception { PostMethod postMethod = null; try { postMethod = new PostMethod(getBaseURI() + "/dstest"); String input = "This is some test input"; RequestEntity requestEntity = new ByteArrayRequestEntity(input.getBytes(), "application/datasource"); postMethod.setRequestEntity(requestEntity); HttpClient client = new HttpClient(); client.executeMethod(postMethod); // just use our provider to read the response DataSourceProvider provider = new DataSourceProvider(); DataSource returnedData = provider.readFrom(DataSource.class, null, null, new MediaType("application", "datasource"), null, postMethod.getResponseBodyAsStream()); assertNotNull(returnedData); assertNotNull(returnedData.getInputStream()); byte[] responseBytes = new byte[input.getBytes().length]; returnedData.getInputStream().read(responseBytes); assertNotNull(responseBytes); String response = new String(responseBytes); assertEquals("This is some test input", response); } finally { if (postMethod != null) { postMethod.releaseConnection(); } } }
From source file:org.apromore.service.impl.CanoniserServiceImplIntgTest.java
private AnnotationsType getANFTypeFromXML(final DataSource anf) throws JAXBException, SAXException, IOException { return ANFSchema.unmarshalAnnotationFormat(anf.getInputStream(), false).getValue(); }
From source file:org.apromore.util.StreamUtil.java
/** * Convert a DataHandler to a String//from www. j a v a 2s. com * * @param ds the DataSource to convert * @return the string for that DataSource */ public static String convertStreamToString(final DataSource ds) { try { return inputStream2String(ds.getInputStream()); } catch (IOException e) { return "error in readin the DataSource: " + e.toString(); } }