1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: UrlInfo.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.util;
27   
28   import java.net.*;
29   
30   import java.util.*;
31   
32   import org.apache.log4j.Logger;
33   
34   public class UrlInfo
35   {
36       private static final Logger log = Logger.getLogger(UrlInfo.class.getName());
37       private String url;
38       private String protocol;
39       private String host;
40       private int port;
41       private String path;
42       private String query;
43       private Map params;
44   
45       public UrlInfo(String url, String[] requiredParameters)
46               throws MalformedURLException
47       {
48           this(url);
49   
50           if ((requiredParameters != null) && (requiredParameters.length > 0))
51           {
52               if (params == null)
53               {
54                   throw new MalformedURLException("Missing Required Parameters");
55               }
56               else
57               {
58                   for (int i = 0; i < requiredParameters.length; i++)
59                   {
60                       if (params.get(requiredParameters[i]) == null)
61                       {
62                           throw new MalformedURLException(
63                                   "Missing Required Parameter: "
64                                   + requiredParameters[i]);
65                       }
66                   }
67               }
68           }
69       }
70   
71       public UrlInfo(String url)
72               throws MalformedURLException
73       {
74           if (url == null)
75           {
76               throw new MalformedURLException("Null Pointer Not Allowed");
77           }
78   
79           this.url = url;
80           parse();
81       }
82   
83       public String getUrl()
84       {
85           return url;
86       }
87   
88       public String getProtocol()
89       {
90           return protocol;
91       }
92   
93       public String getHost()
94       {
95           return host;
96       }
97   
98       public int getPort()
99       {
100          return port;
101      }
102  
103      public String getPath()
104      {
105          return path;
106      }
107  
108      public String getQuery()
109      {
110          return query;
111      }
112  
113      public Map getParameters()
114      {
115          return params;
116      }
117  
118      public String getParameterValue(String name)
119      {
120          return (String) params.get(name);
121      }
122  
123      private void parse() throws MalformedURLException
124      {
125          boolean debug = log.isDebugEnabled();
126          int index = url.indexOf(':');
127  
128          if (index == -1)
129          {
130              throw new MalformedURLException("Unable to find protocol in URL");
131          }
132  
133          protocol = url.substring(0, index);
134          index++;
135  
136          if (debug)
137          {
138              log.debug("1 url.charAt(" + index + ") = " + url.charAt(index));
139          }
140  
141          int length = url.length();
142  
143          while ((url.charAt(index) == '/') && (index < length))
144          {
145              if (debug)
146              {
147                  log.debug("2 url.charAt(" + index + ") = " + url.charAt(index));
148              }
149  
150              index++;
151          }
152  
153          if (debug)
154          {
155              log.debug("3 url.charAt(" + index + ") = " + url.charAt(index));
156          }
157  
158          if (index == length)
159          {
160              throw new MalformedURLException("Missing Host");
161          }
162  
163          int hostStartIndex = index;
164          String hostPort = url.substring(hostStartIndex);
165  
166          if (debug)
167          {
168              log.debug("hostPort = " + hostPort);
169          }
170  
171          int hostEndIndex = hostPort.indexOf('/');
172  
173          if (hostEndIndex == -1)
174          {
175              hostEndIndex = hostPort.length();
176          }
177  
178          hostPort = hostPort.substring(0, hostEndIndex);
179  
180          if (debug)
181          {
182              log.debug("hostPort = " + hostPort);
183          }
184  
185          String portS = null;
186  
187          index = hostPort.indexOf(':');
188          host = hostPort;
189  
190          if (index > 0)
191          {
192              portS = hostPort.substring(index + 1, host.length());
193  
194              Integer portNumber = null;
195  
196              try
197              {
198                  portNumber = new Integer(portS);
199              }
200              catch (Exception ex)
201              {
202                  throw new MalformedURLException("Invalid Port Number in URL");
203              }
204  
205              port = portNumber.intValue();
206  
207              if (debug)
208              {
209                  log.debug("port = " + port);
210              }
211  
212              host = hostPort.substring(0, index);
213  
214              if (debug)
215              {
216                  log.debug("host = " + host);
217              }
218          }
219  
220          int pathStartIndex = hostStartIndex + hostPort.length();
221          int queryIndex = url.indexOf('?');
222          int pathEndIndex = (queryIndex == -1) ? url.length() : queryIndex;
223  
224          path = url.substring(pathStartIndex, pathEndIndex);
225  
226          if (debug)
227          {
228              log.debug("path = " + path);
229          }
230  
231          String query = null;
232  
233          if (queryIndex != -1)
234          {
235              query = url.substring(queryIndex + 1, url.length());
236  
237              if (debug)
238              {
239                  log.debug("query = " + query);
240              }
241  
242              params = parseQuery(query);
243  
244              if (debug)
245              {
246                  log.debug("params = " + params);
247              }
248          }
249      }
250  
251      private static Map parseQuery(String query)
252                             throws MalformedURLException
253      {
254          HashMap map = new HashMap();
255          StringTokenizer st = new StringTokenizer(query, "&");
256  
257          while (st.hasMoreTokens())
258          {
259              String paramValue = st.nextToken();
260              int index = paramValue.indexOf('=');
261  
262              if (index == -1)
263              {
264                  throw new MalformedURLException("Bad Query String at: "
265                                                  + paramValue);
266              }
267  
268              String name = paramValue.substring(0, index);
269              String value = paramValue.substring(index + 1, paramValue.length());
270  
271              map.put(name, value);
272          }
273  
274          return map;
275      }
276  }