public SourceHandler(SourceConfiguration sourceConfiguration) { this.sourceConfiguration = sourceConfiguration; this.metrics = sourceConfiguration.getMetrics(); try { Scheme scheme = sourceConfiguration.getScheme(); if (!scheme.isSSL()) { this.latencyView = new LatencyView(scheme.isSSL()); } else { this.s2sLatencyView = new LatencyView(scheme.isSSL()); } } catch (AxisFault e) { log.error(e.getMessage(), e); } }
public void connected(NHttpServerConnection conn) { // we have to have these two operations in order sourceConfiguration.getSourceConnections().addConnection(conn); SourceContext.create(conn, ProtocolState.REQUEST_READY, sourceConfiguration); metrics.connected(); }
public void inputReady(NHttpServerConnection conn, ContentDecoder decoder) { try { ProtocolState protocolState = SourceContext.getState(conn); if (protocolState != ProtocolState.REQUEST_HEAD && protocolState != ProtocolState.REQUEST_BODY) { handleInvalidState(conn, "Request message body data received"); return; } SourceContext.updateState(conn, ProtocolState.REQUEST_BODY); SourceRequest request = SourceContext.getRequest(conn); int readBytes = request.read(conn, decoder); if (readBytes > 0) { metrics.incrementBytesReceived(readBytes); } } catch (IOException e) { logIOException(conn, e); informReaderError(conn); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } }
/** * Commit the response to the connection. Processes the response through the configured * HttpProcessor and submits it to be sent out. This method hides any exceptions and is targetted * for non critical (i.e. browser requests etc) requests, which are not core messages * * @param conn the connection being processed * @param response the response to commit over the connection */ public void commitResponseHideExceptions( final NHttpServerConnection conn, final HttpResponse response) { try { conn.suspendInput(); sourceConfiguration.getHttpProcessor().process(response, conn.getContext()); conn.submitResponse(response); } catch (HttpException e) { handleException("Unexpected HTTP protocol error : " + e.getMessage(), e, conn); } catch (IOException e) { handleException("IO error submiting response : " + e.getMessage(), e, conn); } }
public void responseReady(NHttpServerConnection conn) { try { ProtocolState protocolState = SourceContext.getState(conn); if (protocolState.compareTo(ProtocolState.REQUEST_DONE) < 0) { return; } if (protocolState.compareTo(ProtocolState.CLOSING) >= 0) { return; } if (protocolState != ProtocolState.REQUEST_DONE) { handleInvalidState(conn, "Writing a response"); return; } // because the duplex nature of http core we can reach hear without a actual response SourceResponse response = SourceContext.getResponse(conn); if (response != null) { response.start(conn); metrics.incrementMessagesSent(); } } catch (IOException e) { logIOException(conn, e); informWriterError(conn); SourceContext.updateState(conn, ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } catch (HttpException e) { log.error(e.getMessage(), e); informWriterError(conn); SourceContext.updateState(conn, ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } }
public void exception(NHttpServerConnection conn, Exception ex) { if (ex instanceof IOException) { logIOException(conn, (IOException) ex); metrics.incrementFaultsReceiving(); ProtocolState state = SourceContext.getState(conn); if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) { informReaderError(conn); } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) { informWriterError(conn); } else if (state == ProtocolState.REQUEST_DONE) { informWriterError(conn); } else if (state == ProtocolState.RESPONSE_DONE) { informWriterError(conn); } SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } else if (ex instanceof HttpException) { try { if (conn.isResponseSubmitted()) { sourceConfiguration.getSourceConnections().shutDownConnection(conn); return; } HttpContext httpContext = conn.getContext(); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad request"); response.setParams( new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams())); response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); // Pre-process HTTP request httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null); httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response); sourceConfiguration.getHttpProcessor().process(response, httpContext); conn.submitResponse(response); SourceContext.updateState(conn, ProtocolState.CLOSED); conn.close(); } catch (Exception ex1) { log.error(ex.getMessage(), ex); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } } else { log.error("Unexoected error: " + ex.getMessage(), ex); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } }
public void closed(NHttpServerConnection conn) { ProtocolState state = SourceContext.getState(conn); if (state == ProtocolState.REQUEST_READY || state == ProtocolState.RESPONSE_DONE) { if (log.isDebugEnabled()) { log.debug(conn + ": Keep-Alive connection was closed: " + conn); } } else if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) { informReaderError(conn); log.warn("Connection closed while reading the request: " + conn); } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) { informWriterError(conn); log.warn("Connection closed while writing the response: " + conn); } else if (state == ProtocolState.REQUEST_DONE) { log.warn("Connection closed by the client after request is read: " + conn); } metrics.disconnected(); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); }
public void requestReceived(NHttpServerConnection conn) { try { HttpContext _context = conn.getContext(); _context.setAttribute(PassThroughConstants.REQ_ARRIVAL_TIME, System.currentTimeMillis()); if (!SourceContext.assertState(conn, ProtocolState.REQUEST_READY) && !SourceContext.assertState(conn, ProtocolState.WSDL_RESPONSE_DONE)) { handleInvalidState(conn, "Request received"); return; } // we have received a message over this connection. So we must inform the pool sourceConfiguration.getSourceConnections().useConnection(conn); // at this point we have read the HTTP Headers SourceContext.updateState(conn, ProtocolState.REQUEST_HEAD); SourceRequest request = new SourceRequest(sourceConfiguration, conn.getHttpRequest(), conn); SourceContext.setRequest(conn, request); request.start(conn); metrics.incrementMessagesReceived(); /** *** */ String method = request.getRequest() != null ? request.getRequest().getRequestLine().getMethod().toUpperCase() : ""; OutputStream os = null; if ("GET".equals(method) || "HEAD".equals(method)) { HttpContext context = request.getConnection().getContext(); ContentOutputBuffer outputBuffer = new SimpleOutputBuffer(8192, new HeapByteBufferAllocator()); // ContentOutputBuffer outputBuffer // = new SharedOutputBuffer(8192, conn, new // HeapByteBufferAllocator()); context.setAttribute("synapse.response-source-buffer", outputBuffer); os = new ContentOutputStream(outputBuffer); } sourceConfiguration .getWorkerPool() .execute(new ServerWorker(request, sourceConfiguration, os)); } catch (HttpException e) { log.error(e.getMessage(), e); informReaderError(conn); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } catch (IOException e) { logIOException(conn, e); informReaderError(conn); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } }
private void handleInvalidState(NHttpServerConnection conn, String action) { log.warn( action + " while the handler is in an inconsistent state " + SourceContext.getState(conn)); SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn); }
public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) { try { 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); } 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"); 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(); if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) { if (latencyView != null) { latencyView.notifyTimes( (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME), (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME), (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME), System.currentTimeMillis()); } else if (s2sLatencyView != null) { s2sLatencyView.notifyTimes( (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME), (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME), (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME), System.currentTimeMillis()); } } context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME); context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME); context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME); } metrics.incrementBytesSent(bytesSent); } catch (IOException e) { logIOException(conn, e); informWriterError(conn); SourceContext.updateState(conn, ProtocolState.CLOSING); sourceConfiguration.getSourceConnections().shutDownConnection(conn); } }
/** * Handle the response or error (during a failed send) message received for an outgoing request * * @param messageID Request message ID * @param response the Axis2 MessageContext that has been received and has to be handled * @param synapseOutMsgCtx the corresponding (outgoing) Synapse MessageContext for the above Axis2 * MC, that holds Synapse specific information such as the error handler stack and local * properties etc. * @throws AxisFault if the message cannot be processed */ private void handleMessage( String messageID, MessageContext response, org.apache.synapse.MessageContext synapseOutMsgCtx, AsyncCallback callback) throws AxisFault { // apply the tenant information to the out message context TenantInfoConfigurator configurator = synapseOutMsgCtx.getEnvironment().getTenantInfoConfigurator(); if (configurator != null) { configurator.applyTenantInfo(synapseOutMsgCtx); } Object o = response.getProperty(SynapseConstants.SENDING_FAULT); if (o != null && Boolean.TRUE.equals(o)) { StatisticsReporter.reportFaultForAll( synapseOutMsgCtx, ErrorLogFactory.createErrorLog(response)); // there is a sending fault. propagate the fault to fault handlers. Stack faultStack = synapseOutMsgCtx.getFaultStack(); if (faultStack != null && !faultStack.isEmpty()) { // if we have access to the full synapseOutMsgCtx.getEnvelope(), then let // it flow with the error details. Else, replace its envelope with the // fault envelope try { synapseOutMsgCtx.getEnvelope().build(); } catch (OMException x) { synapseOutMsgCtx.setEnvelope(response.getEnvelope()); } Exception e = (Exception) response.getProperty(SynapseConstants.ERROR_EXCEPTION); synapseOutMsgCtx.setProperty(SynapseConstants.SENDING_FAULT, Boolean.TRUE); synapseOutMsgCtx.setProperty( SynapseConstants.ERROR_CODE, response.getProperty(SynapseConstants.ERROR_CODE)); synapseOutMsgCtx.setProperty( SynapseConstants.ERROR_MESSAGE, response.getProperty(SynapseConstants.ERROR_MESSAGE)); synapseOutMsgCtx.setProperty( SynapseConstants.ERROR_DETAIL, response.getProperty(SynapseConstants.ERROR_DETAIL)); synapseOutMsgCtx.setProperty(SynapseConstants.ERROR_EXCEPTION, e); if (log.isDebugEnabled()) { log.debug( "[Failed Request Message ID : " + messageID + "]" + " [New to be Retried Request Message ID : " + synapseOutMsgCtx.getMessageID() + "]"); } int errorCode = (Integer) response.getProperty(SynapseConstants.ERROR_CODE); // If a timeout has occured and the timeout action of the callback is to discard the message if (errorCode == SynapseConstants.NHTTP_CONNECTION_TIMEOUT && callback.getTimeOutAction() == SynapseConstants.DISCARD) { // Do not execute any fault sequences. Discard message if (log.isWarnEnabled()) { log.warn( "Synapse timed out for the request with Message ID : " + messageID + ". Ignoring fault handlers since the timeout action is DISCARD"); } faultStack.removeAllElements(); } else { ((FaultHandler) faultStack.pop()).handleFault(synapseOutMsgCtx, null); } } } else { // there can always be only one instance of an Endpoint in the faultStack of a message // if the send was successful, so remove it before we proceed any further Stack faultStack = synapseOutMsgCtx.getFaultStack(); Endpoint successfulEndpoint = null; if (faultStack != null && !faultStack.isEmpty() && faultStack.peek() instanceof Endpoint) { successfulEndpoint = (Endpoint) faultStack.pop(); } if (log.isDebugEnabled()) { log.debug("Synapse received an asynchronous response message"); log.debug( "Received To: " + (response.getTo() != null ? response.getTo().getAddress() : "null")); log.debug( "SOAPAction: " + (response.getSoapAction() != null ? response.getSoapAction() : "null")); log.debug( "WSA-Action: " + (response.getWSAAction() != null ? response.getWSAAction() : "null")); String[] cids = response.getAttachmentMap().getAllContentIDs(); if (cids != null && cids.length > 0) { for (String cid : cids) { log.debug("Attachment : " + cid); } } log.debug("Body : \n" + response.getEnvelope()); } MessageContext axisOutMsgCtx = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext(); // Processes 'Accept-Encoding' ResponseAcceptEncodingProcessor.process(response, axisOutMsgCtx); response.setServiceContext(null); response.setOperationContext(axisOutMsgCtx.getOperationContext()); response.setAxisMessage( axisOutMsgCtx.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE)); // set properties on response response.setServerSide(true); response.setProperty(SynapseConstants.ISRESPONSE_PROPERTY, Boolean.TRUE); response.setProperty( MessageContext.TRANSPORT_OUT, axisOutMsgCtx.getProperty(MessageContext.TRANSPORT_OUT)); response.setProperty( org.apache.axis2.Constants.OUT_TRANSPORT_INFO, axisOutMsgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO)); response.setTransportIn(axisOutMsgCtx.getTransportIn()); response.setTransportOut(axisOutMsgCtx.getTransportOut()); // If request is REST assume that the response is REST too response.setDoingREST(axisOutMsgCtx.isDoingREST()); if (axisOutMsgCtx.isDoingMTOM()) { response.setDoingMTOM(true); response.setProperty( org.apache.axis2.Constants.Configuration.ENABLE_MTOM, org.apache.axis2.Constants.VALUE_TRUE); } if (axisOutMsgCtx.isDoingSwA()) { response.setDoingSwA(true); response.setProperty( org.apache.axis2.Constants.Configuration.ENABLE_SWA, org.apache.axis2.Constants.VALUE_TRUE); } // when axis2 receives a soap message without addressing headers it users // DISABLE_ADDRESSING_FOR_OUT_MESSAGES property to keep it and hence avoid addressing // headers on the response. this causes a problem for synapse if the original message // it receivs (from client) has addressing and the synaspse service invocation has not // engage addressing. in this case when synapse receives the response from the server // addessing In handler dissable addressing since that response does not have addressing // headers. synapse sends the response to its orignal client using the same message // context. Then this response does not have addressing headers since it already // disable. to avoid this we need to set the DISABLE_ADDRESSING_FOR_OUT_MESSAGES // property state to original state. if (axisOutMsgCtx.getProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES) != null) { response.setProperty( AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES, axisOutMsgCtx.getProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES)); } else { response.removeProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES); } Object messageType = axisOutMsgCtx.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE); if (!HTTPConstants.MEDIA_TYPE_X_WWW_FORM.equals(messageType)) { // copy the message type property that's used by the out message to the // response message response.setProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE, messageType); } // compare original received message (axisOutMsgCtx) soap version with the response // if they are different change to original version if (axisOutMsgCtx.isSOAP11() != response.isSOAP11()) { if (axisOutMsgCtx.isSOAP11()) { SOAPUtils.convertSOAP12toSOAP11(response); } else { SOAPUtils.convertSOAP11toSOAP12(response); } } if (axisOutMsgCtx.getMessageID() != null) { response.setRelationships(new RelatesTo[] {new RelatesTo(axisOutMsgCtx.getMessageID())}); } response.setReplyTo(axisOutMsgCtx.getReplyTo()); response.setFaultTo(axisOutMsgCtx.getFaultTo()); if (axisOutMsgCtx.isPropertyTrue(NhttpConstants.IGNORE_SC_ACCEPTED)) { response.setProperty(NhttpConstants.FORCE_SC_ACCEPTED, Constants.VALUE_TRUE); } // create the synapse message context for the response Axis2MessageContext synapseInMessageContext = new Axis2MessageContext( response, synapseOutMsgCtx.getConfiguration(), synapseOutMsgCtx.getEnvironment()); synapseInMessageContext.setResponse(true); Object obj = synapseOutMsgCtx.getProperty(SynapseConstants.FORCE_ERROR_PROPERTY); String errorOnSOAPFault = (String) obj; if (Constants.VALUE_TRUE.equals(errorOnSOAPFault) && successfulEndpoint != null) { if (log.isDebugEnabled()) { log.debug("FORCE_ERROR_ON_SOAP_FAULT is true, checking for SOAPFault"); } try { RelayUtils.buildMessage( ((Axis2MessageContext) synapseInMessageContext).getAxis2MessageContext(), true); } catch (Exception e) { // handleException("Error while building message", e, synapseInMessageContext); } if ((synapseInMessageContext.getEnvelope() != null) && synapseInMessageContext.getEnvelope().hasFault()) { if (log.isDebugEnabled()) { log.debug( "SOAPFault found in response message, forcing endpoint " + successfulEndpoint.getName() + " to fail"); } // setup new pipe configuration..if failure happens (this will be setup as the source // writer and during the TargetContext // clean up operation the writer will be reset and pull to the buffer MessageContext axis2OUTMC = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext(); NHttpServerConnection conn = (NHttpServerConnection) axis2OUTMC.getProperty("pass-through.Source-Connection"); if (conn != null) { SourceConfiguration sourceConfiguration = (SourceConfiguration) axis2OUTMC.getProperty("PASS_THROUGH_SOURCE_CONFIGURATION"); Pipe pipe = new Pipe( conn, sourceConfiguration.getBufferFactory().getBuffer(), "source", sourceConfiguration); axis2OUTMC.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe); } StatisticsReporter.reportFaultForAll( synapseOutMsgCtx, ErrorLogFactory.createErrorLog(response)); synapseOutMsgCtx.setProperty(SynapseConstants.SENDING_FAULT, Boolean.TRUE); synapseOutMsgCtx.setProperty( SynapseConstants.ERROR_CODE, SynapseConstants.ENDPOINT_CUSTOM_ERROR); ((FaultHandler) successfulEndpoint).handleFault(synapseOutMsgCtx, null); return; } else { successfulEndpoint.onSuccess(); } } else if (successfulEndpoint != null) { successfulEndpoint.onSuccess(); } synapseInMessageContext.setTo( new EndpointReference(AddressingConstants.Final.WSA_ANONYMOUS_URL)); synapseInMessageContext.setTracingState(synapseOutMsgCtx.getTracingState()); // set the properties of the original MC to the new MC for (Object key : synapseOutMsgCtx.getPropertyKeySet()) { synapseInMessageContext.setProperty( (String) key, synapseOutMsgCtx.getProperty((String) key)); } // Copy SequenceCallStack from original MC to the new MC if (synapseOutMsgCtx.isContinuationEnabled()) { // Set the message direction if (!synapseOutMsgCtx.isResponse()) { synapseInMessageContext.setResponse(false); } Stack<ContinuationState> seqContinuationStates = synapseOutMsgCtx.getContinuationStateStack(); for (int i = 0; i < seqContinuationStates.size(); i++) { synapseInMessageContext.pushContinuationState(seqContinuationStates.get(i)); } } // If this response is related to session affinity endpoints -Server initiated session Dispatcher dispatcher = (Dispatcher) synapseOutMsgCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_DISPATCHER); if (dispatcher != null && dispatcher.isServerInitiatedSession()) { dispatcher.updateSession(synapseInMessageContext); } StatisticsReporter.reportForAllOnResponseReceived(synapseInMessageContext); // send the response message through the synapse mediation flow try { synapseOutMsgCtx.getEnvironment().injectMessage(synapseInMessageContext); } catch (SynapseException syne) { Stack stack = synapseInMessageContext.getFaultStack(); if (stack != null && !stack.isEmpty()) { ((FaultHandler) stack.pop()).handleFault(synapseInMessageContext, syne); } else { log.error( "Synapse encountered an exception, " + "No error handlers found - [Message Dropped]\n" + syne.getMessage()); } } } }