/*
 * 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.gadgets.http;

import org.apache.shindig.gadgets.GadgetContext;
import org.apache.shindig.gadgets.SyndicatorConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Set;

import javax.servlet.http.HttpServletResponse;

/**
 * Collection of HTTP utilities
 */
public class HttpUtil {
  public static final long START_TIME = System.currentTimeMillis();
  // 1 year.
  private static final int DEFAULT_TTL = 60 * 60 * 24 * 365;

  /**
   * Sets default caching Headers (Expires, Cache-Control, Last-Modified)
   *
   * @param response The HTTP response
   */
  public static void setCachingHeaders(HttpServletResponse response) {
    setCachingHeaders(response, DEFAULT_TTL);
  }

  /**
   * Sets HTTP headers that instruct the browser to cache indefinitely.
   * Implementations should take care to use cache-busting techniques on the
   * url.
   *
   * @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) {
    response.setDateHeader("Expires",
        System.currentTimeMillis() + (1000L * ttl));

    // IE seems to need this (10 years should be enough).
    response.setHeader("Cache-Control", "public,max-age=" +
        Integer.toString(ttl));

    // Firefox requires this for certain cases.
    response.setDateHeader("Last-Modified", START_TIME);
  }

  /**
   * Fetches js configuration for the given feature set & syndicator
   * @param config
   * @param context
   * @param features
   */
  public static JSONObject getJsConfig(SyndicatorConfig config,
      GadgetContext context, Set<String> features) {
    JSONObject syndFeatures = config.getJsonObject(context.getSyndicator(),
                                                   "gadgets.features");
    if (syndFeatures != null) {
      String[] featArray = features.toArray(new String[features.size()]);
      try {
        return new JSONObject(syndFeatures, featArray);
      } catch (JSONException e) {
        return null;
      }
    }
    return new JSONObject();
  }
}

@@ -1604,20 +1604,15 @@
 che 
-indefinitely
+content
 .%0A  
@@ -2020,24 +2020,162 @@
 L * ttl));%0A%0A
+    if (ttl == 0) %7B%0A      response.setHeader(%22Pragma%22, %22no-cache%22);%0A      response.setHeader(%22Cache-Control%22, %22no-cache%22);%0A    %7D else %7B%0A  
     // IE se
@@ -2220,24 +2220,26 @@
 nough).%0A    
+ 
+ 
 response.set
@@ -2290,16 +2290,18 @@
         
+  
 Integer.
@@ -2312,24 +2312,29 @@
 ring(ttl));%0A
+    %7D
 %0A    // Fire
@@ -2427,16 +2427,17 @@
 _TIME);%0A
+%0A
   %7D%0A%0A  /

Set correct cache-control headers with TTL=0