use of javax.mail.internet.ContentType in project rest.li by linkedin.
the class TestRestLiServer method testMultipartRelatedRequestNoUserAttachments.
@Test
public void testMultipartRelatedRequestNoUserAttachments() throws Exception {
//This test verifies the server's ability to handle a multipart related request that has only one part which is
//the rest.li payload; meaning there are no user defined attachments. Technically the client builders shouldn't do
//this but we allow this to keep the protocol somewhat flexible.
final AsyncStatusCollectionResource statusResource = getMockResource(AsyncStatusCollectionResource.class);
statusResource.streamingAction(EasyMock.<String>anyObject(), EasyMock.<RestLiAttachmentReader>anyObject(), EasyMock.<Callback<Long>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
//Verify there are no attachments.
final RestLiAttachmentReader attachmentReader = (RestLiAttachmentReader) EasyMock.getCurrentArguments()[1];
Assert.assertNull(attachmentReader);
//Verify the action param.
Assert.assertEquals((String) EasyMock.getCurrentArguments()[0], "someMetadata");
//Now respond back to the request.
@SuppressWarnings("unchecked") Callback<Long> callback = (Callback<Long>) EasyMock.getCurrentArguments()[2];
callback.onSuccess(1234l);
return null;
}
});
EasyMock.replay(statusResource);
//Now we create a multipart/related payload.
final String payload = "{\"metadata\": \"someMetadata\"}";
final ByteStringWriter byteStringWriter = new ByteStringWriter(ByteString.copyString(payload, Charset.defaultCharset()));
final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
final MultiPartMIMEWriter writer = AttachmentUtils.createMultiPartMIMEWriter(byteStringWriter, "application/json", builder);
final StreamRequest streamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(new URI("/asyncstatuses/?action=streamingAction"), "related", writer, Collections.<String, String>emptyMap(), "POST", ImmutableMap.of(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED), Collections.emptyList());
final Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
Assert.assertEquals(result.getStatus(), 200);
try {
ContentType contentType = new ContentType(streamResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE));
Assert.assertEquals(contentType.getBaseType(), RestConstants.HEADER_VALUE_APPLICATION_JSON);
} catch (ParseException parseException) {
Assert.fail();
}
//Verify the response body
Assert.assertEquals(result.getEntity().asAvroString(), "{\"value\":1234}");
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
}
use of javax.mail.internet.ContentType in project ats-framework by Axway.
the class MimePackage method getAttachmentCharset.
/**
* Get the attachment character set
*
* @param partIndex
* the index of the attachment
* @return the character set for this attachment, null if there is no such
* @throws PackageException
*/
@PublicAtsApi
public String getAttachmentCharset(int partIndex) throws PackageException {
// first check if there is part at this position at all
if (partIndex >= attachmentPartIndices.size()) {
throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
}
try {
MimePart part = getPart(attachmentPartIndices.get(partIndex));
// get the content type header
ContentType contentType = new ContentType(part.getContentType());
return contentType.getParameter("charset");
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.internet.ContentType in project ats-framework by Axway.
the class MimePackage method getAttachmentContentType.
/**
* Get the attachment content type
*
* @param partIndex
* @return
* @throws PackageException
*/
@PublicAtsApi
public String getAttachmentContentType(int partIndex) throws PackageException {
// first check if there is part at this position at all
if (partIndex >= attachmentPartIndices.size()) {
throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
}
try {
MimePart part = getPart(attachmentPartIndices.get(partIndex));
// get the content type header
ContentType contentType = new ContentType(part.getContentType());
return contentType.getBaseType();
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.internet.ContentType 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);
}
}
use of javax.mail.internet.ContentType in project nhin-d by DirectProject.
the class SMIMECryptographerImpl_createSignatureEntityTest method validatedSignatureHeaders.
protected void validatedSignatureHeaders(MimeMultipart mm) throws Exception {
/*
* explicit header checking for compliance with Applicability
* Statement v 1.2
*/
final ContentType type = new ContentType(mm.getContentType());
assertTrue(type.match("multipart/signed"));
assertEquals(SMIMEStandard.SignatureContentMediaType, type.getParameter("protocol"));
assertEquals(CryptoAlgorithmsHelper.toDigestAlgorithmMicalg(DigestAlgorithm.SHA256), type.getParameter("micalg"));
final ContentType signedTypetype = new ContentType(mm.getBodyPart(1).getContentType());
assertTrue(signedTypetype.match(SMIMEStandard.SignatureContentMediaType));
}
Aggregations