List of usage examples for org.apache.http.entity BasicHttpEntity setChunked
public void setChunked(boolean z)
From source file:org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit.java
@Override protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException { if (factory.isShutdown()) { message.put(USE_ASYNC, Boolean.FALSE); super.setupConnection(message, address, csPolicy); return;/* w ww . j a v a 2 s .co m*/ } boolean addressChanged = false; // need to do some clean up work on the URI address URI uri = address.getURI(); String uriString = uri.toString(); if (uriString.startsWith("hc://")) { try { uriString = uriString.substring(5); uri = new URI(uriString); addressChanged = true; } catch (URISyntaxException ex) { throw new MalformedURLException("unsupport uri: " + uriString); } } String s = uri.getScheme(); if (!"http".equals(s) && !"https".equals(s)) { throw new MalformedURLException("unknown protocol: " + s); } Object o = message.getContextualProperty(USE_ASYNC); if (o == null) { o = factory.getUseAsyncPolicy(); } switch (UseAsyncPolicy.getPolicy(o)) { case ALWAYS: o = true; break; case NEVER: o = false; break; case ASYNC_ONLY: default: o = !message.getExchange().isSynchronous(); break; } // check tlsClientParameters from message header TLSClientParameters clientParameters = message.get(TLSClientParameters.class); if (clientParameters == null) { clientParameters = tlsClientParameters; } if (uri.getScheme().equals("https") && clientParameters != null && clientParameters.getSSLSocketFactory() != null) { //if they configured in an SSLSocketFactory, we cannot do anything //with it as the NIO based transport cannot use socket created from //the SSLSocketFactory. o = false; } if (!MessageUtils.isTrue(o)) { message.put(USE_ASYNC, Boolean.FALSE); super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy); return; } if (StringUtils.isEmpty(uri.getPath())) { //hc needs to have the path be "/" uri = uri.resolve("/"); addressChanged = true; } message.put(USE_ASYNC, Boolean.TRUE); if (LOG.isLoggable(Level.FINE)) { LOG.fine("Asynchronous connection to " + uri.toString() + " has been set up"); } message.put("http.scheme", uri.getScheme()); String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD); if (httpRequestMethod == null) { httpRequestMethod = "POST"; message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod); } final CXFHttpRequest e = new CXFHttpRequest(httpRequestMethod); BasicHttpEntity entity = new BasicHttpEntity() { public boolean isRepeatable() { return e.getOutputStream().retransmitable(); } }; entity.setChunked(true); entity.setContentType((String) message.get(Message.CONTENT_TYPE)); e.setURI(uri); e.setEntity(entity); RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout((int) csPolicy.getConnectionTimeout()) .setSocketTimeout((int) csPolicy.getReceiveTimeout()) .setConnectionRequestTimeout((int) csPolicy.getReceiveTimeout()); Proxy p = proxyFactory.createProxy(csPolicy, uri); if (p != null && p.type() != Proxy.Type.DIRECT) { InetSocketAddress isa = (InetSocketAddress) p.address(); HttpHost proxy = new HttpHost(isa.getHostName(), isa.getPort()); b.setProxy(proxy); } e.setConfig(b.build()); message.put(CXFHttpRequest.class, e); }
From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java
@Test public void testIsChunked() throws Exception { String input = "0123456789"; BasicHttpEntity basic; CappedBufferHttpEntity replay;/*from w w w . ja va2 s . c om*/ basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); replay = new CappedBufferHttpEntity(basic, 5); assertThat(replay.isChunked(), is(false)); basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); basic.setChunked(true); replay = new CappedBufferHttpEntity(basic, 5); assertThat(replay.isChunked(), is(true)); }
From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java
@Test public void testIsChunked() throws Exception { String input = "0123456789"; BasicHttpEntity basic; PartiallyRepeatableHttpEntity replay; basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); replay = new PartiallyRepeatableHttpEntity(basic, 5); assertThat(replay.isChunked(), is(false)); basic = new BasicHttpEntity(); basic.setContent(new ByteArrayInputStream(input.getBytes(UTF8))); basic.setChunked(true); replay = new PartiallyRepeatableHttpEntity(basic, 5); assertThat(replay.isChunked(), is(true)); }
From source file:org.apache.synapse.transport.nhttp.ServerHandler.java
/** * Commit the response to the connection. Processes the response through the configured * HttpProcessor and submits it to be sent out. Re-Throws exceptions, after closing connections * @param conn the connection being processed * @param response the response to commit over the connection * @throws IOException if an IO error occurs while sending the response * @throws HttpException if a HTTP protocol violation occurs while sending the response *//* ww w.ja v a 2s . c o m*/ public void commitResponse(final NHttpServerConnection conn, final HttpResponse response) throws IOException, HttpException { try { BasicHttpEntity entity = (BasicHttpEntity) response.getEntity(); Header[] headers = response.getAllHeaders(); int contentLength = -1; if (canResponseHaveBody(response, conn)) { if (entity == null) { entity = new BasicHttpEntity(); } for (Header header : headers) { if (header.getName().equals(HTTP.CONTENT_LEN) && Integer.parseInt(header.getValue()) > 0) { contentLength = Integer.parseInt(header.getValue()); response.removeHeader(header); } } if (contentLength != -1) { entity.setChunked(false); entity.setContentLength(contentLength); } else { entity.setChunked(true); } } else { if (entity != null) { entity.setChunked(false); entity.setContentLength(contentLength); } } response.setEntity(entity); conn.suspendInput(); HttpContext context = conn.getContext(); httpProcessor.process(response, context); conn.getContext().setAttribute(NhttpConstants.RES_TO_CLIENT_WRITE_START_TIME, System.currentTimeMillis()); conn.submitResponse(response); } catch (HttpException e) { shutdownConnection(conn, true, e.getMessage()); throw e; } catch (IOException e) { shutdownConnection(conn, true, e.getMessage()); throw e; } }
From source file:org.apache.synapse.transport.nhttp.ClientHandler.java
/** * Perform processing of the received response though Axis2 * * @param conn HTTP connection to be processed * @param context HTTP context associated with the connection * @param response HTTP response associated with the connection *///from ww w . j a v a 2 s . c om private void processResponse(final NHttpClientConnection conn, HttpContext context, HttpResponse response) { ContentInputBuffer inputBuffer = null; MessageContext outMsgContext = (MessageContext) context.getAttribute(OUTGOING_MESSAGE_CONTEXT); String endptPrefix = (String) context.getAttribute(NhttpConstants.ENDPOINT_PREFIX); String requestMethod = (String) context.getAttribute(NhttpConstants.HTTP_REQ_METHOD); int statusCode = response.getStatusLine().getStatusCode(); boolean expectEntityBody = false; if (!"HEAD".equals(requestMethod) && !"OPTIONS".equals(requestMethod) && statusCode >= HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT && statusCode != HttpStatus.SC_NOT_MODIFIED && statusCode != HttpStatus.SC_RESET_CONTENT) { expectEntityBody = true; } else if (NhttpConstants.HTTP_HEAD.equals(requestMethod)) { // When invoking http HEAD request esb set content length as 0 to response header. Since there is no message // body content length cannot be calculated inside synapse. Hence additional two headers are added to // which contains content length of the backend response and the request method. These headers are removed // before submitting the actual response. response.addHeader(NhttpConstants.HTTP_REQUEST_METHOD, requestMethod); if (response.getFirstHeader(HTTP.CONTENT_LEN) != null) { response.addHeader(NhttpConstants.ORIGINAL_CONTENT_LEN, response.getFirstHeader(HTTP.CONTENT_LEN).getValue()); } } if (expectEntityBody) { inputBuffer = new SharedInputBuffer(cfg.getBufferSize(), conn, allocator); context.setAttribute(RESPONSE_SINK_BUFFER, inputBuffer); BasicHttpEntity entity = new BasicHttpEntity(); if (response.getStatusLine().getProtocolVersion().greaterEquals(HttpVersion.HTTP_1_1)) { entity.setChunked(true); } response.setEntity(entity); context.setAttribute(ExecutionContext.HTTP_RESPONSE, response); } else { conn.resetInput(); conn.resetOutput(); if (context.getAttribute(NhttpConstants.DISCARD_ON_COMPLETE) != null || !connStrategy.keepAlive(response, context)) { try { // this is a connection we should not re-use connpool.forget(conn); shutdownConnection(conn, false, null); context.removeAttribute(RESPONSE_SINK_BUFFER); context.removeAttribute(REQUEST_SOURCE_BUFFER); } catch (Exception ignore) { } } else { connpool.release(conn); } } workerPool .execute(new ClientWorker(cfgCtx, inputBuffer == null ? null : new ContentInputStream(inputBuffer), response, outMsgContext, endptPrefix)); }