System.out.println( "HTML Content Type: " + htmlPart.getContentType() ); System.out.println( "HTML Content Type: " + htmlPart.getContentType() ); System.out.println( "HTML Data Handler: " + htmlPart.getDataHandler().getContentType() );
dh = new DataHandler(cachedContent, getContentType()); cachedContent = null; content = null;
dh = new DataHandler(cachedContent, getContentType()); cachedContent = null; content = null;
final String contentType = msgHtml.getContentType(); if (contentType == null || !contentType.equals(EmailConstants.TEXT_HTML))
public boolean isSigned(MimeBodyPart part) throws MessagingException { ContentType contentType = new ContentType(part.getContentType()); String baseType = contentType.getBaseType().toLowerCase(); return baseType.equalsIgnoreCase("multipart/signed"); }
/** * Tests to see if this message has a mime-type match with the * given type name. * * @param type The tested type name. * * @return If this is a type match on the primary and secondare portion of the types. * @exception MessagingException */ public boolean isMimeType(String type) throws MessagingException { return new ContentType(getContentType()).match(type); }
public boolean isSigned (@Nonnull final MimeBodyPart aPart) throws MessagingException { ValueEnforcer.notNull (aPart, "Part"); final ContentType aContentType = AS2HttpHelper.parseContentType (aPart.getContentType ()); if (aContentType == null) return false; final String sBaseType = aContentType.getBaseType (); return sBaseType.equalsIgnoreCase ("multipart/signed"); }
/** * Tests to see if this message has a mime-type match with the * given type name. * * @param type The tested type name. * * @return If this is a type match on the primary and secondare portion of the types. * @exception MessagingException */ public boolean isMimeType(String type) throws MessagingException { return new ContentType(getContentType()).match(type); }
public boolean isEncrypted(MimeBodyPart part) throws MessagingException { ContentType contentType = new ContentType(part.getContentType()); String baseType = contentType.getBaseType().toLowerCase(); if (baseType.equalsIgnoreCase("application/pkcs7-mime")) { String smimeType = contentType.getParameter("smime-type"); boolean checkResult = (smimeType != null) && smimeType.equalsIgnoreCase("enveloped-data"); if (!checkResult && logger.isDebugEnabled()) { logger.debug("Check for encrypted data failed on SMIME content type: " + smimeType); } return (checkResult); } if (logger.isDebugEnabled()) { logger.debug("Check for encrypted data failed on BASE content type: " + baseType); } return false; }
MimeMultipart multipart = (MimeMultipart) message.getContent(); System.out.println("This message consists of " + multipart.getCount() + " parts"); for (int i = 0; i < multipart.getCount(); i++) { // Note we're downcasting here, MimeBodyPart is the only subclass // of BodyPart available in the Java EE spec. MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(i); System.out.println("Part " + i + " has type " + bodyPart.getContentType()); System.out.println("Part " + i " + has filename " + bodyPart.getFileName()); // if it was an attachment // So on, so forth. }
public boolean isEncrypted (@Nonnull final MimeBodyPart aPart) throws MessagingException { ValueEnforcer.notNull (aPart, "Part"); // Content-Type is sthg like this if encrypted: // application/pkcs7-mime; name=smime.p7m; smime-type=enveloped-data final ContentType aContentType = AS2HttpHelper.parseContentType (aPart.getContentType ()); if (aContentType == null) return false; final String sBaseType = aContentType.getBaseType ().toLowerCase (Locale.US); if (!sBaseType.equals ("application/pkcs7-mime")) return false; final String sSmimeType = aContentType.getParameter ("smime-type"); return sSmimeType != null && sSmimeType.equalsIgnoreCase ("enveloped-data"); }
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); JceKeyAgreeRecipientInfoGenerator rig = new JceKeyAgreeRecipientInfoGenerator(CMSAlgorithm.ECDH_SHA1KDF, senderPrivateKey, senderPublicKey, CMSAlgorithm.AES128_WRAP); rig.setProvider("BC"); gen.addRecipientInfoGenerator(rig); MimeBodyPart msg = new MimeBodyPart(); msg.setText("This is a secret message"); MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC").build()); Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props, null); String to = "bob@example.com"; Address fromUser = new InternetAddress("alice@example.com"); Address toUser = new InternetAddress(to); MimeMessage body = new MimeMessage(session); body.setFrom(fromUser); body.setRecipient(Message.RecipientType.TO, toUser); body.setSubject("example encrypted message"); body.setContent(mp.getContent(), mp.getContentType()); body.saveChanges(); body.writeTo(new FileOutputStream("/tmp/encrypted.msg"));
public DataHistoryItem setData(MimeBodyPart data) throws OpenAS2Exception { try { DataHistoryItem historyItem = new DataHistoryItem(data.getContentType()); setData(data, historyItem); return historyItem; } catch (Exception e) { throw new WrappedException(e); } }
public void setData (@Nullable final MimeBodyPart aData) { // Remember data m_aData = aData; if (aData != null) { // Set content type from data try { setContentType (aData.getContentType ()); } catch (final MessagingException ex) { LOGGER.warn ("Failed to set the Content-Type from the MimeBodyPart. Defaulting to null."); setContentType (null); } // Set content disposition from data try { setContentDisposition (aData.getHeader (CHttpHeader.CONTENT_DISPOSITION, null)); } catch (final MessagingException ex) { LOGGER.warn ("Failed to set the Content-Disposition from the MimeBodyPart. Defaulting to null."); setContentDisposition (null); } } }
public static MimeMessage encryptMessage(MimeMessage message) throws Exception { Security.addProvider(new BouncyCastleProvider()); // create the generator for creating an smime/encrypted message SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator(); X509Certificate recipientCert = getRecipientPublicCertificate(message); gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(recipientCert).setProvider("BC")); MimeBodyPart msg = new MimeBodyPart(); msg.setContent(message.getContent(), message.getContentType()); MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).setProvider("BC").build()); message.setContent(mp.getContent(), mp.getContentType()); message.saveChanges(); return message; }
public MimeBodyPart compress(Message msg, MimeBodyPart mbp, String compressionType, String contentTxfrEncoding) throws SMIMEException, OpenAS2Exception { OutputCompressor compressor = null; if (compressionType != null) { if (compressionType.equalsIgnoreCase(ICryptoHelper.COMPRESSION_ZLIB)) { compressor = new ZlibCompressor(); } else { throw new OpenAS2Exception("Unsupported compression type: " + compressionType); } } SMIMECompressedGenerator sCompGen = new SMIMECompressedGenerator(); sCompGen.setContentTransferEncoding(getEncoding(contentTxfrEncoding)); MimeBodyPart smime = sCompGen.generate(mbp, compressor); if (logger.isTraceEnabled()) { try { logger.trace("Compressed MIME msg AFTER COMPRESSION Content-Type:" + smime.getContentType()); logger.trace("Compressed MIME msg AFTER COMPRESSION Content-Disposition:" + smime.getDisposition()); } catch (MessagingException e) { } } return smime; }
public void setData(MimeBodyPart data, DataHistoryItem historyItem) { this.data = data; if (data != null) { try { setContentType(data.getContentType()); } catch (MessagingException e) { setContentType(null); } try { setContentDisposition(data.getHeader("Content-Disposition", null)); } catch (MessagingException e) { setContentDisposition(null); // TODO: why ignore????? } } if (historyItem != null) { getHistory().getItems().add(historyItem); } }
/** * Retrieve a response to a previous <code>send</code> call and * return it as a string of XML. */ public String receive() throws SOAPException, MessagingException, IOException { String payload = null; if (_responseContext != null && _responseReader != null) { MimeBodyPart rootPart = _responseContext.getRootPart(); if (rootPart.isMimeType("text/*")) { // Get the input stream to read the response envelope from. payload = IOUtils.getStringFromReader(_responseReader); } // Check Content-Type of root part of response to see if it's // consistent with a SOAP envelope (text/xml). if (!rootPart.isMimeType(Constants.HEADERVAL_CONTENT_TYPE)) { throw new SOAPException(Constants.FAULT_CODE_PROTOCOL, "Unsupported response content type \"" + rootPart.getContentType() + "\", must be: \"" + Constants.HEADERVAL_CONTENT_TYPE + "\"." + (payload == null ? "" : " Response was:\n" + payload)); } } if (_monitor != null) _monitor.receive(payload); return payload; }
public void setFileName(String name) throws MessagingException { // there's an optional session property that requests file name encoding...we need to process this before // setting the value. if (name != null && SessionUtil.getBooleanProperty(MIME_ENCODEFILENAME, false)) { try { name = MimeUtility.encodeText(name); } catch (UnsupportedEncodingException e) { throw new MessagingException("Unable to encode filename", e); } } // get the disposition string. String disposition = getDisposition(); // if not there, then this is an attachment. if (disposition == null) { disposition = Part.ATTACHMENT; } // now create a disposition object and set the parameter. ContentDisposition contentDisposition = new ContentDisposition(disposition); contentDisposition.setParameter("filename", name); // serialize this back out and reset. setHeader("Content-Disposition", contentDisposition.toString()); // The Sun implementation appears to update the Content-type name parameter too, based on // another system property if (SessionUtil.getBooleanProperty(MIME_SETCONTENTTYPEFILENAME, true)) { ContentType type = new ContentType(getContentType()); type.setParameter("name", name); setHeader("Content-Type", type.toString()); } }
public void setFileName(String name) throws MessagingException { // there's an optional session property that requests file name encoding...we need to process this before // setting the value. if (name != null && SessionUtil.getBooleanProperty(MIME_ENCODEFILENAME, false)) { try { name = MimeUtility.encodeText(name); } catch (UnsupportedEncodingException e) { throw new MessagingException("Unable to encode filename", e); } } // get the disposition string. String disposition = getDisposition(); // if not there, then this is an attachment. if (disposition == null) { disposition = Part.ATTACHMENT; } // now create a disposition object and set the parameter. ContentDisposition contentDisposition = new ContentDisposition(disposition); contentDisposition.setParameter("filename", name); // serialize this back out and reset. setDisposition(contentDisposition.toString()); setHeader("Content-Disposition", contentDisposition.toString()); // The Sun implementation appears to update the Content-type name parameter too, based on // another system property if (SessionUtil.getBooleanProperty(MIME_SETCONTENTTYPEFILENAME, true)) { ContentType type = new ContentType(getContentType()); type.setParameter("name", name); setHeader("Content-Type", type.toString()); } }