public void testMultiPartFormUploadWithBodyField() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("post"); String xml = "<Hello service='hello' message='Hello world!' version='1.0.0' />"; MimeMultipart body = new MimeMultipart(); request.setContentType(body.getContentType()); InternetHeaders headers = new InternetHeaders(); headers.setHeader("Content-Disposition", "form-data; name=\"body\";"); headers.setHeader("Content-Type", "application/xml"); body.addBodyPart(new MimeBodyPart(headers, xml.getBytes())); ByteArrayOutputStream bout = new ByteArrayOutputStream(); body.writeTo(bout); request.setContent(bout.toByteArray()); MockHttpServletResponse response = new MockHttpServletResponse(); URL url = getClass().getResource("applicationContext.xml"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString()); Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher"); dispatcher.handleRequestInternal(request, response); assertEquals("Hello world!", response.getContentAsString()); }
public void testMultiPartFormUpload() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/geoserver"); request.setRequestURI("/geoserver/hello"); request.setMethod("post"); String xml = "<Hello service='hello' message='Hello world!' version='1.0.0' />"; MimeMultipart body = new MimeMultipart(); request.setContentType(body.getContentType()); InternetHeaders headers = new InternetHeaders(); headers.setHeader( "Content-Disposition", "form-data; name=\"upload\"; filename=\"request.xml\""); headers.setHeader("Content-Type", "application/xml"); body.addBodyPart(new MimeBodyPart(headers, xml.getBytes())); ByteArrayOutputStream bout = new ByteArrayOutputStream(); body.writeTo(bout); request.setContent(bout.toByteArray()); MockHttpServletResponse response = new MockHttpServletResponse(); URL url = getClass().getResource("applicationContext.xml"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString()); Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher"); dispatcher.handleRequestInternal(request, response); assertEquals("Hello world!", response.getContentAsString()); }
@Test void testTextHtml() throws MessagingException, IOException { final Email email = Email.create() .from(FROM_EXAMPLE_COM) .to(TO_EXAMPLE_COM) .subject(SUB) .textMessage(HELLO) .htmlMessage("<html><body><h1>Hey!</h1></body></html>"); final Message message = createMessage(email); assertEquals(1, message.getFrom().length); assertEquals(FROM_EXAMPLE_COM, message.getFrom()[0].toString()); assertEquals(1, message.getRecipients(RecipientType.TO).length); assertEquals(TO_EXAMPLE_COM, message.getRecipients(RecipientType.TO)[0].toString()); assertEquals(SUB, message.getSubject()); // wrapper final MimeMultipart multipart = (MimeMultipart) message.getContent(); assertEquals(1, multipart.getCount()); assertTrue(multipart.getContentType().contains("multipart/mixed")); // inner content final MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0); final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent(); assertEquals(2, mimeMultipart.getCount()); assertTrue(mimeMultipart.getContentType().contains("multipart/alternative")); MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0); assertEquals(HELLO, bodyPart.getContent()); assertTrue(bodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_PLAIN)); bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1); assertEquals("<html><body><h1>Hey!</h1></body></html>", bodyPart.getContent()); assertTrue(bodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_HTML)); }
assertTrue(htmlMessage.getContentType().contains("multipart/related")); assertEquals(2, htmlMessage.getCount());
httpHeaders.putSingle("Content-Type", MediaType.valueOf(mimeMultipart.getContentType())); mimeMultipart.writeTo(entityStream);
@Override public void writeTo(SignedOutput out, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { try { SMIMESignedGenerator gen = new SMIMESignedGenerator(); SignerInfoGenerator signer = new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC").build("SHA1WITHRSA", out.getPrivateKey(), out.getCertificate()); gen.addSignerInfoGenerator(signer); MimeMultipart mp = gen.generate(EnvelopedWriter.createBodyPart(providers, out)); String contentType = mp.getContentType(); contentType = contentType.replace("\r\n", "").replace("\t", " "); headers.putSingle("Content-Type", contentType); mp.writeTo(os); } catch (Exception e) { throw new WriterException(e); } } }
/** * Returns the new content type of this encoder. This value must be specified * in the mime Content-type header in what ever way that the transport * requires it. * * @return the content type */ public String getContentType() { String contentType = multipart.getContentType(); return contentType; }
@Override public String getMimeType(Object value, Operation operation) throws ServiceException { // javamail outputs multipart/mixed, but in our case we're producing multipart/related return multipart.getContentType().replace("mixed", "related"); }
@Override public String getMimeType(Object value, Operation operation) throws ServiceException { // javamail outputs multipart/mixed, but in our case we're producing multipart/related return multipart .getContentType() .replace("mixed", "related") .replace("\n", "") .replace("\r", ""); }
public MimeMultipartDataSource(String name, MimeMultipart data) { this.name = name; this.contentType = data == null ? CONTENT_TYPE : data.getContentType(); os = new ByteArrayOutputStream(); try { if (data != null) { data.writeTo(os); } } catch (Exception e) { // Is this sufficient? } } // ctor
public static MimeBodyPart createMimeBodyPart(MimeMultipart multipart) throws MessagingException { MimeBodyPart part = new MimeBodyPart(); part.setContent(multipart); part.setHeader("Content-Type", multipart.getContentType()); return part; }
/** * Resolve the mime type for an object. * Default to <code>application/octet-stream</code> * if no mapping could be found. */ public static String resolveMimeType(Object obj) { String mimeType = (obj instanceof MimeMultipart) ? ((MimeMultipart)obj).getContentType() : resolveMimeType(obj.getClass()); return mimeType; }
public String stringFromMultipart(MimeMultipart multipart, QName schemaTypeQName, CoreAbstractSession session) { DataHandler handler = new DataHandler(multipart, multipart.getContentType()); return stringFromDataHandler(handler, schemaTypeQName, session); }
public String stringFromMultipart(MimeMultipart multipart, QName schemaTypeQName, AbstractSession session) { DataHandler handler = new DataHandler(multipart, multipart.getContentType()); return stringFromDataHandler(handler, schemaTypeQName, session); }
public MimeMultipartDataSource(String name, MimeMultipart data) { this.name = name; this.contentType = data == null ? CONTENT_TYPE : data.getContentType(); os = new ByteArrayOutputStream(); try { if (data != null) { data.writeTo(os); } } catch (Exception e) { // Is this sufficient? } } // ctor
public MimeMultipartDataSource(String name, MimeMultipart data) { this.name = name; this.contentType = data == null ? CONTENT_TYPE : data.getContentType(); os = new ByteArrayOutputStream(); try { if (data != null) { data.writeTo(os); } } catch (Exception e) { // Is this sufficient? } } // ctor
public String getContentType() { return getMimeMultipart().getContentType(); }
SMIMESignedGenerator gen = new SMIMESignedGenerator(); SignerInfoGenerator sigGen = new JcaSimpleSignerInfoGeneratorBuilder() .setProvider(BC) .build("SHA1withRSA", senderKey, senderCert); gen.addSignerInfoGenerator(sigGen); MimeMultipart smime = gen.generate(part); MimeBodyPart tmpBody = new MimeBodyPart(); tmpBody.setContent(smime); tmpBody.setHeader("Content-Type", smime.getContentType()); return tmpBody;
@Test public void createMultipartRelatedContentWithoutStartParameter() throws Exception { MimeMultipart mimeMultipart = toMimeMultipart(httpEntity, "multipart/related; boundary=\"MIMEBoundary\"; type=\"text/plain\";"); verifyBodyPart(mimeMultipart.getBodyPart(0), FIRST_PART_CONTENT, "firstPart"); verifyBodyPart(mimeMultipart.getBodyPart(1), SECOND_PART_CONTENT, "secondPart"); verifyBodyPart(mimeMultipart.getBodyPart(2), THIRD_PART_CONTENT, "thirdPart"); assertThat(mimeMultipart.getContentType(), containsString("type=\"text/plain\"")); }
@Test public void createMultipartRelatedContentWithStartParameter() throws Exception { MimeMultipart mimeMultipart = toMimeMultipart(httpEntity, "multipart/related; boundary= \"MIMEBoundary\"; type=\"text/plain\"; start=thirdPart"); verifyBodyPart(mimeMultipart.getBodyPart(0), THIRD_PART_CONTENT, "thirdPart"); verifyBodyPart(mimeMultipart.getBodyPart(1), FIRST_PART_CONTENT, "firstPart"); verifyBodyPart(mimeMultipart.getBodyPart(2), SECOND_PART_CONTENT, "secondPart"); assertThat(mimeMultipart.getContentType(), containsString("type=\"text/plain\"")); }