/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.shindig.common.servlet;

import org.apache.shindig.common.Pair;
import org.apache.shindig.common.util.DateUtil;
import org.apache.shindig.common.util.TimeSource;

import com.google.common.collect.Lists;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Collection of HTTP utilities
 */
public final class HttpUtil {
  private HttpUtil() {}
  
  // 1 year.
  private static int defaultTtl = 60 * 60 * 24 * 365;

  private static TimeSource timeSource;

  static {
    setTimeSource(new TimeSource());
  }

  public static void setTimeSource(TimeSource timeSource) {
    HttpUtil.timeSource = timeSource;
  }

  /**
   * Sets HTTP headers that instruct the browser to cache content. Implementations should take care
   * to use cache-busting techniques on the url if caching for a long period of time.
   *
   * @param response The HTTP response
   */
  public static void setCachingHeaders(HttpServletResponse response) {
    setCachingHeaders(response, defaultTtl, false);
  }

  /**
   * Sets HTTP headers that instruct the browser to cache content. Implementations should take care
   * to use cache-busting techniques on the url if caching for a long period of time.
   *
   * @param response The HTTP response
   * @param noProxy True if you don't want the response to be cacheable by proxies.
   */
  public static void setCachingHeaders(HttpServletResponse response, boolean noProxy) {
    setCachingHeaders(response, defaultTtl, noProxy);
  }

  /**
   * Sets HTTP headers that instruct the browser to cache content. Implementations should take care
   * to use cache-busting techniques on the url if caching for a long period of time.
   *
   * @param response The HTTP response
   * @param ttl The time to cache for, in seconds. If 0, then insure that
   *            this object is not cached.
   */
  public static void setCachingHeaders(HttpServletResponse response, int ttl) {
    setCachingHeaders(response, ttl, false);
  }
  
  public static void setNoCache(HttpServletResponse response) {
    setCachingHeaders(response, 0, false);
  }

  /**
   * Sets HTTP headers that instruct the browser to cache content. Implementations should take care
   * to use cache-busting techniques on the url if caching for a long period of time.
   *
   * @param response The HTTP response
   * @param ttl The time to cache for, in seconds. If 0, then insure that
   *            this object is not cached.
   * @param noProxy True if you don't want the response to be cacheable by proxies.
   */
  public static void setCachingHeaders(HttpServletResponse response, int ttl, boolean noProxy) {
    for (Pair<String, String> header : getCachingHeadersToSet(ttl, noProxy)) {
      response.setHeader(header.one, header.two);
    }
  }
  
  public static List<Pair<String, String>> getCachingHeadersToSet(int ttl, boolean noProxy) {
    List<Pair<String, String>> cachingHeaders = Lists.newArrayListWithExpectedSize(3);
    cachingHeaders.add(Pair.of("Expires",
        DateUtil.formatRfc1123Date(timeSource.currentTimeMillis() + (1000L * ttl))));

    if (ttl <= 0) {
      cachingHeaders.add(Pair.of("Pragma", "no-cache"));
      cachingHeaders.add(Pair.of("Cache-Control", "no-cache"));
    } else {
      if (noProxy) {
        cachingHeaders.add(Pair.of("Cache-Control", "private,max-age=" + Integer.toString(ttl)));
      } else {
        cachingHeaders.add(Pair.of("Cache-Control", "public,max-age=" + Integer.toString(ttl)));
      }
    }
    
    return cachingHeaders;
  }

  public static int getDefaultTtl() {
    return defaultTtl;
  }

  public static void setDefaultTtl(int defaultTtl) {
    HttpUtil.defaultTtl = defaultTtl;
  }


  static final Pattern GET_REQUEST_CALLBACK_PATTERN = Pattern.compile("[A-Za-z_][A-Za-z0-9_\\.]+");

  public static boolean isJSONP(HttpServletRequest request) throws IllegalArgumentException {
    String callback = request.getParameter("callback");

    // Must be a GET
    if (!"GET".equals(request.getMethod()))
      return false;
    
    // No callback specified
    if (callback == null) return false;

    if (!GET_REQUEST_CALLBACK_PATTERN.matcher(callback).matches()) {
       throw new IllegalArgumentException("Wrong format for parameter 'callback' specified. Must match: " +
            GET_REQUEST_CALLBACK_PATTERN.toString());
    }
    return true;
  }


  public static final String ACCESS_CONTROL_ALLOW_ORIGIN_HEADER = "Access-Control-Allow-Origin";

  /**
   * Set the header for Cross-Site Resource Sharing.
   * @param resp HttpServletResponse to modify
   * @param validOrigins a space separated list of Origins as defined by the html5 spec
   * @see http://dev.w3.org/html5/spec/browsers.html#origin-0
   */
  public static void setCORSheader(HttpServletResponse resp, Collection<String> validOrigins) {
    if (validOrigins == null) {
      return;
    }
    for (String origin : validOrigins) {
      resp.addHeader(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, origin);
    }
  }
}
@@ -1296,18 +1296,16 @@
 il() %7B%7D%0A
-  
 %0A  // 1 
@@ -1558,24 +1558,97 @@
 ource;%0A  %7D%0A%0A
+  public static TimeSource getTimeSource() %7B%0A    return timeSource;%0A  %7D%0A%0A
   /**%0A   * S
@@ -2962,26 +2962,24 @@
 false);%0A  %7D%0A
-  
 %0A  public st
@@ -5007,20 +5007,16 @@
  false;%0A
-    
 %0A    // 

Update instead of add "Date" header in HttpResponse http://codereview.appspot.com/1888045/show