1 31 32 package org.opencms.main; 33 34 import org.opencms.file.CmsProject; 35 import org.opencms.file.CmsRequestContext; 36 import org.opencms.file.CmsUser; 37 import org.opencms.i18n.CmsEncoder; 38 import org.opencms.i18n.CmsLocaleManager; 39 40 import java.util.Locale ; 41 42 50 public class CmsContextInfo { 51 52 53 public static final String LOCALHOST = "127.0.0.1"; 54 55 56 private String m_encoding; 57 58 59 private boolean m_frozen; 60 61 62 private Locale m_locale; 63 64 65 private String m_localeName; 66 67 68 private CmsProject m_project; 69 70 71 private String m_projectName; 72 73 74 private String m_remoteAddr; 75 76 77 private String m_requestedUri; 78 79 80 private String m_siteRoot; 81 82 83 private CmsUser m_user; 84 85 86 private String m_userName; 87 88 101 public CmsContextInfo() { 102 103 setUserName(OpenCms.getDefaultUsers().getUserGuest()); 104 setProjectName(CmsProject.ONLINE_PROJECT_NAME); 105 setRequestedUri("/"); 106 setSiteRoot("/"); 107 setLocaleName(CmsLocaleManager.getDefaultLocale().toString()); 108 setEncoding(OpenCms.getSystemInfo().getDefaultEncoding()); 109 setRemoteAddr(CmsContextInfo.LOCALHOST); 110 } 111 112 117 public CmsContextInfo(CmsRequestContext requestContext) { 118 119 setUserName(requestContext.currentUser().getName()); 120 setProjectName(requestContext.currentProject().getName()); 121 setRequestedUri(requestContext.getUri()); 122 setSiteRoot(requestContext.getSiteRoot()); 123 setLocale(requestContext.getLocale()); 124 setEncoding(requestContext.getEncoding()); 125 setRemoteAddr(requestContext.getRemoteAddress()); 126 } 127 128 139 public CmsContextInfo( 140 CmsUser user, 141 CmsProject project, 142 String requestedUri, 143 String siteRoot, 144 Locale locale, 145 String encoding, 146 String remoteAddr) { 147 148 m_user = user; 149 setUserName(m_user.getName()); 150 m_project = project; 151 setProjectName(m_project.getName()); 152 setRequestedUri(requestedUri); 153 setSiteRoot(siteRoot); 154 setLocale(locale); 155 setEncoding(encoding); 156 setRemoteAddr(remoteAddr); 157 } 158 159 167 public CmsContextInfo(String userName) { 168 169 this(); 170 setUserName(userName); 171 } 172 173 184 public CmsContextInfo( 185 String userName, 186 String projectName, 187 String requestedUri, 188 String siteRoot, 189 String localeName, 190 String encoding, 191 String remoteAddr) { 192 193 super(); 194 setUserName(userName); 195 setProjectName(projectName); 196 setRequestedUri(requestedUri); 197 setSiteRoot(siteRoot); 198 setLocaleName(localeName); 199 setEncoding(encoding); 200 setRemoteAddr(remoteAddr); 201 } 202 203 208 public Object clone() { 209 210 CmsContextInfo result = new CmsContextInfo(); 211 result.m_encoding = m_encoding; 212 result.m_frozen = false; 213 result.m_locale = m_locale; 214 result.m_localeName = m_localeName; 215 result.m_project = m_project; 216 result.m_projectName = m_projectName; 217 result.m_remoteAddr = m_remoteAddr; 218 result.m_requestedUri = m_requestedUri; 219 result.m_siteRoot = m_siteRoot; 220 result.m_user = m_user; 221 result.m_userName = m_userName; 222 return result; 223 } 224 225 232 public void freeze() { 233 234 m_frozen = true; 235 } 236 237 242 public String getEncoding() { 243 244 return m_encoding; 245 } 246 247 252 public Locale getLocale() { 253 254 return m_locale; 255 } 256 257 262 public String getLocaleName() { 263 264 return m_localeName; 265 } 266 267 278 public CmsProject getProject() { 279 280 return m_project; 281 } 282 283 290 public String getProjectName() { 291 292 return m_projectName; 293 } 294 295 300 public String getRemoteAddr() { 301 302 return m_remoteAddr; 303 } 304 305 310 public String getRequestedUri() { 311 312 return m_requestedUri; 313 } 314 315 320 public String getSiteRoot() { 321 322 return m_siteRoot; 323 } 324 325 336 public CmsUser getUser() { 337 338 return m_user; 339 } 340 341 348 public String getUserName() { 349 350 return m_userName; 351 } 352 353 358 public void setEncoding(String encoding) { 359 360 checkFrozen(); 361 m_encoding = CmsEncoder.lookupEncoding(encoding, OpenCms.getSystemInfo().getDefaultEncoding()); 362 } 363 364 374 public void setLocale(Locale locale) { 375 376 checkFrozen(); 377 m_locale = locale; 378 m_localeName = m_locale.toString(); 379 } 380 381 391 public void setLocaleName(String localeName) { 392 393 checkFrozen(); 394 m_localeName = localeName; 395 m_locale = CmsLocaleManager.getLocale(localeName); 396 } 397 398 403 public void setProjectName(String projectName) { 404 405 checkFrozen(); 406 m_projectName = projectName; 407 } 408 409 414 public void setRemoteAddr(String remoteAddr) { 415 416 checkFrozen(); 417 m_remoteAddr = remoteAddr; 418 } 419 420 425 public void setRequestedUri(String requestedUri) { 426 427 checkFrozen(); 428 m_requestedUri = requestedUri; 429 } 430 431 436 public void setSiteRoot(String siteRoot) { 437 438 checkFrozen(); 439 m_siteRoot = siteRoot; 440 } 441 442 447 public void setUserName(String userName) { 448 449 checkFrozen(); 450 m_userName = userName; 451 } 452 453 458 protected void checkFrozen() throws CmsRuntimeException { 459 460 if (m_frozen) { 461 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONTEXT_INFO_FROZEN_0)); 462 } 463 } 464 } | Popular Tags |