/*
 * Copyright (c) 2006-2012 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ambraproject.admin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * Logs attributes of incoming HTTP requests.
 */
public class LoggingInterceptor extends HandlerInterceptorAdapter {

  private static final Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (log.isDebugEnabled()) {
      log.debug(describe(request));
    }
    return super.preHandle(request, response, handler);
  }

  private static String describe(HttpServletRequest request) {
    StringBuilder message = new StringBuilder();
    message.append(request.getMethod()).append(' ').append(request.getRequestURI());
    String queryString = request.getQueryString();
    if (queryString != null) {
      message.append(" ? ").append(queryString);
    }
    message.append('\n');
    for (Enumeration headerNames = request.getHeaderNames(); headerNames.hasMoreElements(); ) {
      String headerName = (String) headerNames.nextElement();
      String headerValue = request.getHeader(headerName);
      message.append("\t\"").append(headerName).append("\": \"").append(headerValue).append('\"').append('\n');
    }
    return message.toString();
  }

}

@@ -701,24 +701,146 @@
 ontroller;%0A%0A
+import com.google.common.base.Function;%0Aimport com.google.common.base.Joiner;%0Aimport com.google.common.collect.Iterators;%0A
 import org.s
@@ -848,24 +848,24 @@
 f4j.Logger;%0A
-
 import org.s
@@ -955,24 +955,58 @@
 orAdapter;%0A%0A
+import javax.annotation.Nullable;%0A
 import javax
@@ -1115,16 +1115,43 @@
 eration;
+%0Aimport java.util.Iterator;
 %0A%0A/**%0A *
@@ -1622,16 +1622,915 @@
 );%0A  %7D%0A%0A
+  private static final Function%3CString, String%3E LITERAL = new Function%3CString, String%3E() %7B%0A    @Override%0A    public String apply(@Nullable String input) %7B%0A      if (input == null) %7B%0A        return String.valueOf(null);%0A      %7D%0A      /*%0A       * TODO: Use org.apache.commons.lang.StringEscapeUtils?%0A       * StringEscapeUtils is a little funny with forward-slashes, which makes these values hard to read. So for now,%0A       * just handle some simple cases by hand. The returned values are not guaranteed to be parsable in any particular%0A       * context; this is just for human readability.%0A       */%0A      input = input.replace(%22%5C%22%22, %22%5C%5C%5C%22%22).replace(%22%5C%5C%22, %22%5C%5C%5C%5C%22);%0A      return '%22' + input + '%22';%0A    %7D%0A  %7D;%0A%0A  private static final Joiner JOINER = Joiner.on(%22, %22);%0A%0A  /**%0A   * Build a message describing a request.%0A   *%0A   * @param request the request to describe%0A   * @return the description%0A   */%0A
   privat
@@ -2876,24 +2876,57 @@
 ppend('%5Cn');
+%0A%0A    // Append a list of headers
 %0A    for (En
@@ -3082,26 +3082,110 @@
     
+message.append('%5Ct').append(LITERAL.apply(headerName)).append(%22: %22);%0A      Enumeration%3C
 String
+%3E
  header
-Value
+s
  = r
@@ -3200,16 +3200,17 @@
 etHeader
+s
 (headerN
@@ -3225,98 +3225,153 @@
     
-message.append(%22%5Ct%5C%22%22).append(headerName).append(%22%5C%22: %5C%22%22).append(headerValue).append('%5C%22'
+Iterator%3CString%3E headerStrings = Iterators.transform(Iterators.forEnumeration(headers), LITERAL);%0A      message.append(JOINER.join(headerStrings)
 ).ap
@@ -3380,32 +3380,33 @@
 nd('%5Cn');%0A    %7D%0A
+%0A
     return messa

Handle possible multiple headers in HTTP logging