View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpsURL.java,v 1.6.2.3 2004/05/14 09:47:36 oglueck Exp $
3    * $Revision: 1.6.2.3 $
4    * $Date: 2004/05/14 09:47:36 $
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         checkValid();
126     }
127 
128 
129     /***
130      * Construct a HTTPS URL from given components.
131      *
132      * @param host the host string
133      * @param port the port number
134      * @param path the path string
135      * @param query the query string
136      * @throws URIException If {@link #checkValid()} fails
137      * @see #getDefaultProtocolCharset
138      */
139     public HttpsURL(String host, int port, String path, String query)
140         throws URIException {
141 
142         this(null, host, port, path, query, null);
143         checkValid();
144     }
145 
146 
147     /***
148      * Construct a HTTPS URL from given components.
149      *
150      * @param user the user name
151      * @param password his or her password
152      * @param host the host string
153      * @throws URIException If {@link #checkValid()} fails
154      * @see #getDefaultProtocolCharset
155      */
156     public HttpsURL(String user, String password, String host)
157         throws URIException {
158 
159         this((user == null) ? null : user 
160             + ((password == null) ? "" : ':' +  password),
161                 host, -1, null, null, null);
162         checkValid();
163     }
164 
165 
166     /***
167      * Construct a HTTPS URL from given components.
168      *
169      * @param user the user name
170      * @param password his or her password
171      * @param host the host string
172      * @param port the port number
173      * @throws URIException If {@link #checkValid()} fails
174      * @see #getDefaultProtocolCharset
175      */
176     public HttpsURL(String user, String password, String host, int port)
177         throws URIException {
178 
179         this((user == null) ? null : user 
180             + ((password == null) ? "" : ':' +  password),
181                 host, port, null, null, null);
182         checkValid();
183     }
184 
185 
186     /***
187      * Construct a HTTPS URL from given components.
188      *
189      * @param user the user name
190      * @param password his or her password
191      * @param host the host string
192      * @param port the port number
193      * @param path the path string
194      * @throws URIException If {@link #checkValid()} fails
195      * @see #getDefaultProtocolCharset
196      */
197     public HttpsURL(String user, String password, String host, int port,
198             String path) throws URIException {
199 
200         this((user == null) ? null : user 
201             + ((password == null) ? "" : ':' +  password),
202                 host, port, path, null, null);
203         checkValid();
204     }
205 
206 
207     /***
208      * Construct a HTTPS URL from given components.
209      *
210      * @param user the user name
211      * @param password his or her password
212      * @param host the host string
213      * @param port the port number
214      * @param path the path string
215      * @param query The query string.
216      * @throws URIException If {@link #checkValid()} fails
217      * @see #getDefaultProtocolCharset
218      */
219     public HttpsURL(String user, String password, String host, int port,
220             String path, String query) throws URIException {
221 
222         this((user == null) ? null : user 
223             + ((password == null) ? "" : ':' + password),
224                 host, port, path, query, null);
225         checkValid();
226     }
227 
228 
229     /***
230      * Construct a HTTPS URL from given components.
231      *
232      * @param host the host string
233      * @param path the path string
234      * @param query the query string
235      * @param fragment the fragment string
236      * @throws URIException If {@link #checkValid()} fails
237      * @see #getDefaultProtocolCharset
238      */
239     public HttpsURL(String host, String path, String query, String fragment)
240         throws URIException {
241 
242         this(null, host, -1, path, query, fragment);
243         checkValid();
244     }
245 
246 
247     /***
248      * Construct a HTTPS URL from given components.
249      *
250      * @param userinfo the userinfo string
251      * @param host the host string
252      * @param path the path string
253      * @param query the query string
254      * @param fragment the fragment string
255      * @throws URIException If {@link #checkValid()} fails
256      * @see #getDefaultProtocolCharset
257      */
258     public HttpsURL(String userinfo, String host, String path, String query,
259             String fragment) throws URIException {
260 
261         this(userinfo, host, -1, path, query, fragment);
262         checkValid();
263     }
264 
265 
266     /***
267      * Construct a HTTPS URL from given components.
268      *
269      * @param userinfo the userinfo string
270      * @param host the host string
271      * @param port the port number
272      * @param path the path string
273      * @throws URIException If {@link #checkValid()} fails
274      * @see #getDefaultProtocolCharset
275      */
276     public HttpsURL(String userinfo, String host, int port, String path)
277         throws URIException {
278 
279         this(userinfo, host, port, path, null, null);
280         checkValid();
281     }
282 
283 
284     /***
285      * Construct a HTTPS URL from given components.
286      *
287      * @param userinfo the userinfo string
288      * @param host the host string
289      * @param port the port number
290      * @param path the path string
291      * @param query the query string
292      * @throws URIException If {@link #checkValid()} fails
293      * @see #getDefaultProtocolCharset
294      */
295     public HttpsURL(String userinfo, String host, int port, String path,
296             String query) throws URIException {
297 
298         this(userinfo, host, port, path, query, null);
299         checkValid();
300     }
301 
302 
303     /***
304      * Construct a HTTPS URL from given components.
305      *
306      * @param userinfo the userinfo string
307      * @param host the host string
308      * @param port the port number
309      * @param path the path string
310      * @param query the query string
311      * @param fragment the fragment string
312      * @throws URIException If {@link #checkValid()} fails
313      * @see #getDefaultProtocolCharset
314      */
315     public HttpsURL(String userinfo, String host, int port, String path,
316             String query, String fragment) throws URIException {
317 
318         // validate and contruct the URI character sequence
319         StringBuffer buff = new StringBuffer();
320         if (userinfo != null || host != null || port != -1) {
321             _scheme = DEFAULT_SCHEME; // in order to verify the own protocol
322             buff.append(_default_scheme);
323             buff.append("://");
324             if (userinfo != null) {
325                 buff.append(URIUtil.encode(userinfo, URI.allowed_userinfo));
326                 buff.append('@');
327             }
328             if (host != null) {
329                 buff.append(URIUtil.encode(host, URI.allowed_host));
330                 if (port != -1 || port != DEFAULT_PORT) {
331                     buff.append(':');
332                     buff.append(port);
333                 }
334             }
335         }
336         if (path != null) {  // accept empty path
337             if (scheme != null && !path.startsWith("/")) {
338                 throw new URIException(URIException.PARSING,
339                         "abs_path requested");
340             }
341             buff.append(URIUtil.encode(path, URI.allowed_abs_path));
342         }
343         if (query != null) {
344             buff.append('?');
345             buff.append(URIUtil.encode(query, URI.allowed_query));
346         }
347         if (fragment != null) {
348             buff.append('#');
349             buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
350         }
351         parseUriReference(buff.toString(), true);
352         checkValid();
353     }
354 
355 
356     /***
357      * Construct a HTTPS URL with a given relative HTTPS URL string.
358      *
359      * @param base the base HttpsURL
360      * @param relative the relative HTTPS URL string
361      * @throws URIException If {@link #checkValid()} fails
362      */
363     public HttpsURL(HttpsURL base, String relative) throws URIException {
364         this(base, new HttpsURL(relative));
365     }
366 
367 
368     /***
369      * Construct a HTTPS URL with a given relative URL.
370      *
371      * @param base the base HttpsURL
372      * @param relative the relative HttpsURL
373      * @throws URIException If {@link #checkValid()} fails
374      */
375     public HttpsURL(HttpsURL base, HttpsURL relative) throws URIException {
376         super(base, relative);
377         checkValid();
378     }
379 
380     // -------------------------------------------------------------- Constants
381 
382     /***
383      * Default scheme for HTTPS URL.
384      */
385     public static final char[] DEFAULT_SCHEME = { 'h', 't', 't', 'p', 's' };
386     
387     /***
388      * Default scheme for HTTPS URL.
389      * @deprecated Use {@link #DEFAULT_SCHEME} instead.  This one doesn't
390      * conform to the project naming conventions.
391      */
392     public static final char[] _default_scheme = DEFAULT_SCHEME;
393 
394 
395     /***
396      * Default port for HTTPS URL.
397      */
398     public static final int DEFAULT_PORT = 443;
399 
400     /***
401      * Default port for HTTPS URL.
402      * @deprecated Use {@link #DEFAULT_PORT} instead.  This one doesn't conform
403      * to the project naming conventions.
404      */
405     public static final int _default_port = DEFAULT_PORT;
406 
407 
408     /***
409      * The serialVersionUID.
410      */
411     static final long serialVersionUID = 887844277028676648L;
412 
413     // ------------------------------------------------------------- The scheme
414 
415     /***
416      * Get the scheme.  You can get the scheme explicitly.
417      *
418      * @return the scheme
419      */
420     public char[] getRawScheme() {
421         return (_scheme == null) ? null : HttpsURL.DEFAULT_SCHEME;
422     }
423 
424 
425     /***
426      * Get the scheme.  You can get the scheme explicitly.
427      *
428      * @return the scheme null if empty or undefined
429      */
430     public String getScheme() {
431         return (_scheme == null) ? null : new String(HttpsURL.DEFAULT_SCHEME);
432     }
433 
434     // --------------------------------------------------------------- The port
435 
436     /***
437      * Get the port number.
438      * @return the port number
439      */
440     public int getPort() {
441         return (_port == -1) ? HttpsURL.DEFAULT_PORT : _port;
442     }    
443     
444     // ---------------------------------------------------------------- Utility
445 
446     /***
447      * Verify the valid class use for construction.
448      *
449      * @throws URIException the wrong scheme use
450      */
451     protected void checkValid() throws URIException {
452         // could be explicit protocol or undefined.
453         if (!(equals(_scheme, DEFAULT_SCHEME) || _scheme == null)) {
454             throw new URIException(URIException.PARSING, "wrong class use");
455         }
456     }
457 
458 }
459