//# 0 errors, 35 messages
//#
/*
    //#ModDateHeaderUtil.java:1:1: class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  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.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.ui.rendering.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Utility class to localize the modification date header-related logic.
 */
public class ModDateHeaderUtil {
    private static final Log log = LogFactory.getLog(ModDateHeaderUtil.class);
    //#ModDateHeaderUtil.java:31: method: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init
    //#ModDateHeaderUtil.java:31: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
    //#    method: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/util/ModDateHeaderUtil]
    //#output(org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init): log
    //#post(org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/util/ModDateHeaderUtil] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init): init'ed(log)
    //#ModDateHeaderUtil.java:31: end of method: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil__static_init

    // Utility class with static methods; inhibit construction
    private ModDateHeaderUtil() {
    //#ModDateHeaderUtil.java:34: method: void org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil()

    }
    //#ModDateHeaderUtil.java:36: end of method: void org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil()

    /**
     * Sets the HTTP response status to 304 (NOT MODIFIED) if the request contains an
     * If-Modified-Since header that specifies a time that is
     * at or after the time specified by the value of lastModifiedTimeMillis
     * <em>truncated to second granularity</em>.  Returns true if
     * the response status was set, false if not.
     *
     * @param request
     * @param response
     * @param lastModifiedTimeMillis
     * @return true if a response status was sent, false otherwise.
     */
    public static boolean respondIfNotModified(HttpServletRequest request,
                                               HttpServletResponse response,
                                               long lastModifiedTimeMillis) {
        long sinceDate = 0;
    //#ModDateHeaderUtil.java:53: method: bool org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
    //#ModDateHeaderUtil.java:53: Warning: unused assignment
    //#    unused assignment into sinceDate
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
    //#    method: bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
    //#    Attribs:  Uncertain
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): "NOT MODIFIED "._tainted
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): "last mod date (trucated to seconds) = "._tainted
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): "since date = "._tainted
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): lastModifiedTimeMillis
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): log
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): request
    //#input(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): response
    //#output(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): return_value
    //#pre[3] (bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): (soft) lastModifiedTimeMillis%1_000 - lastModifiedTimeMillis in -18_446_744_073_709_551_615..9_223_372_036_854_775_808
    //#pre[4] (bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): (soft) request != null
    //#pre[5] (bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): (soft) response != null
    //#presumption(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): org.apache.commons.logging.LogFactory:getLog(...)@31 != null
    //#post(bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)): init'ed(return_value)
        try {
            sinceDate = request.getDateHeader("If-Modified-Since");
        } catch(IllegalArgumentException ex) {
            // this indicates there was some problem parsing the header value as a date
            return false;
        }
        
        // truncate to seconds
        lastModifiedTimeMillis -= (lastModifiedTimeMillis % 1000);
        log.debug("since date = " + sinceDate);
    //#ModDateHeaderUtil.java:63: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
    //#    method: bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        log.debug("last mod date (trucated to seconds) = " + lastModifiedTimeMillis);
    //#ModDateHeaderUtil.java:64: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
    //#    method: bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        if (lastModifiedTimeMillis <= sinceDate) {
            log.debug("NOT MODIFIED " + request.getRequestURL());
    //#ModDateHeaderUtil.java:66: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
    //#    method: bool respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return true;
        } else {
            return false;
    //#ModDateHeaderUtil.java:70: end of method: bool org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.respondIfNotModified(HttpServletRequest, HttpServletResponse, long)
        }
    }

    /**
     * Set the Last-Modified header using the given time in milliseconds.  Note that because the
     * header has the granularity of one second, the value will get truncated to the nearest second that does not
     * exceed the provided value.
     * <p/>
     * This will also set the Expires header to a date in the past.  This forces clients to revalidate the cache each
     * time.
     *
     * @param response
     * @param lastModifiedTimeMillis
     */
    public static void setLastModifiedHeader(HttpServletResponse response, long lastModifiedTimeMillis) {
        response.setDateHeader("Last-Modified", lastModifiedTimeMillis);
    //#ModDateHeaderUtil.java:86: method: void org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.setLastModifiedHeader(HttpServletResponse, long)
    //#input(void setLastModifiedHeader(HttpServletResponse, long)): lastModifiedTimeMillis
    //#input(void setLastModifiedHeader(HttpServletResponse, long)): response
    //#pre[2] (void setLastModifiedHeader(HttpServletResponse, long)): response != null
        // Force clients to revalidate each time
        // See RFC 2616 (HTTP 1.1 spec) secs 14.21, 13.2.1
        response.setDateHeader("Expires", 0);
        // We may also want this (See 13.2.1 and 14.9.4)
        // response.setHeader("Cache-Control","must-revalidate");
    }
    //#ModDateHeaderUtil.java:92: end of method: void org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil.setLastModifiedHeader(HttpServletResponse, long)
}
    //#ModDateHeaderUtil.java:: end of class: org.apache.roller.weblogger.ui.rendering.util.ModDateHeaderUtil
