use of org.wso2.carbon.apimgt.impl.dto.WebhooksDTO in project carbon-apimgt by wso2.
the class SubscriberInfoLoader method handleThrottle.
private boolean handleThrottle(WebhooksDTO subscriber, MessageContext messageContext) {
AuthenticationContext authContext = new AuthenticationContext();
populateAuthContext(subscriber.getTenantDomain(), Integer.parseInt(subscriber.getAppID()), authContext);
messageContext.setProperty(APISecurityUtils.API_AUTH_CONTEXT, authContext);
if (subscriber.isThrottled()) {
if (APIUtil.isAnalyticsEnabled()) {
String errorMessage = "Message throttled out";
String errorDescription = "You have exceeded your quota";
int errorCode = APIThrottleConstants.EVENTS_COUNT_THROTTLE_OUT_ERROR_CODE;
messageContext.setProperty(SynapseConstants.ERROR_CODE, errorCode);
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, errorMessage);
messageContext.setProperty(SynapseConstants.ERROR_DETAIL, errorDescription);
messageContext.setProperty(Constants.BACKEND_RESPONSE_CODE, APIThrottleConstants.SC_TOO_MANY_REQUESTS);
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(SynapseConstants.HTTP_SC, APIThrottleConstants.SC_TOO_MANY_REQUESTS);
WebhooksUtils.publishAnalyticsData(messageContext);
}
return false;
}
if (doThrottle(subscriber, messageContext, authContext)) {
messageContext.setProperty(APIConstants.Webhooks.SUBSCRIBER_CALLBACK_PROPERTY, subscriber.getCallbackURL());
String errorMessage = "Message throttled out";
String errorDescription = "You have exceeded your quota";
int errorCode = APIThrottleConstants.EVENTS_COUNT_THROTTLE_OUT_ERROR_CODE;
int httpErrorCode = APIThrottleConstants.SC_TOO_MANY_REQUESTS;
messageContext.setProperty(SynapseConstants.ERROR_CODE, errorCode);
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, errorMessage);
messageContext.setProperty(SynapseConstants.ERROR_DETAIL, errorDescription);
messageContext.setProperty(APIMgtGatewayConstants.HTTP_RESPONSE_STATUS_CODE, httpErrorCode);
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 (APIUtil.isAnalyticsEnabled()) {
messageContext.setProperty(Constants.BACKEND_RESPONSE_CODE, httpErrorCode);
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(SynapseConstants.HTTP_SC, APIThrottleConstants.SC_TOO_MANY_REQUESTS);
WebhooksUtils.publishAnalyticsData(messageContext);
}
Mediator sequence = messageContext.getSequence(APIThrottleConstants.API_THROTTLE_OUT_HANDLER);
// Invoke the custom error handler specified by the user
if (sequence != null && !sequence.mediate(messageContext)) {
// logic from getting executed
return true;
}
Utils.sendFault(messageContext, httpErrorCode);
}
return true;
}
Aggregations