Java Code Examples for org.apache.synapse.transport.passthru.util.RelayUtils#consumeAndDiscardMessage()

The following examples show how to use org.apache.synapse.transport.passthru.util.RelayUtils#consumeAndDiscardMessage() . 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: APIAuthenticationHandler.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
private void handleAuthFailure(MessageContext messageContext, APISecurityException e) {
    messageContext.setProperty(SynapseConstants.ERROR_CODE, e.getErrorCode());
    messageContext.setProperty(SynapseConstants.ERROR_MESSAGE,
            APISecurityConstants.getAuthenticationFailureMessage(e.getErrorCode()));
    messageContext.setProperty(SynapseConstants.ERROR_EXCEPTION, e);

    Mediator sequence = messageContext.getSequence(APISecurityConstants.API_AUTH_FAILURE_HANDLER);

    //Setting error description which will be available to the handler
    String errorDetail = APISecurityConstants.getFailureMessageDetailDescription(e.getErrorCode(), e.getMessage());
    // if custom auth header is configured, the error message should specify its name instead of default value
    if (e.getErrorCode() == APISecurityConstants.API_AUTH_MISSING_CREDENTIALS) {
        errorDetail =
                APISecurityConstants.getFailureMessageDetailDescription(e.getErrorCode(), e.getMessage()) + "'"
                        + authorizationHeader + " : Bearer ACCESS_TOKEN' or '" + authorizationHeader +
                        " : Basic ACCESS_TOKEN' or 'apikey: API_KEY'" ;
    }
    messageContext.setProperty(SynapseConstants.ERROR_DETAIL, errorDetail);

    // By default we send a 401 response back
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).
            getAxis2MessageContext();
    // This property need to be set to avoid sending the content in pass-through pipe (request message)
    // as the response.
    axis2MC.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
    try {
        RelayUtils.consumeAndDiscardMessage(axis2MC);
    } catch (AxisFault axisFault) {
        //In case of an error it is logged and the process is continued because we're setting a fault message in the payload.
        log.error("Error occurred while consuming and discarding the message", axisFault);
    }
    axis2MC.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/soap+xml");
    int status;
    if (e.getErrorCode() == APISecurityConstants.API_AUTH_GENERAL_ERROR ||
            e.getErrorCode() == APISecurityConstants.API_AUTH_MISSING_OPEN_API_DEF) {
        status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    } else if (e.getErrorCode() == APISecurityConstants.API_AUTH_INCORRECT_API_RESOURCE ||
            e.getErrorCode() == APISecurityConstants.API_AUTH_FORBIDDEN ||
            e.getErrorCode() == APISecurityConstants.INVALID_SCOPE) {
        status = HttpStatus.SC_FORBIDDEN;
    } else {
        status = HttpStatus.SC_UNAUTHORIZED;
        Map<String, String> headers =
                (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        if (headers != null) {
            headers.put(HttpHeaders.WWW_AUTHENTICATE, getAuthenticatorsChallengeString() +
                    ", error=\"invalid_token\"" +
                    ", error_description=\"The access token expired\"");
            axis2MC.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headers);
        }
    }

    messageContext.setProperty(APIMgtGatewayConstants.HTTP_RESPONSE_STATUS_CODE, status);
    // Invoke the custom error handler specified by the user
    if (sequence != null && !sequence.mediate(messageContext)) {
        // If needed user should be able to prevent the rest of the fault handling
        // logic from getting executed
        return;
    }

    if (messageContext.isDoingPOX() || messageContext.isDoingGET()) {
        setFaultPayload(messageContext, e);
    } else {
        setSOAPFault(messageContext, e);
    }
    sendFault(messageContext, status);
}
 
Example 2
Source File: APIThrottleHandler.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
private void handleThrottleOut(MessageContext messageContext) {

        String errorMessage = null;
        String errorDescription = null;
        int errorCode = -1;
        int httpErrorCode = -1;

        if (APIThrottleConstants.HARD_LIMIT_EXCEEDED.equals(
                messageContext.getProperty(APIThrottleConstants.THROTTLED_OUT_REASON))) {
            errorCode = APIThrottleConstants.HARD_LIMIT_EXCEEDED_ERROR_CODE;
            errorMessage = "API Limit Reached";
            errorDescription = "API not accepting requests";
            // It it's a hard limit exceeding, we tell it as service not being available.
            httpErrorCode = HttpStatus.SC_SERVICE_UNAVAILABLE;
        } else if (APIThrottleConstants.API_LIMIT_EXCEEDED
                .equals(messageContext.getProperty(APIThrottleConstants.THROTTLED_OUT_REASON))) {
            errorCode = APIThrottleConstants.API_THROTTLE_OUT_ERROR_CODE;
            errorMessage = "Message throttled out";
            // By default we send a 429 response back
            httpErrorCode = APIThrottleConstants.SC_TOO_MANY_REQUESTS;
            errorDescription = "You have exceeded your quota";
        } else if (APIThrottleConstants.RESOURCE_LIMIT_EXCEEDED
                .equals(messageContext.getProperty(APIThrottleConstants.THROTTLED_OUT_REASON))) {
            errorCode = APIThrottleConstants.RESOURCE_THROTTLE_OUT_ERROR_CODE;
            errorMessage = "Message throttled out";
            // By default we send a 429 response back
            httpErrorCode = APIThrottleConstants.SC_TOO_MANY_REQUESTS;
            errorDescription = "You have exceeded your quota";
        } else {
            errorCode = APIThrottleConstants.APPLICATION_THROTTLE_OUT_ERROR_CODE;
            errorMessage = "Message throttled out";
            // By default we send a 429 response back
            httpErrorCode = APIThrottleConstants.SC_TOO_MANY_REQUESTS;
            errorDescription = "You have exceeded your quota";
        }

        messageContext.setProperty(SynapseConstants.ERROR_CODE, errorCode);
        messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, errorMessage);
        Mediator sequence = messageContext.getSequence(APIThrottleConstants.API_THROTTLE_OUT_HANDLER);

        // Invoke the custom error handler specified by the user
        if (sequence != null && !sequence.mediate(messageContext)) {
            // If needed user should be able to prevent the rest of the fault handling
            // logic from getting executed
            return;
        }
        org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).
                getAxis2MessageContext();
        // This property need to be set to avoid sending the content in pass-through pipe (request message)
        // as the response.
        axis2MC.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
        try {
            RelayUtils.consumeAndDiscardMessage(axis2MC);
        } catch (AxisFault axisFault) {
            //In case of an error it is logged and the process is continued because we're setting a fault message in the payload.
            log.error("Error occurred while consuming and discarding the message", axisFault);
        }

        if (messageContext.isDoingPOX() || messageContext.isDoingGET()) {
            Utils.setFaultPayload(messageContext, getFaultPayload(errorCode, errorMessage, errorDescription));
        } else {
            setSOAPFault(messageContext, errorMessage, errorDescription);
        }

        sendFault(messageContext, httpErrorCode);
    }