KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > uri > URIHelper


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: URIHelper.java,v 1.3 2005/05/27 13:58:03 tanderson Exp $
44  */

45 package org.exolab.jms.net.uri;
46
47 import java.net.InetAddress JavaDoc;
48 import java.net.UnknownHostException JavaDoc;
49
50 import org.exolab.jms.common.security.BasicPrincipal;
51
52
53 /**
54  * Helper for operations on URIs
55  *
56  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
57  * @version $Revision: 1.3 $ $Date: 2005/05/27 13:58:03 $
58  * @see URI
59  */

60 public final class URIHelper {
61
62     /**
63      * Prevent construction of helper class
64      */

65     private URIHelper() {
66     }
67
68     /**
69      * Helper to create an URI
70      *
71      * @param scheme the URI scheme
72      * @param host the host
73      * @param port the port
74      * @return a new URI
75      * @throws InvalidURIException if any argument is invalid
76      */

77     public static URI create(String JavaDoc scheme, String JavaDoc host, int port)
78             throws InvalidURIException {
79         return create(scheme, host, port, "/");
80     }
81
82     /**
83      * Helper to create an URI
84      *
85      * @param scheme the URI scheme
86      * @param host the host
87      * @param port the port
88      * @param path the path
89      * @return a new URI
90      * @throws InvalidURIException if any argument is invalid
91      */

92     public static URI create(String JavaDoc scheme, String JavaDoc host, int port, String JavaDoc path)
93             throws InvalidURIException {
94         URI result;
95         try {
96             result = new URI(scheme, null, host, port, path, null, null);
97         } catch (URI.MalformedURIException exception) {
98             throw new InvalidURIException(exception.getMessage());
99         }
100         return result;
101     }
102
103     /**
104      * Helper to parse a String to an URI. If no path was specified in, the URI,
105      * it will default to <code>"/"</code>
106      *
107      * @param uri the URI to parse
108      * @return the parsed URI
109      * @throws InvalidURIException if <code>uri</code> is invalid
110      */

111     public static URI parse(String JavaDoc uri) throws InvalidURIException {
112         URI result;
113         try {
114             result = new URI(uri);
115             fixPath(result);
116         } catch (URI.MalformedURIException exception) {
117             throw new InvalidURIException(exception.getMessage());
118         }
119         return result;
120     }
121
122     /**
123      * Helper to parse an URI, verifying that it has the correct scheme
124      *
125      * @param uri the URI to parse
126      * @param scheme the expected scheme
127      * @return the parsed URI
128      * @throws InvalidURIException if <code>uri</code> is invalid
129      */

130     public static URI parse(String JavaDoc uri, String JavaDoc scheme)
131             throws InvalidURIException {
132         URI result = parse(uri);
133         if (!result.getScheme().equals(scheme)) {
134             throw new InvalidURIException(
135                     "Invalid scheme: " + result.getScheme());
136         }
137         return result;
138     }
139
140     /**
141      * Helper to parse an URI, verifying that it has the correct scheme, host
142      * and port specification, and no path
143      *
144      * @param uri the URI to parse
145      * @param scheme the expected scheme
146      * @return the parsed URI
147      * @throws InvalidURIException if <code>uri</code> is invalid
148      */

149     public static URI parseHostPort(String JavaDoc uri, String JavaDoc scheme)
150             throws InvalidURIException {
151         URI result = parse(uri, scheme);
152         if (result.getHost() == null) {
153             throw new InvalidURIException("No host specified in URI: " + uri);
154         }
155         if (result.getPort() == -1) {
156             throw new InvalidURIException("No port specified in URI: " + uri);
157         }
158         if (result.getPath() != null && !result.getPath().equals("")
159                 && !result.getPath().equals("/")) {
160             throw new InvalidURIException(
161                     "URI must not specify a path: " + uri);
162         }
163         return result;
164     }
165
166     /**
167      * Helper to convert the host name portion of a URI to its corresponding IP
168      * address. If the URI doesn't contain a host, or the host is specified as
169      * an IP address, or the IP address can't be determined, then the URI is
170      * returned unchanged.
171      *
172      * @param uri the uri to convert
173      * @return the converted URI
174      */

175     public static URI convertHostToAddress(URI uri) {
176         URI result = uri;
177         String JavaDoc host = uri.getHost();
178         if (host != null && !host.equals("")) {
179             try {
180                 InetAddress JavaDoc address = InetAddress.getByName(host);
181                 result = new URI(uri.getScheme(), uri.getUserinfo(),
182                                  address.getHostAddress(), uri.getPort(),
183                                  uri.getPath(), uri.getQueryString(),
184                                  uri.getFragment());
185                 fixPath(result);
186             } catch (UnknownHostException JavaDoc ignore) {
187             } catch (URI.MalformedURIException exception) {
188                 // should *never* happen
189
throw new IllegalArgumentException JavaDoc("Failed to construct URI: "
190                                                    + exception.getMessage());
191             }
192         }
193         return result;
194     }
195
196     /**
197      * Returns a {@link BasicPrincipal} corresponding to the user info specified
198      * in a URI
199      *
200      * @param uri the URI to get the user info from
201      * @return a new BasicPrincipal containing the user info, or
202      * <code>null</code> if none was specified
203      */

204     public static BasicPrincipal getPrincipal(URI uri) {
205         BasicPrincipal principal = null;
206         String JavaDoc userinfo = uri.getUserinfo();
207         if (userinfo != null && userinfo.length() != 0) {
208             int index = userinfo.indexOf(":");
209             String JavaDoc user;
210             String JavaDoc password = "";
211             if (index != -1) {
212                 user = userinfo.substring(0, index);
213                 password = userinfo.substring(index + 1);
214             } else {
215                 user = userinfo;
216             }
217             principal = new BasicPrincipal(user, password);
218         }
219         return principal;
220     }
221
222     /**
223      * Ensures that the path is set to <code>"/"</code> if not specified in a
224      * URI, so equality tests return true for instances of the form a://b:1234
225      * == a://b:1234/
226      *
227      * @param uri the URI the fix the path for
228      * @throws URI.MalformedURIException if the path can't be set
229      */

230     private static void fixPath(URI uri) throws URI.MalformedURIException {
231         String JavaDoc path = uri.getPath();
232         if (path == null || path.equals("")) {
233             uri.setPath("/");
234         }
235     }
236 }
237
Popular Tags