1 31 32 package org.opencms.file; 33 34 import org.opencms.main.CmsRuntimeException; 35 import org.opencms.util.CmsResourceTranslator; 36 import org.opencms.workplace.CmsWorkplace; 37 38 import java.util.Hashtable ; 39 import java.util.Locale ; 40 import java.util.Map ; 41 42 53 public final class CmsRequestContext { 54 55 56 public static final String ATTRIBUTE_EDITOR = "org.opencms.file.CmsRequestContext.ATTRIBUTE_EDITOR"; 57 58 59 private Map m_attributeMap; 60 61 62 private CmsProject m_currentProject; 63 64 65 private CmsResourceTranslator m_directoryTranslator; 66 67 68 private String m_encoding; 69 70 71 private CmsResourceTranslator m_fileTranslator; 72 73 74 private Locale m_locale; 75 76 77 private String m_remoteAddr; 78 79 80 private long m_requestTime; 81 82 83 private String m_savedSiteRoot; 84 85 86 private String m_siteRoot; 87 88 89 private boolean m_updateSession; 90 91 92 private String m_uri; 93 94 95 private CmsUser m_user; 96 97 110 public CmsRequestContext( 111 CmsUser user, 112 CmsProject project, 113 String requestedUri, 114 String siteRoot, 115 Locale locale, 116 String encoding, 117 String remoteAddr, 118 CmsResourceTranslator directoryTranslator, 119 CmsResourceTranslator fileTranslator) { 120 121 m_updateSession = true; 122 m_user = user; 123 m_currentProject = project; 124 m_uri = requestedUri; 125 setSiteRoot(siteRoot); 126 m_locale = locale; 127 m_encoding = encoding; 128 m_remoteAddr = remoteAddr; 129 m_directoryTranslator = directoryTranslator; 130 m_fileTranslator = fileTranslator; 131 m_requestTime = System.currentTimeMillis(); 132 } 133 134 145 public static String getAdjustedSiteRoot(String siteRoot, String resourcename) { 146 147 if (resourcename.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) { 148 return ""; 149 } else { 150 return siteRoot; 151 } 152 } 153 154 162 public String addSiteRoot(String resourcename) { 163 164 return addSiteRoot(m_siteRoot, resourcename); 165 } 166 167 176 public String addSiteRoot(String siteRoot, String resourcename) { 177 178 if ((resourcename == null) || (siteRoot == null)) { 179 return null; 180 } 181 siteRoot = getAdjustedSiteRoot(siteRoot, resourcename); 182 StringBuffer result = new StringBuffer (128); 183 result.append(siteRoot); 184 if (((siteRoot.length() == 0) || (siteRoot.charAt(siteRoot.length() - 1) != '/')) 185 && ((resourcename.length() == 0) || (resourcename.charAt(0) != '/'))) { 186 result.append('/'); 188 } 189 result.append(resourcename); 190 return m_directoryTranslator.translateResource(result.toString()); 191 } 192 193 198 public CmsProject currentProject() { 199 200 return m_currentProject; 201 } 202 203 208 public CmsUser currentUser() { 209 210 return m_user; 211 } 212 213 220 public String getAdjustedSiteRoot(String resourcename) { 221 222 return getAdjustedSiteRoot(m_siteRoot, resourcename); 223 } 224 225 231 public Object getAttribute(String attributeName) { 232 233 if (m_attributeMap == null) { 234 return null; 235 } 236 return m_attributeMap.get(attributeName); 237 } 238 239 247 public CmsResourceTranslator getDirectoryTranslator() { 248 249 return m_directoryTranslator; 250 } 251 252 257 public String getEncoding() { 258 259 return m_encoding; 260 } 261 262 270 public CmsResourceTranslator getFileTranslator() { 271 272 return m_fileTranslator; 273 } 274 275 280 public String getFolderUri() { 281 282 return getUri().substring(0, getUri().lastIndexOf("/") + 1); 283 } 284 285 290 public Locale getLocale() { 291 292 return m_locale; 293 } 294 295 300 public String getRemoteAddress() { 301 302 return m_remoteAddr; 303 } 304 305 310 public long getRequestTime() { 311 312 return m_requestTime; 313 } 314 315 334 public String getSitePath(CmsResource resource) { 335 336 return removeSiteRoot(resource.getRootPath()); 337 } 338 339 344 public String getSiteRoot() { 345 346 return m_siteRoot; 347 } 348 349 354 public String getUri() { 355 356 return m_uri; 357 } 358 359 367 public boolean isUpdateSessionEnabled() { 368 369 return m_updateSession; 370 } 371 372 385 public String removeSiteRoot(String resourcename) { 386 387 String siteRoot = getAdjustedSiteRoot(m_siteRoot, resourcename); 388 if ((siteRoot == m_siteRoot) && resourcename.startsWith(siteRoot)) { 389 resourcename = resourcename.substring(siteRoot.length()); 390 } 391 return resourcename; 392 } 393 394 399 public void restoreSiteRoot() throws RuntimeException { 400 401 if (m_savedSiteRoot == null) { 402 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_EMPTY_SITEROOT_0)); 403 } 404 m_siteRoot = m_savedSiteRoot; 405 m_savedSiteRoot = null; 406 } 407 408 413 public void saveSiteRoot() throws RuntimeException { 414 415 if (m_savedSiteRoot != null) { 416 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_NONEMPTY_SITEROOT_1, m_savedSiteRoot)); 417 } 418 m_savedSiteRoot = m_siteRoot; 419 } 420 421 427 public void setAttribute(String key, Object value) { 428 429 if (m_attributeMap == null) { 430 m_attributeMap = new Hashtable (); 432 } 433 m_attributeMap.put(key, value); 434 } 435 436 442 public CmsProject setCurrentProject(CmsProject project) { 443 444 if (project != null) { 445 m_currentProject = project; 446 } 447 return m_currentProject; 448 } 449 450 455 public void setEncoding(String encoding) { 456 457 m_encoding = encoding; 458 } 459 460 465 public void setRequestTime(long time) { 466 467 m_requestTime = time; 468 } 469 470 475 public void setSiteRoot(String root) { 476 477 if (root.endsWith("/")) { 479 m_siteRoot = root.substring(0, root.length() - 1); 480 } else { 481 m_siteRoot = root; 482 } 483 } 484 485 490 public void setUpdateSessionEnabled(boolean value) { 491 492 m_updateSession = value; 493 } 494 495 504 public void setUri(String value) { 505 506 m_uri = value; 507 } 508 509 515 protected void switchUser(CmsUser user, CmsProject project) { 516 517 m_user = user; 518 m_currentProject = project; 519 } 520 } | Popular Tags |