KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > repositorylocation > RepositoryInformation


1 /*
2  * Copyright 2004 Hippo Webworks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package nl.hippo.cms.repositorylocation;
17
18 /**
19  * <p>
20  * Information about a base location in a WebDAV repository. It can be used to
21  * obtain absolute URIs for URIs which are relative to a base location.
22  * </p>
23  *
24  * @author Johan Stuyts
25  */

26 public class RepositoryInformation
27 {
28
29     /**
30      * <p>
31      * The host name on which the WebDAV server is running.
32      * </p>
33      */

34     private String JavaDoc m_host;
35
36     /**
37      * <p>
38      * The port number on which the WebDAV server is listening.
39      * </p>
40      */

41     private int m_port;
42
43     /**
44      * <p>
45      * The authentication realm of the WebDAV server.
46      * </p>
47      */

48     private String JavaDoc m_realm;
49
50     /**
51      * <p>
52      * The username to use to authenticate.
53      * </p>
54      */

55     private String JavaDoc m_username;
56
57     /**
58      * <p>
59      * The password to use to authenticate.
60      * </p>
61      */

62     private String JavaDoc m_password;
63
64     /**
65      * <p>
66      * The URI pointing to the root location of the WebDAV server. It is
67      * relative to the host.
68      * </p>
69      */

70     private String JavaDoc m_rootUri;
71
72     /**
73      * <p>
74      * The URI pointing to the base location. It is relative to the root
75      * location.
76      * </p>
77      */

78     private String JavaDoc m_baseUri;
79
80     /**
81      * <p>
82      * Create a repository information object.
83      * </p>
84      *
85      * @param host
86      * The host on which the WebDAV server is running.
87      * @param port
88      * The port on which the WebDAV server is running.
89      * @param realm
90      * The authentication realm the WebDAV server uses.
91      * @param username
92      * The username to be used to log in to the WebDAV server.
93      * @param password
94      * The password to be used to log in to the WebDAV server.
95      * @param rootUri
96      * The URI to the root of the WebDAV server relative to the
97      * host's root. Prefix and postfix slashes will be ignored.
98      * @param baseUri
99      * The URI to the document location relative to the absolute root
100      * URI. Prefix and postfix slashes will be ignored.
101      */

102     public RepositoryInformation(String JavaDoc host, int port, String JavaDoc realm, String JavaDoc username,
103             String JavaDoc password, String JavaDoc rootUri, String JavaDoc baseUri)
104     {
105         super();
106
107         m_host = host;
108         m_port = port;
109         m_realm = realm;
110         m_username = username;
111         m_password = password;
112         m_rootUri = trimSlashes(rootUri);
113         m_baseUri = trimSlashes(baseUri);
114     }
115
116     public String JavaDoc getHost()
117     {
118         return m_host;
119     }
120
121     public int getPort()
122     {
123         return m_port;
124     }
125
126     public String JavaDoc getRealm()
127     {
128         return m_realm;
129     }
130
131     public String JavaDoc getUsername()
132     {
133         return m_username;
134     }
135
136     public String JavaDoc getPassword()
137     {
138         return m_password;
139     }
140
141     public String JavaDoc getRootUri()
142     {
143         return m_rootUri;
144     }
145
146     public String JavaDoc getBaseUri()
147     {
148         return m_baseUri;
149     }
150
151     /**
152      * <p>
153      * Get the absolute URI to the base location represented by this object.
154      * </p>
155      *
156      * @return The absolute URI to the base location.
157      */

158     public String JavaDoc getAbsoluteBaseUri()
159     {
160         return getAbsoluteBaseUri(false);
161     }
162
163     /**
164      * <p>
165      * Get the absolute URI composed of the base URI represented by this object
166      * and a user-supplied relative URI.
167      * </p>
168      *
169      * @param uri
170      * The user-supplied relative URI.
171      * @return An absolute URI to the resource referenced by <code>uri</code>.
172      */

173     public String JavaDoc getAbsoluteUri(String JavaDoc uri)
174     {
175         StringBuffer JavaDoc absoluteUri = new StringBuffer JavaDoc(1000);
176
177         absoluteUri.append(getAbsoluteBaseUri());
178         absoluteUri.append('/');
179         absoluteUri.append(trimSlashes(uri));
180
181         return absoluteUri.toString();
182     }
183
184     /**
185      * <p>
186      * Get the absolute URI to the base location represented by this object with
187      * or without the credentials.
188      * </p>
189      *
190      * @param includeCredentials
191      * Whether or not to include the credentials in the URI.
192      * @return The absolute URI to the base location.
193      */

194     private String JavaDoc getAbsoluteBaseUri(boolean includeCredentials)
195     {
196         StringBuffer JavaDoc absoluteBaseUri = new StringBuffer JavaDoc(1000);
197
198         absoluteBaseUri.append("http://");
199         if (includeCredentials)
200         {
201             absoluteBaseUri.append(m_username);
202             absoluteBaseUri.append(':');
203             absoluteBaseUri.append(m_password);
204             absoluteBaseUri.append('@');
205         }
206         absoluteBaseUri.append(m_host);
207         absoluteBaseUri.append(':');
208         absoluteBaseUri.append(m_port);
209         absoluteBaseUri.append('/');
210         String JavaDoc rootUriWithoutSurroundingSlashes = m_rootUri;
211         if (!rootUriWithoutSurroundingSlashes.equals(""))
212         {
213             absoluteBaseUri.append(rootUriWithoutSurroundingSlashes);
214             absoluteBaseUri.append('/');
215         }
216         String JavaDoc baseUriWithoutSurroundingSlashes = m_baseUri;
217         absoluteBaseUri.append(baseUriWithoutSurroundingSlashes);
218
219         return absoluteBaseUri.toString();
220     }
221
222     /**
223      * <p>
224      * Get the hash code of this object.
225      * </p>
226      *
227      * @return The hash code of this object.
228      */

229     public int hashCode()
230     {
231         int result;
232
233         result = m_host.hashCode();
234         result |= m_port;
235         result |= m_realm.hashCode();
236         result |= m_username.hashCode();
237         result |= m_password.hashCode();
238         result |= m_rootUri.hashCode();
239         result |= m_baseUri.hashCode();
240
241         return result;
242     }
243
244     /**
245      * <p>
246      * Compare this object with another object.
247      * </p>
248      *
249      * @param other
250      * The object to compare this object to.
251      * @return <code>true</code> if this object equals <code>other</code>,
252      * <code>false</code> otherwise.
253      */

254     public boolean equals(Object JavaDoc other)
255     {
256         boolean result;
257
258         if (other == null || !(other instanceof RepositoryInformation))
259         {
260             result = false;
261         }
262         else if (other == this)
263         {
264             result = true;
265         }
266         else
267         {
268             RepositoryInformation otherRepoInfo = (RepositoryInformation) other;
269
270             result = otherRepoInfo.getHost().equals(m_host);
271             result = result && otherRepoInfo.getPort() == m_port;
272             result = result && otherRepoInfo.getRealm().equals(m_realm);
273             result = result && otherRepoInfo.getUsername().equals(m_username);
274             result = result && otherRepoInfo.getPassword().equals(m_password);
275             result = result && otherRepoInfo.getRootUri().equals(m_rootUri);
276             result = result && otherRepoInfo.getBaseUri().equals(m_baseUri);
277         }
278
279         return result;
280     }
281
282     /**
283      * <p>
284      * Get a <code>String</code> representation of this object.
285      * </p>
286      *
287      * @return The <code>String</code> representation of this object.
288      */

289     public String JavaDoc toString()
290     {
291         return "Repository: " + getAbsoluteBaseUri(true);
292     }
293
294     /**
295      * <p>
296      * Remove slashes from the front and the back of <code>text</code>.
297      * </p>
298      *
299      * @param text
300      * The text to remove the slashes from.
301      * @return <code>text</code> with slashed removed from the front and the
302      * back.
303      */

304     private String JavaDoc trimSlashes(String JavaDoc text)
305     {
306         int indexOfNonSlash = 0;
307         while (indexOfNonSlash < text.length() && text.charAt(indexOfNonSlash) == '/')
308         {
309             indexOfNonSlash += 1;
310         }
311         text = text.substring(indexOfNonSlash);
312
313         indexOfNonSlash = text.length() - 1;
314         while (indexOfNonSlash >= 0 && text.charAt(indexOfNonSlash) == '/')
315         {
316             indexOfNonSlash -= 1;
317         }
318         text = text.substring(0, indexOfNonSlash + 1);
319
320         return text;
321     }
322 }
Popular Tags