use of javax.mail.internet.ParseException in project nhin-d by DirectProject.
the class SMIMECryptographerImpl method sign.
/**
* Signs an entity with the provided certificates.
* @param message The entity that will be signed.
* @param signingCertificates The certificates used to sign the message.
* @return A signed entity that consists of a multipart/signed entity containing the original entity and a message signature.
*/
public SignedEntity sign(MimeEntity entity, Collection<X509Certificate> signingCertificates) {
if (entity == null) {
throw new IllegalArgumentException();
}
// Serialize message out as ASCII encoded...
byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entity);
MimeMultipart mm = this.createSignatureEntity(messageBytes, signingCertificates);
SignedEntity retVal = null;
try {
retVal = new SignedEntity(new ContentType(mm.getContentType()), mm);
} catch (ParseException e) {
throw new MimeException(MimeError.InvalidHeader, e);
}
return retVal;
}
use of javax.mail.internet.ParseException in project nhin-d by DirectProject.
the class SignedEntity method createContentType.
/**
* Creates a MIME content type based on the digest algorithm.
* @param digestAlgorithm The digest algorithm used to generate the message signature.
* @return a MIME content type based on the digest algorithm.
*/
static ContentType createContentType(DigestAlgorithm digestAlgorithm) {
ContentType contentType = null;
try {
contentType = new ContentType(SMIMEStandard.MultiPartType_Signed);
contentType.setParameter(SMIMEStandard.MICAlgorithmKey, SMIMEStandard.toString(digestAlgorithm));
} catch (ParseException e) {
}
return contentType;
}
use of javax.mail.internet.ParseException in project jbossws-cxf by jbossws.
the class ClientHandler method getContentType.
protected ContentType getContentType(SOAPMessageContext msgContext) {
ContentType contentType = null;
try {
@SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) msgContext.get(MessageContext.HTTP_RESPONSE_HEADERS);
List<String> ctype = (headers == null) ? null : headers.get("Content-Type");
if (ctype == null) {
// Cxf stores it in lower case
ctype = (headers == null) ? null : headers.get("content-type");
}
log.info("ctype=" + ctype);
if (ctype == null) {
// Native has already processed this header into the message
SOAPMessage soapMessage = msgContext.getMessage();
MimeHeaders mimeHeaders = soapMessage.getMimeHeaders();
String[] ct = mimeHeaders.getHeader("Content-Type");
log.info("ct=" + ct);
if (ct != null) {
contentType = new ContentType(ct[0]);
}
} else {
contentType = new ContentType(ctype.get(0));
}
} catch (ParseException e) {
throw new WebServiceException(e);
}
return contentType;
}
use of javax.mail.internet.ParseException in project spring-integration-samples by spring-projects.
the class EmailParserUtils method handleMultipart.
/**
* Parses any {@link Multipart} instances that contain text or Html attachments,
* {@link InputStream} instances, additional instances of {@link Multipart}
* or other attached instances of {@link javax.mail.Message}.
*
* Will create the respective {@link EmailFragment}s representing those attachments.
*
* Instances of {@link javax.mail.Message} are delegated to
* {@link #handleMessage(File, javax.mail.Message, List)}. Further instances
* of {@link Multipart} are delegated to
* {@link #handleMultipart(File, Multipart, javax.mail.Message, List)}.
*
* @param directory Must not be null
* @param multipart Must not be null
* @param mailMessage Must not be null
* @param emailFragments Must not be null
*/
public static void handleMultipart(File directory, Multipart multipart, javax.mail.Message mailMessage, List<EmailFragment> emailFragments) {
Assert.notNull(directory, "The directory must not be null.");
Assert.notNull(multipart, "The multipart object to be parsed must not be null.");
Assert.notNull(mailMessage, "The mail message to be parsed must not be null.");
Assert.notNull(emailFragments, "The collection of emailfragments must not be null.");
final int count;
try {
count = multipart.getCount();
if (LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("Number of enclosed BodyPart objects: %s.", count));
}
} catch (MessagingException e) {
throw new IllegalStateException("Error while retrieving the number of enclosed BodyPart objects.", e);
}
for (int i = 0; i < count; i++) {
final BodyPart bp;
try {
bp = multipart.getBodyPart(i);
} catch (MessagingException e) {
throw new IllegalStateException("Error while retrieving body part.", e);
}
final String contentType;
String filename;
final String disposition;
final String subject;
try {
contentType = bp.getContentType();
filename = bp.getFileName();
disposition = bp.getDisposition();
subject = mailMessage.getSubject();
if (filename == null && bp instanceof MimeBodyPart) {
filename = ((MimeBodyPart) bp).getContentID();
}
} catch (MessagingException e) {
throw new IllegalStateException("Unable to retrieve body part meta data.", e);
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("BodyPart - Content Type: '%s', filename: '%s', disposition: '%s', subject: '%s'", new Object[] { contentType, filename, disposition, subject }));
}
if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
}
final Object content;
try {
content = bp.getContent();
} catch (IOException e) {
throw new IllegalStateException("Error while retrieving the email contents.", e);
} catch (MessagingException e) {
throw new IllegalStateException("Error while retrieving the email contents.", e);
}
if (content instanceof String) {
if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
emailFragments.add(new EmailFragment(directory, i + "-" + filename, content));
LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
} else {
final String textFilename;
final ContentType ct;
try {
ct = new ContentType(contentType);
} catch (ParseException e) {
throw new IllegalStateException("Error while parsing content type '" + contentType + "'.", e);
}
if ("text/plain".equalsIgnoreCase(ct.getBaseType())) {
textFilename = "message.txt";
} else if ("text/html".equalsIgnoreCase(ct.getBaseType())) {
textFilename = "message.html";
} else {
textFilename = "message.other";
}
emailFragments.add(new EmailFragment(directory, textFilename, content));
}
} else if (content instanceof InputStream) {
final InputStream inputStream = (InputStream) content;
final ByteArrayOutputStream bis = new ByteArrayOutputStream();
try {
IOUtils.copy(inputStream, bis);
} catch (IOException e) {
throw new IllegalStateException("Error while copying input stream to the ByteArrayOutputStream.", e);
}
emailFragments.add(new EmailFragment(directory, filename, bis.toByteArray()));
} else if (content instanceof javax.mail.Message) {
handleMessage(directory, (javax.mail.Message) content, emailFragments);
} else if (content instanceof Multipart) {
final Multipart mp2 = (Multipart) content;
handleMultipart(directory, mp2, mailMessage, emailFragments);
} else {
throw new IllegalStateException("Content type not handled: " + content.getClass().getSimpleName());
}
}
}
use of javax.mail.internet.ParseException in project service-proxy by membrane.
the class XMLContentFilter method removeMatchingElements.
/**
* Removes parts of an XML document based on an XPath expression.
*
* If the message is not valid XML, it is left unchanged.
*/
public void removeMatchingElements(Message message) {
try {
Message xop = null;
try {
xop = xopReconstitutor.getReconstitutedMessage(message);
} catch (ParseException e) {
} catch (EndOfStreamException e) {
} catch (FactoryConfigurationError e) {
}
if (elementFinder != null && !elementFinder.matches(xop != null ? xop.getBodyAsStream() : message.getBodyAsStream())) {
return;
}
DocumentBuilder db = createDocumentBuilder();
Document d;
try {
d = db.parse(xop != null ? xop.getBodyAsStream() : message.getBodyAsStream());
} finally {
db.reset();
}
removeElementsIfNecessary(message, xop, d);
} catch (SAXException e) {
return;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (XMLStreamException e) {
return;
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
} catch (TransformerConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException e) {
throw new RuntimeException(e);
} catch (TransformerFactoryConfigurationError e) {
throw new RuntimeException(e);
}
}
Aggregations