Search in sources :

Example 16 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class TargetHandler method timeout.

public void timeout(NHttpClientConnection conn) {
    ProtocolState state = TargetContext.getState(conn);
    MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx();
    Map<String, String> logDetails = getLoggingInfo(conn, state, requestMsgCtx);
    if (log.isDebugEnabled()) {
        log.debug(getErrorMessage("Connection timeout", conn) + " " + getConnectionLoggingInfo(conn));
    }
    if (state != null && (state == ProtocolState.REQUEST_READY || state == ProtocolState.RESPONSE_DONE)) {
        if (log.isDebugEnabled()) {
            log.debug(getErrorMessage("Keep-alive connection timed out", conn) + " " + getConnectionLoggingInfo(conn));
        }
    } else if (state != null) {
        if (state == ProtocolState.REQUEST_BODY) {
            metrics.incrementTimeoutsSending();
            informWriterError(conn);
            log.warn("ERROR_CODE = " + ErrorCodes.CONNECTION_TIMEOUT + ", STATE_DESCRIPTION = Socket Timeout " + "occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = Connection between the EI and the BackEnd timeouts, TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn + ", SOCKET_TIMEOUT = " + conn.getSocketTimeout() + ", CORRELATION_ID" + " = " + conn.getContext().getAttribute(CorrelationConstants.CORRELATION_ID));
        }
        if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.REQUEST_HEAD) {
            metrics.incrementTimeoutsReceiving();
            informReaderError(conn);
            log.warn("ERROR_CODE = " + ErrorCodes.CONNECTION_TIMEOUT + ", STATE_DESCRIPTION = Socket Timeout " + "occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = Connection between the EI and the BackEnd timeouts, TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn + ", SOCKET_TIMEOUT = " + conn.getSocketTimeout() + ", " + "CORRELATION_ID = " + conn.getContext().getAttribute(CorrelationConstants.CORRELATION_ID));
        }
        if (state.compareTo(ProtocolState.REQUEST_DONE) <= 0) {
            log.warn("ERROR_CODE = " + ErrorCodes.CONNECTION_TIMEOUT + ", STATE_DESCRIPTION = Socket Timeout " + "occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = Connection between the EI and the BackEnd timeouts, TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn + ", SOCKET_TIMEOUT = " + conn.getSocketTimeout() + ", CORRELATION_ID" + " = " + conn.getContext().getAttribute(CorrelationConstants.CORRELATION_ID));
            if (targetConfiguration.isCorrelationLoggingEnabled()) {
                logHttpRequestErrorInCorrelationLog(conn, "Timeout in " + state);
            }
            if (requestMsgCtx != null) {
                requestMsgCtx.setProperty(PassThroughConstants.INTERNAL_EXCEPTION_ORIGIN, PassThroughConstants.INTERNAL_ORIGIN_ERROR_HANDLER);
                targetErrorHandler.handleError(requestMsgCtx, ErrorCodes.CONNECTION_TIMEOUT, "Error in Sender", null, state);
            }
        }
    }
    TargetContext.updateState(conn, ProtocolState.CLOSED);
    targetConfiguration.getConnections().shutdownConnection(conn, true);
}
Also used : MessageContext(org.apache.axis2.context.MessageContext)

Example 17 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class TargetHandler method exception.

public void exception(NHttpClientConnection conn, Exception ex) {
    ProtocolState state = TargetContext.getState(conn);
    MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx();
    Map<String, String> logDetails = getLoggingInfo(conn, state, requestMsgCtx, ex);
    if (state == ProtocolState.REQUEST_HEAD || state == ProtocolState.REQUEST_BODY) {
        informWriterError(conn);
        log.warn("ERROR_CODE = " + logDetails.get("error_code") + ", STATE_DESCRIPTION = Exception occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = " + logDetails.get("cause_of_error") + ", TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
    } else if (state == ProtocolState.RESPONSE_HEAD || state == ProtocolState.RESPONSE_BODY) {
        informReaderError(conn);
        log.warn("ERROR_CODE = " + logDetails.get("error_code") + ", STATE_DESCRIPTION = Exception occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = " + logDetails.get("cause_of_error") + ", TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
    } else if (state == ProtocolState.REQUEST_DONE) {
        log.warn("ERROR_CODE = " + logDetails.get("error_code") + ", STATE_DESCRIPTION = Exception occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = " + logDetails.get("cause_of_error") + ", TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
    } else if (state == ProtocolState.REQUEST_READY) {
        log.warn("ERROR_CODE = " + logDetails.get("error_code") + ", STATE_DESCRIPTION = Exception occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = " + logDetails.get("cause_of_error") + ", TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
    }
    if (ex instanceof IOException) {
        logIOException(conn, (IOException) ex);
        if (targetConfiguration.isCorrelationLoggingEnabled()) {
            logHttpRequestErrorInCorrelationLog(conn, "IO Exception in " + state.name());
        }
        if (state != ProtocolState.RESPONSE_DONE && requestMsgCtx != null) {
            requestMsgCtx.setProperty(PassThroughConstants.INTERNAL_EXCEPTION_ORIGIN, PassThroughConstants.INTERNAL_ORIGIN_ERROR_HANDLER);
            targetErrorHandler.handleError(requestMsgCtx, ErrorCodes.SND_IO_ERROR, "Error in Sender", ex, state);
        }
        TargetContext.updateState(conn, ProtocolState.CLOSING);
    } else if (ex instanceof HttpException) {
        String message = getErrorMessage("HTTP protocol violation : " + ex.getMessage(), conn);
        log.error(message, ex);
        if (targetConfiguration.isCorrelationLoggingEnabled()) {
            logHttpRequestErrorInCorrelationLog(conn, "HTTP Exception in " + state.name());
        }
        if (state != ProtocolState.RESPONSE_DONE && requestMsgCtx != null) {
            requestMsgCtx.setProperty(PassThroughConstants.INTERNAL_EXCEPTION_ORIGIN, PassThroughConstants.INTERNAL_ORIGIN_ERROR_HANDLER);
            targetErrorHandler.handleError(requestMsgCtx, ErrorCodes.PROTOCOL_VIOLATION, "Error in Sender", null, state);
        }
        TargetContext.updateState(conn, ProtocolState.CLOSED);
    } else {
        if (null != ex && null != ex.getMessage()) {
            log.error("Unexpected error: " + ex.getMessage(), ex);
        } else {
            log.error("Unexpected error.");
        }
        if (targetConfiguration.isCorrelationLoggingEnabled()) {
            logHttpRequestErrorInCorrelationLog(conn, "Unexpected error");
        }
        TargetContext.updateState(conn, ProtocolState.CLOSED);
    }
    targetConfiguration.getConnections().shutdownConnection(conn, true);
}
Also used : HttpException(org.apache.http.HttpException) MessageContext(org.apache.axis2.context.MessageContext) IOException(java.io.IOException)

Example 18 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class SourceHandler method inputReady.

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 = 0;
        boolean interceptionEnabled = false;
        Boolean[] interceptorResults = new Boolean[noOfInterceptors];
        if (interceptStream) {
            int index = 0;
            for (StreamInterceptor interceptor : streamInterceptors) {
                interceptorResults[index] = interceptor.interceptSourceRequest((MessageContext) conn.getContext().getAttribute(PassThroughConstants.REQUEST_MESSAGE_CONTEXT));
                if (!interceptionEnabled && interceptorResults[index]) {
                    interceptionEnabled = true;
                }
                index++;
            }
            if (interceptionEnabled) {
                ByteBuffer bytesSent = request.copyAndRead(conn, decoder);
                if (bytesSent != null) {
                    readBytes = bytesSent.remaining();
                    index = 0;
                    for (StreamInterceptor interceptor : streamInterceptors) {
                        if (interceptorResults[index]) {
                            boolean proceed = interceptor.sourceRequest(bytesSent.duplicate().asReadOnlyBuffer(), (MessageContext) conn.getContext().getAttribute(PassThroughConstants.REQUEST_MESSAGE_CONTEXT));
                            if (!proceed) {
                                log.info("Dropping source connection since request is blocked by : " + interceptor.getClass().getName());
                                dropSourceConnection(conn);
                                conn.getContext().setAttribute(PassThroughConstants.SOURCE_CONNECTION_DROPPED, true);
                                request.getPipe().forceProducerComplete(decoder);
                                break;
                            }
                        }
                        index++;
                    }
                }
            } else {
                readBytes = request.read(conn, decoder);
            }
        } else {
            readBytes = request.read(conn, decoder);
        }
        if (isMessageSizeValidationEnabled) {
            HttpContext httpContext = conn.getContext();
            // method requestReceived() Eg:- inbound http/https
            if (httpContext.getAttribute(PassThroughConstants.MESSAGE_SIZE_VALIDATION_SUM) == null) {
                httpContext.setAttribute(PassThroughConstants.MESSAGE_SIZE_VALIDATION_SUM, 0);
            }
            int messageSizeSum = (int) httpContext.getAttribute(PassThroughConstants.MESSAGE_SIZE_VALIDATION_SUM);
            messageSizeSum += readBytes;
            if (messageSizeSum > validMaxMessageSize) {
                log.warn("Payload exceeds valid payload size range, hence discontinuing chunk stream at " + messageSizeSum + " bytes to prevent OOM.");
                dropSourceConnection(conn);
                metrics.exceptionOccured();
                conn.getContext().setAttribute(PassThroughConstants.SOURCE_CONNECTION_DROPPED, true);
                // stopped http chunk stream from here and mark producer complete
                request.getPipe().forceProducerComplete(decoder);
            }
            httpContext.setAttribute(PassThroughConstants.MESSAGE_SIZE_VALIDATION_SUM, messageSizeSum);
        }
        if (readBytes > 0) {
            metrics.incrementBytesReceived(readBytes);
        }
    } catch (IOException e) {
        ProtocolState protocolState = SourceContext.getState(conn);
        Map<String, String> logDetails = getLoggingInfo(conn, protocolState);
        log.warn("STATE_DESCRIPTION = IO/Exception when reading bytes of request body from the underlying stream, " + "INTERNAL_STATE" + " = " + protocolState + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = " + e.getMessage() + ", HTTP_URL = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", CLIENT_ADDRESS = " + getClientConnectionInfo(conn) + ", CONNECTION " + conn);
        logIOException(conn, e);
        informReaderError(conn);
        // decrementing the request metric
        metrics.exceptionOccured();
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
    }
}
Also used : HttpContext(org.apache.http.protocol.HttpContext) MessageContext(org.apache.axis2.context.MessageContext) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class DeliveryAgent method submit.

/**
 * This method queues the message for delivery. If a connection is already existing for
 * the destination epr, the message will be delivered immediately. Otherwise message has
 * to wait until a connection is established. In this case this method will inform the
 * system about the need for a connection.
 *
 * @param msgContext the message context to be sent
 * @param epr the endpoint to which the message should be sent
 * @throws AxisFault if an error occurs
 *
 * @return false if connection can not be acquired due to connection limit exceeds or queue limit exceeds
 *         else return true
 */
public boolean submit(MessageContext msgContext, EndpointReference epr) throws AxisFault {
    try {
        if (log.isDebugEnabled()) {
            log.debug("Submitting request for MessageID: " + msgContext.getMessageID());
        }
        URL url = new URL(epr.getAddress());
        String scheme = url.getProtocol() != null ? url.getProtocol() : "http";
        String hostname = url.getHost();
        int port = url.getPort();
        if (port == -1) {
            // use default
            if ("http".equals(scheme)) {
                port = 80;
            } else if ("https".equals(scheme)) {
                port = 443;
            }
        }
        HttpHost target = new HttpHost(hostname, port, scheme);
        boolean secure = "https".equalsIgnoreCase(target.getSchemeName());
        HttpHost proxy = proxyConfig.selectProxy(target);
        msgContext.setProperty(PassThroughConstants.PROXY_PROFILE_TARGET_HOST, target.getHostName());
        HttpRoute route;
        if (proxy != null) {
            route = new HttpRoute(target, null, proxy, secure);
        } else {
            route = new HttpRoute(target, null, secure);
        }
        // first we queue the message
        Queue<MessageContext> queue = null;
        NHttpClientConnection conn = null;
        lock.lock();
        try {
            queue = waitingMessages.get(route);
            if (queue == null) {
                queue = new ConcurrentLinkedQueue<MessageContext>();
                waitingMessages.put(route, queue);
            }
            if (queue.size() >= maxWaitingMessages) {
                msgContext.setProperty(PassThroughConstants.INTERNAL_EXCEPTION_ORIGIN, PassThroughConstants.INTERNAL_ORIGIN_ERROR_HANDLER);
                log.warn("Delivery agent queue length exceeds the maximum number of waiting messages");
                if (msgContext != null) {
                    targetErrorHandler.handleError(msgContext, ErrorCodes.CONNECTION_TIMEOUT, "Number of queued messages exceeds the limit", null, ProtocolState.REQUEST_READY);
                }
                return false;
            }
            queue.add(msgContext);
            conn = targetConnections.getConnection(route, msgContext, targetErrorHandler, queue);
            if (conn == null && msgContext != null && "true".equalsIgnoreCase((String) msgContext.getProperty(PassThroughConstants.CONNECTION_LIMIT_EXCEEDS))) {
                msgContext.removeProperty(PassThroughConstants.CONNECTION_LIMIT_EXCEEDS);
                return false;
            }
        } finally {
            lock.unlock();
        }
        if (conn != null) {
            if (log.isDebugEnabled()) {
                log.debug("Connection found from pool for MessageID: " + msgContext.getMessageID() + ", conn: " + conn.toString());
            }
            conn.resetInput();
            conn.resetOutput();
            MessageContext messageContext = queue.poll();
            if (messageContext != null) {
                tryNextMessage(messageContext, route, conn);
            }
        }
    } catch (MalformedURLException e) {
        handleException("Malformed URL in the target EPR", e);
    }
    return true;
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) MessageContext(org.apache.axis2.context.MessageContext) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) URL(java.net.URL)

Example 20 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class RESTUtil method processURLRequest.

/**
 * Processes the HTTP GET request and builds the SOAP info-set of the REST message
 *
 * @param msgContext The MessageContext of the Request Message
 * @param out        The output stream of the response
 * @param soapAction SoapAction of the request
 * @param requestURI The URL that the request came to
 * @throws AxisFault - Thrown in case a fault occurs
 */
public static void processURLRequest(MessageContext msgContext, OutputStream out, String soapAction, String requestURI) throws AxisFault {
    if ((soapAction != null) && soapAction.startsWith("\"") && soapAction.endsWith("\"")) {
        soapAction = soapAction.substring(1, soapAction.length() - 1);
    }
    msgContext.setSoapAction(soapAction);
    msgContext.setTo(new EndpointReference(requestURI));
    msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
    msgContext.setServerSide(true);
    msgContext.setDoingREST(true);
    msgContext.setEnvelope(new SOAP11Factory().getDefaultEnvelope());
    msgContext.setProperty(NhttpConstants.NO_ENTITY_BODY, Boolean.TRUE);
    AxisEngine.receive(msgContext);
}
Also used : SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

IOException (java.io.IOException)31 EndpointReference (org.apache.axis2.addressing.EndpointReference)29 AxisFault (org.apache.axis2.AxisFault)27 URL (java.net.URL)21 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)19 URL (org.apache.axis2.util.URL)19 HttpClient (org.apache.http.client.HttpClient)19 Options (org.apache.axis2.client.Options)18 MalformedURLException (java.net.MalformedURLException)17 OMElement (org.apache.axiom.om.OMElement)17 MessageContext (org.apache.axis2.context.MessageContext)16 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)16 ServiceClient (org.apache.axis2.client.ServiceClient)13 SynapseException (org.apache.synapse.SynapseException)13 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)12 AxisService (org.apache.axis2.description.AxisService)10 StringEntity (org.apache.http.entity.StringEntity)9 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)9 Map (java.util.Map)7 JSONObject (org.json.JSONObject)7