use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestQueryTunnel method testNestedMultiPartBody.
@Test
public void testNestedMultiPartBody() throws Exception {
// Construct a request with multi-part entity as a body and make sure it can be tunneled
// The entity will be nested - the original multi-part body will become the 2nd part of the tunneled
// body with the query passed as the form-encoded url
final MimeMultipart multi = new MimeMultipart("mixed");
MimeBodyPart partOne = new MimeBodyPart();
partOne.setContent(new String("{\"name\":\"value\"}").getBytes(), "application/json");
partOne.setHeader("Content-Type", "application/json");
// Encode query params as form-urlencoded
MimeBodyPart partTwo = new MimeBodyPart();
partTwo.setContent("x=y&z=w&q=10", "application/x-www-form-urlencoded");
partTwo.setHeader("Content-Type", "application /x-www-form-urlencoded");
multi.addBodyPart(partTwo);
multi.addBodyPart(partOne);
ByteArrayOutputStream os = new ByteArrayOutputStream();
multi.writeTo(os);
// Create request with multi-part body and query args
final RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?args=xyz")).setMethod("PUT").setHeader("Content-Type", multi.getContentType()).setEntity(ByteString.copy(os.toByteArray())).build();
// Encode and verify
RestRequest encoded = encode(request, 0);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() > 0);
Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
// Decode and make sure we have the original request back
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(encoded, requestContext);
Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?args=xyz");
Assert.assertEquals(decoded.getMethod(), "PUT");
Assert.assertEquals(decoded.getEntity(), request.getEntity());
Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
Assert.assertEquals(decoded.getHeader("Content-Length"), Integer.toString(request.getEntity().length()));
}
use of javax.mail.internet.MimeBodyPart in project google-app-engine-jappstart by taylorleese.
the class MailService method sendActivationEmail.
/**
* Sends the activation e-mail to the given user.
*
* @param user the user
* @param locale the locale
* @throws MessagingException messaging exception
*/
public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException {
final Properties props = new Properties();
final Session session = Session.getDefaultInstance(props, null);
final Message message = new MimeMessage(session);
final Multipart multipart = new MimeMultipart();
final MimeBodyPart htmlPart = new MimeBodyPart();
final MimeBodyPart textPart = new MimeBodyPart();
message.setFrom(new InternetAddress(getFromAddress()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale)));
textPart.setContent(messageSource.getMessage("mail.body.txt", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain");
htmlPart.setContent(messageSource.getMessage("mail.body.html", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html");
multipart.addBodyPart(textPart);
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
Transport.send(message);
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class HumanReadableTextAssembler method makeBodyPart.
protected MimeBodyPart makeBodyPart(List<Address> rejectedRecipients, String errorMessage) throws MessagingException {
String assembleHtmlBody;
try {
assembleHtmlBody = assembleHtmlBody(rejectedRecipients, errorMessage);
} catch (IOException e) {
throw new MessagingException("", e);
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(assembleHtmlBody, "text/html");
return mimeBodyPart;
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class SMIMECryptographerImpl method deserializeSignatureEnvelope.
/**
* Extracts the ASN1 encoded signature data from the signed entity.
* @param entity The entity containing the original signed part and the message signature.
* @return A CMSSignedData object that contains the ASN1 encoded signature data of the message.
*/
public CMSSignedData deserializeSignatureEnvelope(SignedEntity entity) {
if (entity == null) {
throw new NHINDException();
}
CMSSignedData signed = null;
try {
//signed = new SMIMESigned(entity.getMimeMultipart());
byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entity.getContent());
MimeBodyPart signedContent = null;
signedContent = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
signed = new CMSSignedData(new CMSProcessableBodyPart(signedContent), entity.getMimeMultipart().getBodyPart(1).getInputStream());
} catch (Exception e) {
e.printStackTrace();
throw new MimeException(MimeError.Unexpected, e);
}
return signed;
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class SMIMECryptographerImpl method createEncryptedEnvelope.
private MimeBodyPart createEncryptedEnvelope(MimeBodyPart bodyPart, Collection<X509Certificate> encryptingCertificates) {
if (bodyPart == null || encryptingCertificates == null || encryptingCertificates.size() == 0) {
throw new IllegalArgumentException();
}
if (LOGGER.isDebugEnabled()) {
writePreEncypt(EntitySerializer.Default.serializeToBytes(bodyPart));
}
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
for (X509Certificate cert : encryptingCertificates) gen.addKeyTransRecipient(cert);
MimeBodyPart retVal = null;
try {
final String encryAlgOID = this.m_encryptionAlgorithm.getOID();
retVal = gen.generate(bodyPart, encryAlgOID, CryptoExtensions.getJCEProviderNameForTypeAndAlgorithm("Cipher", encryAlgOID));
} catch (Exception e) {
throw new MimeException(MimeError.Unexpected, e);
}
return retVal;
}
Aggregations