Search code examples
wso2wso2-api-managerwso2-identity-server

[ WSO2 Api Manager 4.1 / Custom Handlers]


I have developped a custom handler for my apim platform(4.1) where I call a custom class mediator in order to log MessageContext infos into a separated log file as shown below :

public void logUserRequest(MessageContext messageContext) {
    Map headers = service.getTransportHeaders(messageContext);
    String userId = (String) messageContext.getProperty(APIMgtGatewayConstants.USER_ID);
    String api = (String) messageContext.getProperty(APIMgtGatewayConstants.API);
    String apiKeyType = (String) messageContext.getProperty(AM_KEY_TYPE);
    String appliName = (String) messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME);
    String method = (String) messageContext.getProperty(APIMgtGatewayConstants.HTTP_METHOD);
    String organisation = (String) messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_NAME);   
    String version = (String) messageContext.getProperty(APIMgtGatewayConstants.VERSION);

successCallLog.info(userId + "|" + organisation + "|" + apiKeyType  + "|" + method + "|" + api + "|" + version + "|" + appliName );
 }

It works fine when the log mediator is called inside the handleResponse() method of my handler , but the problem with that is that I don't log the exact time of the request and it is used in another flow to calculate other statistics .

06-10-2022 10:49:00,072|admin@carbon.super|test|PRODUCTION|GET|PizzaShackAPI|1.0.0|test

in the other hand, when I call the log mediator inside the handleRequest() method of my handler , I get null values of the MessageContext properties I'm trying to log :

06-10-2022 10:46:34,311|null|null|null|null|null|null|null|null

PS: I didn't have this problem using the same handler and mediator in apim version 3.2 .

Am I missing something ? or Did something change between the 2 versions ?


Solution

  • As @DushaniW explained in his comment, the MessageContext properties were not set at the moment my mediator was trying to log them , since the handler calling the mediator was engaged before the APIAuthenticationHandler, which explains null values.

    I was able to correct the issue by making sure the default handler APIAuthenticationHandler is called before my custom handler.

    I have updated my configuration in velocity_template.xml to engage my custom handler after the foreach loop that engage the default handlers (if my understanding is correct) .

    Error :

    <handlers xmlns="http://ws.apache.org/ns/synapse">
    
    <!-- ############## CUSTOM : handler X-Correlation-ID ############### -->
    
    <handler class="fr.XX.XX.XX.XX.handler.CustomAPIAuthenticationXCorrelationIDHandler"/>
    
    <!-- ############## CUSTOM : handler X-Correlation-ID ############### -->
    
    <!-- ############## Default handlers ############### -->
    
    #foreach($handler in $handlers)
            <handler xmlns="http://ws.apache.org/ns/synapse" class="$handler.className">
                    #if($handler.hasProperties())
                            #set ($map = $handler.getProperties() )
                            #foreach($property in $map.entrySet())
                                            <property name="$!property.key" value="$!property.value"/>
                            #end
                    #end
            </handler>
    #end
    
    

    Correction :

    <handlers xmlns="http://ws.apache.org/ns/synapse">
    
    <!-- ############## Default handlers ############### -->
    
    #foreach($handler in $handlers)
            <handler xmlns="http://ws.apache.org/ns/synapse" class="$handler.className">
                    #if($handler.hasProperties())
                            #set ($map = $handler.getProperties() )
                            #foreach($property in $map.entrySet())
                                            <property name="$!property.key" value="$!property.value"/>
                            #end
                    #end
            </handler>
    #end
    
    <!-- ############## CUSTOM : handler X-Correlation-ID ############### -->
    
    <handler class="fr.XX.XX.XX.XX.handler.CustomAPIAuthenticationXCorrelationIDHandler"/>
    
    <!-- ############## CUSTOM : handler X-Correlation-ID ############### -->