List of usage examples for javax.activation DataHandler getContent
public Object getContent() throws IOException
From source file:fr.paris.lutece.portal.service.mail.MailUtil.java
/** * This Method convert a UrlAttachmentDataSource to a ByteArrayDataSource * and used MailAttachmentCacheService for caching resource. * @param urlAttachement {@link UrlAttachment} * @return a {@link ByteArrayDataSource} *//*from ww w . j a va2 s . c o m*/ private static ByteArrayDataSource convertUrlAttachmentDataSourceToByteArrayDataSource( UrlAttachment urlAttachement) { String strKey = MailAttachmentCacheService.getInstance().getKey(urlAttachement.getUrlData().toString()); ByteArrayDataSource urlAttachmentDataSource = null; if (!MailAttachmentCacheService.getInstance().isCacheEnable() || (MailAttachmentCacheService.getInstance().getFromCache(strKey) == null)) { DataHandler handler = new DataHandler(urlAttachement.getUrlData()); ByteArrayOutputStream bo = null; InputStream input = null; String strType = null; try { Object o = handler.getContent(); strType = handler.getContentType(); if (o != null) { if (o instanceof InputStream) { input = (InputStream) o; bo = new ByteArrayOutputStream(); int read; byte[] tab = new byte[CONSTANTE_FILE_ATTACHMET_BUFFER]; do { read = input.read(tab); if (read > 0) { bo.write(tab, 0, read); } } while (read > 0); } } } catch (IOException e) { // Document is ignored AppLogService.info(urlAttachement.getContentLocation() + MSG_ATTACHMENT_NOT_FOUND); } finally { //closed inputstream and outputstream try { if (input != null) { input.close(); } if (bo != null) { bo.close(); urlAttachmentDataSource = new ByteArrayDataSource(bo.toByteArray(), strType); } } catch (IOException e) { AppLogService.error(e); } } if (MailAttachmentCacheService.getInstance().isCacheEnable()) { //add resource in cache MailAttachmentCacheService.getInstance().putInCache(strKey, urlAttachmentDataSource); } } else { //used the resource store in cache urlAttachmentDataSource = (ByteArrayDataSource) MailAttachmentCacheService.getInstance() .getFromCache(strKey); } return urlAttachmentDataSource; }
From source file:MultipartViewer.java
public void setCommandContext(String verb, DataHandler dh) throws IOException { this.verb = verb; this.dh = dh; // get the content, and hope it is a Multipart Object Object content = dh.getContent(); if (content instanceof Multipart) { setupDisplay((Multipart) content); } else {//from ww w . j ava2 s .c o m setupErrorDisplay(content); } }
From source file:org.mule.module.http.functional.listener.HttpListenerAttachmentsTestCase.java
private MuleMessage getResponseWithExpectedAttachmentFrom(String path) throws MuleException, IOException { MuleMessage response = muleContext.getClient().send(getUrl(path), getTestMuleMessage()); assertThat(response.getInboundAttachmentNames().size(), is(1)); DataHandler attachment = response.getInboundAttachment(TEXT_BODY_FIELD_NAME); assertThat((String) attachment.getContent(), is(TEXT_BODY_FIELD_VALUE)); assertThat(attachment.getContentType(), is(TEXT_PLAIN)); return response; }
From source file:MessageViewer.java
/** * the CommandObject method to accept our DataHandler * @param dh the datahandler used to get the content *///from w ww . j a va 2 s. c o m public void setCommandContext(String verb, DataHandler dh) throws IOException { this.verb = verb; dataHandler = dh; Object o = dh.getContent(); if (o instanceof Message) { setMessage((Message) o); } else { System.out.println("MessageViewer - content not a Message object, " + o); if (o != null) { System.out.println(o.getClass().toString()); } } }
From source file:org.mule.module.http.functional.listener.HttpListenerAttachmentsTestCase.java
@Test public void respondWithSeveralAttachments() throws Exception { MuleMessage response = muleContext.getClient().send(getUrl(filePath.getValue()), getTestMuleMessage()); assertThat(response.getInboundAttachmentNames().size(), is(2)); DataHandler attachment1 = response.getInboundAttachment(FILE_BODY_FIELD_NAME); HttpPart part = ((HttpPartDataSource) attachment1.getDataSource()).getPart(); assertThat(part.getName(), is(FILE_BODY_FIELD_NAME)); assertThat(part.getFileName(), is(FIELD_BDOY_FILE_NAME)); assertThat(part.getContentType(), is("application/octet-stream")); assertThat(IOUtils.toString(part.getInputStream()), is(FILE_BODY_FIELD_VALUE)); DataHandler attachment2 = response.getInboundAttachment(TEXT_BODY_FIELD_NAME); assertThat((String) attachment2.getContent(), is(TEXT_BODY_FIELD_VALUE)); assertThat(attachment2.getContentType(), is(TEXT_PLAIN)); }
From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java
@DirtiesContext @Test/*from www .java2 s . c o m*/ public void convertCamelMessageToDataPartWithCharsetHeaderAndFilenameInContentType() throws Exception { resultEndpoint.expectedMessageCount(1); String message = DEFAULT_TEST_BODY; Map<String, Object> headers = new HashMap<String, Object>(); headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, DEFAULT_TEST_DATAPART_TRANSFERENCODING); headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE + ";name=" + DEFAULT_TEST_DATAPART_FILENAME); headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length()); headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE); headers.put(Exchange.CHARSET_NAME, "iso-8859-1"); template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers); resultEndpoint.assertIsSatisfied(); List<Exchange> exchanges = resultEndpoint.getExchanges(); Exchange exchange = exchanges.get(0); Message in = exchange.getIn(); DataPart datapart = in.getMandatoryBody(DataPart.class); assertNotNull(datapart); assertEquals((long) datapart.getContentLength(), message.length()); assertTrue(datapart.getContentType().contains("charset=iso-8859-1")); assertTrue(datapart.getContentType().contains("name=" + DEFAULT_TEST_DATAPART_FILENAME)); assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE); assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(datapart.getTransferEncoding(), DEFAULT_TEST_DATAPART_TRANSFERENCODING); assertNotNull(datapart.getDataHandler()); DataHandler dataHandler = datapart.getDataHandler(); assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY); }
From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java
@DirtiesContext @Test//from ww w .ja v a 2s. c o m public void convertCamelMessageToDataPartWithoutFilename() throws Exception { resultEndpoint.expectedMessageCount(1); String message = DEFAULT_TEST_BODY; Map<String, Object> headers = new HashMap<String, Object>(); headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, DEFAULT_TEST_DATAPART_TRANSFERENCODING); headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE); headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length()); headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE); template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers); resultEndpoint.assertIsSatisfied(); List<Exchange> exchanges = resultEndpoint.getExchanges(); Exchange exchange = exchanges.get(0); Message in = exchange.getIn(); DataPart datapart = in.getMandatoryBody(DataPart.class); assertNotNull(datapart); assertEquals((long) datapart.getContentLength(), message.length()); assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE); assertNull(datapart.getFileName()); assertEquals(datapart.getTransferEncoding(), DEFAULT_TEST_DATAPART_TRANSFERENCODING); assertNotNull(datapart.getDataHandler()); DataHandler dataHandler = datapart.getDataHandler(); assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertNull(dataHandler.getName()); assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY); }
From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java
@DirtiesContext @Test//from ww w.j av a 2 s . c om public void convertCamelMessageToDataPart() throws Exception { resultEndpoint.expectedMessageCount(1); String message = DEFAULT_TEST_BODY; Map<String, Object> headers = new HashMap<String, Object>(); headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, DEFAULT_TEST_DATAPART_TRANSFERENCODING); headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE); headers.put(ShsHeaders.DATAPART_FILENAME, DEFAULT_TEST_DATAPART_FILENAME); headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length()); headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE); template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers); resultEndpoint.assertIsSatisfied(); List<Exchange> exchanges = resultEndpoint.getExchanges(); Exchange exchange = exchanges.get(0); Message in = exchange.getIn(); DataPart datapart = in.getMandatoryBody(DataPart.class); assertNotNull(datapart); assertEquals((long) datapart.getContentLength(), message.length()); assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE); assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(datapart.getTransferEncoding(), DEFAULT_TEST_DATAPART_TRANSFERENCODING); assertNotNull(datapart.getDataHandler()); DataHandler dataHandler = datapart.getDataHandler(); assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY); }
From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java
@DirtiesContext @Test/*from w ww. j a va 2 s .com*/ public void convertCamelMessageToDataPartWithBase64() throws Exception { resultEndpoint.expectedMessageCount(1); String message = DEFAULT_TEST_BODY; Map<String, Object> headers = new HashMap<String, Object>(); headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, "base64"); headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE); headers.put(ShsHeaders.DATAPART_FILENAME, DEFAULT_TEST_DATAPART_FILENAME); headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length()); headers.put(ShsHeaders.DATAPART_TYPE, DEFAULT_TEST_DATAPART_TYPE); template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers); resultEndpoint.assertIsSatisfied(); List<Exchange> exchanges = resultEndpoint.getExchanges(); Exchange exchange = exchanges.get(0); Message in = exchange.getIn(); DataPart datapart = in.getMandatoryBody(DataPart.class); assertNotNull(datapart); assertEquals((long) datapart.getContentLength(), message.length()); assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE); assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(datapart.getTransferEncoding(), "base64"); assertNotNull(datapart.getDataHandler()); DataHandler dataHandler = datapart.getDataHandler(); assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY); }
From source file:se.inera.axel.shs.camel.CamelShsDataPartConverterTest.java
@DirtiesContext @Test(expectedExceptions = IllegalDatapartContentException.class) public void convertCamelMessageToDataPartWithNoDataPartTypeShouldThrow() throws Throwable { resultEndpoint.expectedMessageCount(1); String message = DEFAULT_TEST_BODY; Map<String, Object> headers = new HashMap<String, Object>(); headers.put(ShsHeaders.DATAPART_TRANSFERENCODING, "base64"); headers.put(ShsHeaders.DATAPART_CONTENTTYPE, DEFAULT_TEST_DATAPART_CONTENTTYPE); headers.put(ShsHeaders.DATAPART_FILENAME, DEFAULT_TEST_DATAPART_FILENAME); headers.put(ShsHeaders.DATAPART_CONTENTLENGTH, message.length()); try {/*from www. ja v a 2 s . c o m*/ template.sendBodyAndHeaders("direct:camelToShsConverter", message, headers); } catch (CamelExecutionException e) { throw e.getCause(); } resultEndpoint.assertIsSatisfied(); List<Exchange> exchanges = resultEndpoint.getExchanges(); Exchange exchange = exchanges.get(0); Message in = exchange.getIn(); DataPart datapart = in.getMandatoryBody(DataPart.class); assertNotNull(datapart); assertEquals((long) datapart.getContentLength(), message.length()); assertEquals(datapart.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(datapart.getDataPartType(), DEFAULT_TEST_DATAPART_TYPE); assertEquals(datapart.getFileName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(datapart.getTransferEncoding(), "base64"); assertNotNull(datapart.getDataHandler()); DataHandler dataHandler = datapart.getDataHandler(); assertEquals(dataHandler.getContentType(), DEFAULT_TEST_DATAPART_CONTENTTYPE); assertEquals(dataHandler.getName(), DEFAULT_TEST_DATAPART_FILENAME); assertEquals(dataHandler.getContent(), DEFAULT_TEST_BODY); }