List of usage examples for org.apache.http.nio NHttpClientConnection submitRequest
void submitRequest(HttpRequest request) throws IOException, HttpException;
From source file:org.apache.synapse.transport.passthru.TargetHandler.java
public void requestReady(NHttpClientConnection conn) { HttpContext context = conn.getContext(); ProtocolState connState = null;//from ww w. j a va 2 s .c o m try { connState = TargetContext.getState(conn); if (connState == ProtocolState.REQUEST_DONE || connState == ProtocolState.RESPONSE_BODY) { return; } if (connState != ProtocolState.REQUEST_READY) { handleInvalidState(conn, "Request not started"); return; } ProxyTunnelHandler tunnelHandler = (ProxyTunnelHandler) context .getAttribute(PassThroughConstants.TUNNEL_HANDLER); if (tunnelHandler != null && !tunnelHandler.isCompleted()) { if (!tunnelHandler.isRequested()) { HttpRequest request = tunnelHandler.generateRequest(context); if (targetConfiguration.getProxyAuthenticator() != null) { targetConfiguration.getProxyAuthenticator().authenticatePreemptively(request, context); } if (log.isDebugEnabled()) { log.debug(conn + ": Sending CONNECT request to " + tunnelHandler.getProxy()); } conn.submitRequest(request); tunnelHandler.setRequested(); } return; } TargetRequest request = TargetContext.getRequest(conn); if (request != null) { request.start(conn); targetConfiguration.getMetrics().incrementMessagesSent(); } context.setAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_START_TIME, System.currentTimeMillis()); context.setAttribute(PassThroughConstants.REQ_DEPARTURE_TIME, System.currentTimeMillis()); } catch (IOException e) { logIOException(conn, e); TargetContext.updateState(conn, ProtocolState.CLOSED); targetConfiguration.getConnections().shutdownConnection(conn, true); MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx(); if (requestMsgCtx != null) { targetErrorHandler.handleError(requestMsgCtx, ErrorCodes.SND_IO_ERROR, "Error in Sender", null, connState); } } catch (HttpException e) { log.error(e.getMessage(), e); TargetContext.updateState(conn, ProtocolState.CLOSED); targetConfiguration.getConnections().shutdownConnection(conn, true); MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx(); if (requestMsgCtx != null) { targetErrorHandler.handleError(requestMsgCtx, ErrorCodes.SND_HTTP_ERROR, "Error in Sender", null, connState); } } }
From source file:org.apache.synapse.transport.passthru.TargetRequest.java
public void start(NHttpClientConnection conn) throws IOException, HttpException { if (pipe != null) { TargetContext.get(conn).setWriter(pipe); }// w w w.ja v a 2 s. co m String path = fullUrl || (route.getProxyHost() != null && !route.isTunnelled()) ? url.toString() : url.getPath() + (url.getQuery() != null ? "?" + url.getQuery() : ""); long contentLength = -1; String contentLengthHeader = null; if (headers.get(HTTP.CONTENT_LEN) != null && headers.get(HTTP.CONTENT_LEN).size() > 0) { contentLengthHeader = headers.get(HTTP.CONTENT_LEN).first(); } if (contentLengthHeader != null) { contentLength = Integer.parseInt(contentLengthHeader); headers.remove(HTTP.CONTENT_LEN); } MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx(); if (requestMsgCtx.getProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH) != null) { contentLength = (Long) requestMsgCtx.getProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH); } //fix for POST_TO_URI if (requestMsgCtx.isPropertyTrue(NhttpConstants.POST_TO_URI)) { path = url.toString(); } //fix GET request empty body if ((("GET").equals(requestMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD))) || (("DELETE").equals(requestMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD)))) { hasEntityBody = false; MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(requestMsgCtx); OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(requestMsgCtx); if (formatter != null && format != null) { URL _url = formatter.getTargetAddress(requestMsgCtx, format, url); if (_url != null && !_url.toString().isEmpty()) { if (requestMsgCtx.getProperty(NhttpConstants.POST_TO_URI) != null && Boolean.TRUE.toString() .equals(requestMsgCtx.getProperty(NhttpConstants.POST_TO_URI))) { path = _url.toString(); } else { path = _url.getPath() + ((_url.getQuery() != null && !_url.getQuery().isEmpty()) ? ("?" + _url.getQuery()) : ""); } } headers.remove(HTTP.CONTENT_TYPE); } } Object o = requestMsgCtx.getProperty(MessageContext.TRANSPORT_HEADERS); if (o != null && o instanceof TreeMap) { Map _headers = (Map) o; String trpContentType = (String) _headers.get(HTTP.CONTENT_TYPE); if (trpContentType != null && !trpContentType.equals("")) { if (!trpContentType.contains(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) { addHeader(HTTP.CONTENT_TYPE, trpContentType); } } } if (hasEntityBody) { request = new BasicHttpEntityEnclosingRequest(method, path, version != null ? version : HttpVersion.HTTP_1_1); BasicHttpEntity entity = new BasicHttpEntity(); boolean forceContentLength = requestMsgCtx.isPropertyTrue(NhttpConstants.FORCE_HTTP_CONTENT_LENGTH); boolean forceContentLengthCopy = requestMsgCtx .isPropertyTrue(PassThroughConstants.COPY_CONTENT_LENGTH_FROM_INCOMING); if (forceContentLength) { entity.setChunked(false); if (forceContentLengthCopy && contentLength > 0) { entity.setContentLength(contentLength); } } else { if (contentLength != -1) { entity.setChunked(false); entity.setContentLength(contentLength); } else { entity.setChunked(chunk); } } ((BasicHttpEntityEnclosingRequest) request).setEntity(entity); } else { request = new BasicHttpRequest(method, path, version != null ? version : HttpVersion.HTTP_1_1); } Set<Map.Entry<String, TreeSet<String>>> entries = headers.entrySet(); for (Map.Entry<String, TreeSet<String>> entry : entries) { if (entry.getKey() != null) { Iterator<String> i = entry.getValue().iterator(); while (i.hasNext()) { request.addHeader(entry.getKey(), i.next()); } } } //setup wsa action.. if (request != null) { String soapAction = requestMsgCtx.getSoapAction(); if (soapAction == null) { soapAction = requestMsgCtx.getWSAAction(); } if (soapAction == null) { requestMsgCtx.getAxisOperation().getInputAction(); } if (requestMsgCtx.isSOAP11() && soapAction != null && soapAction.length() > 0) { Header existingHeader = request.getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION); if (existingHeader != null) { request.removeHeader(existingHeader); } MessageFormatter messageFormatter = MessageFormatterDecoratorFactory .createMessageFormatterDecorator(requestMsgCtx); request.setHeader(HTTPConstants.HEADER_SOAP_ACTION, messageFormatter.formatSOAPAction(requestMsgCtx, null, soapAction)); //request.setHeader(HTTPConstants.USER_AGENT,"Synapse-PT-HttpComponents-NIO"); } } request.setParams(new DefaultedHttpParams(request.getParams(), targetConfiguration.getHttpParams())); //Chucking is not performed for request has "http 1.0" and "GET" http method if (!((request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)) || (("GET").equals(requestMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD))) || (("DELETE").equals(requestMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD))))) { this.processChunking(conn, requestMsgCtx); } if (!keepAlive) { request.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); } // Pre-process HTTP request conn.getContext().setAttribute(ExecutionContext.HTTP_CONNECTION, conn); conn.getContext().setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost(url.getHost(), port)); conn.getContext().setAttribute(ExecutionContext.HTTP_REQUEST, request); // start the request targetConfiguration.getHttpProcessor().process(request, conn.getContext()); if (targetConfiguration.getProxyAuthenticator() != null && route.getProxyHost() != null && !route.isTunnelled()) { targetConfiguration.getProxyAuthenticator().authenticatePreemptively(request, conn.getContext()); } conn.submitRequest(request); if (hasEntityBody) { TargetContext.updateState(conn, ProtocolState.REQUEST_HEAD); } else { TargetContext.updateState(conn, ProtocolState.REQUEST_DONE); } }
From source file:org.siddhiesb.transport.passthru.TargetHandler.java
public void requestReady(NHttpClientConnection conn) { //System.out.println("============ TargetHandler : requestReady ==============="); HttpContext context = conn.getContext(); org.siddhiesb.transport.passthru.ProtocolState connState = null; try {//from w w w .j a va 2s . c om connState = org.siddhiesb.transport.passthru.TargetContext.getState(conn); if (connState == org.siddhiesb.transport.passthru.ProtocolState.REQUEST_DONE || connState == org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY) { return; } if (connState != org.siddhiesb.transport.passthru.ProtocolState.REQUEST_READY) { handleInvalidState(conn, "Request not started"); return; } ProxyTunnelHandler tunnelHandler = (ProxyTunnelHandler) context .getAttribute(PassThroughConstants.TUNNEL_HANDLER); if (tunnelHandler != null && !tunnelHandler.isCompleted()) { if (!tunnelHandler.isRequested()) { HttpRequest request = tunnelHandler.generateRequest(context); if (targetConfiguration.getProxyAuthenticator() != null) { targetConfiguration.getProxyAuthenticator().authenticatePreemptively(request, context); } if (log.isDebugEnabled()) { log.debug(conn + ": Sending CONNECT request to " + tunnelHandler.getProxy()); } conn.submitRequest(request); tunnelHandler.setRequested(); } return; } TargetRequest request = org.siddhiesb.transport.passthru.TargetContext.getRequest(conn); if (request != null) { request.start(conn); } context.setAttribute(PassThroughConstants.REQ_DEPARTURE_TIME, System.currentTimeMillis()); } catch (IOException e) { logIOException(conn, e); org.siddhiesb.transport.passthru.TargetContext.updateState(conn, org.siddhiesb.transport.passthru.ProtocolState.CLOSED); targetConfiguration.getConnections().shutdownConnection(conn); } catch (HttpException e) { log.error(e.getMessage(), e); org.siddhiesb.transport.passthru.TargetContext.updateState(conn, org.siddhiesb.transport.passthru.ProtocolState.CLOSED); targetConfiguration.getConnections().shutdownConnection(conn); } }