package mil.dds.anet.views;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.HttpHeaders;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.security.Principal;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import mil.dds.anet.config.AnetConfiguration;
import org.eclipse.jetty.security.DefaultUserIdentity;
import org.eclipse.jetty.security.UserAuthentication;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.UserIdentity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ViewResponseFilter implements ContainerResponseFilter {

  private static final Logger logger =
      LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  @Context
  HttpServletRequest request;

  AnetConfiguration config;
  private static final Set<MediaType> uncachedMediaTypes =
      ImmutableSet.of(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_HTML_TYPE);

  public ViewResponseFilter(AnetConfiguration config) {
    this.config = config;
  }

  @Override
  public void filter(ContainerRequestContext requestContext,
      ContainerResponseContext responseContext) throws IOException {
    // Don't cache requests other than GET, and don't cache selected media types
    final MediaType mediaType = responseContext.getMediaType();
    if (!HttpMethod.GET.equals(requestContext.getMethod()) || mediaType == null
        || uncachedMediaTypes.stream().anyMatch(mt -> mt.equals(mediaType))) {
      responseContext.getHeaders().put(HttpHeaders.CACHE_CONTROL,
          ImmutableList.of("no-store, no-cache, must-revalidate, post-check=0, pre-check=0"));
      responseContext.getHeaders().put(HttpHeaders.PRAGMA, ImmutableList.of("no-cache"));
      // Only POST methods should be logged , otherwise GET methods results with exception
      if (!HttpMethod.GET.equals(requestContext.getMethod())) {
        if (requestContext.getSecurityContext().getUserPrincipal() != null) {
          // This code snippet resolves the situation that caused empty user information to be
          // written to the access.log file
          // Start
          Principal userPrincipal = new Principal() {
            @Override
            public String getName() {
              final SecurityContext secContext = requestContext.getSecurityContext();
              Principal p = secContext.getUserPrincipal();
              return p.getName();
            }
          };
          UserIdentity userId = new DefaultUserIdentity(null, userPrincipal, null);
          Request baseRequest = Request.getBaseRequest(request);
          baseRequest.setAuthentication(new UserAuthentication(null, userId));
          // End

          // Log necessary information into userActivities.log file
          logger.info("\"ip\": \"{}\" , \"user\": \"{}\" , \"referer\": \"{}\"",
              request.getRemoteAddr(), userPrincipal.getName(),
              requestContext.getHeaderString("referer"));
        }
      }
    } else {
      responseContext.getHeaders().put(HttpHeaders.CACHE_CONTROL,
          ImmutableList.of("max-age=259200, public"));
    }
  }

}

@@ -1531,27 +1531,8 @@
 ext)
- throws IOException
  %7B%0A 
@@ -2098,89 +2098,68 @@
  // 
-Only POST methods should be logged , otherwise GET methods results with exception
+Log only requests other than GET and if userPrincipal exists
 %0A   
@@ -2215,19 +2215,16 @@
 ethod())
-) %7B
 %0A       
@@ -2224,20 +2224,21 @@
         
-if (
+  && 
 requestC
@@ -2291,26 +2291,24 @@
  != null) %7B%0A
-  
         // T
@@ -2396,18 +2396,16 @@
         
- 
- 
 // writt
@@ -2438,18 +2438,16 @@
         
-  
 // Start
@@ -2447,18 +2447,16 @@
 / Start%0A
-  
         
@@ -2509,18 +2509,16 @@
         
-  
 @Overrid
@@ -2519,18 +2519,16 @@
 verride%0A
-  
         
@@ -2567,18 +2567,16 @@
         
-  
 final Se
@@ -2647,26 +2647,24 @@
             
-  
 Principal p 
@@ -2708,18 +2708,16 @@
         
- 
- 
 return p
@@ -2734,26 +2734,24 @@
 ;%0A          
-  
 %7D%0A          
@@ -2752,15 +2752,11 @@
     
- 
- 
 %7D;%0A
-  
     
@@ -2833,18 +2833,16 @@
  null);%0A
-  
         
@@ -2904,18 +2904,16 @@
         
-  
 baseRequ
@@ -2969,26 +2969,24 @@
 , userId));%0A
-  
         // E
@@ -2993,26 +2993,24 @@
 nd%0A%0A        
-  
 // Log neces
@@ -3051,26 +3051,24 @@
 es.log file%0A
-  
         logg
@@ -3142,26 +3142,24 @@
             
- 
- 
 request.getR
@@ -3204,26 +3204,24 @@
             
-  
 requestConte
@@ -3252,26 +3252,16 @@
 rer%22));%0A
-        %7D%0A
       %7D%0A

Simplify code