package org.sagebionetworks.bridge.play.interceptors;

import static org.apache.http.HttpHeaders.USER_AGENT;
import static org.sagebionetworks.bridge.BridgeConstants.METRICS_EXPIRE_SECONDS;
import static org.sagebionetworks.bridge.BridgeConstants.X_FORWARDED_FOR_HEADER;

import java.util.regex.Pattern;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.sagebionetworks.bridge.models.Metrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import play.cache.Cache;
import play.mvc.Http;
import play.mvc.Http.Request;
import play.mvc.Result;

@Component("metricsInterceptor")
public class MetricsInterceptor implements MethodInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(MetricsInterceptor.class);
    
    private static final Pattern PARTICIPANTS_URL = Pattern.compile("(?<=(/participants/))([^/]*)");
    private static final String EMAIL_TOKEN = ":email";

    @Override
    public Object invoke(MethodInvocation method) throws Throwable {
        final Metrics metrics = initMetrics();
        Cache.set(metrics.getCacheKey(), metrics, METRICS_EXPIRE_SECONDS);
        try {
            final Result result = (Result)method.proceed();
            metrics.setStatus(result.toScala().header().status());
            return result;
        } finally {
            Cache.remove(metrics.getCacheKey());
            metrics.end();
            logger.info(metrics.toJsonString());
        }
    }

    Metrics initMetrics() {
        final Request request = Http.Context.current().request();
        final Metrics metrics = new Metrics(RequestUtils.getRequestId(request));
        // The only key we have for users is their email addresses, but it's sensitive. Don't log it.
        String uri = PARTICIPANTS_URL.matcher(request.path()).replaceAll(EMAIL_TOKEN);
        metrics.setMethod(request.method());
        metrics.setUri(uri);
        metrics.setProtocol(request.version());
        metrics.setRemoteAddress(RequestUtils.header(request, X_FORWARDED_FOR_HEADER, request.remoteAddress()));
        metrics.setUserAgent(RequestUtils.header(request, USER_AGENT, null));
        return metrics;
    }
}

@@ -852,170 +852,8 @@
 ss);
-%0A    %0A    private static final Pattern PARTICIPANTS_URL = Pattern.compile(%22(?%3C=(/participants/))(%5B%5E/%5D*)%22);%0A    private static final String EMAIL_TOKEN = %22:email%22;
 %0A%0A  
@@ -1564,197 +1564,8 @@
 ));%0A
-        // The only key we have for users is their email addresses, but it's sensitive. Don't log it.%0A        String uri = PARTICIPANTS_URL.matcher(request.path()).replaceAll(EMAIL_TOKEN);%0A
     
@@ -1632,11 +1632,22 @@
 Uri(
-uri
+request.path()
 );%0A 

Remove scrubbing of email address; we're removing it from URL.