Tuesday, March 1, 2016

Enabling request/response logging WSO2 API Manager REST API

For debugging purposes sometimes it might be useful to add request/response logging for the REST API webapps. For that you can add the following configuration to beans.xml under <cxf:bus> element.

<cxf:features>
    <cxf:logging/>
</cxf:features>
beans.xml can be found in following path: 

<APIM-Home>/repository/deployment/server/webapps/{webapp-name}/WEB-INF

After Updating beans.xml it will look like following.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cxf="http://cxf.apache.org/core"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <context:property-placeholder/>
    <context:annotation-config/>
    <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"/>
    <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>
    <jaxrs:server id="services" address="/">
        <jaxrs:serviceBeans>
            <bean class="org.wso2.carbon.apimgt.rest.api.publisher.SubscriptionsApi"/>
            <bean class="org.wso2.carbon.apimgt.rest.api.publisher.ApisApi"/>
            <bean class="org.wso2.carbon.apimgt.rest.api.publisher.EnvironmentsApi"/>
            <bean class="org.wso2.carbon.apimgt.rest.api.publisher.TiersApi"/>
            <bean class="org.wso2.carbon.apimgt.rest.api.publisher.ApplicationsApi"/>
            
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <!--<bean class="org.wso2.carbon.apimgt.rest.api.util.handlers.BasicAuthenticationHandler"/>
            <bean class="org.wso2.carbon.apimgt.rest.api.util.handlers.XACMLAuthenticationHandler"/>-->

            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
            <bean class="org.wso2.carbon.apimgt.rest.api.util.exception.GlobalThrowableMapper" />
        </jaxrs:providers>
        <jaxrs:properties>
            <!-- This is added to catch interceptor level exceptions in GlobalThrowableMapper. -->
            <entry key="map.cxf.interceptor.fault" value="true" />
        </jaxrs:properties>
    </jaxrs:server>
    <bean id="AuthenticationInterceptor" class="org.wso2.carbon.apimgt.rest.api.util.interceptors.auth.OAuthAuthenticationInterceptor" />
    <bean id="ValidationInInterceptor" class="org.wso2.carbon.apimgt.rest.api.util.interceptors.validation.ValidationInInterceptor"/>
    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="AuthenticationInterceptor"/>
            <ref bean="ValidationInInterceptor"/>
        </cxf:inInterceptors>
        <cxf:features>
            <cxf:logging />
        </cxf:features>
    </cxf:bus>
</beans>
Example message logging you can see in server's console:
[2015-12-21 12:20:14,268]  INFO - LoggingInInterceptor Inbound Message
----------------------------
ID: 3
Address: http://127.0.0.1:9764/api/am/store/v0.9//tiers/api/Bronze
Http-Method: GET
Content-Type: 
Headers: {accept-encoding=[gzip,deflate], Authorization=[Bearer 28603e8e342069446d3cc1b93b71dc46], connection=[Keep-Alive], Content-Type=[null], host=[127.0.0.1:9764], user-agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
--------------------------------------
[2015-12-21 12:20:14,342]  INFO - LoggingOutInterceptor Outbound Message
---------------------------
ID: 3
Response-Code: 200
Content-Type: application/json
Headers: {Content-Type=[application/json], Date=[Mon, 21 Dec 2015 06:50:14 GMT]}
Payload: {"unitTime":60000,"tierPlan":"FREE","tierLevel":"api","stopOnQuotaReach":true,"requestCount":1,"description":"Allows 1 request(s) per minute.","name":"Bronze","attributes":{}}
--------------------------------------

2 comments:

  1. For API Manager 2.5.0, we need to add slf4j-api-1.x.jar as well to the lib folder.

    ReplyDelete
  2. Spinomenal review 2021 - Casino site, bonuses, odds
    Spinomenal online casino offers a safe way to experience the online games from the highest quality software provider. Our review goes into detail  Rating: 4 · 카지노사이트luckclub ‎Review by LuckyClub

    ReplyDelete