View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpsURL.java,v 1.11 2004/09/30 17:26:41 oglueck Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient;
31  
32  import org.apache.commons.httpclient.util.URIUtil;
33  
34  /***
35   * The HTTPS URL.
36   *
37   * @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
38   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
39   */
40  public class HttpsURL extends HttpURL {
41  
42      // ----------------------------------------------------------- Constructors
43  
44      /***
45       * Create an instance as an internal use.
46       */
47      protected HttpsURL() {
48      }
49  
50  
51      /***
52       * Construct a HTTPS URL as an escaped form of a character array with the
53       * given charset to do escape encoding.
54       *
55       * @param escaped the HTTPS URL character sequence
56       * @param charset the charset to do escape encoding
57       * @throws URIException If {@link #checkValid()} fails
58       * @throws NullPointerException if <code>escaped</code> is <code>null</code>
59       * @see #getProtocolCharset
60       */
61      public HttpsURL(char[] escaped, String charset)
62          throws URIException, NullPointerException {
63          protocolCharset = charset;
64          parseUriReference(new String(escaped), true);
65          checkValid();
66      }
67  
68  
69      /***
70       * Construct a HTTPS URL as an escaped form of a character array.
71       *
72       * @param escaped the HTTPS URL character sequence
73       * @throws URIException If {@link #checkValid()} fails
74       * @throws NullPointerException if <code>escaped</code> is <code>null</code>
75       * @see #getDefaultProtocolCharset
76       */
77      public HttpsURL(char[] escaped) throws URIException, NullPointerException {
78          parseUriReference(new String(escaped), true);
79          checkValid();
80      }
81  
82  
83      /***
84       * Construct a HTTPS URL from a given string with the given charset to do
85       * escape encoding.
86       *
87       * @param original the HTTPS URL string
88       * @param charset the charset to do escape encoding
89       * @throws URIException If {@link #checkValid()} fails
90       * @see #getProtocolCharset
91       */
92      public HttpsURL(String original, String charset) throws URIException {
93          protocolCharset = charset;
94          parseUriReference(original, false);
95          checkValid();
96      }
97  
98  
99      /***
100      * Construct a HTTPS URL from a given string.
101      *
102      * @param original the HTTPS URL string
103      * @throws URIException If {@link #checkValid()} fails
104      * @see #getDefaultProtocolCharset
105      */
106     public HttpsURL(String original) throws URIException {
107         parseUriReference(original, false);
108         checkValid();
109     }
110 
111 
112     /***
113      * Construct a HTTPS URL from given components.
114      *
115      * @param host the host string
116      * @param port the port number
117      * @param path the path string
118      * @throws URIException If {@link #checkValid()} fails
119      * @see #getDefaultProtocolCharset
120      */
121     public HttpsURL(String host, int port, String path) throws URIException {
122         this(null, host, port, path, null, null);
123     }
124 
125 
126     /***
127      * Construct a HTTPS URL from given components.
128      *
129      * @param host the host string
130      * @param port the port number
131      * @param path the path string
132      * @param query the query string
133      * @throws URIException If {@link #checkValid()} fails
134      * @see #getDefaultProtocolCharset
135      */
136     public HttpsURL(String host, int port, String path, String query)
137         throws URIException {
138 
139         this(null, host, port, path, query, null);
140     }
141 
142 
143     /***
144      * Construct a HTTPS URL from given components.
145      *
146      * @param user the user name
147      * @param password his or her password
148      * @param host the host string
149      * @throws URIException If {@link #checkValid()} fails
150      * @see #getDefaultProtocolCharset
151      */
152     public HttpsURL(String user, String password, String host)
153         throws URIException {
154 
155         this(user, password, host, -1, null, null, null);
156     }
157 
158 
159     /***
160      * Construct a HTTPS URL from given components.
161      *
162      * @param user the user name
163      * @param password his or her password
164      * @param host the host string
165      * @param port the port number
166      * @throws URIException If {@link #checkValid()} fails
167      * @see #getDefaultProtocolCharset
168      */
169     public HttpsURL(String user, String password, String host, int port)
170         throws URIException {
171 
172         this(user, password, host, port, null, null, null);
173     }
174 
175 
176     /***
177      * Construct a HTTPS URL from given components.
178      *
179      * @param user the user name
180      * @param password his or her password
181      * @param host the host string
182      * @param port the port number
183      * @param path the path string
184      * @throws URIException If {@link #checkValid()} fails
185      * @see #getDefaultProtocolCharset
186      */
187     public HttpsURL(String user, String password, String host, int port,
188             String path) throws URIException {
189 
190         this(user, password, host, port, path, null, null);
191     }
192 
193 
194     /***
195      * Construct a HTTPS URL from given components.
196      *
197      * @param user the user name
198      * @param password his or her password
199      * @param host the host string
200      * @param port the port number
201      * @param path the path string
202      * @param query The query string.
203      * @throws URIException If {@link #checkValid()} fails
204      * @see #getDefaultProtocolCharset
205      */
206     public HttpsURL(String user, String password, String host, int port,
207             String path, String query) throws URIException {
208 
209         this(user, password, host, port, path, query, null);
210     }
211 
212 
213     /***
214      * Construct a HTTPS URL from given components.
215      *
216      * @param host the host string
217      * @param path the path string
218      * @param query the query string
219      * @param fragment the fragment string
220      * @throws URIException If {@link #checkValid()} fails
221      * @see #getDefaultProtocolCharset
222      */
223     public HttpsURL(String host, String path, String query, String fragment)
224         throws URIException {
225 
226         this(null, host, -1, path, query, fragment);
227     }
228 
229 
230     /***
231      * Construct a HTTPS URL from given components.
232      *
233      * Note: The <code>userinfo</code> format is normally
234      * <code>&lt;username&gt;:&lt;password&gt;</code> where
235      * username and password must both be URL escaped.
236      *  
237      * @param userinfo the userinfo string whose parts are URL escaped
238      * @param host the host string
239      * @param path the path string
240      * @param query the query string
241      * @param fragment the fragment string
242      * @throws URIException If {@link #checkValid()} fails
243      * @see #getDefaultProtocolCharset
244      */
245     public HttpsURL(String userinfo, String host, String path, String query,
246             String fragment) throws URIException {
247 
248         this(userinfo, host, -1, path, query, fragment);
249     }
250 
251 
252     /***
253      * Construct a HTTPS URL from given components.
254      *
255      * Note: The <code>userinfo</code> format is normally
256      * <code>&lt;username&gt;:&lt;password&gt;</code> where
257      * username and password must both be URL escaped.
258      *  
259      * @param userinfo the userinfo string whose parts are URL escaped
260      * @param host the host string
261      * @param port the port number
262      * @param path the path string
263      * @throws URIException If {@link #checkValid()} fails
264      * @see #getDefaultProtocolCharset
265      */
266     public HttpsURL(String userinfo, String host, int port, String path)
267         throws URIException {
268 
269         this(userinfo, host, port, path, null, null);
270     }
271 
272 
273     /***
274      * Construct a HTTPS URL from given components.
275      *
276      * Note: The <code>userinfo</code> format is normally
277      * <code>&lt;username&gt;:&lt;password&gt;</code> where
278      * username and password must both be URL escaped.
279      *  
280      * @param userinfo the userinfo string whose parts are URL escaped
281      * @param host the host string
282      * @param port the port number
283      * @param path the path string
284      * @param query the query string
285      * @throws URIException If {@link #checkValid()} fails
286      * @see #getDefaultProtocolCharset
287      */
288     public HttpsURL(String userinfo, String host, int port, String path,
289             String query) throws URIException {
290 
291         this(userinfo, host, port, path, query, null);
292     }
293 
294 
295     /***
296      * Construct a HTTPS URL from given components.
297      *
298      * Note: The <code>userinfo</code> format is normally
299      * <code>&lt;username&gt;:&lt;password&gt;</code> where
300      * username and password must both be URL escaped.
301      *  
302      * @param userinfo the userinfo string whose parts are URL escaped
303      * @param host the host string
304      * @param port the port number
305      * @param path the path string
306      * @param query the query string
307      * @param fragment the fragment string
308      * @throws URIException If {@link #checkValid()} fails
309      * @see #getDefaultProtocolCharset
310      */
311     public HttpsURL(String userinfo, String host, int port, String path,
312             String query, String fragment) throws URIException {
313 
314         // validate and contruct the URI character sequence
315         StringBuffer buff = new StringBuffer();
316         if (userinfo != null || host != null || port != -1) {
317             _scheme = DEFAULT_SCHEME; // in order to verify the own protocol
318             buff.append(_default_scheme);
319             buff.append("://");
320             if (userinfo != null) {
321                 buff.append(userinfo);
322                 buff.append('@');
323             }
324             if (host != null) {
325                 buff.append(URIUtil.encode(host, URI.allowed_host));
326                 if (port != -1 || port != DEFAULT_PORT) {
327                     buff.append(':');
328                     buff.append(port);
329                 }
330             }
331         }
332         if (path != null) {  // accept empty path
333             if (scheme != null && !path.startsWith("/")) {
334                 throw new URIException(URIException.PARSING,
335                         "abs_path requested");
336             }
337             buff.append(URIUtil.encode(path, URI.allowed_abs_path));
338         }
339         if (query != null) {
340             buff.append('?');
341             buff.append(URIUtil.encode(query, URI.allowed_query));
342         }
343         if (fragment != null) {
344             buff.append('#');
345             buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
346         }
347         parseUriReference(buff.toString(), true);
348         checkValid();
349     }
350 
351     /***
352      * Construct a HTTP URL from given components.
353      *
354      * @param user the user name
355      * @param password his or her password
356      * @param host the host string
357      * @param port the port number
358      * @param path the path string
359      * @param query the query string
360      * @param fragment the fragment string
361      * @throws URIException If {@link #checkValid()} fails
362      * @see #getDefaultProtocolCharset
363      */
364     public HttpsURL(String user, String password, String host, int port,
365             String path, String query, String fragment) throws URIException {
366         this(HttpURL.toUserinfo(user, password), host, port, path, query, fragment);
367     }    
368 
369     /***
370      * Construct a HTTPS URL with a given relative HTTPS URL string.
371      *
372      * @param base the base HttpsURL
373      * @param relative the relative HTTPS URL string
374      * @throws URIException If {@link #checkValid()} fails
375      */
376     public HttpsURL(HttpsURL base, String relative) throws URIException {
377         this(base, new HttpsURL(relative));
378     }
379 
380 
381     /***
382      * Construct a HTTPS URL with a given relative URL.
383      *
384      * @param base the base HttpsURL
385      * @param relative the relative HttpsURL
386      * @throws URIException If {@link #checkValid()} fails
387      */
388     public HttpsURL(HttpsURL base, HttpsURL relative) throws URIException {
389         super(base, relative);
390         checkValid();
391     }
392 
393     // -------------------------------------------------------------- Constants
394 
395     /***
396      * Default scheme for HTTPS URL.
397      */
398     public static final char[] DEFAULT_SCHEME = { 'h', 't', 't', 'p', 's' };
399     
400     /***
401      * Default scheme for HTTPS URL.
402      * @deprecated Use {@link #DEFAULT_SCHEME} instead.  This one doesn't
403      * conform to the project naming conventions.
404      */
405     public static final char[] _default_scheme = DEFAULT_SCHEME;
406 
407 
408     /***
409      * Default port for HTTPS URL.
410      */
411     public static final int DEFAULT_PORT = 443;
412 
413     /***
414      * Default port for HTTPS URL.
415      * @deprecated Use {@link #DEFAULT_PORT} instead.  This one doesn't conform
416      * to the project naming conventions.
417      */
418     public static final int _default_port = DEFAULT_PORT;
419 
420 
421     /***
422      * The serialVersionUID.
423      */
424     static final long serialVersionUID = 887844277028676648L;
425 
426     // ------------------------------------------------------------- The scheme
427 
428     /***
429      * Get the scheme.  You can get the scheme explicitly.
430      *
431      * @return the scheme
432      */
433     public char[] getRawScheme() {
434         return (_scheme == null) ? null : HttpsURL.DEFAULT_SCHEME;
435     }
436 
437 
438     /***
439      * Get the scheme.  You can get the scheme explicitly.
440      *
441      * @return the scheme null if empty or undefined
442      */
443     public String getScheme() {
444         return (_scheme == null) ? null : new String(HttpsURL.DEFAULT_SCHEME);
445     }
446 
447     // --------------------------------------------------------------- The port
448 
449     /***
450      * Get the port number.
451      * @return the port number
452      */
453     public int getPort() {
454         return (_port == -1) ? HttpsURL.DEFAULT_PORT : _port;
455     }    
456     
457     // ---------------------------------------------------------------- Utility
458 
459     /***
460      * Verify the valid class use for construction.
461      *
462      * @throws URIException the wrong scheme use
463      */
464     protected void checkValid() throws URIException {
465         // could be explicit protocol or undefined.
466         if (!(equals(_scheme, DEFAULT_SCHEME) || _scheme == null)) {
467             throw new URIException(URIException.PARSING, "wrong class use");
468         }
469     }
470 
471 }
472