import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;
import org.slf4j.MDC;

import java.util.UUID;


public class RequestTrackerClientFilter extends ClientFilter {

    @Override
    public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {
        // As getNext() is final in ClientFilter and cannot be stubbed in test classes, all work is
        // performed in the protected doWork method which can be unit tested.
        // That the filter hands off to the next in the chain can only be tested in an integration test
        return getNext().handle(doWork(clientRequest));
    }

    protected ClientRequest doWork(ClientRequest clientRequest) {
        String logId= MDC.get("Request-Tracker");

        if(logId == null) {
            logId = UUID.randomUUID().toString();
        }

        clientRequest.getHeaders().add("X-Request-Tracker", logId);

        return clientRequest;
    }
}

@@ -1,28 +1,108 @@
+import com.google.common.base.Optional;%0Aimport com.google.common.base.Supplier;%0A
 import com.sun.jersey.api.cl
@@ -391,16 +391,211 @@
 Filter %7B
+%0A    private static final Supplier%3CString%3E ID_SUPPLIER = new Supplier%3CString%3E() %7B%0A        @Override%0A        public String get() %7B%0A            return UUID.randomUUID().toString();%0A        %7D%0A    %7D;
 %0A%0A    @O
@@ -1119,22 +1119,59 @@
     
-String log
+Optional%3CString%3E request
 Id
+ 
 = 
+Optional.fromNullable(
 MDC.
@@ -1196,98 +1196,10 @@
 er%22)
+)
 ;
-%0A%0A        if(logId == null) %7B%0A            logId = UUID.randomUUID().toString();%0A        %7D
 %0A%0A  
@@ -1260,13 +1260,33 @@
 r%22, 
-logId
+requestId.or(ID_SUPPLIER)
 );%0A%0A

Use Optional and supplier to match the servlet filter