use of org.nhindirect.stagent.mail.MimeException in project nhin-d by DirectProject.
the class EntitySerializer method deserialize.
/**
* Deserializes a MimeMessage from a raw byte array representation.
* @param stream A raw byte array representation of the entity.
* @return A MimeMessage deserialized from the byte array.
*/
public MimeMessage deserialize(byte[] messageBytes) {
MimeMessage retVal = null;
if (messageBytes == null || messageBytes.length == 0) {
throw new IllegalArgumentException();
}
try {
ByteArrayInputStream inStream = new ByteArrayInputStream(messageBytes);
retVal = deserialize(inStream);
} catch (Exception e) {
throw new MimeException(MimeError.Unexpected, e);
}
return retVal;
}
use of org.nhindirect.stagent.mail.MimeException in project nhin-d by DirectProject.
the class EntitySerializer method serialize.
/**
* Serializes a collection of MimeBodyPart to a string with a given boundary.
* @param entity The entities to serialize.
* @param boundary The boundary string that will separate each entity.
* @return A raw String representation of the serialized entities.
*/
public String serialize(Collection<MimeBodyPart> parts, String boundary) {
String retVal = "";
try {
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
serialize(parts, boundary, oStream);
oStream.flush();
retVal = oStream.toString("ASCII");
IOUtils.closeQuietly(oStream);
} catch (Exception e) {
throw new MimeException(MimeError.Unexpected, e);
}
return retVal;
}
use of org.nhindirect.stagent.mail.MimeException in project nhin-d by DirectProject.
the class EntitySerializer method serialize.
/**
* Serializes a MimePart to a writer.
* @param entity The entity to serialize.
* @param stream The writer that the serialized object will be written to.
*/
public void serialize(MimePart message, Writer writer) {
try {
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
serialize(message, oStream);
oStream.flush();
IOUtils.write(oStream.toByteArray(), writer, "ASCII");
IOUtils.closeQuietly(oStream);
} catch (Exception e) {
throw new MimeException(MimeError.Unexpected, e);
}
}
use of org.nhindirect.stagent.mail.MimeException in project nhin-d by DirectProject.
the class DefaultNHINDAgent method signAndEncryptMessage.
//
// First sign, THEN encrypt the message
//
@SuppressWarnings("unchecked")
protected void signAndEncryptMessage(OutgoingMessage message) {
SignedEntity signedEntity = cryptographer.sign(message.getMessage(), message.getSender().getCertificates());
try {
if (encryptionEnabled) {
MimeEntity encryptedEntity = cryptographer.encrypt(signedEntity.getMimeMultipart(), message.getRecipients().getCertificates());
//
// Alter message content to contain encrypted data
//
InternetHeaders headers = new InternetHeaders();
Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = eHeaders.nextElement();
headers.setHeader(hdr.getName(), hdr.getValue());
}
eHeaders = encryptedEntity.getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = (Header) eHeaders.nextElement();
headers.setHeader(hdr.getName(), hdr.getValue());
}
Message msg = new Message(headers, encryptedEntity.getContentAsBytes());
message.setMessage(msg);
} else {
InternetHeaders headers = new InternetHeaders();
Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = eHeaders.nextElement();
headers.setHeader(hdr.getName(), hdr.getValue());
}
headers.setHeader(MimeStandard.ContentTypeHeader, signedEntity.getMimeMultipart().getContentType());
Message msg = new Message(headers, signedEntity.getEntityBodyAsBytes());
message.setMessage(msg);
}
} catch (Exception e) {
throw new MimeException(MimeError.InvalidMimeEntity, e);
}
}
use of org.nhindirect.stagent.mail.MimeException in project nhin-d by DirectProject.
the class DefaultNHINDAgent method decryptSignedContent.
/*
* Decrypts the signed message
*/
@SuppressWarnings("unchecked")
protected void decryptSignedContent(IncomingMessage message) {
MimeEntity decryptedEntity = this.decryptMessage(message);
CMSSignedData signatures;
MimeEntity payload;
try {
if (SMIMEStandard.isContentEnvelopedSignature(new ContentType(decryptedEntity.getContentType()))) {
signatures = cryptographer.deserializeEnvelopedSignature(decryptedEntity);
payload = new MimeEntity(new ByteArrayInputStream(signatures.getContentInfo().getEncoded()));
} else if (SMIMEStandard.isContentMultipartSignature(new ContentType(decryptedEntity.getContentType()))) {
//
// Extract the signature envelope. That contains both the signature and the actual message content
//
ByteArrayDataSource dataSource = new ByteArrayDataSource(decryptedEntity.getRawInputStream(), decryptedEntity.getContentType());
MimeMultipart verifyMM = new MimeMultipart(dataSource);
SignedEntity signedEntity = SignedEntity.load(verifyMM);
signatures = cryptographer.deserializeSignatureEnvelope(signedEntity);
payload = signedEntity.getContent();
} else {
throw new AgentException(AgentError.UnsignedMessage);
}
message.setSignature(signatures);
//
// Alter body to contain actual content. Also clean up mime headers on the message that were there to support
// signatures etc
//
InternetHeaders headers = new InternetHeaders();
// remove all mime headers
Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = (Header) eHeaders.nextElement();
if (!MimeStandard.startsWith(hdr.getName(), MimeStandard.HeaderPrefix))
headers.setHeader(hdr.getName(), hdr.getValue());
}
// add back in headers from original message
eHeaders = payload.getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = (Header) eHeaders.nextElement();
headers.setHeader(hdr.getName(), hdr.getValue());
}
Message msg = new Message(headers, payload.getContentAsBytes());
message.setMessage(msg);
} catch (MessagingException e) {
throw new MimeException(MimeError.InvalidBody, e);
} catch (IOException e) {
throw new MimeException(MimeError.InvalidBody, e);
}
}
Aggregations