message.saveChanges(); try { multiPartKeys.writeTo(keysOutputStream); } catch(Exception e) { logger.error("Exception while writing mutipart to output stream", e);
message.saveChanges(); try { multiPart.writeTo(outputStream); } catch(Exception e) { logger.error("Exception while writing multipart to output stream", e);
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()); }
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()); }
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); } } }
public static byte[] createMultipartContent(MultipartHttpEntity multipartEntity, String contentType) throws IOException, MessagingException { MimeMultipart mimeMultipartContent = HttpMultipartEncoder.createContent(multipartEntity, contentType); final ByteArrayOutputStream byteArrayOutputStream; byteArrayOutputStream = new ByteArrayOutputStream(); mimeMultipartContent.writeTo(byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); }
public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException { if (obj instanceof MimeMultipart) { MimeMultipart mp = (MimeMultipart) obj; try { mp.writeTo(os); } catch (MessagingException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } } } }
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
/** * Write the object to the output stream, using the specific MIME type. */ public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException { if (obj instanceof MimeMultipart) { try { ((MimeMultipart)obj).writeTo(os); } catch (MessagingException e) { throw new IOException(e.toString()); } } } }
public static byte[] toByteArray(HttpEntity multipartEntity, String contentType) throws IOException { MimeMultipart mimeMultipartContent = HttpMultipartEncoder.toMimeMultipart(multipartEntity, contentType); final ByteArrayOutputStream byteArrayOutputStream; byteArrayOutputStream = new ByteArrayOutputStream(); try { mimeMultipartContent.writeTo(byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); } catch (MessagingException e) { throw new IOException(e); } }
/** * Write the object to the output stream, using the specific MIME type. */ public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException { if (obj instanceof MimeMultipart) { try { ((MimeMultipart)obj).writeTo(os); } catch (MessagingException e) { throw new IOException(e.toString()); } } } }
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 payload(String atom) { if (hasAttachments()) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); MimeMultipart multipart = multipart(atom); multipart.writeTo(baos); return new String(baos.toByteArray()); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } else { return atom; } }
/** * Method writeTo * * @param object * @param s * @param outputstream * @throws IOException */ public void writeTo(Object object, String s, OutputStream outputstream) throws IOException { // if this object is a MimeMultipart, then delegate to the part. if (object instanceof MimeMultipart) { try { ((MimeMultipart)object).writeTo(outputstream); } catch (MessagingException e) { // we need to transform any exceptions into an IOException. throw new IOException("Exception writing MimeMultipart: " + e.toString()); } } } }
/** * Method writeTo * * @param object * @param s * @param outputstream * @throws IOException */ public void writeTo(Object object, String s, OutputStream outputstream) throws IOException { // if this object is a MimeMultipart, then delegate to the part. if (object instanceof MimeMultipart) { try { ((MimeMultipart)object).writeTo(outputstream); } catch (MessagingException e) { // we need to transform any exceptions into an IOException. throw new IOException("Exception writing MimeMultipart: " + e.toString()); } } } }
public EncodedData getBytesFromMultipart(MimeMultipart value, Marshaller marshaller) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { ContentType contentType = new ContentType(value.getContentType()); String boundary = contentType.getParameter("boundary"); output.write(Constants.cr().getBytes()); output.write(("Content-Type: " + contentType.getBaseType() + "; boundary=\"" + boundary + "\"\n").getBytes()); } catch (Exception ex) { throw ConversionException.couldNotBeConverted(value, byte[].class, ex); } try { value.writeTo(output); } catch (Exception ex) { throw ConversionException.couldNotBeConverted(value, byte[].class, ex); } return new EncodedData(output.toByteArray(), value.getContentType()); }
public EncodedData getBytesFromMultipart(MimeMultipart value, Marshaller marshaller) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { ContentType contentType = new ContentType(value.getContentType()); String boundary = contentType.getParameter("boundary"); output.write(Constants.cr().getBytes()); output.write(("Content-Type: " + contentType.getBaseType() + "; boundary=\"" + boundary + "\"\n").getBytes()); } catch (Exception ex) { throw ConversionException.couldNotBeConverted(value, byte[].class, ex); } try { value.writeTo(output); } catch (Exception ex) { throw ConversionException.couldNotBeConverted(value, byte[].class, ex); } return new EncodedData(output.toByteArray(), value.getContentType()); }
MimeBodyPart formPart = new MimeBodyPart(); formPart.setContent( URLEncoder.encode(requestData.toString(), "UTF-8"), "application/x-www-form-urlencoded"); formPart.setDisposition("form-data; name=\"form\""); MimeBodyPart filePart = new MimeBodyPart(); filePart.attachFile(fileToUpload); ContentDisposition disposition = new ContentDisposition("form-data"); disposition.getParameterList().set("name", "file"); disposition.getParameterList().set("filename", fileToUpload.toString(), "UTF-8"); filePart.setDisposition(disposition.toString()); MimeMultipart multipart = new MimeMultipart("form-data"); multipart.addBodyPart(formPart); multipart.addBodyPart(filePart); try (OutputStream out = con.getOutputStream()) { multipart.writeTo(out); }