List of usage examples for javax.mail.util ByteArrayDataSource ByteArrayDataSource
public ByteArrayDataSource(String data, String type) throws IOException
From source file:es.caib.sgtsic.util.DataHandlers.java
public static DataHandler byteArrayToDataHandler(byte[] arrayByte, String mimetype) { ByteArrayDataSource dataSource = new ByteArrayDataSource(arrayByte, mimetype); return new DataHandler(dataSource); }
From source file:org.mule.service.http.impl.service.server.grizzly.HttpParser.java
public static Collection<HttpPart> parseMultipartContent(InputStream content, String contentType) throws IOException { MimeMultipart mimeMultipart = null;/*from w w w .j av a2s. c o m*/ List<HttpPart> parts = Lists.newArrayList(); try { mimeMultipart = new MimeMultipart(new ByteArrayDataSource(content, contentType)); } catch (MessagingException e) { throw new IOException(e); } try { int partCount = mimeMultipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart part = mimeMultipart.getBodyPart(i); String filename = part.getFileName(); String partName = filename; String[] contentDispositions = part.getHeader(CONTENT_DISPOSITION); if (contentDispositions != null) { String contentDisposition = contentDispositions[0]; if (contentDisposition.contains(NAME_ATTRIBUTE)) { partName = contentDisposition.substring( contentDisposition.indexOf(NAME_ATTRIBUTE) + NAME_ATTRIBUTE.length() + 2); partName = partName.substring(0, partName.indexOf("\"")); } } if (partName == null && mimeMultipart.getContentType().contains(MULTIPART_RELATED.toString())) { String[] contentIdHeader = part.getHeader(CONTENT_ID); if (contentIdHeader != null && contentIdHeader.length > 0) { partName = contentIdHeader[0]; } } HttpPart httpPart = new HttpPart(partName, filename, IOUtils.toByteArray(part.getInputStream()), part.getContentType(), part.getSize()); Enumeration<Header> headers = part.getAllHeaders(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); httpPart.addHeader(header.getName(), header.getValue()); } parts.add(httpPart); } } catch (MessagingException e) { throw new IOException(e); } return parts; }
From source file:es.caib.sgtsic.util.DataHandlers.java
public static DataHandler byteArrayToDataHandler(byte[] arrayByte) { InputStream is = new BufferedInputStream(new ByteArrayInputStream(arrayByte)); String mimetype = ""; try {//from w w w. ja va 2s . com mimetype = URLConnection.guessContentTypeFromStream(is); } catch (IOException ex) { Logger.getLogger(DataHandlers.class.getName()).log(Level.SEVERE, null, ex); } DataSource dataSource = new ByteArrayDataSource(arrayByte, mimetype); return new DataHandler(dataSource); }
From source file:org.kantega.respiro.mail.SMTPMailSender.java
private static void addAttachments(MultiPartEmail mail, Message msg) { for (Attachment attachment : msg.getAttachments()) { try {/*w w w . j ava 2s . c om*/ mail.attach(new ByteArrayDataSource(attachment.getContent(), attachment.getMimeType()), attachment.getFileName(), ""); } catch (EmailException e) { throw new RuntimeException(e); } } }
From source file:org.mule.module.http.internal.HttpParser.java
public static Collection<HttpPart> parseMultipartContent(InputStream content, String contentType) throws IOException { MimeMultipart mimeMultipart = null;//from w w w. j a v a 2 s . co m List<HttpPart> parts = Lists.newArrayList(); try { mimeMultipart = new MimeMultipart(new ByteArrayDataSource(content, contentType)); } catch (MessagingException e) { throw new IOException(e); } try { int partCount = mimeMultipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart part = mimeMultipart.getBodyPart(i); String filename = part.getFileName(); String partName = filename; String[] contentDispositions = part.getHeader(CONTENT_DISPOSITION_PART_HEADER); if (contentDispositions != null) { String contentDisposition = contentDispositions[0]; if (contentDisposition.contains(NAME_ATTRIBUTE)) { partName = contentDisposition.substring( contentDisposition.indexOf(NAME_ATTRIBUTE) + NAME_ATTRIBUTE.length() + 2); partName = partName.substring(0, partName.indexOf("\"")); } } HttpPart httpPart = new HttpPart(partName, filename, IOUtils.toByteArray(part.getInputStream()), part.getContentType(), part.getSize()); Enumeration<Header> headers = part.getAllHeaders(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); httpPart.addHeader(header.getName(), header.getValue()); } parts.add(httpPart); } } catch (MessagingException e) { throw new IOException(e); } return parts; }
From source file:com.mirth.connect.server.userutil.HTTPUtil.java
/** * Serializes an HTTP request body into XML. Multipart requests will also automatically be * parsed into separate XML nodes./*from w w w. ja va 2s .c o m*/ * * @param httpBody * The request body/payload input stream to parse. * @param contentType * The MIME content type of the request. * @return The serialized XML string. * @throws MessagingException * @throws IOException * @throws DonkeyElementException * @throws ParserConfigurationException */ public static String httpBodyToXml(InputStream httpBody, String contentType) throws MessagingException, IOException, DonkeyElementException, ParserConfigurationException { ContentType type = getContentType(contentType); Object content; if (type.getMimeType().startsWith(FileUploadBase.MULTIPART)) { content = new MimeMultipart(new ByteArrayDataSource(httpBody, type.toString())); } else { content = IOUtils.toString(httpBody, HttpMessageConverter.getDefaultHttpCharset(type.getCharset().name())); } return HttpMessageConverter.contentToXml(content, type, true, null); }
From source file:org.openehealth.ipf.platform.camel.lbs.cxf.process.GreeterImpl.java
@Override public void postMe(Holder<String> name, Holder<DataHandler> attachinfo, DataHandler onewayattach) { name.value = "Hello from the real world. Request was " + name.value; try {/*from w ww.ja v a 2s. co m*/ String content = toString(attachinfo.value); String onewayContent = toString(onewayattach); String contentType = attachinfo.value.getContentType(); String resultContent = "I received: " + content + "/" + onewayContent; attachinfo.value = new DataHandler(new ByteArrayDataSource(resultContent, contentType)); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.apache.axis2.jaxbri.mtom.MtomImpl.java
public RetrieveDocumentResponse retrieveDocument(RetrieveDocument retrieveDocument) { RetrieveDocumentResponse response = new RetrieveDocumentResponse(); response.setContent(new DataHandler( new ByteArrayDataSource(documents.get(retrieveDocument.getId()), "application/octet-stream"))); return response; }
From source file:org.vosao.utils.EmailUtil.java
/** * Send email with html content and attachments. * @param htmlBody//from w w w . j a v a 2s.c o m * @param subject * @param fromAddress * @param fromText * @param toAddress * @return null if OK or error message. */ public static String sendEmail(final String htmlBody, final String subject, final String fromAddress, final String fromText, final String toAddress, final List<FileItem> files) { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); try { Multipart mp = new MimeMultipart(); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlBody, "text/html"); htmlPart.setHeader("Content-type", "text/html; charset=UTF-8"); mp.addBodyPart(htmlPart); for (FileItem item : files) { MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(item.getFilename()); String mimeType = MimeType.getContentTypeByExt(FolderUtil.getFileExt(item.getFilename())); DataSource ds = new ByteArrayDataSource(item.getData(), mimeType); attachment.setDataHandler(new DataHandler(ds)); mp.addBodyPart(attachment); } MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromAddress, fromText)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, toAddress)); msg.setSubject(subject, "UTF-8"); msg.setContent(mp); Transport.send(msg); return null; } catch (AddressException e) { return e.getMessage(); } catch (MessagingException e) { return e.getMessage(); } catch (UnsupportedEncodingException e) { return e.getMessage(); } }
From source file:com.googlecode.ddom.mime.JavaMailTest.java
private void test(boolean preamble) throws Exception { MimeMultipart multipart = new MimeMultipart(); MimeBodyPart bodyPart1 = new MimeBodyPart(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < 1000; i++) { buffer.append('('); buffer.append(i);// w w w . jav a2 s .co m buffer.append(')'); } String content1 = buffer.toString(); bodyPart1 .setDataHandler(new DataHandler(new ByteArrayDataSource(content1.getBytes("UTF-8"), "text/plain"))); Map<String, String> headers1 = new HashMap<String, String>(); headers1.put("Content-ID", "<1@example.com>"); headers1.put("Content-Type", "text/plain; charset=UTF-8"); setHeaders(bodyPart1, headers1); multipart.addBodyPart(bodyPart1); MimeBodyPart bodyPart2 = new MimeBodyPart(); byte[] content2 = new byte[10000]; new Random().nextBytes(content2); bodyPart2.setDataHandler(new DataHandler(new ByteArrayDataSource(content2, "application/octet-stream"))); Map<String, String> headers2 = new HashMap<String, String>(); headers2.put("Content-ID", "<2@example.com>"); headers2.put("Content-Type", "application/octet-stream"); setHeaders(bodyPart2, headers2); multipart.addBodyPart(bodyPart2); if (preamble) { multipart.setPreamble("This is a MIME multipart."); } String boundary = new ContentType(multipart.getContentType()).getParameter("boundary"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); multipart.writeTo(baos); MultipartReader mpr = new MultipartReader(new ByteArrayInputStream(baos.toByteArray()), boundary); assertTrue(mpr.nextPart()); assertEquals(headers1, readHeaders(mpr)); assertEquals(content1, IOUtils.toString(mpr.getContent(), "UTF-8")); assertTrue(mpr.nextPart()); assertEquals(headers2, readHeaders(mpr)); assertArrayEquals(content2, IOUtils.toByteArray(mpr.getContent())); assertFalse(mpr.nextPart()); }