001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/site/CmsSiteMatcher.java,v $
003: * Date : $Date: 2008-02-27 12:05:50 $
004: * Version: $Revision: 1.21 $
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.util.CmsStringUtil;
035:
036: /**
037: * A matcher object to compare request data against the configured sites.<p>
038: *
039: * @author Alexander Kandzior
040: *
041: * @version $Revision: 1.21 $
042: *
043: * @since 6.0.0
044: */
045: public final class CmsSiteMatcher implements Cloneable {
046:
047: /** Constant for the "http" port. */
048: private static final int PORT_HTTP = 80;
049:
050: /** Constant for the "https" port. */
051: private static final int PORT_HTTPS = 443;
052:
053: /** Constant for the "http" scheme. */
054: private static final String SCHEME_HTTP = "http";
055:
056: /** Constant for the "https" scheme. */
057: private static final String SCHEME_HTTPS = "https";
058:
059: /** Wildcard for string matching. */
060: private static final String WILDCARD = "*";
061:
062: /** Default matcher that always matches all other Site matchers. */
063: public static final CmsSiteMatcher DEFAULT_MATCHER = new CmsSiteMatcher(
064: WILDCARD, WILDCARD, 0);
065:
066: /** Hashcode buffer to save multiple calculations. */
067: private Integer m_hashCode;
068:
069: /** The hostname (e.g. localhost) which is required to access this site. */
070: private String m_serverName;
071:
072: /** The port (e.g. 80) which is required to access this site. */
073: private int m_serverPort;
074:
075: /** The protocol (e.g. "http", "https") which is required to access this site. */
076: private String m_serverProtocol;
077:
078: /**
079: * Construct a new site matcher from a String which should be in default URL notation.<p>
080: *
081: * If no port is provided, the default port 80 or 443 will be used for http or https respectively.
082: * If no protocol is provided, the default protocol "http" will be used.
083: *
084: * @param serverString the String, e.g. http://localhost:8080
085: */
086: public CmsSiteMatcher(String serverString) {
087:
088: if (serverString == null) {
089: init(WILDCARD, WILDCARD, 0);
090: return;
091: }
092: // remove whitespace
093: serverString = serverString.trim();
094: // cut trailing "/"
095: if (serverString.endsWith("/")) {
096: serverString = serverString.substring(0, serverString
097: .length() - 1);
098: }
099: int pos, serverPort;
100: String serverProtocol, serverName;
101: // check for protocol
102: pos = serverString.indexOf("://");
103: if (pos >= 0) {
104: serverProtocol = serverString.substring(0, pos);
105: serverString = serverString.substring(pos + 3);
106: } else {
107: serverProtocol = SCHEME_HTTP;
108: }
109: // check for server name and port
110: pos = serverString.indexOf(":");
111: if (pos >= 0) {
112: serverName = serverString.substring(0, pos);
113: try {
114: String port = serverString.substring(pos + 1);
115: pos = port.indexOf("/");
116: if (pos >= 0) {
117: port = port.substring(0, pos);
118: }
119: serverPort = Integer.valueOf(port).intValue();
120: } catch (NumberFormatException e) {
121: serverPort = PORT_HTTP;
122: }
123: } else {
124: serverName = serverString;
125: if (SCHEME_HTTPS.equals(serverProtocol)) {
126: serverPort = PORT_HTTPS;
127: } else {
128: serverPort = PORT_HTTP;
129: }
130: }
131:
132: // cut trailing path in server name
133: pos = serverName.indexOf("/");
134: if (pos >= 0) {
135: serverName = serverName.substring(0, pos);
136: }
137:
138: // initialize members
139: init(serverProtocol, serverName, serverPort);
140: }
141:
142: /**
143: * Constructs a new site matcher object.<p>
144: *
145: * @param serverProtocol to protocol required to access this site
146: * @param serverName the server URL prefix to which this site is mapped
147: * @param serverPort the port required to access this site
148: */
149: public CmsSiteMatcher(String serverProtocol, String serverName,
150: int serverPort) {
151:
152: init(serverProtocol, serverName, serverPort);
153: }
154:
155: /**
156: * Returns a clone of this Objects instance.<p>
157: *
158: * @return a clone of this instance
159: */
160: public Object clone() {
161:
162: return new CmsSiteMatcher(m_serverProtocol, m_serverName,
163: m_serverPort);
164: }
165:
166: /**
167: * @see java.lang.Object#equals(java.lang.Object)
168: */
169: public boolean equals(Object obj) {
170:
171: if (obj == this ) {
172: return true;
173: }
174: if (!(obj instanceof CmsSiteMatcher)) {
175: return false;
176: }
177: // if one of the object is the default matcher the result is always true
178: if ((this == DEFAULT_MATCHER) || (obj == DEFAULT_MATCHER)) {
179: return true;
180: }
181: CmsSiteMatcher other = (CmsSiteMatcher) obj;
182: return (m_serverPort == other.m_serverPort)
183: && m_serverName.equals(other.m_serverName)
184: && m_serverProtocol.equals(other.m_serverProtocol);
185: }
186:
187: /**
188: * Returns the hostname (e.g. localhost) which is required to access this site.<p>
189: *
190: * @return the hostname (e.g. localhost) which is required to access this site
191: */
192: public String getServerName() {
193:
194: return m_serverName;
195: }
196:
197: /**
198: * Returns the port (e.g. 80) which is required to access this site.<p>
199: *
200: * @return the port (e.g. 80) which is required to access this site
201: */
202: public int getServerPort() {
203:
204: return m_serverPort;
205: }
206:
207: /**
208: * Returns the protocol (e.g. "http", "https") which is required to access this site.<p>
209: *
210: * @return the protocol (e.g. "http", "https") which is required to access this site
211: */
212: public String getServerProtocol() {
213:
214: return m_serverProtocol;
215: }
216:
217: /**
218: * Returns the url of this site matcher.<p>
219: *
220: * @return the url, i.e. {protocol}://{servername}[:{port}], port appened only if != 80
221: */
222: public String getUrl() {
223:
224: return m_serverProtocol
225: + "://"
226: + m_serverName
227: + (((m_serverPort != PORT_HTTP) && (m_serverPort != PORT_HTTPS)) ? ":"
228: + m_serverPort
229: : "");
230: }
231:
232: /**
233: * @see java.lang.Object#hashCode()
234: */
235: public int hashCode() {
236:
237: if (m_hashCode == null) {
238: m_hashCode = new Integer(toString().hashCode());
239: }
240: return m_hashCode.intValue();
241: }
242:
243: /**
244: * @see java.lang.Object#toString()
245: */
246: public String toString() {
247:
248: StringBuffer result = new StringBuffer(32);
249: if ((m_serverProtocol != null)
250: && !(WILDCARD.equals(m_serverProtocol))) {
251: result.append(m_serverProtocol);
252: result.append("://");
253: }
254: result.append(m_serverName);
255: if ((m_serverPort > 0)
256: && (!(SCHEME_HTTP.equals(m_serverProtocol) && (m_serverPort == PORT_HTTP)))
257: && (!(SCHEME_HTTPS.equals(m_serverProtocol) && (m_serverPort == PORT_HTTPS)))) {
258: result.append(":");
259: result.append(m_serverPort);
260: }
261: return result.toString();
262: }
263:
264: /**
265: * Sets the hostname (e.g. localhost) which is required to access this site.<p>
266: *
267: * Setting the hostname to "*" is a wildcard that matches all hostnames
268: *
269: * @param serverName the hostname (e.g. localhost) which is required to access this site
270: */
271: protected void setServerName(String serverName) {
272:
273: if (CmsStringUtil.isEmpty(serverName)
274: || (WILDCARD.equals(serverName))) {
275: m_serverName = WILDCARD;
276: } else {
277: m_serverName = serverName.trim();
278: }
279: }
280:
281: /**
282: * Sets the port (e.g. 80) which is required to access this site.<p>
283: *
284: * Setting the port to 0 (zero) is a wildcard that matches all ports
285: *
286: * @param serverPort the port (e.g. 80) which is required to access this site
287: */
288: protected void setServerPort(int serverPort) {
289:
290: m_serverPort = serverPort;
291: if (m_serverPort < 0) {
292: m_serverPort = 0;
293: }
294: }
295:
296: /**
297: * Sets the protocol (e.g. "http", "https") which is required to access this site.<p>
298: *
299: * Setting the protocol to "*" is a wildcard that matches all protocols.<p>
300: *
301: * @param serverProtocol the protocol (e.g. "http", "https") which is required to access this site
302: */
303: protected void setServerProtocol(String serverProtocol) {
304:
305: if (CmsStringUtil.isEmpty(serverProtocol)
306: || (WILDCARD.equals(serverProtocol))) {
307: m_serverProtocol = WILDCARD;
308: } else {
309: int pos = serverProtocol.indexOf("/");
310: if (pos > 0) {
311: m_serverProtocol = serverProtocol.substring(0, pos)
312: .toLowerCase();
313: } else {
314: m_serverProtocol = serverProtocol.toLowerCase().trim();
315: }
316: }
317: }
318:
319: /**
320: * Inits the member variables.<p>
321: *
322: * @param serverProtocol to protocol required to access this site
323: * @param serverName the server URL prefix to which this site is mapped
324: * @param serverPort the port required to access this site
325: */
326: private void init(String serverProtocol, String serverName,
327: int serverPort) {
328:
329: setServerProtocol(serverProtocol);
330: setServerName(serverName);
331: setServerPort(serverPort);
332: }
333: }
|