use of javax.mail.internet.MimeMultipart in project ats-framework by Axway.
the class MimePackage method addPart.
private void addPart(BodyPart part, int position) throws NoSuchMimePartException, PackageException {
try {
Object messageContent = message.getContent();
if (messageContent instanceof MimeMultipart) {
MimeMultipart multipartContent = (MimeMultipart) messageContent;
int positionToInsertAt = position;
if (position > multipartContent.getCount()) {
positionToInsertAt = multipartContent.getCount();
}
multipartContent.addBodyPart(part, positionToInsertAt);
// set back the modified content
message.setContent(multipartContent);
// decomposing
try {
message.saveChanges();
} catch (MessagingException me) {
throw new PackageException("Could not save message changes", me);
}
// we need to decompose again, as a new part has been added
decompose();
} else {
// TODO: we can transform the part to MimeMultipart if desired
throw new PackageException("Message is not multipart!");
}
} catch (MessagingException me) {
throw new PackageException(me);
} catch (IOException ioe) {
throw new PackageException(ioe);
}
}
use of javax.mail.internet.MimeMultipart in project Activiti by Activiti.
the class EmailSendTaskTest method testTextMailWithFileAttachmentsByPath.
@Deployment
public void testTextMailWithFileAttachmentsByPath() throws Exception {
HashMap<String, Object> vars = new HashMap<String, Object>();
vars.put("attachmentsBean", new AttachmentsBean());
runtimeService.startProcessInstanceByKey("textMailWithFileAttachmentsByPath", vars);
List<WiserMessage> messages = wiser.getMessages();
assertEquals(1, messages.size());
WiserMessage message = messages.get(0);
MimeMultipart mm = (MimeMultipart) message.getMimeMessage().getContent();
File[] files = new AttachmentsBean().getFiles();
assertEquals(1 + files.length, mm.getCount());
for (int i = 0; i < files.length; i++) {
String attachmentFileName = mm.getBodyPart(1 + i).getDataHandler().getName();
assertEquals(files[i].getName(), attachmentFileName);
}
}
use of javax.mail.internet.MimeMultipart in project Activiti by Activiti.
the class EmailServiceTaskTest method testTextMailWithFileAttachment.
@Deployment
public void testTextMailWithFileAttachment() throws Exception {
HashMap<String, Object> vars = new HashMap<String, Object>();
vars.put("attachmentsBean", new AttachmentsBean());
runtimeService.startProcessInstanceByKey("textMailWithFileAttachment", vars);
List<WiserMessage> messages = wiser.getMessages();
assertEquals(1, messages.size());
WiserMessage message = messages.get(0);
MimeMultipart mm = (MimeMultipart) message.getMimeMessage().getContent();
assertEquals(2, mm.getCount());
String attachmentFileName = mm.getBodyPart(1).getDataHandler().getName();
assertEquals(new AttachmentsBean().getFile().getName(), attachmentFileName);
}
use of javax.mail.internet.MimeMultipart 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.MimeMultipart in project nhin-d by DirectProject.
the class MessageSigInspector method main.
public static void main(String[] args) {
if (args.length == 0) {
//printUsage();
System.exit(-1);
}
String messgefile = null;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
// Options
if (!arg.startsWith("-")) {
System.err.println("Error: Unexpected argument [" + arg + "]\n");
//printUsage();
System.exit(-1);
} else if (arg.equalsIgnoreCase("-msgFile")) {
if (i == args.length - 1 || args[i + 1].startsWith("-")) {
System.err.println("Error: Missing message file");
System.exit(-1);
}
messgefile = args[++i];
} else if (arg.equals("-help")) {
//printUsage();
System.exit(-1);
} else {
System.err.println("Error: Unknown argument " + arg + "\n");
//printUsage();
System.exit(-1);
}
}
if (messgefile == null) {
System.err.println("Error: missing message file\n");
}
InputStream inStream = null;
try {
inStream = FileUtils.openInputStream(new File(messgefile));
MimeMessage message = new MimeMessage(null, inStream);
MimeMultipart mm = (MimeMultipart) message.getContent();
//byte[] messageBytes = EntitySerializer.Default.serializeToBytes(mm.getBodyPart(0).getContent());
//MimeBodyPart signedContent = null;
//signedContent = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
final CMSSignedData signed = new CMSSignedData(new CMSProcessableBodyPart(mm.getBodyPart(0)), mm.getBodyPart(1).getInputStream());
CertStore certs = signed.getCertificatesAndCRLs("Collection", CryptoExtensions.getJCEProviderName());
SignerInformationStore signers = signed.getSignerInfos();
@SuppressWarnings("unchecked") Collection<SignerInformation> c = signers.getSigners();
System.out.println("Found " + c.size() + " signers");
int cnt = 1;
for (SignerInformation signer : c) {
Collection<? extends Certificate> certCollection = certs.getCertificates(signer.getSID());
if (certCollection != null && certCollection.size() > 0) {
X509Certificate cert = (X509Certificate) certCollection.iterator().next();
System.out.println("\r\nInfo for certificate " + cnt++);
System.out.println("\tSubject " + cert.getSubjectDN());
FileUtils.writeByteArrayToFile(new File("SigCert.der"), cert.getEncoded());
byte[] bytes = cert.getExtensionValue("2.5.29.15");
if (bytes != null) {
final DERObject obj = getObject(bytes);
final KeyUsage keyUsage = new KeyUsage((DERBitString) obj);
final byte[] data = keyUsage.getBytes();
final int intValue = (data.length == 1) ? data[0] & 0xff : (data[1] & 0xff) << 8 | (data[0] & 0xff);
System.out.println("\tKey Usage: " + intValue);
} else
System.out.println("\tKey Usage: NONE");
//verify and get the digests
final Attribute digAttr = signer.getSignedAttributes().get(CMSAttributes.messageDigest);
final DERObject hashObj = digAttr.getAttrValues().getObjectAt(0).getDERObject();
final byte[] signedDigest = ((ASN1OctetString) hashObj).getOctets();
final String signedDigestHex = org.apache.commons.codec.binary.Hex.encodeHexString(signedDigest);
System.out.println("\r\nSigned Message Digest: " + signedDigestHex);
try {
signer.verify(cert, "BC");
System.out.println("Signature verified.");
} catch (CMSException e) {
System.out.println("Signature failed to verify.");
}
// should have the computed digest now
final byte[] digest = signer.getContentDigest();
final String digestHex = org.apache.commons.codec.binary.Hex.encodeHexString(digest);
System.out.println("\r\nComputed Message Digest: " + digestHex);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inStream);
}
}
Aggregations