List of usage examples for org.apache.commons.httpclient.methods.multipart Part sendParts
public static void sendParts(OutputStream paramOutputStream, Part[] paramArrayOfPart, byte[] paramArrayOfByte) throws IOException
From source file:ccc.api.jaxrs.providers.FileProvider.java
/** {@inheritDoc} */ @Override/*from w w w . ja v a2 s . com*/ public void writeTo(final File t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException { try { final String providedCharset = mediaType.getParameters().get("charset"); final String charset = (null == providedCharset) ? "utf-8" : providedCharset; final String boundary = UUID.randomUUID().toString(); final List<Part> parts = new ArrayList<Part>(); addPart(parts, TITLE, charset, t.getTitle()); addPart(parts, DESCRIPTION, charset, t.getDescription()); addPart(parts, COMMENT, charset, t.getComment()); addPart(parts, FILE_NAME, charset, t.getName().toString()); addPart(parts, PATH, charset, t.getParent().toString()); addPart(parts, MAJOR_EDIT, charset, t.isMajorEdit()); addPart(parts, LAST_UPDATE, charset, t.getDateChanged()); final FilePart fp = new UnicodeFilePart(FILE, new SimplePart(t), charset); fp.setContentType(t.getMimeType().toString()); fp.setCharSet(t.getCharset()); parts.add(fp); httpHeaders.add("Content-Type", mediaType.toString() + ";boundary=" + boundary); Part.sendParts(entityStream, parts.toArray(new Part[parts.size()]), boundary.getBytes("ascii")); } finally { try { entityStream.close(); } catch (final IOException e) { LOG.warn("Error closing output stream.", e); } } }
From source file:at.gv.egiz.bku.binding.DataUrlConnectionImpl.java
@Override public void transmit(SLResult slResult) throws IOException { log.trace("Sending data."); if (urlEncoded) { ////from w w w . ja va2s . c om // application/x-www-form-urlencoded (legacy, SL < 1.2) // OutputStream os = connection.getOutputStream(); OutputStreamWriter streamWriter = new OutputStreamWriter(os, HttpUtil.DEFAULT_CHARSET); // ResponseType streamWriter.write(FORMPARAM_RESPONSETYPE); streamWriter.write("="); streamWriter.write(URLEncoder.encode(DEFAULT_RESPONSETYPE, "UTF-8")); streamWriter.write("&"); // XMLResponse / Binary Response if (slResult.getResultType() == SLResultType.XML) { streamWriter.write(DataUrlConnection.FORMPARAM_XMLRESPONSE); } else { streamWriter.write(DataUrlConnection.FORMPARAM_BINARYRESPONSE); } streamWriter.write("="); streamWriter.flush(); URLEncodingWriter urlEnc = new URLEncodingWriter(streamWriter); slResult.writeTo(new StreamResult(urlEnc), false); urlEnc.flush(); // transfer parameters char[] cbuf = new char[512]; int len; for (HTTPFormParameter formParameter : httpFormParameter) { streamWriter.write("&"); streamWriter.write(URLEncoder.encode(formParameter.getName(), "UTF-8")); streamWriter.write("="); InputStreamReader reader = new InputStreamReader(formParameter.getData(), (formParameter.getCharSet() != null) ? formParameter.getCharSet() : "UTF-8"); // assume request was // application/x-www-form-urlencoded, // formParam therefore UTF-8 while ((len = reader.read(cbuf)) != -1) { urlEnc.write(cbuf, 0, len); } urlEnc.flush(); } streamWriter.close(); } else { // // multipart/form-data (conforming to SL 1.2) // ArrayList<Part> parts = new ArrayList<Part>(); // ResponseType StringPart responseType = new StringPart(FORMPARAM_RESPONSETYPE, DEFAULT_RESPONSETYPE, "UTF-8"); responseType.setTransferEncoding(null); parts.add(responseType); // XMLResponse / Binary Response SLResultPart slResultPart = new SLResultPart(slResult, XML_RESPONSE_ENCODING); if (slResult.getResultType() == SLResultType.XML) { slResultPart.setTransferEncoding(null); slResultPart.setContentType(slResult.getMimeType()); slResultPart.setCharSet(XML_RESPONSE_ENCODING); } else { slResultPart.setTransferEncoding(null); slResultPart.setContentType(slResult.getMimeType()); } parts.add(slResultPart); // transfer parameters for (HTTPFormParameter formParameter : httpFormParameter) { InputStreamPartSource source = new InputStreamPartSource(null, formParameter.getData()); FilePart part = new FilePart(formParameter.getName(), source, formParameter.getContentType(), formParameter.getCharSet()); part.setTransferEncoding(formParameter.getTransferEncoding()); parts.add(part); } OutputStream os = connection.getOutputStream(); Part.sendParts(os, parts.toArray(new Part[parts.size()]), boundary.getBytes()); os.close(); } // MultipartRequestEntity PostMethod InputStream is = null; try { is = connection.getInputStream(); } catch (IOException iox) { log.info("Failed to get InputStream of HTTPUrlConnection.", iox); } log.trace("Reading response."); response = new DataUrlResponse(url.toString(), connection.getResponseCode(), is); Map<String, String> responseHttpHeaders = new HashMap<String, String>(); Map<String, List<String>> httpHeaders = connection.getHeaderFields(); for (Iterator<String> keyIt = httpHeaders.keySet().iterator(); keyIt.hasNext();) { String key = keyIt.next(); StringBuffer value = new StringBuffer(); for (String val : httpHeaders.get(key)) { value.append(val); value.append(HttpUtil.SEPARATOR[0]); } String valString = value.substring(0, value.length() - 1); if ((key != null) && (value.length() > 0)) { responseHttpHeaders.put(key, valString); } } response.setResponseHttpHeaders(responseHttpHeaders); }
From source file:org.apache.axis2.transport.http.MultipartFormDataFormatter.java
/** * @return a byte array of the message formatted according to the given * message format.//from w w w. j av a 2s .c o m */ public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault { OMElement omElement = messageContext.getEnvelope().getBody().getFirstElement(); Part[] parts = createMultipatFormDataRequest(omElement); if (parts.length > 0) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); try { // This is accessing a class of Commons-FlieUpload Part.sendParts(bytesOut, parts, format.getMimeBoundary().getBytes()); } catch (IOException e) { throw AxisFault.makeFault(e); } return bytesOut.toByteArray(); } return new byte[0]; //To change body of implemented methods use File | Settings | File Templates. }
From source file:org.apache.commons.httpclient.methods.multipart.MultipartEntity.java
public void writeTo(OutputStream out) throws IOException { Part.sendParts(out, parts, getMultipartBoundary()); }
From source file:org.apache.commons.httpclient.methods.multipart.MultipartEntity.java
public InputStream getContent() throws IOException, IllegalStateException { if (!isRepeatable() && this.contentConsumed) { throw new IllegalStateException("Content has been consumed"); }/* w w w . j a v a2s .c o m*/ this.contentConsumed = true; ByteArrayOutputStream baos = new ByteArrayOutputStream(); Part.sendParts(baos, this.parts, this.multipartBoundary); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); return bais; }