Search in sources :

Example 6 with EvaluatorContext

use of org.apache.synapse.commons.evaluators.EvaluatorContext in project wso2-synapse by wso2.

the class ConditionalRouterMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
    org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
    Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    Map<String, String> evaluatorHeaders = new HashMap<String, String>();
    if (headers != null && headers instanceof Map) {
        Map headersMap = (Map) headers;
        for (Object entryObj : headersMap.entrySet()) {
            Map.Entry entry = (Map.Entry) entryObj;
            if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
                evaluatorHeaders.put((String) entry.getKey(), (String) entry.getValue());
            }
        }
    }
    String url = synCtx.getTo().getAddress();
    EvaluatorContext context = new EvaluatorContext(url, evaluatorHeaders);
    context.setProperties(((Axis2MessageContext) synCtx).getProperties());
    context.setMessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext());
    try {
        for (ConditionalRoute conditionalRoute : conditionalRoutes) {
            if (conditionalRoute.getEvaluator().evaluate(context)) {
                conditionalRoute.getTarget().mediate(synCtx);
                if (conditionalRoute.isBreakRoute()) {
                    break;
                }
            }
        }
    } catch (EvaluatorException ee) {
        handleException("Couldn't evaluate the route condition", ee, synCtx);
    }
    return continueAfter;
}
Also used : HashMap(java.util.HashMap) EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext) EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException) Map(java.util.Map) HashMap(java.util.HashMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 7 with EvaluatorContext

use of org.apache.synapse.commons.evaluators.EvaluatorContext in project wso2-synapse by wso2.

the class ServerHandler method requestReceived.

/**
 * Process a new incoming request
 * @param conn the connection
 */
public void requestReceived(final NHttpServerConnection conn) {
    HttpContext context = conn.getContext();
    context.setAttribute(NhttpConstants.REQ_ARRIVAL_TIME, System.currentTimeMillis());
    context.setAttribute(NhttpConstants.REQ_FROM_CLIENT_READ_START_TIME, System.currentTimeMillis());
    HttpRequest request = conn.getHttpRequest();
    context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
    context.setAttribute(NhttpConstants.MESSAGE_IN_FLIGHT, "true");
    if (isMessageSizeValidationEnabled) {
        context.setAttribute(NhttpConstants.MESSAGE_SIZE_VALIDATION_SUM, 0);
    }
    // prepare to collect debug information
    conn.getContext().setAttribute(ServerHandler.SERVER_CONNECTION_DEBUG, new ServerConnectionDebug(conn));
    NHttpConfiguration cfg = NHttpConfiguration.getInstance();
    try {
        InputStream is;
        // Only create an input buffer and ContentInputStream if the request has content
        if (request instanceof HttpEntityEnclosingRequest) {
            // Mark request as not yet fully read, to detect timeouts from harmless keepalive deaths
            conn.getContext().setAttribute(NhttpConstants.REQUEST_READ, Boolean.FALSE);
            ContentInputBuffer inputBuffer = new SharedInputBuffer(cfg.getBufferSize(), conn, allocator);
            context.setAttribute(REQUEST_SINK_BUFFER, inputBuffer);
            is = new ContentInputStream(inputBuffer);
        } else {
            is = null;
            conn.getContext().removeAttribute(NhttpConstants.REQUEST_READ);
        }
        ContentOutputBuffer outputBuffer = new NhttpSharedOutputBuffer(bufferSize, conn, allocator, socketTimeout);
        context.setAttribute(RESPONSE_SOURCE_BUFFER, outputBuffer);
        OutputStream os = new ContentOutputStream(outputBuffer);
        // create the default response to this request
        ProtocolVersion httpVersion = request.getRequestLine().getProtocolVersion();
        HttpResponse response = responseFactory.newHttpResponse(httpVersion, HttpStatus.SC_OK, context);
        // create a basic HttpEntity using the source channel of the response pipe
        BasicHttpEntity entity = new BasicHttpEntity();
        if (httpVersion.greaterEquals(HttpVersion.HTTP_1_1)) {
            entity.setChunked(true);
        }
        response.setEntity(entity);
        if (metrics != null) {
            metrics.incrementMessagesReceived();
        }
        // hand off processing of the request to a thread off the pool
        ServerWorker worker = new ServerWorker(cfgCtx, scheme.getName(), metrics, conn, this, request, is, response, os, listenerContext.isRestDispatching(), listenerContext.getHttpGetRequestProcessor());
        if (workerPool != null) {
            workerPool.execute(worker);
        } else if (executor != null) {
            Map<String, String> headers = new HashMap<String, String>();
            for (Header header : request.getAllHeaders()) {
                headers.put(header.getName(), header.getValue());
            }
            EvaluatorContext evaluatorContext = new EvaluatorContext(request.getRequestLine().getUri(), headers);
            int priority = parser.parse(evaluatorContext);
            executor.execute(worker, priority);
        }
        // See if the client expects a 100-Continue
        Header expect = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
        if (expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue())) {
            HttpResponse ack = new BasicHttpResponse(request.getProtocolVersion(), HttpStatus.SC_CONTINUE, "Continue");
            conn.submitResponse(ack);
            if (log.isDebugEnabled()) {
                log.debug(conn + ": Expect :100 Continue hit, sending ack back to the server");
            }
            return;
        }
    } catch (Exception e) {
        if (metrics != null) {
            metrics.incrementFaultsReceiving();
        }
        handleException("Error processing request received for : " + request.getRequestLine().getUri(), e, conn);
    }
}
Also used : ServerConnectionDebug(org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug) EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext) ContentInputStream(org.apache.http.nio.entity.ContentInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream) IOException(java.io.IOException) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ContentInputStream(org.apache.http.nio.entity.ContentInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with EvaluatorContext

use of org.apache.synapse.commons.evaluators.EvaluatorContext in project wso2-synapse by wso2.

the class RewriteRule method rewrite.

public void rewrite(URIFragments fragments, MessageContext messageContext) throws URISyntaxException {
    if (condition != null) {
        String uriString = fragments.toURIString();
        Map<String, String> headers = getHeaders(messageContext);
        EvaluatorContext ctx = new EvaluatorContext(uriString, headers);
        ctx.setProperties(((Axis2MessageContext) messageContext).getProperties());
        ctx.setMessageContext(((Axis2MessageContext) messageContext).getAxis2MessageContext());
        if (log.isTraceEnabled()) {
            log.trace("Evaluating condition with URI: " + uriString);
        }
        try {
            if (!condition.evaluate(ctx)) {
                if (log.isTraceEnabled()) {
                    log.trace("Condition evaluated to 'false' - Skipping the current action");
                }
                return;
            }
            if (log.isTraceEnabled()) {
                log.trace("Condition evaluated to 'true' - Performing the stated action");
            }
        } catch (EvaluatorException e) {
            log.warn("Error while evaluating the condition - Skipping the rule as it failed", e);
            return;
        }
    }
    for (RewriteAction action : actions) {
        action.execute(fragments, messageContext);
    }
}
Also used : EvaluatorContext(org.apache.synapse.commons.evaluators.EvaluatorContext) EvaluatorException(org.apache.synapse.commons.evaluators.EvaluatorException)

Aggregations

EvaluatorContext (org.apache.synapse.commons.evaluators.EvaluatorContext)8 HashMap (java.util.HashMap)4 Map (java.util.Map)2 EvaluatorException (org.apache.synapse.commons.evaluators.EvaluatorException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 OMElement (org.apache.axiom.om.OMElement)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPFactory (org.apache.axiom.soap.SOAPFactory)1 MessageContext (org.apache.axis2.context.MessageContext)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)1 ContentInputStream (org.apache.http.nio.entity.ContentInputStream)1 ContentOutputStream (org.apache.http.nio.entity.ContentOutputStream)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 ServerConnectionDebug (org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug)1