001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/site/CmsSite.java,v $
003: * Date : $Date: 2008-02-27 12:05:50 $
004: * Version: $Revision: 1.31 $
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.site;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsPropertyDefinition;
036: import org.opencms.file.CmsResource;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsLog;
039: import org.opencms.main.OpenCms;
040: import org.opencms.util.CmsUUID;
041:
042: import java.util.ArrayList;
043: import java.util.List;
044:
045: import org.apache.commons.logging.Log;
046:
047: /**
048: * Describes a configured site in OpenCms.<p>
049: *
050: * @author Alexander Kandzior
051: * @author Jan Baudisch
052: *
053: * @version $Revision: 1.31 $
054: *
055: * @since 6.0.0
056: */
057: public final class CmsSite implements Cloneable, Comparable {
058:
059: /** The log object for this class. */
060: private static final Log LOG = CmsLog.getLog(CmsSite.class);
061:
062: /** The aliases for this site, a vector of CmsSiteMatcher Objects. */
063: private List m_aliases;
064:
065: /** If exclusive, and set to true will generate a 404 error, if set to false will redirect to secure url. */
066: private boolean m_exclusiveError;
067:
068: /** If set to true, secure resources will only be available using the configured secure url. */
069: private boolean m_exclusiveUrl;
070:
071: /** This value defines a relative sorting order. */
072: private float m_position;
073:
074: /** The Url of the secure server. */
075: private CmsSiteMatcher m_secureServer;
076:
077: /** The site matcher that describes the site. */
078: private CmsSiteMatcher m_siteMatcher;
079:
080: /** Root directory of this site in the OpenCms VFS. */
081: private String m_siteRoot;
082:
083: /** UUID of this site's root directory in the OpenCms VFS. */
084: private CmsUUID m_siteRootUUID;
085:
086: /** Display title of this site. */
087: private String m_title;
088:
089: /**
090: * Constructs a new site object without title and id information,
091: * this is to be used for lookup purposes only.<p>
092: *
093: * @param siteRoot root directory of this site in the OpenCms VFS
094: * @param siteMatcher the site matcher for this site
095: */
096: public CmsSite(String siteRoot, CmsSiteMatcher siteMatcher) {
097:
098: this (siteRoot, CmsUUID.getNullUUID(), siteRoot, siteMatcher, "");
099: }
100:
101: /**
102: * Constructs a new site object with a default (wildcard) a site matcher,
103: * this is to be used for display purposes only.<p>
104: *
105: * @param siteRoot root directory of this site in the OpenCms VFS
106: * @param siteRootUUID UUID of this site's root directory in the OpenCms VFS
107: * @param title display name of this site
108: */
109: public CmsSite(String siteRoot, CmsUUID siteRootUUID, String title) {
110:
111: this (siteRoot, siteRootUUID, title,
112: CmsSiteMatcher.DEFAULT_MATCHER, "");
113: }
114:
115: /**
116: * Constructs a new site object.<p>
117: *
118: * @param siteRoot root directory of this site in the OpenCms VFS
119: * @param siteRootUUID UUID of this site's root directory in the OpenCms VFS
120: * @param title display name of this site
121: * @param siteMatcher the site matcher for this site
122: * @param position the sorting position
123: */
124: public CmsSite(String siteRoot, CmsUUID siteRootUUID, String title,
125: CmsSiteMatcher siteMatcher, String position) {
126:
127: setSiteRoot(siteRoot);
128: setSiteRootUUID(siteRootUUID);
129: setTitle(title);
130: setSiteMatcher(siteMatcher);
131: // init the position value
132: m_position = Float.MAX_VALUE;
133: try {
134: m_position = Float.parseFloat(position);
135: } catch (Throwable e) {
136: // m_position will have Float.MAX_VALUE, so this site will appear last
137: }
138: m_aliases = new ArrayList();
139: }
140:
141: /**
142: * Returns a clone of this Objects instance.<p>
143: *
144: * @return a clone of this instance
145: */
146: public Object clone() {
147:
148: return new CmsSite(getSiteRoot(), (CmsUUID) getSiteRootUUID()
149: .clone(), getTitle(), (CmsSiteMatcher) getSiteMatcher()
150: .clone(), String.valueOf(getPosition()));
151: }
152:
153: /**
154: * @see java.lang.Comparable#compareTo(java.lang.Object)
155: */
156: public int compareTo(Object that) {
157:
158: if (that == this ) {
159: return 0;
160: }
161: if (that instanceof CmsSite) {
162: float thatPos = ((CmsSite) that).getPosition();
163: // please note: can't just subtract and cast to int here because of float precision loss
164: if (m_position == thatPos) {
165: if (m_position == Float.MAX_VALUE) {
166: // if they both do not have any position, sort by title
167: return m_title.compareTo(((CmsSite) that)
168: .getTitle());
169: }
170: return 0;
171: }
172: return (m_position < thatPos) ? -1 : 1;
173: }
174: return 0;
175: }
176:
177: /**
178: * @see java.lang.Object#equals(java.lang.Object)
179: */
180: public boolean equals(Object obj) {
181:
182: if (obj == this ) {
183: return true;
184: }
185: if (obj instanceof CmsSite) {
186: ((CmsSite) obj).m_siteMatcher.equals(m_siteMatcher);
187: }
188: return false;
189: }
190:
191: /**
192: * Returns the aliases for this site.<p>
193: *
194: * @return a ArrayList with the aliases
195: */
196: public List getAliases() {
197:
198: return m_aliases;
199: }
200:
201: /**
202: * Returns the sorting position.<p>
203: *
204: * @return the sorting position
205: */
206: public float getPosition() {
207:
208: return m_position;
209: }
210:
211: /**
212: * Returns the secure server url of this site root.<p>
213: *
214: * @return the secure server url
215: */
216: public String getSecureUrl() {
217:
218: return m_secureServer.getUrl();
219: }
220:
221: /**
222: * Returns the server prefix for the given resource in this site, used to distinguish between
223: * secure (https) and non-secure (http) sites.<p>
224: *
225: * This is required since a resource may have an individual "secure" setting using the property
226: * {@link CmsPropertyDefinition#PROPERTY_SECURE}, which means this resource
227: * must be delivered only using a secure protocol.<p>
228: *
229: * The result will look like <code>http://site.enterprise.com:8080/</code> or <code>https://site.enterprise.com/</code>.<p>
230: *
231: * @param cms the current users OpenCms context
232: * @param resource the resource to use
233: *
234: * @return the server prefix for the given resource in this site
235: *
236: * @see #getSecureUrl()
237: * @see #getUrl()
238: */
239: public String getServerPrefix(CmsObject cms, CmsResource resource) {
240:
241: if (equals(OpenCms.getSiteManager().getDefaultSite())) {
242: return OpenCms.getSiteManager().getWorkplaceServer();
243: }
244: boolean secure = false;
245: if (hasSecureServer()) {
246: try {
247: secure = Boolean.valueOf(
248: cms.readPropertyObject(resource,
249: CmsPropertyDefinition.PROPERTY_SECURE,
250: true).getValue()).booleanValue();
251: } catch (CmsException e) {
252: if (LOG.isErrorEnabled()) {
253: LOG.error(e.getLocalizedMessage(), e);
254: }
255: }
256: }
257: return (secure ? getSecureUrl() : getUrl());
258: }
259:
260: /**
261: * Returns the server prefix for the given resource in this site, used to distinguish between
262: * secure (https) and non-secure (http) sites.<p>
263: *
264: * This is required since a resource may have an individual "secure" setting using the property
265: * {@link CmsPropertyDefinition#PROPERTY_SECURE}, which means this resource
266: * must be delivered only using a secure protocol.<p>
267: *
268: * The result will look like <code>http://site.enterprise.com:8080/</code> or <code>https://site.enterprise.com/</code>.<p>
269: *
270: * @param cms the current users OpenCms context
271: * @param resourceName the resource name
272: *
273: * @return the server prefix for the given resource in this site
274: *
275: * @see #getSecureUrl()
276: * @see #getUrl()
277: */
278: public String getServerPrefix(CmsObject cms, String resourceName) {
279:
280: if (equals(OpenCms.getSiteManager().getDefaultSite())) {
281: return OpenCms.getSiteManager().getWorkplaceServer();
282: }
283: boolean secure = false;
284: if (hasSecureServer()) {
285: if (resourceName.startsWith(cms.getRequestContext()
286: .getSiteRoot())) {
287: // make sure this can also be used with a resource root path
288: resourceName = resourceName.substring(cms
289: .getRequestContext().getSiteRoot().length());
290: }
291: try {
292: secure = Boolean.valueOf(
293: cms.readPropertyObject(resourceName,
294: CmsPropertyDefinition.PROPERTY_SECURE,
295: true).getValue()).booleanValue();
296: } catch (CmsException e) {
297: if (LOG.isErrorEnabled()) {
298: LOG.error(e.getLocalizedMessage(), e);
299: }
300: }
301: }
302: return (secure ? getSecureUrl() : getUrl());
303: }
304:
305: /**
306: * Returns the site matcher that describes the URL of this site.<p>
307: *
308: * @return the site matcher that describes the URL of this site
309: */
310: public CmsSiteMatcher getSiteMatcher() {
311:
312: return m_siteMatcher;
313: }
314:
315: /**
316: * Returns the server URL prefix to which this site is mapped.<p>
317: *
318: * @return the server URL prefix to which this site is mapped
319: */
320: public String getSiteRoot() {
321:
322: return m_siteRoot;
323: }
324:
325: /**
326: * Returns the UUID of this site's root directory in the OpenCms VFS.<p>
327: *
328: * @return the UUID of this site's root directory in the OpenCms VFS
329: */
330: public CmsUUID getSiteRootUUID() {
331:
332: return m_siteRootUUID;
333: }
334:
335: /**
336: * Returns the root directory of this site in the OpenCms VFS.<p>
337: *
338: * @return the root directory of this site in the OpenCms VFS
339: */
340: public String getTitle() {
341:
342: return m_title;
343: }
344:
345: /**
346: * Returns the server url of this site root.<p>
347: *
348: * @return the server url
349: */
350: public String getUrl() {
351:
352: return m_siteMatcher.getUrl();
353: }
354:
355: /**
356: * @see java.lang.Object#hashCode()
357: */
358: public int hashCode() {
359:
360: return m_siteRootUUID.hashCode();
361: }
362:
363: /**
364: * Returns true, if the site has a secure server.<p>
365: *
366: * @return true, if the site has a secure server
367: */
368: public boolean hasSecureServer() {
369:
370: return m_secureServer != null;
371: }
372:
373: /**
374: * Returns the exclusive error flag.<p>
375: *
376: * @return <code>true</code> will generate a 404 error,
377: * or <code>false</code> will redirect to secure url.
378: */
379: public boolean isExclusiveError() {
380:
381: return m_exclusiveError;
382: }
383:
384: /**
385: * Returns the exclusive protocol flag.<p>
386: *
387: * @return <code>true</code> secure resources will only be available using the configured secure url,
388: * or <code>false</code> if the uri (protocol + servername) does not really matter.
389: */
390: public boolean isExclusiveUrl() {
391:
392: return m_exclusiveUrl;
393: }
394:
395: /**
396: * Sets the exclusive error flag.<p>
397: *
398: * @param error the exclusive error flag
399: */
400: public void setExclusiveError(boolean error) {
401:
402: m_exclusiveError = error;
403: }
404:
405: /**
406: * Sets the exclusive protocol flag.<p>
407: *
408: * @param exclusive the exclusive protocol flag
409: */
410: public void setExclusiveUrl(boolean exclusive) {
411:
412: m_exclusiveUrl = exclusive;
413: }
414:
415: /**
416: * @see java.lang.Object#toString()
417: */
418: public String toString() {
419:
420: StringBuffer result = new StringBuffer(128);
421: result.append("server: ");
422: result.append(m_siteMatcher != null ? m_siteMatcher.toString()
423: : "null");
424: result.append(" uri: ");
425: result.append(m_siteRoot);
426: result.append(" title: ");
427: result.append(m_title);
428: return result.toString();
429: }
430:
431: /**
432: * Adds an alias for the site.<p>
433: *
434: * @param aliasServer the sitematcher for the alias
435: */
436: protected void addAlias(CmsSiteMatcher aliasServer) {
437:
438: m_aliases.add(aliasServer);
439: }
440:
441: /**
442: * Sets the aliases for the site.<p>
443: *
444: * @param aliases the aliases for the site
445: */
446: protected void setAliases(List aliases) {
447:
448: m_aliases = aliases;
449: }
450:
451: /**
452: * Sets the secure server.<p>
453: *
454: * @param secureServer the sitematcher of the secure server
455: */
456: protected void setSecureServer(CmsSiteMatcher secureServer) {
457:
458: m_secureServer = secureServer;
459: }
460:
461: /**
462: * Sets the site matcher that describes the URL of this site.<p>
463: *
464: * @param siteMatcher the site matcher that describes the URL of this site
465: */
466: protected void setSiteMatcher(CmsSiteMatcher siteMatcher) {
467:
468: m_siteMatcher = siteMatcher;
469: }
470:
471: /**
472: * Sets the server URL prefix to which this site is mapped.<p>
473: *
474: * @param siteRoot the server URL prefix to which this site is mapped
475: */
476: protected void setSiteRoot(String siteRoot) {
477:
478: // site roots must never end with a "/"
479: if (siteRoot.endsWith("/")) {
480: m_siteRoot = siteRoot.substring(0, siteRoot.length() - 1);
481: } else {
482: m_siteRoot = siteRoot;
483: }
484: }
485:
486: /**
487: * Sets the UUID of this site's root directory in the OpenCms VFS.<p>
488: *
489: * @param siteRootUUID the UUID of this site's root directory in the OpenCms VFS
490: */
491: protected void setSiteRootUUID(CmsUUID siteRootUUID) {
492:
493: m_siteRootUUID = siteRootUUID;
494: }
495:
496: /**
497: * Sets the display title of this site in the OpenCms VFS.<p>
498: *
499: * @param name the display title of this site in the OpenCms VFS
500: */
501: protected void setTitle(String name) {
502:
503: m_title = name;
504: }
505: }
|