gt:         long now = System.currentTimeMillis();
context: /*
   * This library is part of OpenCms -
   * the Open Source Content Management System
   *
   * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * For further information about Alkacon Software GmbH & Co. KG, please see the
   * company website: http://www.alkacon.com
   *
   * For further information about OpenCms, please see the
   * project website: http://www.opencms.org
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  package org.opencms.flex;
  
  import org.opencms.ade.detailpage.CmsDetailPageResourceHandler;
  import org.opencms.file.CmsObject;
  import org.opencms.file.CmsResource;
  import org.opencms.main.CmsLog;
  import org.opencms.util.CmsRequestUtil;
  
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.Vector;
  
  import javax.servlet.ServletRequest;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.commons.logging.Log;
  
  /**
   * Controller for getting access to the CmsObject, should be used as a
   * request attribute.<p>
   *
   * @since 6.0.0
   */
  public class CmsFlexController {
  
      /** Constant for the controller request attribute name. */
      public static final String ATTRIBUTE_NAME = "org.opencms.flex.CmsFlexController";
  
      /** The log object for this class. */
      private static final Log LOG = CmsLog.getLog(CmsFlexController.class);
  
      /** Set of uncacheable attributes. */
      private static Set<String> uncacheableAttributes = new HashSet<String>();
  
      /** The CmsFlexCache where the result will be cached in, required for the dispatcher. */
      private CmsFlexCache m_cache;
  
      /** The wrapped CmsObject provides JSP with access to the core system. */
      private CmsObject m_cmsObject;
  
      /** List of wrapped RequestContext info object. */
      private List<CmsFlexRequestContextInfo> m_flexContextInfoList;
  
      /** List of wrapped CmsFlexRequests. */
      private List<CmsFlexRequest> m_flexRequestList;
  
      /** List of wrapped CmsFlexResponses. */
      private List<CmsFlexResponse> m_flexResponseList;
  
      /** Indicates if this controller is currently in "forward" mode. */
      private boolean m_forwardMode;
  
      /** Wrapped top request. */
      private HttpServletRequest m_req;
  
      /** Wrapped top response. */
      private HttpServletResponse m_res;
  
      /** The CmsResource that was initialized by the original request, required for URI actions. */
      private CmsResource m_resource;
  
      /** Indicates if the response should be streamed. */
      private boolean m_streaming;
  
      /** Exception that was caught during inclusion of sub elements. */
      private Throwable m_throwable;
  
      /** URI of a VFS resource that caused the exception. */
      private String m_throwableResourceUri;
  
      /** Indicates if the request is the top request. */
      private boolean m_top;
  
      /**
       * Creates a new controller form the old one, exchanging just the provided OpenCms user context.<p>
       *
       * @param cms the OpenCms user context for this controller
       * @param base the base controller
       */
      public CmsFlexController(CmsObject cms, CmsFlexController base) {
  
          m_cmsObject = cms;
          m_resource = base.m_resource;
          m_cache = base.m_cache;
          m_req = base.m_req;
          m_res = base.m_res;
          m_streaming = base.m_streaming;
          m_top = base.m_top;
          m_flexRequestList = base.m_flexRequestList;
          m_flexResponseList = base.m_flexResponseList;
          m_flexContextInfoList = base.m_flexContextInfoList;
          m_forwardMode = base.m_forwardMode;
          m_throwableResourceUri = base.m_throwableResourceUri;
      }
  
      /**
       * Default constructor.<p>
       *
       * @param cms the initial CmsObject to wrap in the controller
       * @param resource the file requested
       * @param cache the instance of the flex cache
       * @param req the current request
       * @param res the current response
       * @param streaming indicates if the response is streaming
       * @param top indicates if the response is the top response
       */
      public CmsFlexController(
          CmsObject cms,
          CmsResource resource,
          CmsFlexCache cache,
          HttpServletRequest req,
          HttpServletResponse res,
          boolean streaming,
          boolean top) {
  
          m_cmsObject = cms;
          m_resource = resource;
          m_cache = cache;
          m_req = req;
          m_res = res;
          m_streaming = streaming;
          m_top = top;
          m_flexRequestList = new Vector<CmsFlexRequest>();
          m_flexResponseList = new Vector<CmsFlexResponse>();
          m_flexContextInfoList = new Vector<CmsFlexRequestContextInfo>();
          m_forwardMode = false;
          m_throwableResourceUri = null;
      }
  
      /**
       * Returns the wrapped CmsObject form the provided request, or <code>null</code> if the
       * request is not running inside OpenCms.<p>
       *
       * @param req the current request
       * @return the wrapped CmsObject
       */
      public static CmsObject getCmsObject(ServletRequest req) {
  
          CmsFlexController controller = (CmsFlexController)req.getAttribute(ATTRIBUTE_NAME);
          if (controller != null) {
              return controller.getCmsObject();
          } else {
              return null;
          }
      }
  
      /**
       * Returns the controller from the given request, or <code>null</code> if the
       * request is not running inside OpenCms.<p>
       *
       * @param req the request to get the controller from
       *
       * @return the controller from the given request, or <code>null</code> if the request is not running inside OpenCms
       */
      public static CmsFlexController getController(ServletRequest req) {
  
          return (CmsFlexController)req.getAttribute(ATTRIBUTE_NAME);
      }
  
      /**
       * Provides access to a root cause Exception that might have occurred in a complex include scenario.<p>
       *
       * @param req the current request
       *
       * @return the root cause exception or null if no root cause exception is available
       *
       * @see #getThrowable()
       */
      public static Throwable getThrowable(ServletRequest req) {
  
          CmsFlexController controller = (CmsFlexController)req.getAttribute(ATTRIBUTE_NAME);
          if (controller != null) {
              return controller.getThrowable();
          } else {
              return null;
          }
      }
  
      /**
       * Provides access to URI of a VFS resource that caused an exception that might have occurred in a complex include scenario.<p>
       *
       * @param req the current request
       *
       * @return to URI of a VFS resource that caused an exception, or <code>null</code>
       *
       * @see #getThrowableResourceUri()
       */
      public static String getThrowableResourceUri(ServletRequest req) {
  
          CmsFlexController controller = (CmsFlexController)req.getAttribute(ATTRIBUTE_NAME);
          if (controller != null) {
              return controller.getThrowableResourceUri();
          } else {
              return null;
          }
      }
  
      /**
       * Checks if the provided request is running in OpenCms and the current users project is the online project.<p>
       *
       * @param req the current request
       *
       * @return <code>true</code> if the request is running in OpenCms and the current users project is
       *      the online project, <code>false</code> otherwise
       */
      public static boolean isCmsOnlineRequest(ServletRequest req) {
  
          if (req == null) {
              return false;
          }
          return getController(req).getCmsObject().getRequestContext().getCurrentProject().isOnlineProject();
      }
  
      /**
       * Checks if the provided request is running in OpenCms.<p>
       *
       * @param req the current request
       *
       * @return <code>true</code> if the request is running in OpenCms, <code>false</code> otherwise
       */
      public static boolean isCmsRequest(ServletRequest req) {
  
          return ((req != null) && (req.getAttribute(ATTRIBUTE_NAME) != null));
      }
  
      /**
       * Checks if the request has the "If-Modified-Since" header set, and if so,
       * if the header date value is equal to the provided last modification date.<p>
       *
       * @param req the request to set the "If-Modified-Since" date header from
       * @param dateLastModified the date to compare the header with
       *
       * @return <code>true</code> if the header is set and the header date is equal to the provided date
       */
      public static boolean isNotModifiedSince(HttpServletRequest req, long dateLastModified) {
  
          // check if the request contains a last modified header
          try {
              long lastModifiedHeader = req.getDateHeader(CmsRequestUtil.HEADER_IF_MODIFIED_SINCE);
              // if last modified header is set (> -1), compare it to the requested resource
              return ((lastModifiedHeader > -1) && (((dateLastModified / 1000) * 1000) == lastModifiedHeader));
          } catch (Exception ex) {
              // some clients (e.g. User-Agent: BlackBerry7290/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/111)
              // send an invalid "If-Modified-Since" header (e.g. in german locale)
              // which breaks with http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
              // this has to be caught because the subsequent request for the 500 error handler
              // would run into the same exception.
              LOG.warn(
                  Messages.get().getBundle().key(
                      Messages.ERR_HEADER_IFMODIFIEDSINCE_FORMAT_3,
                      new Object[] {
                          CmsRequestUtil.HEADER_IF_MODIFIED_SINCE,
                          req.getHeader(CmsRequestUtil.HEADER_USER_AGENT),
                          req.getHeader(CmsRequestUtil.HEADER_IF_MODIFIED_SINCE)}));
          }
          return false;
      }
  
      /**
       * Tells the flex controller to never cache the given attribute.<p>
       *
       * @param attributeName the attribute which shouldn't be cached
       */
      public static void registerUncacheableAttribute(String attributeName) {
  
          uncacheableAttributes.add(attributeName);
      }
  
      /**
       * Removes the controller attribute from a request.<p>
       *
       * @param req the request to remove the controller from
       */
      public static void removeController(ServletRequest req) {
  
          CmsFlexController controller = (CmsFlexController)req.getAttribute(ATTRIBUTE_NAME);
          if (controller != null) {
              controller.clear();
          }
      }
  
      /**
       * Stores the given controller in the given request (using a request attribute).<p>
       *
       * @param req the request where to store the controller in
       * @param controller the controller to store
       */
      public static void setController(ServletRequest req, CmsFlexController controller) {
  
          req.setAttribute(CmsFlexController.ATTRIBUTE_NAME, controller);
      }
  
      /**
       * Sets the <code>Expires</code> date header for a given http request.<p>
       *
       * Also sets the <code>cache-control: max-age</code> header to the time of the expiration.
       * A certain upper limit is imposed on the expiration date parameter to ensure the resources are
       * not cached to long in proxies. This can be controlled by the <code>maxAge</code> parameter.
       * If <code>maxAge</code> is lower then 0, then a default max age of 86400000 msec (1 day) is used.<p>
       *
       * @param res the response to set the "Expires" date header for
       * @param maxAge maximum amount of time in milliseconds the response remains valid
       * @param dateExpires the date to set (if this is not in the future, it is ignored)
       */
      public static void setDateExpiresHeader(HttpServletResponse res, long dateExpires, long maxAge) {
  
  