use of javax.mail.internet.ParseException in project camel by apache.
the class MimeMultipartDataFormat method unmarshal.
@Override
public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, MessagingException {
MimeBodyPart mimeMessage;
String contentType;
Message camelMessage;
Object content = null;
if (headersInline) {
mimeMessage = new MimeBodyPart(stream);
camelMessage = exchange.getOut();
MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
contentType = mimeMessage.getHeader(CONTENT_TYPE, null);
// write the MIME headers not generated by javamail as Camel headers
Enumeration<?> headersEnum = mimeMessage.getNonMatchingHeaders(STANDARD_HEADERS);
while (headersEnum.hasMoreElements()) {
Object ho = headersEnum.nextElement();
if (ho instanceof Header) {
Header header = (Header) ho;
camelMessage.setHeader(header.getName(), header.getValue());
}
}
} else {
// check if this a multipart at all. Otherwise do nothing
contentType = exchange.getIn().getHeader(CONTENT_TYPE, String.class);
if (contentType == null) {
return stream;
}
try {
ContentType ct = new ContentType(contentType);
if (!ct.match("multipart/*")) {
return stream;
}
} catch (ParseException e) {
LOG.warn("Invalid Content-Type " + contentType + " ignored");
return stream;
}
camelMessage = exchange.getOut();
MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(stream, bos);
InternetHeaders headers = new InternetHeaders();
extractHeader(CONTENT_TYPE, camelMessage, headers);
extractHeader(MIME_VERSION, camelMessage, headers);
mimeMessage = new MimeBodyPart(headers, bos.toByteArray());
bos.close();
}
DataHandler dh;
try {
dh = mimeMessage.getDataHandler();
if (dh != null) {
content = dh.getContent();
contentType = dh.getContentType();
}
} catch (MessagingException e) {
LOG.warn("cannot parse message, no unmarshalling done");
}
if (content instanceof MimeMultipart) {
MimeMultipart mp = (MimeMultipart) content;
content = mp.getBodyPart(0);
for (int i = 1; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
DefaultAttachment camelAttachment = new DefaultAttachment(bp.getDataHandler());
@SuppressWarnings("unchecked") Enumeration<Header> headers = bp.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = headers.nextElement();
camelAttachment.addHeader(header.getName(), header.getValue());
}
camelMessage.addAttachmentObject(getAttachmentKey(bp), camelAttachment);
}
}
if (content instanceof BodyPart) {
BodyPart bp = (BodyPart) content;
camelMessage.setBody(bp.getInputStream());
contentType = bp.getContentType();
if (contentType != null && !DEFAULT_CONTENT_TYPE.equals(contentType)) {
camelMessage.setHeader(CONTENT_TYPE, contentType);
ContentType ct = new ContentType(contentType);
String charset = ct.getParameter("charset");
if (charset != null) {
camelMessage.setHeader(Exchange.CONTENT_ENCODING, MimeUtility.javaCharset(charset));
}
}
} else {
// If we find no body part, try to leave the message alone
LOG.info("no MIME part found");
}
return camelMessage;
}
use of javax.mail.internet.ParseException in project stanbol by apache.
the class MessageBodyReaderUtils method fromMultipart.
/**
* Returns content parsed from {@link MediaType#MULTIPART_FORM_DATA}.
* It iterates over all {@link BodyPart}s and tries to create {@link RequestData}
* instances. In case the {@link BodyPart#getContentType()} is not present or
* can not be parsed, the {@link RequestData#getMediaType()} is set to
* <code>null</code>. If {@link BodyPart#getInputStream()} is not defined an
* {@link IllegalArgumentException} is thrown. The {@link BodyPart#getFileName()}
* is used for {@link RequestData#getName()}. The ordering of the returned
* Content instances is the same as within the {@link MimeMultipart} instance
* parsed from the input stream. <p>
* This Method does NOT load the data into memory, but returns directly the
* {@link InputStream}s as returned by the {@link BodyPart}s. Therefore
* it is saved to be used with big attachments.<p>
* This Method is necessary because within {@link MessageBodyReader} one
* can not use the usual annotations as used within Resources. so this method
* allows to access the data directly from the parameters available from the
* {@link MessageBodyReader#readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
* method<p>
* To test this Method with curl use:
* <code><pre>
* curl -v -X POST -F "content=@{dataFile};type={mimeType}"
* {serviceURL}
* </pre></code>
* Note that between {contentParam} and the datafile MUST NOT be a '='!
* @param mimeData the mime encoded data
* @param mediaType the mediaType (parsed to the {@link ByteArrayDataSource}
* constructor)
* @return the contents parsed from the {@link BodyPart}s
* @throws IOException an any Exception while reading the stream or
* {@link MessagingException} exceptions other than {@link ParseException}s
* @throws IllegalArgumentException If a {@link InputStream} is not available
* for any {@link BodyPart} or on {@link ParseException}s while reading the
* MimeData from the stream.
*/
public static List<RequestData> fromMultipart(InputStream mimeData, MediaType mediaType) throws IOException, IllegalArgumentException {
ByteArrayDataSource ds = new ByteArrayDataSource(mimeData, mediaType.toString());
List<RequestData> contents = new ArrayList<RequestData>();
try {
MimeMultipart data = new MimeMultipart(ds);
// For now search the first bodypart that fits and only debug the others
for (int i = 0; i < data.getCount(); i++) {
BodyPart bp = data.getBodyPart(i);
String fileName = bp.getFileName();
MediaType mt;
try {
mt = bp.getContentType() != null ? MediaType.valueOf(bp.getContentType()) : null;
} catch (IllegalArgumentException e) {
log.warn(String.format("Unable to parse MediaType form Mime Bodypart %s: " + " fileName %s | Disposition %s | Description %s", i + 1, fileName, bp.getDisposition(), bp.getDescription()), e);
mt = null;
}
InputStream stream = bp.getInputStream();
if (stream == null) {
throw new IllegalArgumentException(String.format("Unable to get InputStream for Mime Bodypart %s: " + "mediaType %s fileName %s | Disposition %s | Description %s", i + 1, fileName, bp.getDisposition(), bp.getDescription()));
} else {
contents.add(new RequestData(mt, bp.getFileName(), stream));
}
}
} catch (ParseException e) {
throw new IllegalStateException(String.format("Unable to parse data from %s request", MediaType.MULTIPART_FORM_DATA_TYPE), e);
} catch (MessagingException e) {
throw new IOException("Exception while reading " + MediaType.MULTIPART_FORM_DATA_TYPE + " request", e);
}
return contents;
}
use of javax.mail.internet.ParseException in project wso2-axis2-transports by wso2.
the class JettyByteArrayAsyncEndpoint method handle.
@Override
protected IncomingMessage<byte[]> handle(HttpRequest request) throws HttpException, IOException {
byte[] data = IOUtils.toByteArray(request.getInputStream());
logRequest(request, data);
ContentType contentType;
try {
contentType = new ContentType(request.getContentType());
} catch (ParseException ex) {
throw new HttpException(500, "Unparsable Content-Type");
}
return new IncomingMessage<byte[]>(contentType, data);
}
use of javax.mail.internet.ParseException in project wso2-axis2-transports by wso2.
the class TextMessageBuilderAdapter method processDocument.
public OMElement processDocument(Reader reader, String contentType, MessageContext messageContext) throws AxisFault {
String charset;
try {
ContentType ct = new ContentType(contentType);
charset = ct.getParameter("charset");
} catch (ParseException ex) {
charset = null;
}
if (charset == null) {
charset = MessageContext.DEFAULT_CHAR_SET_ENCODING;
}
messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charset);
return processDocument(new ReaderInputStream(reader, charset), contentType, messageContext);
}
use of javax.mail.internet.ParseException in project wso2-axis2-transports by wso2.
the class RabbitMQUtils method getBuilder.
private static Builder getBuilder(MessageContext msgContext, String rawContentType) throws AxisFault {
try {
ContentType contentType = new ContentType(rawContentType);
String charset = contentType.getParameter("charset");
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charset);
Builder builder = BuilderUtil.getBuilderFromSelector(contentType.getBaseType(), msgContext);
if (builder == null) {
if (log.isDebugEnabled()) {
log.debug("No message builder found for type '" + contentType.getBaseType() + "'. Falling back to SOAP.");
}
builder = new SOAPBuilder();
}
return builder;
} catch (ParseException e) {
throw new AxisFault("Error parsing content type: " + rawContentType, e);
}
}
Aggregations