View Javadoc

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