Terminating a Call During Processing of an Event
This version of the SDK allows a user to terminate a call during pre or post processing, or in authentication event handling. For example, if the request does not have a required URL parameter,
Local Edition can be configured to terminate the call in the pre-processing.
Note: All the headers, status code and status messages set in the custom processing is returned to the client as part of the response in pre processing and post processing.
Note: All the headers, status code and status messages set in the custom authentication would not be returned as part of response in case of Authentication Event handling (Custom authenticator). If you want to fail authentication request from the custom authenticator, then you need to terminate the call in order to throw "ERR_403_NOT_AUTHORIZED" for a request.
For example, if you want to terminate the call in authenticator, if request doesn't contain the authorization header, then the call can be terminated by marking the response as complete as shown in the following example:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mashery.http.HTTPHeaders; import com.mashery.trafficmanager.debug.DebugContext; import com.mashery.trafficmanager.event.listener.Authenticator; import com.mashery.trafficmanager.event.listener.TrafficEventListener; import com.mashery.trafficmanager.event.model.TrafficEvent; import com.mashery.trafficmanager.event.processor.model.AuthenticationEvent; import com.mashery.trafficmanager.event.processor.model.PostProcessEvent; import com.mashery.trafficmanager.event.processor.model.PreProcessEvent; import com.mashery.trafficmanager.processor.ProcessorBean; import com.mashery.trafficmanager.processor.ProcessorException; @ProcessorBean(enabled = true, name = "CustomAuthentication", immediate = true) public class CustomAuthentication implements TrafficEventListener,Authenticator { @Override public void handleEvent(TrafficEvent event) { try { if (event instanceof AuthenticationEvent) { authenticate((AuthenticationEvent) event); } } catch (ProcessorException e) { } } private void authenticate(AuthenticationEvent event) throws ProcessorException { //For example request doesn't contain the authorization header then user can terminate the call by marking response as complete // in order to thrown 403 ERR_403_NOT_AUTHORIZED for the incoming request. if (headers != null) { String authorization = headers.get(HEADER_AUTHORIZATION); if ((null == authorization || authorization == "") || !authorization.startsWith(AUTH_BASIC)) { debugContext.logEntry("Final Value", "DIY-CUSTOM-AUTH-HEADER-FAILIURE"); event.getCallContext().getResponse().setComplete(); } } }If you want to terminate the call in pre or post processing, refer to the following example:
package com.mashery.processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mashery.trafficmanager.event.listener.TrafficEventListener; import com.mashery.trafficmanager.event.listener.Authenticator; import com.mashery.trafficmanager.event.model.TrafficEvent; import com.mashery.trafficmanager.event.processor.model.PostProcessEvent; import com.mashery.trafficmanager.event.processor.model.PreProcessEvent; import com.mashery.trafficmanager.model.core.ExtendedAttributes; import com.mashery.trafficmanager.processor.ProcessorBean; import com.mashery.trafficmanager.processor.ProcessorException; @ProcessorBean(enabled = true, name = "PrePostProcessing", immediate = true) public class PrePostProcessing implements TrafficEventListener{ private final Logger log = LoggerFactory.getLogger(PrePostProcessing.class); @Override public void handleEvent(TrafficEvent event) { try { if (event instanceof PreProcessEvent) { preProcess((PreProcessEvent) event); } else if (event instanceof PostProcessEvent) { postProcess((PostProcessEvent) event); } } catch (ProcessorException e) { log.error("Exception occurred when handling processor event"); } } //In the below example we checking the query parameter's value to decide whether to terminate the call or not. private void preProcess(PreProcessEvent event) throws ProcessorException { String complete = event.getCallContext().getRequest().getQueryData().get("preComplete"); if (complete != null) { event.getCallContext().getResponse().getHTTPResponse().setBody(new StringContentProducer("{\"response\": \"Terminated the call in pre-processing\"}")); event.getCallContext().getResponse().setComplete(); } } //In the below example we checking the query parameter's value to decide whether to terminate the call or not. private void postProcess(PostProcessEvent event) throws ProcessorException { String complete = event.getCallContext().getRequest().getQueryData().get("postComplete"); if (complete != null) { event.getCallContext().getResponse().getHTTPResponse().setBody(new StringContentProducer("{\"response\": \"Terminated the call in post-processing\"}")); event.getCallContext().getResponse().setComplete(); } } }
Copyright © 2022. Cloud Software Group, Inc. All Rights Reserved.