Java Code Examples for org.apache.synapse.MessageContext#setResponse()

The following examples show how to use org.apache.synapse.MessageContext#setResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ODataPassThroughHandler.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public boolean handleRequestInFlow(MessageContext messageContext) {
    try {
        org.apache.axis2.context.MessageContext axis2MessageContext =
                ((Axis2MessageContext) messageContext).getAxis2MessageContext();
        Object isODataService = axis2MessageContext.getProperty("IsODataService");
        // In this if block we are skipping proxy services, inbound related message contexts & api.
        if (axis2MessageContext.getProperty("TransportInURL") != null && isODataService != null) {
            RelayUtils.buildMessage(axis2MessageContext);
            ODataServletRequest request = new ODataServletRequest(axis2MessageContext);
            ODataServletResponse response = new ODataServletResponse(axis2MessageContext);
            ODataEndpoint.process(request, response);
            setContent(axis2MessageContext, response);
            setHeaders(axis2MessageContext, response);
            messageContext.setTo(null);
            messageContext.setResponse(true);
            Axis2Sender.sendBack(messageContext);
        }
        return true;
    } catch (Exception e) {
        this.handleException("Error occurred in integrator handler.", e, messageContext);
        return true;
    }
}
 
Example 2
Source File: Utils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public static void sendFault(MessageContext messageContext, int status) {
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).
            getAxis2MessageContext();

    axis2MC.setProperty(NhttpConstants.HTTP_SC, status);
    messageContext.setResponse(true);
    messageContext.setProperty("RESPONSE", "true");
    messageContext.setTo(null);        
    axis2MC.removeProperty("NO_ENTITY_BODY");

    // Always remove the ContentType - Let the formatter do its thing
    axis2MC.removeProperty(Constants.Configuration.CONTENT_TYPE);
    Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    if (headers != null) {
        headers.remove(HttpHeaders.AUTHORIZATION);

        headers.remove(HttpHeaders.HOST);
    }
    Axis2Sender.sendBack(messageContext);
}
 
Example 3
Source File: Utils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method used to send the response back from the request.
 *
 * @param messageContext messageContext of the request
 * @param status         HTTP Status to return from the response
 */
public static void send(MessageContext messageContext, int status) {
    org.apache.axis2.context.MessageContext axis2MC =
            ((Axis2MessageContext) messageContext).getAxis2MessageContext();
    axis2MC.setProperty(NhttpConstants.HTTP_SC, status);
    messageContext.setResponse(true);
    messageContext.setProperty(SynapseConstants.RESPONSE, "true");
    messageContext.setTo(null);
    axis2MC.removeProperty(Constants.Configuration.CONTENT_TYPE);
    Axis2Sender.sendBack(messageContext);
}
 
Example 4
Source File: CacheMediator.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * Processes a request message through the cache mediator. Generates the request hash and looks up for a hit, if
 * found; then the specified named or anonymous sequence is executed or marks this message as a response and sends
 * back directly to client.
 *
 * @param synCtx incoming request message
 * @param synLog the Synapse log to use
 * @return should this mediator terminate further processing?
 */
private boolean processRequestMessage(MessageContext synCtx, SynapseLog synLog)
        throws ExecutionException {
    if (collector) {
        handleException("Request messages cannot be handled in a collector cache", synCtx);
    }
    org.apache.axis2.context.MessageContext msgCtx =
            ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    String requestHash = null;
    try {
        requestHash = digestGenerator.getDigest(((Axis2MessageContext) synCtx).getAxis2MessageContext());
        synCtx.setProperty(CachingConstants.REQUEST_HASH, requestHash);
    } catch (CachingException e) {
        handleException("Error in calculating the hash value of the request", e, synCtx);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Generated request hash : " + requestHash);
    }
    CachableResponse cachedResponse = getMediatorCache().get(requestHash);
    synCtx.setProperty(CachingConstants.CACHED_OBJECT, cachedResponse);
    //This is used to store the http method of the request.
    String httpMethod = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD);
    cachedResponse.setHttpMethod(httpMethod);
    cachedResponse.setProtocolType(protocolType);
    cachedResponse.setResponseCodePattern(responseCodePattern);
    cachedResponse.setHTTPMethodsToCache(hTTPMethodsToCache);
    cachedResponse.setMaxMessageSize(maxMessageSize);
    cachedResponse.setCacheControlEnabled(cacheControlEnabled);
    cachedResponse.setAddAgeHeaderEnabled(addAgeHeaderEnabled);
    if (cachedResponse.getResponsePayload() != null || cachedResponse.getResponseEnvelope() != null) {
        // get the response from the cache and attach to the context and change the
        // direction of the message
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Cache-hit for message ID : " + synCtx.getMessageID());
        }
        //Validate the response based on max-age and no-cache headers.
        if (CachingConstants.HTTP_PROTOCOL_TYPE.equals(getProtocolType())
                && cachedResponse.isCacheControlEnabled() &&
                HttpCachingFilter.isValidCacheEntry(cachedResponse, synCtx)) {
            return true;
        }
        // mark as a response and replace envelope from cache
        synCtx.setResponse(true);
        replaceEnvelopeWithCachedResponse(synCtx, synLog, msgCtx, cachedResponse);
        return false;
    }
    return true;
}