List of usage examples for org.apache.http.nio NHttpServerConnection requestInput
void requestInput();
From source file:org.apache.synapse.transport.passthru.SourceResponse.java
/** * Consume the content through the Pipe and write them to the wire * @param conn connection//from w w w. ja v a 2 s .co m * @param encoder encoder * @throws java.io.IOException if an error occurs * @return number of bytes written */ public int write(NHttpServerConnection conn, ContentEncoder encoder) throws IOException { int bytes = 0; if (pipe != null) { bytes = pipe.consume(encoder); } else { encoder.complete(); } // Update connection state if (encoder.isCompleted()) { SourceContext.updateState(conn, ProtocolState.RESPONSE_DONE); sourceConfiguration.getMetrics().notifySentMessageSize(conn.getMetrics().getSentBytesCount()); if (response != null && !this.connStrategy.keepAlive(response, conn.getContext())) { SourceContext.updateState(conn, ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().closeConnection(conn); } else if (SourceContext.get(conn).isShutDown()) { // we need to shut down if the shutdown flag is set SourceContext.updateState(conn, ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().closeConnection(conn); } else { // Reset connection state sourceConfiguration.getSourceConnections().releaseConnection(conn); // Ready to deal with a new request conn.requestInput(); } } return bytes; }
From source file:org.apache.synapse.transport.nhttp.ServerHandler.java
/** * Process ready output by writing into the channel * @param conn the connection being processed * @param encoder the content encoder in use *//*from w w w . j a v a 2 s.c o m*/ public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) { HttpContext context = conn.getContext(); HttpResponse response = conn.getHttpResponse(); ContentOutputBuffer outBuf = (ContentOutputBuffer) context.getAttribute(RESPONSE_SOURCE_BUFFER); if (outBuf == null) { // fix for SYNAPSE 584. This is a temporaly fix becuase of HTTPCORE-208 shutdownConnection(conn, false, null); return; } try { int bytesWritten = outBuf.produceContent(encoder); if (metrics != null && bytesWritten > 0) { metrics.incrementBytesSent(bytesWritten); } if (encoder.isCompleted()) { long currentTime = System.currentTimeMillis(); context.setAttribute(NhttpConstants.RES_TO_CLIENT_WRITE_END_TIME, currentTime); context.setAttribute(NhttpConstants.RES_DEPARTURE_TIME, currentTime); updateLatencyView(context); context.removeAttribute(NhttpConstants.REQ_ARRIVAL_TIME); context.removeAttribute(NhttpConstants.REQ_DEPARTURE_TIME); context.removeAttribute(NhttpConstants.RES_ARRIVAL_TIME); ((ServerConnectionDebug) conn.getContext().getAttribute(SERVER_CONNECTION_DEBUG)) .recordResponseCompletionTime(); Boolean reqRead = (Boolean) conn.getContext().getAttribute(NhttpConstants.REQUEST_READ); Boolean forceConnectionClose = (Boolean) conn.getContext() .getAttribute(NhttpConstants.FORCE_CONNECTION_CLOSE); if (reqRead != null && !reqRead) { try { // this is a connection we should not re-use conn.close(); } catch (Exception ignore) { } } else if (!connStrategy.keepAlive(response, context)) { conn.close(); } else if (forceConnectionClose != null && forceConnectionClose) { conn.close(); } else { conn.requestInput(); } } } catch (IOException e) { if (metrics != null) { metrics.incrementFaultsSending(); } handleException("I/O Error at outputReady : " + e.getMessage(), e, conn); } }
From source file:org.apache.synapse.transport.passthru.SourceHandler.java
public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) { try {//from www . ja va 2 s. c om ProtocolState protocolState = SourceContext.getState(conn); //special case to handle WSDLs if (protocolState == ProtocolState.WSDL_RESPONSE_DONE) { // we need to shut down if the shutdown flag is set HttpContext context = conn.getContext(); ContentOutputBuffer outBuf = (ContentOutputBuffer) context .getAttribute("synapse.response-source-buffer"); int bytesWritten = outBuf.produceContent(encoder); if (metrics != null && bytesWritten > 0) { metrics.incrementBytesSent(bytesWritten); } conn.requestInput(); if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) { sourceConfiguration.getSourceConnections().releaseConnection(conn); } endTransaction(conn); return; } if (protocolState != ProtocolState.RESPONSE_HEAD && protocolState != ProtocolState.RESPONSE_BODY) { log.warn("Illegal incoming connection state: " + protocolState + " . Possibly two send backs " + "are happening for the same request"); handleInvalidState(conn, "Trying to write response body"); endTransaction(conn); return; } SourceContext.updateState(conn, ProtocolState.RESPONSE_BODY); SourceResponse response = SourceContext.getResponse(conn); int bytesSent = response.write(conn, encoder); if (encoder.isCompleted()) { HttpContext context = conn.getContext(); long departure = System.currentTimeMillis(); context.setAttribute(PassThroughConstants.RES_TO_CLIENT_WRITE_END_TIME, departure); context.setAttribute(PassThroughConstants.RES_DEPARTURE_TIME, departure); updateLatencyView(context); } endTransaction(conn); metrics.incrementBytesSent(bytesSent); } catch (IOException e) { logIOException(conn, e); informWriterError(conn); SourceContext.updateState(conn, ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); } }
From source file:org.apache.synapse.transport.passthru.SourceResponse.java
/** * Starts the response by writing the headers * @param conn connection/*w w w . java2s . c o m*/ * @throws java.io.IOException if an error occurs * @throws org.apache.http.HttpException if an error occurs */ public void start(NHttpServerConnection conn) throws IOException, HttpException { // create the response response = sourceConfiguration.getResponseFactory().newHttpResponse(request.getVersion(), this.status, request.getConnection().getContext()); if (statusLine != null) { response.setStatusLine(version, status, statusLine); } else { response.setStatusCode(status); } BasicHttpEntity entity = null; if (canResponseHaveBody(request.getRequest(), response)) { entity = new BasicHttpEntity(); int 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); } if (contentLength != -1) { entity.setChunked(false); entity.setContentLength(contentLength); } else { entity.setChunked(true); } } response.setEntity(entity); // set any transport headers 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()) { response.addHeader(entry.getKey(), i.next()); } } } response.setParams(new DefaultedHttpParams(response.getParams(), sourceConfiguration.getHttpParams())); SourceContext.updateState(conn, ProtocolState.RESPONSE_HEAD); // Pre-process HTTP response conn.getContext().setAttribute(ExecutionContext.HTTP_CONNECTION, conn); conn.getContext().setAttribute(ExecutionContext.HTTP_RESPONSE, response); conn.getContext().setAttribute(ExecutionContext.HTTP_REQUEST, SourceContext.getRequest(conn).getRequest()); sourceConfiguration.getHttpProcessor().process(response, conn.getContext()); conn.submitResponse(response); // Handle non entity body responses if (entity == null) { hasEntity = false; // Reset connection state sourceConfiguration.getSourceConnections().releaseConnection(conn); // Make ready to deal with a new request conn.requestInput(); } }
From source file:org.siddhiesb.transport.passthru.SourceHandler.java
public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) { try {/* ww w . j a va2 s . c o m*/ org.siddhiesb.transport.passthru.ProtocolState protocolState = org.siddhiesb.transport.passthru.SourceContext .getState(conn); //special case to handle WSDLs if (protocolState == org.siddhiesb.transport.passthru.ProtocolState.WSDL_RESPONSE_DONE) { // we need to shut down if the shutdown flag is set HttpContext context = conn.getContext(); ContentOutputBuffer outBuf = (ContentOutputBuffer) context .getAttribute("synapse.response-source-buffer"); int bytesWritten = outBuf.produceContent(encoder); conn.requestInput(); if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) { sourceConfiguration.getSourceConnections().releaseConnection(conn); } return; } if (protocolState != org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_HEAD && protocolState != org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY) { log.warn("Illegal incoming connection state: " + protocolState + " . Possibly two send backs " + "are happening for the same request"); handleInvalidState(conn, "Trying to write response body"); return; } org.siddhiesb.transport.passthru.SourceContext.updateState(conn, org.siddhiesb.transport.passthru.ProtocolState.RESPONSE_BODY); SourceResponse response = org.siddhiesb.transport.passthru.SourceContext.getResponse(conn); int bytesSent = response.write(conn, encoder); if (encoder.isCompleted()) { HttpContext context = conn.getContext(); if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) { } context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME); context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME); context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME); } } catch (IOException e) { logIOException(conn, e); informWriterError(conn); org.siddhiesb.transport.passthru.SourceContext.updateState(conn, org.siddhiesb.transport.passthru.ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } }