KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > site > CmsSiteMatcher


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/site/CmsSiteMatcher.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.19 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.site;
33
34 import org.opencms.util.CmsStringUtil;
35
36 /**
37  * A matcher object to compare request data against the configured sites.<p>
38  *
39  * @author Alexander Kandzior
40  *
41  * @version $Revision: 1.19 $
42  *
43  * @since 6.0.0
44  */

45 public final class CmsSiteMatcher implements Cloneable JavaDoc {
46
47     /** Constant for the "http" port. */
48     private static final int PORT_HTTP = 80;
49
50     /** Constant for the "https" port. */
51     private static final int PORT_HTTPS = 443;
52
53     /** Constant for the "http" scheme. */
54     private static final String JavaDoc SCHEME_HTTP = "http";
55
56     /** Constant for the "https" scheme. */
57     private static final String JavaDoc SCHEME_HTTPS = "https";
58
59     /** Wildcard for string matching. */
60     private static final String JavaDoc WILDCARD = "*";
61
62     /** Default matcher that always matches all other Site matchers. */
63     public static final CmsSiteMatcher DEFAULT_MATCHER = new CmsSiteMatcher(WILDCARD, WILDCARD, 0);
64
65     /** Hashcode buffer to save multiple calculations. */
66     private Integer JavaDoc m_hashCode;
67
68     /** The hostname (e.g. localhost) which is required to access this site. */
69     private String JavaDoc m_serverName;
70
71     /** The port (e.g. 80) which is required to access this site. */
72     private int m_serverPort;
73
74     /** The protocol (e.g. "http", "https") which is required to access this site. */
75     private String JavaDoc m_serverProtocol;
76
77     /**
78      * Construct a new site matcher from a String which should be in default URL notation.<p>
79      *
80      * If no port is provided, the default port 80 or 443 will be used for http or https respectively.
81      * If no protocol is provided, the default protocol "http" will be used.
82      *
83      * @param serverString the String, e.g. http://localhost:8080
84      */

85     public CmsSiteMatcher(String JavaDoc serverString) {
86
87         if (serverString == null) {
88             init(WILDCARD, WILDCARD, 0);
89             return;
90         }
91         // remove whitespace
92
serverString = serverString.trim();
93         // cut trailing "/"
94
if (serverString.endsWith("/")) {
95             serverString = serverString.substring(0, serverString.length() - 1);
96         }
97         int pos, serverPort;
98         String JavaDoc serverProtocol, serverName;
99         // check for protocol
100
pos = serverString.indexOf("://");
101         if (pos >= 0) {
102             serverProtocol = serverString.substring(0, pos);
103             serverString = serverString.substring(pos + 3);
104         } else {
105             serverProtocol = SCHEME_HTTP;
106         }
107         // check for server name and port
108
pos = serverString.indexOf(":");
109         if (pos >= 0) {
110             serverName = serverString.substring(0, pos);
111             try {
112                 String JavaDoc port = serverString.substring(pos + 1);
113                 pos = port.indexOf("/");
114                 if (pos >= 0) {
115                     port = port.substring(0, pos);
116                 }
117                 serverPort = Integer.valueOf(port).intValue();
118             } catch (NumberFormatException JavaDoc e) {
119                 serverPort = PORT_HTTP;
120             }
121         } else {
122             serverName = serverString;
123             if (SCHEME_HTTPS.equals(serverProtocol)) {
124                 serverPort = PORT_HTTPS;
125             } else {
126                 serverPort = PORT_HTTP;
127             }
128         }
129
130         // cut trailing path in server name
131
pos = serverName.indexOf("/");
132         if (pos >= 0) {
133             serverName = serverName.substring(0, pos);
134         }
135
136         // initialize members
137
init(serverProtocol, serverName, serverPort);
138     }
139
140     /**
141      * Constructs a new site matcher object.<p>
142      *
143      * @param serverProtocol to protocol required to access this site
144      * @param serverName the server URL prefix to which this site is mapped
145      * @param serverPort the port required to access this site
146      */

147     public CmsSiteMatcher(String JavaDoc serverProtocol, String JavaDoc serverName, int serverPort) {
148
149         init(serverProtocol, serverName, serverPort);
150     }
151
152     /**
153      * Returns a clone of this Objects instance.<p>
154      *
155      * @return a clone of this instance
156      */

157     public Object JavaDoc clone() {
158
159         return new CmsSiteMatcher(m_serverProtocol, m_serverName, m_serverPort);
160     }
161
162     /**
163      * @see java.lang.Object#equals(java.lang.Object)
164      */

165     public boolean equals(Object JavaDoc obj) {
166
167         if (obj == this) {
168             return true;
169         }
170         if (!(obj instanceof CmsSiteMatcher)) {
171             return false;
172         }
173         // if one of the object is the default matcher the result is always true
174
if ((this == DEFAULT_MATCHER) || (obj == DEFAULT_MATCHER)) {
175             return true;
176         }
177         CmsSiteMatcher other = (CmsSiteMatcher)obj;
178         return (m_serverPort == other.m_serverPort)
179             && m_serverName.equals(other.m_serverName)
180             && m_serverProtocol.equals(other.m_serverProtocol);
181     }
182
183     /**
184      * Returns the hostname (e.g. localhost) which is required to access this site.<p>
185      *
186      * @return the hostname (e.g. localhost) which is required to access this site
187      */

188     public String JavaDoc getServerName() {
189
190         return m_serverName;
191     }
192
193     /**
194      * Returns the port (e.g. 80) which is required to access this site.<p>
195      *
196      * @return the port (e.g. 80) which is required to access this site
197      */

198     public int getServerPort() {
199
200         return m_serverPort;
201     }
202
203     /**
204      * Returns the protocol (e.g. "http", "https") which is required to access this site.<p>
205      *
206      * @return the protocol (e.g. "http", "https") which is required to access this site
207      */

208     public String JavaDoc getServerProtocol() {
209
210         return m_serverProtocol;
211     }
212
213     /**
214      * Returns the url of this site matcher.<p>
215      *
216      * @return the url, i.e. {protocol}://{servername}[:{port}], port appened only if != 80
217      */

218     public String JavaDoc getUrl() {
219
220         return m_serverProtocol
221             + "://"
222             + m_serverName
223             + (((m_serverPort != PORT_HTTP) && (m_serverPort != PORT_HTTPS)) ? ":" + m_serverPort : "");
224     }
225
226     /**
227      * @see java.lang.Object#hashCode()
228      */

229     public int hashCode() {
230
231         if (m_hashCode == null) {
232             m_hashCode = new Integer JavaDoc(toString().hashCode());
233         }
234         return m_hashCode.intValue();
235     }
236
237     /**
238      * @see java.lang.Object#toString()
239      */

240     public String JavaDoc toString() {
241
242         StringBuffer JavaDoc result = new StringBuffer JavaDoc(32);
243         if ((m_serverProtocol != null) && !(WILDCARD.equals(m_serverProtocol))) {
244             result.append(m_serverProtocol);
245             result.append("://");
246         }
247         result.append(m_serverName);
248         if ((m_serverPort > 0)
249             && (!(SCHEME_HTTP.equals(m_serverProtocol) && (m_serverPort == PORT_HTTP)))
250             && (!(SCHEME_HTTPS.equals(m_serverProtocol) && (m_serverPort == PORT_HTTPS)))) {
251             result.append(":");
252             result.append(m_serverPort);
253         }
254         return result.toString();
255     }
256
257     /**
258      * Sets the hostname (e.g. localhost) which is required to access this site.<p>
259      *
260      * Setting the hostname to "*" is a wildcard that matches all hostnames
261      *
262      * @param serverName the hostname (e.g. localhost) which is required to access this site
263      */

264     protected void setServerName(String JavaDoc serverName) {
265
266         if (CmsStringUtil.isEmpty(serverName) || (WILDCARD.equals(serverName))) {
267             m_serverName = WILDCARD;
268         } else {
269             m_serverName = serverName.trim();
270         }
271     }
272
273     /**
274      * Sets the port (e.g. 80) which is required to access this site.<p>
275      *
276      * Setting the port to 0 (zero) is a wildcard that matches all ports
277      *
278      * @param serverPort the port (e.g. 80) which is required to access this site
279      */

280     protected void setServerPort(int serverPort) {
281
282         m_serverPort = serverPort;
283         if (m_serverPort < 0) {
284             m_serverPort = 0;
285         }
286     }
287
288     /**
289      * Sets the protocol (e.g. "http", "https") which is required to access this site.<p>
290      *
291      * Setting the protocol to "*" is a wildcard that matches all protocols.<p>
292      *
293      * @param serverProtocol the protocol (e.g. "http", "https") which is required to access this site
294      */

295     protected void setServerProtocol(String JavaDoc serverProtocol) {
296
297         if (CmsStringUtil.isEmpty(serverProtocol) || (WILDCARD.equals(serverProtocol))) {
298             m_serverProtocol = WILDCARD;
299         } else {
300             int pos = serverProtocol.indexOf("/");
301             if (pos > 0) {
302                 m_serverProtocol = serverProtocol.substring(0, pos).toLowerCase();
303             } else {
304                 m_serverProtocol = serverProtocol.toLowerCase().trim();
305             }
306         }
307     }
308
309     /**
310      * Inits the member variables.<p>
311      *
312      * @param serverProtocol to protocol required to access this site
313      * @param serverName the server URL prefix to which this site is mapped
314      * @param serverPort the port required to access this site
315      */

316     private void init(String JavaDoc serverProtocol, String JavaDoc serverName, int serverPort) {
317
318         setServerProtocol(serverProtocol);
319         setServerName(serverName);
320         setServerPort(serverPort);
321     }
322 }
Popular Tags