001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsContextInfo.java,v $
003: * Date : $Date: 2008-02-27 12:05:39 $
004: * Version: $Revision: 1.17 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.main;
033:
034: import org.opencms.file.CmsProject;
035: import org.opencms.file.CmsRequestContext;
036: import org.opencms.file.CmsUser;
037: import org.opencms.i18n.CmsEncoder;
038: import org.opencms.i18n.CmsLocaleManager;
039: import org.opencms.security.CmsOrganizationalUnit;
040:
041: import java.util.Locale;
042:
043: /**
044: * Contains user information for automated creation of a
045: * {@link org.opencms.file.CmsRequestContext} during system runtime.<p>
046: *
047: * @version $Revision: 1.17 $
048: *
049: * @since 6.0.0
050: */
051: public class CmsContextInfo {
052:
053: /** Name of the http session attribute the request time is stored in. */
054: public static final String ATTRIBUTE_REQUEST_TIME = "__org.opencms.main.CmsContextInfo#m_requestTime";
055:
056: /** Indicates the request time should always be the current time. */
057: public static final long CURRENT_TIME = -1L;
058:
059: /** Localhost ip used in fallback cases. */
060: public static final String LOCALHOST = "127.0.0.1";
061:
062: /** The encoding to create the context with. */
063: private String m_encoding;
064:
065: /** Indicates if the configuration if this context info can still be changed or not. */
066: private boolean m_frozen;
067:
068: /** The locale to create the context with. */
069: private Locale m_locale;
070:
071: /** The locale name to create the context with. */
072: private String m_localeName;
073:
074: /** The organizational unit to create the context with. */
075: private String m_ouFqn;
076:
077: /** The project to create the context with. */
078: private CmsProject m_project;
079:
080: /** The user name to create the context with. */
081: private String m_projectName;
082:
083: /** The remote ip address to create the context with. */
084: private String m_remoteAddr;
085:
086: /** The request URI to create the context with. */
087: private String m_requestedUri;
088:
089: /** The time for the request, used for resource publication and expiration dates. */
090: private long m_requestTime;
091:
092: /** The site root to create the context with. */
093: private String m_siteRoot;
094:
095: /** The user to create the context with. */
096: private CmsUser m_user;
097:
098: /** The user name to create the context with. */
099: private String m_userName;
100:
101: /**
102: * Creates a new instance, initializing the variables with some reasonable default values.<p>
103: *
104: * The default values are:<dl>
105: * <dt>User name</dt><dd>(configured default guest user)</dd>
106: * <dt>Project name</dt><dd>Online</dd>
107: * <dt>Requested URI</dt><dd>/</dd>
108: * <dt>Site root</dt><dd>/</dd>
109: * <dt>Locale name</dt><dd>(configured default locale name)</dd>
110: * <dt>Encoding</dt><dd>(configured default system encoding)</dd>
111: * <dt>Remote address</dt><dd>127.0.0.1</dd>
112: * <dt>Organizational unit</dt><dd>/</dd>
113: * </dl><p>
114: */
115: public CmsContextInfo() {
116:
117: setUserName(OpenCms.getDefaultUsers().getUserGuest());
118: setProjectName(CmsProject.ONLINE_PROJECT_NAME);
119: setRequestedUri("/");
120: setSiteRoot("/");
121: setLocaleName(CmsLocaleManager.getDefaultLocale().toString());
122: setEncoding(OpenCms.getSystemInfo().getDefaultEncoding());
123: setRemoteAddr(CmsContextInfo.LOCALHOST);
124: setRequestTime(CURRENT_TIME);
125: setOuFqn("");
126: }
127:
128: /**
129: * Creates a new instance with all context variables initialized from the given request context.<p>
130: *
131: * @param requestContext the request context to initialize this context info with
132: */
133: public CmsContextInfo(CmsRequestContext requestContext) {
134:
135: setUserName(requestContext.currentUser().getName());
136: setProjectName(requestContext.currentProject().getName());
137: setRequestedUri(requestContext.getUri());
138: setSiteRoot(requestContext.getSiteRoot());
139: setLocale(requestContext.getLocale());
140: setEncoding(requestContext.getEncoding());
141: setRemoteAddr(requestContext.getRemoteAddress());
142: setRequestTime(requestContext.getRequestTime());
143: setOuFqn(requestContext.getOuFqn());
144: }
145:
146: /**
147: * Creates a new instance with all context variables initialized.<p>
148: *
149: * @param user the user to create the context with
150: * @param project the project to create the context with
151: * @param requestedUri the request URI to create the context with
152: * @param siteRoot the site root to create the context with
153: * @param locale the locale to create the context with
154: * @param encoding the encoding to create the context with
155: * @param remoteAddr the remote ip address to create the context with
156: * @param requestTime the time of the request (used for resource publication / expiration date)
157: * @param ouFqn the fully qualified name of the organizational unit to create the context with
158: */
159: public CmsContextInfo(CmsUser user, CmsProject project,
160: String requestedUri, String siteRoot, Locale locale,
161: String encoding, String remoteAddr, long requestTime,
162: String ouFqn) {
163:
164: m_user = user;
165: setUserName(m_user.getName());
166: m_project = project;
167: setProjectName(m_project.getName());
168: setRequestedUri(requestedUri);
169: setSiteRoot(siteRoot);
170: setLocale(locale);
171: setEncoding(encoding);
172: setRemoteAddr(remoteAddr);
173: setRequestTime(requestTime);
174: setOuFqn(ouFqn);
175: }
176:
177: /**
178: * Creates a new instance, initializing the user name as provided and
179: * all other vaiables with the same default values as in {@link #CmsContextInfo()}.<p>
180: *
181: * @param userName the user name to create the context with
182: *
183: * @see #CmsContextInfo()
184: */
185: public CmsContextInfo(String userName) {
186:
187: this ();
188: setUserName(userName);
189: }
190:
191: /**
192: * Creates a clone of this context info object.<p>
193: *
194: * @see java.lang.Object#clone()
195: */
196: public Object clone() {
197:
198: CmsContextInfo result = new CmsContextInfo();
199: result.m_encoding = m_encoding;
200: result.m_frozen = false;
201: result.m_locale = m_locale;
202: result.m_localeName = m_localeName;
203: result.m_project = m_project;
204: result.m_projectName = m_projectName;
205: result.m_remoteAddr = m_remoteAddr;
206: result.m_requestedUri = m_requestedUri;
207: result.m_requestTime = m_requestTime;
208: result.m_siteRoot = m_siteRoot;
209: result.m_user = m_user;
210: result.m_userName = m_userName;
211: return result;
212: }
213:
214: /**
215: * Finalizes (freezes) the configuration of this context information.<p>
216: *
217: * After this entry has been frozen, any attempt to change the
218: * configuration of this context info with one of the "set..." methods
219: * will lead to a <code>RuntimeException</code>.<p>
220: */
221: public void freeze() {
222:
223: m_frozen = true;
224: }
225:
226: /**
227: * Returns the encoding.<p>
228: *
229: * @return the encoding
230: *
231: * @see CmsRequestContext#getEncoding()
232: */
233: public String getEncoding() {
234:
235: return m_encoding;
236: }
237:
238: /**
239: * Returns the locale.<p>
240: *
241: * @return the locale
242: *
243: * @see CmsRequestContext#getLocale()
244: */
245: public Locale getLocale() {
246:
247: return m_locale;
248: }
249:
250: /**
251: * Returns the locale name.<p>
252: *
253: * @return the locale name
254: *
255: * @see CmsRequestContext#getLocale()
256: */
257: public String getLocaleName() {
258:
259: return m_localeName;
260: }
261:
262: /**
263: * Returns the fully qualified name of the organizational unit.<p>
264: *
265: * @return the fully qualified name of the organizational unit
266: */
267: public String getOuFqn() {
268:
269: return m_ouFqn;
270: }
271:
272: /**
273: * Returns the project, or <code>null</code> if the project
274: * has not been configured.<p>
275: *
276: * If the project has not been configured, at last the
277: * project name will be available.<p>
278: *
279: * @return the project
280: *
281: * @see #getProjectName()
282: * @see CmsRequestContext#currentProject()
283: */
284: public CmsProject getProject() {
285:
286: return m_project;
287: }
288:
289: /**
290: * Returns the project name.<p>
291: *
292: * @return the project name
293: *
294: * @see #getProject()
295: * @see CmsRequestContext#currentProject()
296: */
297: public String getProjectName() {
298:
299: return m_projectName;
300: }
301:
302: /**
303: * Returns the remote ip address.<p>
304: *
305: * @return the remote ip address
306: *
307: * @see CmsRequestContext#getRemoteAddress()
308: */
309: public String getRemoteAddr() {
310:
311: return m_remoteAddr;
312: }
313:
314: /**
315: * Returns the requested uri.<p>
316: *
317: * @return the requested uri
318: *
319: * @see CmsRequestContext#getUri()
320: */
321: public String getRequestedUri() {
322:
323: return m_requestedUri;
324: }
325:
326: /**
327: * Returns the request time used for validation of resource publication and expiration dates.<p>
328: *
329: * @return the request time used for validation of resource publication and expiration dates
330: *
331: * @see CmsRequestContext#getRequestTime()
332: */
333: public long getRequestTime() {
334:
335: return m_requestTime;
336: }
337:
338: /**
339: * Returns the siteroot.<p>
340: *
341: * @return the siteroot
342: *
343: * @see CmsRequestContext#getSiteRoot()
344: */
345: public String getSiteRoot() {
346:
347: return m_siteRoot;
348: }
349:
350: /**
351: * Returns the user, or <code>null</code> if the user
352: * has not been configured.<p>
353: *
354: * If the user has not been configured, at last the
355: * user name will be available.<p>
356: *
357: * @return the user
358: *
359: * @see #getUserName()
360: * @see CmsRequestContext#currentUser()
361: */
362: public CmsUser getUser() {
363:
364: return m_user;
365: }
366:
367: /**
368: * Returns the username.<p>
369: *
370: * @return the username
371: *
372: * @see #getUser()
373: * @see CmsRequestContext#currentUser()
374: */
375: public String getUserName() {
376:
377: return m_userName;
378: }
379:
380: /**
381: * Sets the encoding.<p>
382: *
383: * @param encoding the encoding to set
384: *
385: * @see CmsRequestContext#setEncoding(String)
386: */
387: public void setEncoding(String encoding) {
388:
389: checkFrozen();
390: m_encoding = CmsEncoder.lookupEncoding(encoding, OpenCms
391: .getSystemInfo().getDefaultEncoding());
392: }
393:
394: /**
395: * Sets the locale.<p>
396: *
397: * Setting the locale name will override the currently selected locale
398: * and vice-versa. The locale name and the locale will always match.<p>
399: *
400: * @param locale the locale to set
401: *
402: * @see #setLocaleName(String)
403: * @see CmsRequestContext#getLocale()
404: */
405: public void setLocale(Locale locale) {
406:
407: checkFrozen();
408: m_locale = locale;
409: m_localeName = m_locale.toString();
410: }
411:
412: /**
413: * Sets the locale name.<p>
414: *
415: * Setting the locale name will override the currently selected locale
416: * and vice-versa. The locale name and the locale will always match.<p>
417: *
418: * @param localeName the locale name to set
419: *
420: * @see #setLocale(Locale)
421: * @see CmsRequestContext#getLocale()
422: */
423: public void setLocaleName(String localeName) {
424:
425: checkFrozen();
426: m_localeName = localeName;
427: m_locale = CmsLocaleManager.getLocale(localeName);
428: }
429:
430: /**
431: * Sets the fully qualified name of the organizational unit.<p>
432: *
433: * @param ouFqn the fully qualified name of the organizational unit to set
434: */
435: public void setOuFqn(String ouFqn) {
436:
437: checkFrozen();
438: m_ouFqn = ouFqn;
439: }
440:
441: /**
442: * Sets the project name.<p>
443: *
444: * @param projectName the project name to set
445: *
446: * @see CmsRequestContext#currentProject()
447: */
448: public void setProjectName(String projectName) {
449:
450: checkFrozen();
451: m_projectName = projectName;
452: }
453:
454: /**
455: * Sets the remote ip address.<p>
456: *
457: * @param remoteAddr the remote ip address
458: *
459: * @see CmsRequestContext#getRemoteAddress()
460: */
461: public void setRemoteAddr(String remoteAddr) {
462:
463: checkFrozen();
464: m_remoteAddr = remoteAddr;
465: }
466:
467: /**
468: * Sets the requested uri.<p>
469: *
470: * @param requestedUri the requested uri to set
471: *
472: * @see CmsRequestContext#setUri(String)
473: */
474: public void setRequestedUri(String requestedUri) {
475:
476: checkFrozen();
477: m_requestedUri = requestedUri;
478: }
479:
480: /**
481: * Sets the request time used for validation of resource publication and expiration dates.<p>
482: *
483: * @param requestTime the request time to set
484: *
485: * @see CmsRequestContext#getRequestTime()
486: */
487: public void setRequestTime(long requestTime) {
488:
489: checkFrozen();
490: if (requestTime == CURRENT_TIME) {
491: m_requestTime = System.currentTimeMillis();
492: } else {
493: m_requestTime = requestTime;
494: }
495: }
496:
497: /**
498: * Sets the siteroot.<p>
499: *
500: * @param siteRoot the siteroot to set
501: *
502: * @see CmsRequestContext#setSiteRoot(String)
503: */
504: public void setSiteRoot(String siteRoot) {
505:
506: checkFrozen();
507: m_siteRoot = siteRoot;
508: }
509:
510: /**
511: * Sets the username.<p>
512: *
513: * @param userName the username to set
514: *
515: * @see CmsRequestContext#currentUser()
516: */
517: public void setUserName(String userName) {
518:
519: checkFrozen();
520: m_userName = userName;
521: setOuFqn(CmsOrganizationalUnit.getParentFqn(userName));
522: }
523:
524: /**
525: * Checks if this context info configuration is frozen.<p>
526: *
527: * @throws CmsRuntimeException in case the configuration is already frozen
528: */
529: protected void checkFrozen() throws CmsRuntimeException {
530:
531: if (m_frozen) {
532: throw new CmsRuntimeException(Messages.get().container(
533: Messages.ERR_CONTEXT_INFO_FROZEN_0));
534: }
535: }
536: }
|