View Javadoc

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