List of usage examples for org.apache.commons.httpclient ChunkedOutputStream ChunkedOutputStream
public ChunkedOutputStream(OutputStream paramOutputStream) throws IOException
From source file:edu.ucsb.eucalyptus.cloud.ws.PutMethodWithProgress.java
@Override protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException { InputStream inputStream;//from w w w. ja va 2 s . c om if (outFile != null) { inputStream = new FileInputStream(outFile); ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream()); byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE]; int bytesRead; int numberProcessed = 0; long totalBytesProcessed = 0; while ((bytesRead = inputStream.read(buffer)) > 0) { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream zip = new GZIPOutputStream(out); zip.write(buffer, 0, bytesRead); zip.close(); chunkedOut.write(out.toByteArray()); totalBytesProcessed += bytesRead; if (++numberProcessed >= callback.getUpdateThreshold()) { callback.run(); numberProcessed = 0; } } if (totalBytesProcessed > 0) { callback.finish(); } else { callback.failed(); } chunkedOut.finish(); inputStream.close(); } else { return false; } return true; }
From source file:com.eucalyptus.blockstorage.PutMethodWithProgress.java
@Override protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException { InputStream inputStream;//from ww w . ja v a 2 s .com if (outFile != null) { inputStream = new FileInputStream(outFile); ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream()); byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE]; int bytesRead; long totalBytesProcessed = 0; while ((bytesRead = inputStream.read(buffer)) > 0) { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream zip = new GZIPOutputStream(out); zip.write(buffer, 0, bytesRead); zip.close(); chunkedOut.write(out.toByteArray()); totalBytesProcessed += bytesRead; callback.update(totalBytesProcessed); } if (totalBytesProcessed > 0) { callback.finish(); } else { callback.failed(); } chunkedOut.finish(); inputStream.close(); } else { return false; } return true; }
From source file:org.apache.webdav.lib.methods.HttpRequestBodyMethodBase.java
/** * Do write the request body.//from w ww . j a v a 2 s .co m * Override the method of {@link HttpMethodBase} * if the method should wait until a <tt>100 Continue</tt> status code * is expected (@link readContinueCode) * * @since 2.0 */ protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException, HttpException { OutputStream out = conn.getRequestOutputStream(); if (isHttp11() && (null == getRequestHeader("Content-Length"))) { out = new ChunkedOutputStream(out); } InputStream inputStream = null; if (file != null && file.exists()) { inputStream = new FileInputStream(file); } else if (url != null) { inputStream = url.openConnection().getInputStream(); } else if (data != null) { inputStream = new ByteArrayInputStream(data); } else { return true; } byte[] buffer = new byte[4096]; int nb = 0; while (true) { nb = inputStream.read(buffer); if (nb == -1) { break; } out.write(buffer, 0, nb); } out.flush(); return true; }
From source file:org.apache.wink.itest.contentencoding.ContentEncodingTest.java
/** * Tests sending in small bits of gzip encoded content. * /* w w w. jav a 2 s.co m*/ * @throws HttpException * @throws IOException */ public void testSendSmallGzipContentEncoded() throws HttpException, IOException { PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook"); postMethod.addRequestHeader("Accept", "text/plain"); postMethod.setRequestHeader("Content-Encoding", "gzip"); postMethod.setRequestHeader("Transfer-Encoding", "chunked"); // postMethod.setRequestHeader("Content-) ByteArrayOutputStream originalContent = new ByteArrayOutputStream(); originalContent.write("Hello world".getBytes("UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos); GZIPOutputStream gzipOut = new GZIPOutputStream(chunkedOut); originalContent.writeTo(gzipOut); gzipOut.finish(); chunkedOut.finish(); byte[] content = baos.toByteArray(); postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8")); try { int result = client.executeMethod(postMethod); assertEquals(200, result); String response = postMethod.getResponseBodyAsString(); assertEquals("Hello world" + "helloworld", response); } finally { postMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.contentencoding.WinkContentEncodingTest.java
/** * Tests sending in small bits of gzip encoded content. * /*from w w w.j av a2s .c o m*/ * @throws HttpException * @throws IOException */ public void testSendSmallGzipContentEncoded() throws HttpException, IOException { ByteArrayOutputStream originalContent = new ByteArrayOutputStream(); originalContent.write("Hello world".getBytes("UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos); GZIPOutputStream gzipOut = new GZIPOutputStream(chunkedOut); originalContent.writeTo(gzipOut); gzipOut.finish(); chunkedOut.finish(); byte[] content = baos.toByteArray(); ClientResponse response = client.resource(BASE_URI + "/bigbook").accept(MediaType.TEXT_PLAIN) .header("Transfer-Encoding", "chunked").header("Content-Encoding", "gzip") .contentType("text/plain; charset=utf-8").post(content); assertEquals(200, response.getStatusCode()); String responseBody = response.getEntity(String.class); assertEquals("Hello world" + "helloworld", responseBody); }
From source file:org.mule.transport.http.HttpServerConnection.java
public void writeRequest(final HttpRequest request) throws IOException { if (request == null) { return;//from w ww. ja v a2 s .c om } ResponseWriter writer = new ResponseWriter(this.out, encoding); writer.println(request.getRequestLine().toString()); Iterator item = request.getHeaderIterator(); while (item.hasNext()) { Header header = (Header) item.next(); writer.print(header.toExternalForm()); } writer.println(); writer.flush(); OutputStream outstream = this.out; InputStream content = request.getBody(); if (content != null) { Header transferenc = request.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); if (transferenc != null) { request.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH); if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) { outstream = new ChunkedOutputStream(outstream); } } IOUtils.copy(content, outstream); if (outstream instanceof ChunkedOutputStream) { ((ChunkedOutputStream) outstream).finish(); } } outstream.flush(); }
From source file:org.mule.transport.http.HttpServerConnection.java
/** * Write an HttpResponse and add the map entries as headers * * @param response http response with the content of the response * @param headers headers to add to the http response besides the one already contained in the HttpResponse object * @throws IOException//from w ww.j av a 2 s . c om */ public void writeResponse(final HttpResponse response, Map<String, String> headers) throws IOException { if (response == null) { return; } if (!response.isKeepAlive()) { Header header = new Header(HttpConstants.HEADER_CONNECTION, "close"); response.setHeader(header); } setKeepAlive(response.isKeepAlive()); addHeadersToHttpResponse(response, headers); ResponseWriter writer = new ResponseWriter(this.out, encoding); OutputStream outstream = this.out; writer.println(response.getStatusLine()); Iterator item = response.getHeaderIterator(); while (item.hasNext()) { Header header = (Header) item.next(); writer.print(header.toExternalForm()); } writer.println(); writer.flush(); OutputHandler content = response.getBody(); if (content != null) { Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); if (transferenc != null) { response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH); if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) { outstream = new ChunkedOutputStream(outstream); } } content.write(RequestContext.getEvent(), outstream); if (outstream instanceof ChunkedOutputStream) { ((ChunkedOutputStream) outstream).finish(); } } outstream.flush(); }
From source file:org.mule.transport.http.transformers.HttpResponseToString.java
/** * Perform the transformation to always return a String object *//*w ww . j a va2s . c om*/ @Override protected Object doTransform(Object src, String encoding) throws TransformerException { try { HttpResponse response = (HttpResponse) src; ByteArrayOutputStream bos = new ByteArrayOutputStream(8192); OutputStream outstream = bos; ResponseWriter writer = new ResponseWriter(outstream, encoding); writer.println(response.getStatusLine()); Iterator item = response.getHeaderIterator(); while (item.hasNext()) { Header header = (Header) item.next(); writer.print(header.toExternalForm()); } writer.println(); writer.flush(); if (response.hasBody()) { OutputHandler handler = response.getBody(); Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); if (transferenc != null) { response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH); if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) { outstream = new ChunkedOutputStream(outstream); } } handler.write(RequestContext.getEvent(), outstream); if (outstream instanceof ChunkedOutputStream) { ((ChunkedOutputStream) outstream).finish(); } } outstream.flush(); bos.flush(); byte[] result = bos.toByteArray(); outstream.close(); writer.close(); bos.close(); String output = null; try { output = new String(result, encoding); } catch (UnsupportedEncodingException uee) { // I believe this is never reached since a TransformerExcpetion // is thrown before at new ResponseWriter(outstream, encoding) if // encoding is not supported output = new String(result); } return output; } catch (IOException e) { throw new TransformerException(this, e); } }