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. package org.apache.commons.httpclient;
  30. import org.apache.commons.httpclient.util.URIUtil;
  31. /**
  32. * The HTTPS URL.
  33. *
  34. * @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
  35. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  36. */
  37. public class HttpsURL extends HttpURL {
  38. // ----------------------------------------------------------- Constructors
  39. /**
  40. * Create an instance as an internal use.
  41. */
  42. protected HttpsURL() {
  43. }
  44. /**
  45. * Construct a HTTPS URL as an escaped form of a character array with the
  46. * given charset to do escape encoding.
  47. *
  48. * @param escaped the HTTPS URL character sequence
  49. * @param charset the charset to do escape encoding
  50. * @throws URIException If {@link #checkValid()} fails
  51. * @throws NullPointerException if <code>escaped</code> is <code>null</code>
  52. * @see #getProtocolCharset
  53. */
  54. public HttpsURL(char[] escaped, String charset)
  55. throws URIException, NullPointerException {
  56. protocolCharset = charset;
  57. parseUriReference(new String(escaped), true);
  58. checkValid();
  59. }
  60. /**
  61. * Construct a HTTPS URL as an escaped form of a character array.
  62. *
  63. * @param escaped the HTTPS URL character sequence
  64. * @throws URIException If {@link #checkValid()} fails
  65. * @throws NullPointerException if <code>escaped</code> is <code>null</code>
  66. * @see #getDefaultProtocolCharset
  67. */
  68. public HttpsURL(char[] escaped) throws URIException, NullPointerException {
  69. parseUriReference(new String(escaped), true);
  70. checkValid();
  71. }
  72. /**
  73. * Construct a HTTPS URL from a given string with the given charset to do
  74. * escape encoding.
  75. *
  76. * @param original the HTTPS URL string
  77. * @param charset the charset to do escape encoding
  78. * @throws URIException If {@link #checkValid()} fails
  79. * @see #getProtocolCharset
  80. */
  81. public HttpsURL(String original, String charset) throws URIException {
  82. protocolCharset = charset;
  83. parseUriReference(original, false);
  84. checkValid();
  85. }
  86. /**
  87. * Construct a HTTPS URL from a given string.
  88. *
  89. * @param original the HTTPS URL string
  90. * @throws URIException If {@link #checkValid()} fails
  91. * @see #getDefaultProtocolCharset
  92. */
  93. public HttpsURL(String original) throws URIException {
  94. parseUriReference(original, false);
  95. checkValid();
  96. }
  97. /**
  98. * Construct a HTTPS URL from given components.
  99. *
  100. * @param host the host string
  101. * @param port the port number
  102. * @param path the path string
  103. * @throws URIException If {@link #checkValid()} fails
  104. * @see #getDefaultProtocolCharset
  105. */
  106. public HttpsURL(String host, int port, String path) throws URIException {
  107. this(null, host, port, path, null, null);
  108. checkValid();
  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. * @param query the query string
  117. * @throws URIException If {@link #checkValid()} fails
  118. * @see #getDefaultProtocolCharset
  119. */
  120. public HttpsURL(String host, int port, String path, String query)
  121. throws URIException {
  122. this(null, host, port, path, query, null);
  123. checkValid();
  124. }
  125. /**
  126. * Construct a HTTPS URL from given components.
  127. *
  128. * @param user the user name
  129. * @param password his or her password
  130. * @param host the host string
  131. * @throws URIException If {@link #checkValid()} fails
  132. * @see #getDefaultProtocolCharset
  133. */
  134. public HttpsURL(String user, String password, String host)
  135. throws URIException {
  136. this((user == null) ? null : user
  137. + ((password == null) ? "" : ':' + password),
  138. host, -1, null, null, null);
  139. checkValid();
  140. }
  141. /**
  142. * Construct a HTTPS URL from given components.
  143. *
  144. * @param user the user name
  145. * @param password his or her password
  146. * @param host the host string
  147. * @param port the port number
  148. * @throws URIException If {@link #checkValid()} fails
  149. * @see #getDefaultProtocolCharset
  150. */
  151. public HttpsURL(String user, String password, String host, int port)
  152. throws URIException {
  153. this((user == null) ? null : user
  154. + ((password == null) ? "" : ':' + password),
  155. host, port, null, null, null);
  156. checkValid();
  157. }
  158. /**
  159. * Construct a HTTPS URL from given components.
  160. *
  161. * @param user the user name
  162. * @param password his or her password
  163. * @param host the host string
  164. * @param port the port number
  165. * @param path the path string
  166. * @throws URIException If {@link #checkValid()} fails
  167. * @see #getDefaultProtocolCharset
  168. */
  169. public HttpsURL(String user, String password, String host, int port,
  170. String path) throws URIException {
  171. this((user == null) ? null : user
  172. + ((password == null) ? "" : ':' + password),
  173. host, port, path, null, null);
  174. checkValid();
  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. * @param query The query string.
  185. * @throws URIException If {@link #checkValid()} fails
  186. * @see #getDefaultProtocolCharset
  187. */
  188. public HttpsURL(String user, String password, String host, int port,
  189. String path, String query) throws URIException {
  190. this((user == null) ? null : user
  191. + ((password == null) ? "" : ':' + password),
  192. host, port, path, query, null);
  193. checkValid();
  194. }
  195. /**
  196. * Construct a HTTPS URL from given components.
  197. *
  198. * @param host the host string
  199. * @param path the path string
  200. * @param query the query string
  201. * @param fragment the fragment string
  202. * @throws URIException If {@link #checkValid()} fails
  203. * @see #getDefaultProtocolCharset
  204. */
  205. public HttpsURL(String host, String path, String query, String fragment)
  206. throws URIException {
  207. this(null, host, -1, path, query, fragment);
  208. checkValid();
  209. }
  210. /**
  211. * Construct a HTTPS URL from given components.
  212. *
  213. * @param userinfo the userinfo string
  214. * @param host the host string
  215. * @param path the path string
  216. * @param query the query string
  217. * @param fragment the fragment string
  218. * @throws URIException If {@link #checkValid()} fails
  219. * @see #getDefaultProtocolCharset
  220. */
  221. public HttpsURL(String userinfo, String host, String path, String query,
  222. String fragment) throws URIException {
  223. this(userinfo, host, -1, path, query, fragment);
  224. checkValid();
  225. }
  226. /**
  227. * Construct a HTTPS URL from given components.
  228. *
  229. * @param userinfo the userinfo string
  230. * @param host the host string
  231. * @param port the port number
  232. * @param path the path string
  233. * @throws URIException If {@link #checkValid()} fails
  234. * @see #getDefaultProtocolCharset
  235. */
  236. public HttpsURL(String userinfo, String host, int port, String path)
  237. throws URIException {
  238. this(userinfo, host, port, path, null, null);
  239. checkValid();
  240. }
  241. /**
  242. * Construct a HTTPS URL from given components.
  243. *
  244. * @param userinfo the userinfo string
  245. * @param host the host string
  246. * @param port the port number
  247. * @param path the path string
  248. * @param query the query string
  249. * @throws URIException If {@link #checkValid()} fails
  250. * @see #getDefaultProtocolCharset
  251. */
  252. public HttpsURL(String userinfo, String host, int port, String path,
  253. String query) throws URIException {
  254. this(userinfo, host, port, path, query, null);
  255. checkValid();
  256. }
  257. /**
  258. * Construct a HTTPS URL from given components.
  259. *
  260. * @param userinfo the userinfo string
  261. * @param host the host string
  262. * @param port the port number
  263. * @param path the path string
  264. * @param query the query string
  265. * @param fragment the fragment string
  266. * @throws URIException If {@link #checkValid()} fails
  267. * @see #getDefaultProtocolCharset
  268. */
  269. public HttpsURL(String userinfo, String host, int port, String path,
  270. String query, String fragment) throws URIException {
  271. // validate and contruct the URI character sequence
  272. StringBuffer buff = new StringBuffer();
  273. if (userinfo != null || host != null || port != -1) {
  274. _scheme = DEFAULT_SCHEME; // in order to verify the own protocol
  275. buff.append(_default_scheme);
  276. buff.append("://");
  277. if (userinfo != null) {
  278. buff.append(URIUtil.encode(userinfo, URI.allowed_userinfo));
  279. buff.append('@');
  280. }
  281. if (host != null) {
  282. buff.append(URIUtil.encode(host, URI.allowed_host));
  283. if (port != -1 || port != DEFAULT_PORT) {
  284. buff.append(':');
  285. buff.append(port);
  286. }
  287. }
  288. }
  289. if (path != null) { // accept empty path
  290. if (scheme != null && !path.startsWith("/")) {
  291. throw new URIException(URIException.PARSING,
  292. "abs_path requested");
  293. }
  294. buff.append(URIUtil.encode(path, URI.allowed_abs_path));
  295. }
  296. if (query != null) {
  297. buff.append('?');
  298. buff.append(URIUtil.encode(query, URI.allowed_query));
  299. }
  300. if (fragment != null) {
  301. buff.append('#');
  302. buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
  303. }
  304. parseUriReference(buff.toString(), true);
  305. checkValid();
  306. }
  307. /**
  308. * Construct a HTTPS URL with a given relative HTTPS URL string.
  309. *
  310. * @param base the base HttpsURL
  311. * @param relative the relative HTTPS URL string
  312. * @throws URIException If {@link #checkValid()} fails
  313. */
  314. public HttpsURL(HttpsURL base, String relative) throws URIException {
  315. this(base, new HttpsURL(relative));
  316. }
  317. /**
  318. * Construct a HTTPS URL with a given relative URL.
  319. *
  320. * @param base the base HttpsURL
  321. * @param relative the relative HttpsURL
  322. * @throws URIException If {@link #checkValid()} fails
  323. */
  324. public HttpsURL(HttpsURL base, HttpsURL relative) throws URIException {
  325. super(base, relative);
  326. checkValid();
  327. }
  328. // -------------------------------------------------------------- Constants
  329. /**
  330. * Default scheme for HTTPS URL.
  331. */
  332. public static final char[] DEFAULT_SCHEME = { 'h', 't', 't', 'p', 's' };
  333. /**
  334. * Default scheme for HTTPS URL.
  335. * @deprecated Use {@link #DEFAULT_SCHEME} instead. This one doesn't
  336. * conform to the project naming conventions.
  337. */
  338. public static final char[] _default_scheme = DEFAULT_SCHEME;
  339. /**
  340. * Default port for HTTPS URL.
  341. */
  342. public static final int DEFAULT_PORT = 443;
  343. /**
  344. * Default port for HTTPS URL.
  345. * @deprecated Use {@link #DEFAULT_PORT} instead. This one doesn't conform
  346. * to the project naming conventions.
  347. */
  348. public static final int _default_port = DEFAULT_PORT;
  349. /**
  350. * The serialVersionUID.
  351. */
  352. static final long serialVersionUID = 887844277028676648L;
  353. // ------------------------------------------------------------- The scheme
  354. /**
  355. * Get the scheme. You can get the scheme explicitly.
  356. *
  357. * @return the scheme
  358. */
  359. public char[] getRawScheme() {
  360. return (_scheme == null) ? null : HttpsURL.DEFAULT_SCHEME;
  361. }
  362. /**
  363. * Get the scheme. You can get the scheme explicitly.
  364. *
  365. * @return the scheme null if empty or undefined
  366. */
  367. public String getScheme() {
  368. return (_scheme == null) ? null : new String(HttpsURL.DEFAULT_SCHEME);
  369. }
  370. // --------------------------------------------------------------- The port
  371. /**
  372. * Get the port number.
  373. * @return the port number
  374. */
  375. public int getPort() {
  376. return (_port == -1) ? HttpsURL.DEFAULT_PORT : _port;
  377. }
  378. // ---------------------------------------------------------------- Utility
  379. /**
  380. * Verify the valid class use for construction.
  381. *
  382. * @throws URIException the wrong scheme use
  383. */
  384. protected void checkValid() throws URIException {
  385. // could be explicit protocol or undefined.
  386. if (!(equals(_scheme, DEFAULT_SCHEME) || _scheme == null)) {
  387. throw new URIException(URIException.PARSING, "wrong class use");
  388. }
  389. }
  390. }