List of usage examples for org.apache.http.nio NHttpServerConnection requestOutput
void requestOutput();
From source file:org.apache.synapse.transport.nhttp.ServerHandler.java
public void markActiveConnectionsToBeClosed() { log.info("Marking the closing signal on the connection pool of size : " + activeConnections.size()); synchronized (this) { for (NHttpServerConnection conn : activeConnections) { conn.getContext().setAttribute(NhttpConstants.FORCE_CLOSING, "true"); conn.requestOutput();// ww w . j av a2s. c o m } } }
From source file:org.apache.synapse.transport.passthru.ServerWorker.java
public void sendAck(MessageContext msgContext) { String respWritten = ""; if (msgContext.getOperationContext() != null) { respWritten = (String) msgContext.getOperationContext().getProperty(Constants.RESPONSE_WRITTEN); }//from w ww.ja v a 2 s.c o m if (msgContext.getProperty(PassThroughConstants.FORCE_SOAP_FAULT) != null) { respWritten = "SKIP"; } boolean respWillFollow = !Constants.VALUE_TRUE.equals(respWritten) && !"SKIP".equals(respWritten); boolean ack = (((RequestResponseTransport) msgContext .getProperty(RequestResponseTransport.TRANSPORT_CONTROL)) .getStatus() == RequestResponseTransport.RequestResponseTransportStatus.ACKED); boolean forced = msgContext.isPropertyTrue(NhttpConstants.FORCE_SC_ACCEPTED); boolean nioAck = msgContext.isPropertyTrue("NIO-ACK-Requested", false); if (respWillFollow || ack || forced || nioAck) { NHttpServerConnection conn = request.getConnection(); SourceResponse sourceResponse; if (!nioAck) { msgContext.removeProperty(MessageContext.TRANSPORT_HEADERS); sourceResponse = SourceResponseFactory.create(msgContext, request, sourceConfiguration); sourceResponse.setStatus(HttpStatus.SC_ACCEPTED); } else { if (log.isDebugEnabled()) { log.debug("Sending ACK response with status " + msgContext.getProperty(NhttpConstants.HTTP_SC) + ", for MessageID : " + msgContext.getMessageID()); } sourceResponse = SourceResponseFactory.create(msgContext, request, sourceConfiguration); sourceResponse .setStatus(Integer.parseInt(msgContext.getProperty(NhttpConstants.HTTP_SC).toString())); } SourceContext.setResponse(conn, sourceResponse); ProtocolState state = SourceContext.getState(conn); if (state != null && state.compareTo(ProtocolState.REQUEST_DONE) <= 0) { conn.requestOutput(); } else { SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } } }
From source file:org.apache.synapse.transport.passthru.ServerWorker.java
private void handleException(String msg, Exception e) { if (e == null) { log.error(msg);// w w w . j av a2 s . c o m } else { log.error(msg, e); } if (e == null) { e = new Exception(msg); } try { MessageContext faultContext = MessageContextBuilder.createFaultMessageContext(msgContext, e); msgContext.setProperty(PassThroughConstants.FORCE_SOAP_FAULT, Boolean.TRUE); AxisEngine.sendFault(faultContext); } catch (Exception ex) { NHttpServerConnection conn = request.getConnection(); SourceResponse sourceResponse; msgContext.removeProperty(MessageContext.TRANSPORT_HEADERS); if (log.isDebugEnabled()) { log.debug("Sending ACK response with status " + msgContext.getProperty(NhttpConstants.HTTP_SC) + ", for MessageID : " + msgContext.getMessageID()); } sourceResponse = SourceResponseFactory.create(msgContext, request, sourceConfiguration); sourceResponse.addHeader(HTTP.CONTENT_TYPE, "text/html"); sourceResponse.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); Pipe pipe = new Pipe(sourceConfiguration.getBufferFactory().getBuffer(), "Test", sourceConfiguration); msgContext.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe); msgContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE); pipe.attachConsumer(conn); sourceResponse.connect(pipe); OutputStream os = pipe.getOutputStream(); try { String body = "<html><body><h1>" + "Failed to process the request" + "</h1><p>" + msg + "</p>"; if (e != null) { body = body + "<p>" + e.getMessage() + "</p></body></html>"; } if (ex != null) { body = body + "<p>" + ex.getMessage() + "</p></body></html>"; } os.write(body.getBytes()); os.flush(); os.close(); } catch (Exception ignore) { } pipe.setSerializationCompleteWithoutData(true); SourceContext.setResponse(conn, sourceResponse); ProtocolState state = SourceContext.getState(conn); if (state != null && state.compareTo(ProtocolState.REQUEST_DONE) <= 0) { conn.requestOutput(); } else { SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); } } }
From source file:org.apache.synapse.transport.passthru.PassThroughHttpSender.java
public void submitResponse(MessageContext msgContext) throws IOException, HttpException { SourceConfiguration sourceConfiguration = (SourceConfiguration) msgContext .getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONFIGURATION); NHttpServerConnection conn = (NHttpServerConnection) msgContext .getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION); if (conn == null) { ServerWorker serverWorker = (ServerWorker) msgContext.getProperty(Constants.OUT_TRANSPORT_INFO); if (serverWorker != null) { MessageContext requestContext = serverWorker.getRequestContext(); conn = (NHttpServerConnection) requestContext .getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION); sourceConfiguration = (SourceConfiguration) requestContext .getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONFIGURATION); } else {/* w w w. j a va2s .c o m*/ throw new IllegalStateException("Unable to correlate the response to a request"); } } // Handle ETag caching if (msgContext.getProperty(PassThroughConstants.HTTP_ETAG_ENABLED) != null && (Boolean) msgContext.getProperty(PassThroughConstants.HTTP_ETAG_ENABLED)) { try { RelayUtils.buildMessage(msgContext); } catch (IOException e) { handleException("IO Error occurred while building the message", e); } catch (XMLStreamException e) { handleException("XML Error occurred while building the message", e); } String hash = digestGenerator.getDigest(msgContext); Map headers = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS); headers.put(HttpHeaders.ETAG, "\"" + hash + "\""); } SourceRequest sourceRequest = SourceContext.getRequest(conn); SourceResponse sourceResponse = SourceResponseFactory.create(msgContext, sourceRequest, sourceConfiguration); sourceResponse.checkResponseChunkDisable(msgContext); SourceContext.setResponse(conn, sourceResponse); Boolean noEntityBody = (Boolean) msgContext.getProperty(PassThroughConstants.NO_ENTITY_BODY); Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE); if ((noEntityBody == null || !noEntityBody) || pipe != null) { if (pipe == null) { pipe = new Pipe(sourceConfiguration.getBufferFactory().getBuffer(), "Test", sourceConfiguration); msgContext.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe); msgContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE); } pipe.attachConsumer(conn); sourceResponse.connect(pipe); } Integer errorCode = (Integer) msgContext.getProperty(PassThroughConstants.ERROR_CODE); if (errorCode != null) { sourceResponse.setStatus(HttpStatus.SC_BAD_GATEWAY); SourceContext.get(conn).setShutDown(true); } ProtocolState state = SourceContext.getState(conn); if (state != null && state.compareTo(ProtocolState.REQUEST_DONE) <= 0) { // start sending the response if we boolean noEntityBodyResponse = false; if (noEntityBody != null && Boolean.TRUE == noEntityBody && pipe != null) { OutputStream out = pipe.getOutputStream(); out.write(new byte[0]); pipe.setRawSerializationComplete(true); out.close(); noEntityBodyResponse = true; } if (!noEntityBodyResponse && msgContext.isPropertyTrue(PassThroughConstants.MESSAGE_BUILDER_INVOKED) && pipe != null) { OutputStream out = pipe.getOutputStream(); /*if (msgContext.isPropertyTrue(NhttpConstants.SC_ACCEPTED)) { out.write(new byte[0]); }else {*/ //This is to support MTOM in response path for requests sent without a SOAPAction. The reason is //axis2 selects application/xml formatter as the formatter for formatting the ESB to client response //when there is no SOAPAction. if (Constants.VALUE_TRUE.equals(msgContext.getProperty(Constants.Configuration.ENABLE_MTOM))) { msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED); msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED); } MessageFormatter formatter = MessageFormatterDecoratorFactory .createMessageFormatterDecorator(msgContext); OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext); Object contentTypeInMsgCtx = msgContext .getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE); boolean isContentTypeSetFromMsgCtx = false; // If ContentType header is set in the axis2 message context, use it. if (contentTypeInMsgCtx != null) { String contentTypeValueInMsgCtx = contentTypeInMsgCtx.toString(); // Skip multipart/related as it should be taken from formatter. if (!contentTypeValueInMsgCtx.contains(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) { if (format != null) { String encoding = format.getCharSetEncoding(); if (encoding != null) { sourceResponse.removeHeader(HTTP.CONTENT_TYPE); contentTypeValueInMsgCtx += "; charset=" + encoding; } } sourceResponse.addHeader(HTTP.CONTENT_TYPE, contentTypeValueInMsgCtx); isContentTypeSetFromMsgCtx = true; } } // If ContentType is not set from msg context, get the formatter ContentType if (!isContentTypeSetFromMsgCtx) { sourceResponse.removeHeader(HTTP.CONTENT_TYPE); sourceResponse.addHeader(HTTP.CONTENT_TYPE, formatter.getContentType(msgContext, format, msgContext.getSoapAction())); } try { formatter.writeTo(msgContext, format, out, false); } catch (RemoteException fault) { IOUtils.closeQuietly(out); throw fault; } pipe.setSerializationComplete(true); out.close(); } conn.requestOutput(); } else { // nothing much to do as we have started the response already if (errorCode != null) { if (log.isDebugEnabled()) { log.warn("A Source connection is closed because of an " + "error in target: " + conn); } } else { log.debug("A Source Connection is closed, because source handler " + "is already in the process of writing a response while " + "another response is submitted: " + conn); } SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); } }