001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import org.apache.commons.httpclient.util.URIUtil;
034:
035: /**
036: * The HTTPS URL.
037: *
038: * @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
039: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
040: */
041: public class HttpsURL extends HttpURL {
042:
043: // ----------------------------------------------------------- Constructors
044:
045: /**
046: * Create an instance as an internal use.
047: */
048: protected HttpsURL() {
049: }
050:
051: /**
052: * Construct a HTTPS URL as an escaped form of a character array with the
053: * given charset to do escape encoding.
054: *
055: * @param escaped the HTTPS URL character sequence
056: * @param charset the charset to do escape encoding
057: * @throws URIException If {@link #checkValid()} fails
058: * @throws NullPointerException if <code>escaped</code> is <code>null</code>
059: * @see #getProtocolCharset
060: */
061: public HttpsURL(char[] escaped, String charset)
062: throws URIException, NullPointerException {
063: protocolCharset = charset;
064: parseUriReference(new String(escaped), true);
065: checkValid();
066: }
067:
068: /**
069: * Construct a HTTPS URL as an escaped form of a character array.
070: *
071: * @param escaped the HTTPS URL character sequence
072: * @throws URIException If {@link #checkValid()} fails
073: * @throws NullPointerException if <code>escaped</code> is <code>null</code>
074: * @see #getDefaultProtocolCharset
075: */
076: public HttpsURL(char[] escaped) throws URIException,
077: NullPointerException {
078: parseUriReference(new String(escaped), true);
079: checkValid();
080: }
081:
082: /**
083: * Construct a HTTPS URL from a given string with the given charset to do
084: * escape encoding.
085: *
086: * @param original the HTTPS URL string
087: * @param charset the charset to do escape encoding
088: * @throws URIException If {@link #checkValid()} fails
089: * @see #getProtocolCharset
090: */
091: public HttpsURL(String original, String charset)
092: throws URIException {
093: protocolCharset = charset;
094: parseUriReference(original, false);
095: checkValid();
096: }
097:
098: /**
099: * Construct a HTTPS URL from a given string.
100: *
101: * @param original the HTTPS URL string
102: * @throws URIException If {@link #checkValid()} fails
103: * @see #getDefaultProtocolCharset
104: */
105: public HttpsURL(String original) throws URIException {
106: parseUriReference(original, false);
107: checkValid();
108: }
109:
110: /**
111: * Construct a HTTPS URL from given components.
112: *
113: * @param host the host string
114: * @param port the port number
115: * @param path the path string
116: * @throws URIException If {@link #checkValid()} fails
117: * @see #getDefaultProtocolCharset
118: */
119: public HttpsURL(String host, int port, String path)
120: throws URIException {
121: this (null, host, port, path, null, null);
122: }
123:
124: /**
125: * Construct a HTTPS URL from given components.
126: *
127: * @param host the host string
128: * @param port the port number
129: * @param path the path string
130: * @param query the query string
131: * @throws URIException If {@link #checkValid()} fails
132: * @see #getDefaultProtocolCharset
133: */
134: public HttpsURL(String host, int port, String path, String query)
135: throws URIException {
136:
137: this (null, host, port, path, query, null);
138: }
139:
140: /**
141: * Construct a HTTPS URL from given components.
142: *
143: * @param user the user name
144: * @param password his or her password
145: * @param host the host string
146: * @throws URIException If {@link #checkValid()} fails
147: * @see #getDefaultProtocolCharset
148: */
149: public HttpsURL(String user, String password, String host)
150: throws URIException {
151:
152: this (user, password, host, -1, null, null, null);
153: }
154:
155: /**
156: * Construct a HTTPS URL from given components.
157: *
158: * @param user the user name
159: * @param password his or her password
160: * @param host the host string
161: * @param port the port number
162: * @throws URIException If {@link #checkValid()} fails
163: * @see #getDefaultProtocolCharset
164: */
165: public HttpsURL(String user, String password, String host, int port)
166: throws URIException {
167:
168: this (user, password, host, port, null, null, null);
169: }
170:
171: /**
172: * Construct a HTTPS URL from given components.
173: *
174: * @param user the user name
175: * @param password his or her password
176: * @param host the host string
177: * @param port the port number
178: * @param path the path string
179: * @throws URIException If {@link #checkValid()} fails
180: * @see #getDefaultProtocolCharset
181: */
182: public HttpsURL(String user, String password, String host,
183: int port, String path) throws URIException {
184:
185: this (user, password, host, port, path, null, null);
186: }
187:
188: /**
189: * Construct a HTTPS URL from given components.
190: *
191: * @param user the user name
192: * @param password his or her password
193: * @param host the host string
194: * @param port the port number
195: * @param path the path string
196: * @param query The query string.
197: * @throws URIException If {@link #checkValid()} fails
198: * @see #getDefaultProtocolCharset
199: */
200: public HttpsURL(String user, String password, String host,
201: int port, String path, String query) throws URIException {
202:
203: this (user, password, host, port, path, query, null);
204: }
205:
206: /**
207: * Construct a HTTPS URL from given components.
208: *
209: * @param host the host string
210: * @param path the path string
211: * @param query the query string
212: * @param fragment the fragment string
213: * @throws URIException If {@link #checkValid()} fails
214: * @see #getDefaultProtocolCharset
215: */
216: public HttpsURL(String host, String path, String query,
217: String fragment) throws URIException {
218:
219: this (null, host, -1, path, query, fragment);
220: }
221:
222: /**
223: * Construct a HTTPS URL from given components.
224: *
225: * Note: The <code>userinfo</code> format is normally
226: * <code><username>:<password></code> where
227: * username and password must both be URL escaped.
228: *
229: * @param userinfo the userinfo string whose parts are URL escaped
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 userinfo, String host, String path,
238: String query, String fragment) throws URIException {
239:
240: this (userinfo, host, -1, path, query, fragment);
241: }
242:
243: /**
244: * Construct a HTTPS URL from given components.
245: *
246: * Note: The <code>userinfo</code> format is normally
247: * <code><username>:<password></code> where
248: * username and password must both be URL escaped.
249: *
250: * @param userinfo the userinfo string whose parts are URL escaped
251: * @param host the host string
252: * @param port the port number
253: * @param path the path string
254: * @throws URIException If {@link #checkValid()} fails
255: * @see #getDefaultProtocolCharset
256: */
257: public HttpsURL(String userinfo, String host, int port, String path)
258: throws URIException {
259:
260: this (userinfo, host, port, path, null, null);
261: }
262:
263: /**
264: * Construct a HTTPS URL from given components.
265: *
266: * Note: The <code>userinfo</code> format is normally
267: * <code><username>:<password></code> where
268: * username and password must both be URL escaped.
269: *
270: * @param userinfo the userinfo string whose parts are URL escaped
271: * @param host the host string
272: * @param port the port number
273: * @param path the path string
274: * @param query the query string
275: * @throws URIException If {@link #checkValid()} fails
276: * @see #getDefaultProtocolCharset
277: */
278: public HttpsURL(String userinfo, String host, int port,
279: String path, String query) throws URIException {
280:
281: this (userinfo, host, port, path, query, null);
282: }
283:
284: /**
285: * Construct a HTTPS URL from given components.
286: *
287: * Note: The <code>userinfo</code> format is normally
288: * <code><username>:<password></code> where
289: * username and password must both be URL escaped.
290: *
291: * @param userinfo the userinfo string whose parts are URL escaped
292: * @param host the host string
293: * @param port the port number
294: * @param path the path string
295: * @param query the query string
296: * @param fragment the fragment string
297: * @throws URIException If {@link #checkValid()} fails
298: * @see #getDefaultProtocolCharset
299: */
300: public HttpsURL(String userinfo, String host, int port,
301: String path, String query, String fragment)
302: throws URIException {
303:
304: // validate and contruct the URI character sequence
305: StringBuffer buff = new StringBuffer();
306: if (userinfo != null || host != null || port != -1) {
307: _scheme = DEFAULT_SCHEME; // in order to verify the own protocol
308: buff.append(_default_scheme);
309: buff.append("://");
310: if (userinfo != null) {
311: buff.append(userinfo);
312: buff.append('@');
313: }
314: if (host != null) {
315: buff.append(URIUtil.encode(host, URI.allowed_host));
316: if (port != -1 || port != DEFAULT_PORT) {
317: buff.append(':');
318: buff.append(port);
319: }
320: }
321: }
322: if (path != null) { // accept empty path
323: if (scheme != null && !path.startsWith("/")) {
324: throw new URIException(URIException.PARSING,
325: "abs_path requested");
326: }
327: buff.append(URIUtil.encode(path, URI.allowed_abs_path));
328: }
329: if (query != null) {
330: buff.append('?');
331: buff.append(URIUtil.encode(query, URI.allowed_query));
332: }
333: if (fragment != null) {
334: buff.append('#');
335: buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
336: }
337: parseUriReference(buff.toString(), true);
338: checkValid();
339: }
340:
341: /**
342: * Construct a HTTP URL from given components.
343: *
344: * @param user the user name
345: * @param password his or her password
346: * @param host the host string
347: * @param port the port number
348: * @param path the path string
349: * @param query the query string
350: * @param fragment the fragment string
351: * @throws URIException If {@link #checkValid()} fails
352: * @see #getDefaultProtocolCharset
353: */
354: public HttpsURL(String user, String password, String host,
355: int port, String path, String query, String fragment)
356: throws URIException {
357: this (HttpURL.toUserinfo(user, password), host, port, path,
358: query, fragment);
359: }
360:
361: /**
362: * Construct a HTTPS URL with a given relative HTTPS URL string.
363: *
364: * @param base the base HttpsURL
365: * @param relative the relative HTTPS URL string
366: * @throws URIException If {@link #checkValid()} fails
367: */
368: public HttpsURL(HttpsURL base, String relative) throws URIException {
369: this (base, new HttpsURL(relative));
370: }
371:
372: /**
373: * Construct a HTTPS URL with a given relative URL.
374: *
375: * @param base the base HttpsURL
376: * @param relative the relative HttpsURL
377: * @throws URIException If {@link #checkValid()} fails
378: */
379: public HttpsURL(HttpsURL base, HttpsURL relative)
380: throws URIException {
381: super (base, relative);
382: checkValid();
383: }
384:
385: // -------------------------------------------------------------- Constants
386:
387: /**
388: * Default scheme for HTTPS URL.
389: */
390: public static final char[] DEFAULT_SCHEME = { 'h', 't', 't', 'p',
391: 's' };
392:
393: /**
394: * Default scheme for HTTPS URL.
395: * @deprecated Use {@link #DEFAULT_SCHEME} instead. This one doesn't
396: * conform to the project naming conventions.
397: */
398: public static final char[] _default_scheme = DEFAULT_SCHEME;
399:
400: /**
401: * Default port for HTTPS URL.
402: */
403: public static final int DEFAULT_PORT = 443;
404:
405: /**
406: * Default port for HTTPS URL.
407: * @deprecated Use {@link #DEFAULT_PORT} instead. This one doesn't conform
408: * to the project naming conventions.
409: */
410: public static final int _default_port = DEFAULT_PORT;
411:
412: /**
413: * The serialVersionUID.
414: */
415: static final long serialVersionUID = 887844277028676648L;
416:
417: // ------------------------------------------------------------- The scheme
418:
419: /**
420: * Get the scheme. You can get the scheme explicitly.
421: *
422: * @return the scheme
423: */
424: public char[] getRawScheme() {
425: return (_scheme == null) ? null : HttpsURL.DEFAULT_SCHEME;
426: }
427:
428: /**
429: * Get the scheme. You can get the scheme explicitly.
430: *
431: * @return the scheme null if empty or undefined
432: */
433: public String getScheme() {
434: return (_scheme == null) ? null : new String(
435: HttpsURL.DEFAULT_SCHEME);
436: }
437:
438: // --------------------------------------------------------------- The port
439:
440: /**
441: * Get the port number.
442: * @return the port number
443: */
444: public int getPort() {
445: return (_port == -1) ? HttpsURL.DEFAULT_PORT : _port;
446: }
447:
448: // ---------------------------------------------------------------- Utility
449:
450: /**
451: * Verify the valid class use for construction.
452: *
453: * @throws URIException the wrong scheme use
454: */
455: protected void checkValid() throws URIException {
456: // could be explicit protocol or undefined.
457: if (!(equals(_scheme, DEFAULT_SCHEME) || _scheme == null)) {
458: throw new URIException(URIException.PARSING,
459: "wrong class use");
460: }
461: }
462:
463: }
|