List of usage examples for javax.mail.internet MimeMessage getContentType
@Override public String getContentType() throws MessagingException
From source file:mitm.application.djigzo.james.mailets.SMIMEEncryptTest.java
@Test public void testProtectedHeadersStatic() throws Exception { MockMailetConfig mailetConfig = new MockMailetConfig("test"); SMIMEEncrypt mailet = new SMIMEEncrypt(); mailetConfig.setInitParameter("retainMessageID", "false"); mailetConfig.setInitParameter("protectedHeader", "from, message-id"); mailet.init(mailetConfig);/* ww w.ja v a2 s .co m*/ Mail mail = new MockMail(); MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message-with-id.eml")); assertEquals("<123456>", message.getMessageID()); mail.setMessage(message); Set<MailAddress> recipients = new HashSet<MailAddress>(); recipients.add(new MailAddress("test@example.com")); mail.setRecipients(recipients); DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail); mailAttributes.setCertificates(certificates); mailet.service(mail); MimeMessage encrypted = mail.getMessage(); /* * Change from to see whether it was protected */ encrypted.setFrom(new InternetAddress("changed@changed.example.com")); MailUtils.validateMessage(encrypted); assertFalse("<123456>".equals(encrypted.getMessageID())); assertEquals(SMIMEHeader.ENCRYPTED_CONTENT_TYPE, encrypted.getContentType()); KeyStoreKeyProvider keyStore = new KeyStoreKeyProvider( loadKeyStore(new File("test/resources/testdata/keys/testCertificates.p12"), "test"), "test"); SMIMEInspector inspector = new SMIMEInspectorImpl(encrypted, keyStore, "BC"); assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType()); /* * now decrypt and check whether message-id and from was protected */ MimeMessage decrypted = inspector.getContentAsMimeMessage(); inspector = new SMIMEInspectorImpl(decrypted, keyStore, "BC"); assertEquals(SMIMEType.NONE, inspector.getSMIMEType()); assertTrue("<123456>".equals(decrypted.getMessageID())); assertEquals("test@example.com", StringUtils.join(decrypted.getFrom())); }
From source file:mitm.application.djigzo.james.mailets.SMIMEEncryptTest.java
@Test public void testProtectedHeadersMailAttributes() throws Exception { MockMailetConfig mailetConfig = new MockMailetConfig("test"); SMIMEEncrypt mailet = new SMIMEEncrypt(); mailetConfig.setInitParameter("retainMessageID", "false"); mailet.init(mailetConfig);//from www . j a v a 2 s. c o m Mail mail = new MockMail(); DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail); /* * set protected headers in the mail attributes */ mailAttributes.setProtectedHeaders(new String[] { "from", "message-id" }); MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message-with-id.eml")); assertEquals("<123456>", message.getMessageID()); mail.setMessage(message); Set<MailAddress> recipients = new HashSet<MailAddress>(); recipients.add(new MailAddress("test@example.com")); mail.setRecipients(recipients); mailAttributes.setCertificates(certificates); mailet.service(mail); MimeMessage encrypted = mail.getMessage(); /* * Change from to see whether it was protected */ encrypted.setFrom(new InternetAddress("changed@changed.example.com")); MailUtils.validateMessage(encrypted); assertFalse("<123456>".equals(encrypted.getMessageID())); assertEquals(SMIMEHeader.ENCRYPTED_CONTENT_TYPE, encrypted.getContentType()); KeyStoreKeyProvider keyStore = new KeyStoreKeyProvider( loadKeyStore(new File("test/resources/testdata/keys/testCertificates.p12"), "test"), "test"); SMIMEInspector inspector = new SMIMEInspectorImpl(encrypted, keyStore, "BC"); assertEquals(SMIMEType.ENCRYPTED, inspector.getSMIMEType()); /* * now decrypt and check whether message-id and from was protected */ MimeMessage decrypted = inspector.getContentAsMimeMessage(); inspector = new SMIMEInspectorImpl(decrypted, keyStore, "BC"); assertEquals(SMIMEType.NONE, inspector.getSMIMEType()); assertTrue("<123456>".equals(decrypted.getMessageID())); assertEquals("test@example.com", StringUtils.join(decrypted.getFrom())); }
From source file:de.mendelson.comm.as2.message.AS2MessageParser.java
/**Analyzes and creates passed message data *///w w w . j a v a 2s . c o m public AS2Message createMessageFromRequest(byte[] rawMessageData, Properties header, String contentType) throws AS2Exception { AS2Message message = new AS2Message(new AS2MessageInfo()); if (this.configConnection == null) { throw new AS2Exception(AS2Exception.PROCESSING_ERROR, "AS2MessageParser: Pass a DB connection before calling createMessageFromRequest()", message); } try { //decode the content transfer encoding if set rawMessageData = this.processContentTransferEncoding(rawMessageData, header); //check if this is a MDN MDNParser mdnParser = new MDNParser(); AS2MDNInfo mdnInfo = mdnParser.parseMDNData(rawMessageData, contentType); //its a MDN if (mdnInfo != null) { message.setAS2Info(mdnInfo); mdnInfo.initializeByRequestHeader(header); return (this.createFromMDNRequest(rawMessageData, header, contentType, mdnInfo, mdnParser)); } else { AS2MessageInfo messageInfo = (AS2MessageInfo) message.getAS2Info(); messageInfo.initializeByRequestHeader(header); //inbound AS2 message, no MDN //no futher processing if the message does not contain a message id if (messageInfo.getMessageId() == null) { return (message); } //figure out if the MDN should be signed NOW. If an error occurs beyond this point //the info has to know if the returned MDN should be signed messageInfo.getDispositionNotificationOptions() .setHeaderValue(header.getProperty("disposition-notification-options")); //check for existing partners PartnerAccessDB partnerAccess = new PartnerAccessDB(this.configConnection, this.runtimeConnection); Partner sender = partnerAccess.getPartner(messageInfo.getSenderId()); Partner receiver = partnerAccess.getPartner(messageInfo.getReceiverId()); if (sender == null) { throw new AS2Exception(AS2Exception.UNKNOWN_TRADING_PARTNER_ERROR, "Sender AS2 id " + messageInfo.getSenderId() + " is unknown.", message); } if (receiver == null) { throw new AS2Exception(AS2Exception.UNKNOWN_TRADING_PARTNER_ERROR, "Receiver AS2 id " + messageInfo.getReceiverId() + " is unknown.", message); } if (!receiver.isLocalStation()) { throw new AS2Exception( AS2Exception.PROCESSING_ERROR, "The receiver of the message (" + receiver.getAS2Identification() + ") is not defined as a local station.", message); } MessageAccessDB messageAccess = new MessageAccessDB(this.configConnection, this.runtimeConnection); //check if the message already exists AS2MessageInfo alreadyExistingInfo = this.messageAlreadyExists(messageAccess, messageInfo.getMessageId()); if (alreadyExistingInfo != null) { //perform notification: Resend detected, manual interaction might be required Notification notification = new Notification(this.configConnection, this.runtimeConnection); notification.sendResendDetected(messageInfo, alreadyExistingInfo, sender, receiver); throw new AS2Exception(AS2Exception.PROCESSING_ERROR, "An AS2 message with the message id " + messageInfo.getMessageId() + " has been already processed successfully by the system or is pending (" + alreadyExistingInfo.getInitDate() + "). Please " + " resubmit the message with a new message id instead or resending it if it should be processed again.", new AS2Message(messageInfo)); } messageAccess.initializeOrUpdateMessage(messageInfo); if (this.logger != null) { //do not log before because the logging process is related to an already created message in the transaction log this.logger.log( Level.FINE, this.rb .getResourceString("msg.incoming", new Object[] { messageInfo.getMessageId(), AS2Tools.getDataSizeDisplay(rawMessageData.length) }), messageInfo); } //indicates if a sync or async mdn is requested messageInfo.setAsyncMDNURL(header.getProperty("receipt-delivery-option")); messageInfo.setRequestsSyncMDN(header.getProperty("receipt-delivery-option") == null || header.getProperty("receipt-delivery-option").trim().length() == 0); message.setRawData(rawMessageData); byte[] decryptedData = this.decryptMessage(message, rawMessageData, contentType, sender, receiver); //may be already compressed here. Decompress first before going further if (this.contentTypeIndicatesCompression(contentType)) { byte[] decompressed = this.decompressData((AS2MessageInfo) message.getAS2Info(), decryptedData, contentType); message.setDecryptedRawData(decompressed); //content type has changed now, get it from the decompressed data ByteArrayInputStream memIn = new ByteArrayInputStream(decompressed); MimeBodyPart tempPart = new MimeBodyPart(memIn); memIn.close(); contentType = tempPart.getContentType(); } else { //check the MIME structure that is embedded in decryptedData for its content type ByteArrayInputStream memIn = new ByteArrayInputStream(decryptedData); MimeMessage possibleCompressedPart = new MimeMessage( Session.getInstance(System.getProperties()), memIn); memIn.close(); if (this.contentTypeIndicatesCompression(possibleCompressedPart.getContentType())) { long compressedSize = possibleCompressedPart.getSize(); byte[] decompressed = this.decompressData((AS2MessageInfo) message.getAS2Info(), new SMIMECompressed(possibleCompressedPart), compressedSize); message.setDecryptedRawData(decompressed); } else { message.setDecryptedRawData(decryptedData); } } decryptedData = null; Part payloadPart = this.verifySignature(message, sender, contentType); //decompress the data if it has been sent compressed, only possible for signed data if (message.getAS2Info().getSignType() != AS2Message.SIGNATURE_NONE) { //signed message: //http://tools.ietf.org/html/draft-ietf-ediint-compression-12 //4.1 MIC Calculation For Signed Message //For any signed message, the MIC to be returned is calculated over //the same data that was signed in the original message as per [AS1]. //The signed content will be a mime bodypart that contains either //compressed or uncompressed data. this.computeReceivedContentMIC(rawMessageData, message, payloadPart, contentType); payloadPart = this.decompressData(payloadPart, message); this.writePayloadsToMessage(payloadPart, message, header); } else { //this is an unsigned message this.writePayloadsToMessage(message.getDecryptedRawData(), message, header); //unsigned message: //http://tools.ietf.org/html/draft-ietf-ediint-compression-12 //4.2 MIC Calculation For Encrypted, Unsigned Message //For encrypted, unsigned messages, the MIC to be returned is //calculated over the uncompressed data content including all //MIME header fields and any applied Content-Transfer-Encoding. //http://tools.ietf.org/html/draft-ietf-ediint-compression-12 //4.3 MIC Calculation For Unencrypted, Unsigned Message //For unsigned, unencrypted messages, the MIC is calculated //over the uncompressed data content including all MIME header //fields and any applied Content-Transfer-Encoding. this.computeReceivedContentMIC(rawMessageData, message, payloadPart, contentType); } if (this.logger != null) { this.logger.log(Level.INFO, this.rb.getResourceString("found.attachments", new Object[] { messageInfo.getMessageId(), String.valueOf(message.getPayloadCount()) }), messageInfo); } return (message); } } catch (Exception e) { if (e instanceof AS2Exception) { throw (AS2Exception) e; } else { throw new AS2Exception(AS2Exception.PROCESSING_ERROR, e.getMessage(), message); } } }
From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java
/** * Process an email, extracting attachments and constructing a Message. * * @param mail the email to be processed. * @param mailingList the mailing list it is going to be affected to. * @param event the event which will be send at the end of all processing. * @throws MessagingException/*from ww w .j a v a2 s .c om*/ * @throws IOException */ public void prepareMessage(MimeMessage mail, MessageListener mailingList, MessageEvent event) throws MessagingException, IOException { String sender = ((InternetAddress[]) mail.getFrom())[0].getAddress(); if (!mailingList.checkSender(sender)) { return; } Message message = new Message(); message.setComponentId(mailingList.getComponentId()); message.setSender(sender); message.setSentDate(mail.getSentDate()); message.setMessageId(mail.getMessageID()); String[] referenceId = mail.getHeader(MAIL_HEADER_IN_REPLY_TO); if (referenceId == null || referenceId.length == 0) { referenceId = mail.getHeader(MAIL_HEADER_REFERENCES); } if (referenceId == null || referenceId.length == 0) { message.setReferenceId(null); } else { message.setReferenceId(referenceId[0]); } message.setTitle(mail.getSubject()); SilverTrace.info("mailingList", "MailProcessor.prepareMessage()", "mailinglist.notification.error", "Processing message " + mail.getSubject()); Object content = mail.getContent(); if (content instanceof Multipart) { processMultipart((Multipart) content, message); } else if (content instanceof String) { processBody((String) content, mail.getContentType(), message); } event.addMessage(message); }
From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java
@Test public void testCompressed() throws Exception { MimeMessage message = loadMessage("normal-message-with-attach.eml"); SMIMEBuilder builder = new SMIMEBuilderImpl(message); builder.compress();/*from w ww. j ava 2 s . c o m*/ MimeMessage newMessage = builder.buildMessage(); File file = new File(tempDir, "testCompressed.eml"); FileOutputStream output = new FileOutputStream(file); MailUtils.writeMessage(newMessage, output); newMessage = MailUtils.loadMessage(file); assertEquals(SMIMEHeader.COMPRESSED_CONTENT_TYPE, newMessage.getContentType()); assertEquals(SMIMEHeader.Type.COMPRESSED, SMIMEHeader.getSMIMEContentType(newMessage)); checkForSourceHeaders(newMessage); }
From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java
@Test public void testCompressDeprecatedHeaders() throws Exception { MimeMessage message = loadMessage("simple-text-message.eml"); SMIMEBuilder builder = new SMIMEBuilderImpl(message); builder.setUseDeprecatedContentTypes(true); builder.compress();/*from w w w.j av a2 s . c om*/ MimeMessage newMessage = builder.buildMessage(); File file = new File(tempDir, "testCompressDeprecatedHeaders.eml"); FileOutputStream output = new FileOutputStream(file); MailUtils.writeMessage(newMessage, output); newMessage = MailUtils.loadMessage(file); assertEquals(SMIMEHeader.DEPRECATED_COMPRESSED_CONTENT_TYPE, newMessage.getContentType()); assertEquals(SMIMEHeader.Type.COMPRESSED, SMIMEHeader.getSMIMEContentType(newMessage)); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); }
From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java
@Test public void testSimpleMessageEncrypt() throws Exception { MimeMessage message = loadMessage("simple-text-message.eml"); SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from"); builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL); builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC); MimeMessage newMessage = builder.buildMessage(); File file = new File(tempDir, "simple-text-message-encrypted.eml"); FileOutputStream output = new FileOutputStream(file); MailUtils.writeMessage(newMessage, output); newMessage = MailUtils.loadMessage(file); assertEquals(SMIMEHeader.ENCRYPTED_CONTENT_TYPE, newMessage.getContentType()); assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage)); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); File opensslOutputFileSigned = new File(tempDir, "simple-text-message-encrypted-openssl.eml"); decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned); newMessage = MailUtils.loadMessage(opensslOutputFileSigned); assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage)); assertTrue(newMessage.isMimeType("text/plain")); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage)); String content = (String) newMessage.getContent(); assertEquals("test", content.trim()); }
From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java
@Test public void testEncryptDeprecatedHeaders() throws Exception { MimeMessage message = loadMessage("simple-text-message.eml"); SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from"); builder.setUseDeprecatedContentTypes(true); builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL); builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC); MimeMessage newMessage = builder.buildMessage(); File file = new File(tempDir, "testEncryptDeprecatedHeaders.eml"); FileOutputStream output = new FileOutputStream(file); MailUtils.writeMessage(newMessage, output); newMessage = MailUtils.loadMessage(file); assertEquals(SMIMEHeader.DEPRECATED_ENCRYPTED_CONTENT_TYPE, newMessage.getContentType()); assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage)); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); File opensslOutputFileSigned = new File(tempDir, "testEncryptDeprecatedHeaders-openssl.eml"); decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned); newMessage = MailUtils.loadMessage(opensslOutputFileSigned); assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage)); assertTrue(newMessage.isMimeType("text/plain")); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage)); String content = (String) newMessage.getContent(); assertEquals("test", content.trim()); }
From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java
@Test public void testOpaqueSignSimpleTextMessage() throws Exception { MimeMessage message = loadMessage("simple-text-message.eml"); SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from"); builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(), SMIMESigningAlgorithm.SHA1WITHRSA); builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain())); builder.sign(SMIMESignMode.OPAQUE);//from w w w . ja v a 2s. c o m MimeMessage newMessage = builder.buildMessage(); File file = new File(tempDir, "test-simple-text-message-opaque-signed.eml"); FileOutputStream output = new FileOutputStream(file); MailUtils.writeMessage(newMessage, output); newMessage = MailUtils.loadMessage(file); assertEquals(SMIMEHeader.ENCAPSULATED_SIGNED_CONTENT_TYPE, newMessage.getContentType()); assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage)); File opensslOutputFile = new File(tempDir, "test-simple-text-message-opaque-openssl.eml"); verifyMessage(file, rootCertificate, opensslOutputFile); newMessage = MailUtils.loadMessage(opensslOutputFile); assertTrue(newMessage.isMimeType("text/plain")); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage)); String content = (String) newMessage.getContent(); assertEquals("test", content.trim()); }
From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java
@Test public void testOpaqueSignDeprecatedHeaders() throws Exception { MimeMessage message = loadMessage("simple-text-message.eml"); SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from"); builder.setUseDeprecatedContentTypes(true); builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(), SMIMESigningAlgorithm.SHA1WITHRSA); builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain())); builder.sign(SMIMESignMode.OPAQUE);/*www . j av a2 s . c o m*/ MimeMessage newMessage = builder.buildMessage(); File file = new File(tempDir, "testOpaqueSignDeprecatedHeaders.eml"); FileOutputStream output = new FileOutputStream(file); MailUtils.writeMessage(newMessage, output); newMessage = MailUtils.loadMessage(file); assertEquals(SMIMEHeader.DEPRECATED_ENCAPSULATED_SIGNED_CONTENT_TYPE, newMessage.getContentType()); assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage)); File opensslOutputFile = new File(tempDir, "testOpaqueSignDeprecatedHeaders-openssl.eml"); verifyMessage(file, rootCertificate, opensslOutputFile); newMessage = MailUtils.loadMessage(opensslOutputFile); assertTrue(newMessage.isMimeType("text/plain")); assertEquals("test@example.com", newMessage.getHeader("from", ",")); assertEquals("test@example.com", newMessage.getHeader("to", ",")); assertEquals("test simple message", newMessage.getHeader("subject", ",")); assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage)); String content = (String) newMessage.getContent(); assertEquals("test", content.trim()); }