package com.amee.restlet;

import org.restlet.Filter;
import org.restlet.data.Request;
import org.restlet.data.Response;

/**
 * Filter to manage {@link RequestContext}s. Will create the {@link RequestContext} for the transaction log.
 */
public class AMEERequestFilter extends Filter {

    protected int doHandle(Request request, Response response) {
        RequestContext requestContext = new RequestContext();
        try {
            // Create the RequestContext.
            requestContext.setRequest(request);
            request.getAttributes().put("requestContext", requestContext);
            // Delegate request handling to the super class.
            return super.doHandle(request, response);
        } catch (Throwable t) {
            // Record the exception in the RequestContext.
            requestContext.setError(t.getMessage());
            requestContext.error();
            // Wrap and re-throw the exception.
            throw new RuntimeException(t);
        } finally {
            // Update and record the RequestContext at the end of the request.
            requestContext.setStatus(response.getStatus());
            requestContext.record();
        }
    }
}
@@ -113,16 +113,38 @@
 esponse;
+%0Aimport org.slf4j.MDC;
 %0A%0A/**%0A *
@@ -440,24 +440,25 @@
       try %7B%0A
+%0A
             
@@ -606,24 +606,152 @@
 stContext);%0A
+%0A            // Add the client IP address for logging.%0A            MDC.put(%22ipAddress%22, request.getClientInfo().getAddress());%0A%0A
             
@@ -881,24 +881,25 @@
 owable t) %7B%0A
+%0A
             
@@ -1030,24 +1030,25 @@
 xt.error();%0A
+%0A
             
@@ -1146,16 +1146,17 @@
 nally %7B%0A
+%0A
         
@@ -1282,24 +1282,24 @@
 tStatus());%0A
-
             
@@ -1319,24 +1319,158 @@
 t.record();%0A
+            MDC.remove(%22ipAddress%22);%0A%0A            // User UID is added in CollectCredentialsGuard.%0A            MDC.remove(%22userUid%22);%0A
         %7D%0A  

Add client IP address to MDC for logging. PL-11395.