001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.flex; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsRequestContext; 032import org.opencms.jsp.util.CmsJspStandardContextBean; 033import org.opencms.jsp.util.CmsJspStandardContextBean.TemplateBean; 034import org.opencms.loader.CmsTemplateContextManager; 035import org.opencms.loader.I_CmsResourceLoader; 036import org.opencms.main.CmsLog; 037import org.opencms.main.OpenCms; 038import org.opencms.util.CmsCollectionsGenericWrapper; 039import org.opencms.util.CmsRequestUtil; 040import org.opencms.util.CmsUUID; 041 042import java.util.Map; 043 044import javax.servlet.http.HttpServletRequest; 045import javax.servlet.http.HttpSession; 046 047import org.apache.commons.logging.Log; 048 049/** 050 * Describes the caching behaviour (or caching options) for a Flex request.<p> 051 * 052 * @since 6.0.0 053 */ 054public class CmsFlexRequestKey { 055 056 /** The log object for this class. */ 057 private static final Log LOG = CmsLog.getLog(CmsFlexRequestKey.class); 058 059 /** The current container element. */ 060 private String m_containerElement; 061 062 /** The request context this request was made in. */ 063 private CmsRequestContext m_context; 064 065 /** The current detail view id. */ 066 private CmsUUID m_detailViewId; 067 068 /** Stores the device this request was made with. */ 069 private String m_device; 070 071 /** The (Flex) Http request this key was constructed for. */ 072 private HttpServletRequest m_request; 073 074 /** The OpenCms resource that this key is used for. */ 075 private String m_resource; 076 077 /** 078 * This constructor is used when building a cache key from a request.<p> 079 * 080 * The request contains several data items that are neccessary to construct 081 * the output. These items are e.g. the Query-String, the requested resource, 082 * the current time etc. etc. 083 * All required items are saved in the constructed cache - key.<p> 084 * 085 * @param req the request to construct the key for 086 * @param target the requested resource in the OpenCms VFS 087 * @param online must be true for an online resource, false for offline resources 088 */ 089 public CmsFlexRequestKey(HttpServletRequest req, String target, boolean online) { 090 091 // store the request 092 m_request = req; 093 094 // fetch the cms from the request 095 CmsObject cms = CmsFlexController.getCmsObject(req); 096 097 // store the request context 098 m_context = cms.getRequestContext(); 099 100 // calculate the resource name 101 m_resource = CmsFlexCacheKey.getKeyName(m_context.addSiteRoot(target), online); 102 103 // calculate the device 104 m_device = OpenCms.getSystemInfo().getDeviceSelector().getDeviceType(req); 105 106 CmsJspStandardContextBean standardContext = CmsJspStandardContextBean.getInstance(req); 107 // get the current container element 108 String templateContextKey = ""; 109 TemplateBean templateBean = (TemplateBean)(req.getAttribute(CmsTemplateContextManager.ATTR_TEMPLATE_BEAN)); 110 if (templateBean != null) { 111 templateContextKey = templateBean.getName(); 112 } 113 m_containerElement = standardContext.elementCachingHash() + "_tc_" + templateContextKey; 114 115 // get the current detail view id 116 m_detailViewId = standardContext.getDetailContentId(); 117 118 if (LOG.isDebugEnabled()) { 119 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXREQUESTKEY_CREATED_NEW_KEY_1, m_resource)); 120 } 121 } 122 123 /** 124 * Returns the request attributes.<p> 125 * 126 * @return the request attributes 127 */ 128 public Map<String, Object> getAttributes() { 129 130 Map<String, Object> attrs = CmsRequestUtil.getAtrributeMap(m_request); 131 if (attrs.size() == 0) { 132 return null; 133 } 134 return attrs; 135 } 136 137 /** 138 * Returns the current container element.<p> 139 * 140 * @return the current container element 141 */ 142 public String getContainerElement() { 143 144 return m_containerElement; 145 } 146 147 /** 148 * Returns the device.<p> 149 * 150 * @return the device 151 */ 152 public String getDevice() { 153 154 return m_device; 155 } 156 157 /** 158 * Returns the element.<p> 159 * 160 * @return the element 161 */ 162 public String getElement() { 163 164 return m_request.getParameter(I_CmsResourceLoader.PARAMETER_ELEMENT); 165 } 166 167 /** 168 * Returns the encoding.<p> 169 * 170 * @return the encoding 171 */ 172 public String getEncoding() { 173 174 return m_context.getEncoding(); 175 } 176 177 /** 178 * Returns the ip.<p> 179 * 180 * @return the ip 181 */ 182 public String getIp() { 183 184 return m_context.getRemoteAddress(); 185 } 186 187 /** 188 * Returns the locale.<p> 189 * 190 * @return the locale 191 */ 192 public String getLocale() { 193 194 return m_context.getLocale().toString(); 195 } 196 197 /** 198 * Returns the parameters.<p> 199 * 200 * @return the parameters 201 */ 202 public Map<String, String[]> getParams() { 203 204 // get the params 205 Map<String, String[]> params = CmsCollectionsGenericWrapper.map(m_request.getParameterMap()); 206 if (params.size() == 0) { 207 return null; 208 } 209 return params; 210 } 211 212 /** 213 * Returns the port.<p> 214 * 215 * @return the port 216 */ 217 public Integer getPort() { 218 219 return new Integer(m_request.getServerPort()); 220 } 221 222 /** 223 * Returns the resource.<p> 224 * 225 * @return the resource 226 */ 227 public String getResource() { 228 229 return m_resource; 230 } 231 232 /** 233 * Returns the schemes.<p> 234 * 235 * @return the schemes 236 */ 237 public String getScheme() { 238 239 return m_request.getScheme().toLowerCase(); 240 } 241 242 /** 243 * Returns the the current users session, or <code>null</code> if the current user 244 * has no session.<p> 245 * 246 * @return the current users session, or <code>null</code> if the current user has no session 247 */ 248 public HttpSession getSession() { 249 250 return m_request.getSession(false); 251 } 252 253 /** 254 * Returns the site root.<p> 255 * 256 * @return the site root 257 */ 258 public String getSite() { 259 260 return m_context.getSiteRoot(); 261 } 262 263 /** 264 * Returns the uri.<p> 265 * 266 * @return the uri 267 */ 268 public String getUri() { 269 270 String uri = m_context.addSiteRoot(m_context.getUri()); 271 if (m_detailViewId != null) { 272 uri += m_detailViewId.toString() + "/"; 273 } 274 return uri; 275 } 276 277 /** 278 * Returns the user.<p> 279 * 280 * @return the user 281 */ 282 public String getUser() { 283 284 return m_context.getCurrentUser().getName(); 285 } 286}