001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.ObjectOutputStream;
023: import java.security.AccessController;
024: import java.util.Hashtable;
025: import java.util.StringTokenizer;
026:
027: import org.apache.harmony.luni.util.Msg;
028: import org.apache.harmony.luni.util.PriviAction;
029: import org.apache.harmony.luni.util.Util;
030:
031: /**
032: * An instance of class URL specifies the location of a resource on the world
033: * wide web as specified by RFC 1738.
034: */
035: public final class URL implements java.io.Serializable {
036: private static final long serialVersionUID = -7627629688361524110L;
037:
038: private static final NetPermission specifyStreamHandlerPermission = new NetPermission(
039: "specifyStreamHandler"); //$NON-NLS-1$
040:
041: private int hashCode;
042:
043: /**
044: * The receiver's filename.
045: *
046: * @serial the file of this URL
047: *
048: */
049: private String file;
050:
051: /**
052: * The receiver's protocol identifier.
053: *
054: * @serial the protocol of this URL (http, file)
055: *
056: */
057: private String protocol = null;
058:
059: /**
060: * The receiver's host name.
061: *
062: * @serial the host of this URL
063: *
064: */
065: private String host;
066:
067: /**
068: * The receiver's port number.
069: *
070: * @serial the port of this URL
071: *
072: */
073: private int port = -1;
074:
075: /**
076: * The receiver's authority.
077: *
078: * @serial the authority of this URL
079: *
080: */
081: private String authority = null;
082:
083: /**
084: * The receiver's userInfo.
085: */
086: private transient String userInfo = null;
087:
088: /**
089: * The receiver's path.
090: */
091: private transient String path = null;
092:
093: /**
094: * The receiver's query.
095: */
096: private transient String query = null;
097:
098: /**
099: * The receiver's reference.
100: *
101: * @serial the reference of this URL
102: *
103: */
104: private String ref = null;
105:
106: /**
107: * Cache for storing protocol handler
108: */
109: private static Hashtable<String, URLStreamHandler> streamHandlers = new Hashtable<String, URLStreamHandler>();
110:
111: /**
112: * The URL Stream (protocol) Handler
113: */
114: transient URLStreamHandler strmHandler;
115:
116: /**
117: * The factory responsible for producing URL Stream (protocol) Handler
118: */
119: private static URLStreamHandlerFactory streamHandlerFactory;
120:
121: /**
122: * Sets the URL Stream (protocol) handler factory. This method can be
123: * invoked only once during an application's lifetime.
124: * <p>
125: * A security check is performed to verify that the current Policy allows
126: * the stream handler factory to be set.
127: *
128: * @param streamFactory
129: * URLStreamHandlerFactory The factory to use for finding stream
130: * handlers.
131: */
132: public static synchronized void setURLStreamHandlerFactory(
133: URLStreamHandlerFactory streamFactory) {
134: if (streamHandlerFactory != null) {
135: throw new Error(Msg.getString("K004b")); //$NON-NLS-1$
136: }
137: SecurityManager sm = System.getSecurityManager();
138: if (sm != null) {
139: sm.checkSetFactory();
140: }
141: streamHandlers.clear();
142: streamHandlerFactory = streamFactory;
143: }
144:
145: /**
146: * Constructs a new URL instance by parsing the specification.
147: *
148: * @param spec
149: * java.lang.String a URL specification.
150: *
151: * @throws MalformedURLException
152: * if the spec could not be parsed as an URL.
153: */
154: public URL(String spec) throws MalformedURLException {
155: this ((URL) null, spec, (URLStreamHandler) null);
156: }
157:
158: /**
159: * Constructs a new URL by parsing the specification given by
160: * <code>spec</code> and using the context provided by
161: * <code>context</code>.
162: * <p>
163: * The protocol of the specification is obtained by parsing the
164: * <code> spec </code> string.
165: * <p>
166: * If the <code>spec</code> does not specify a protocol:
167: * <ul>
168: * <li>If the context is <code>null</code>, then a
169: * <code>MalformedURLException</code>.</li>
170: * <li>If the context is not <code>null</code>, then the protocol is
171: * obtained from the context.</li>
172: * </ul>
173: * If the <code>spec</code> does specify a protocol:
174: * <ul>
175: * <li>If the context is <code>null</code>, or specifies a different
176: * protocol than the spec, the context is ignored.</li>
177: * <li>If the context is not <code>null</code> and specifies the same
178: * protocol as the specification, the properties of the new <code>URL</code>
179: * are obtained from the context.</li>
180: * </ul>
181: *
182: * @param context
183: * java.net.URL URL to use as context.
184: * @param spec
185: * java.lang.String a URL specification.
186: *
187: * @throws MalformedURLException
188: * if the spec could not be parsed as an URL.
189: */
190: public URL(URL context, String spec) throws MalformedURLException {
191: this (context, spec, (URLStreamHandler) null);
192: }
193:
194: /**
195: * Constructs a new URL by parsing the specification given by
196: * <code>spec</code> and using the context provided by
197: * <code>context</code>.
198: * <p>
199: * If the handler argument is non-null, a security check is made to verify
200: * that user-defined protocol handlers can be specified.
201: * <p>
202: * The protocol of the specification is obtained by parsing the
203: * <code> spec </code> string.
204: * <p>
205: * If the <code>spec</code> does not specify a protocol:
206: * <ul>
207: * <li>If the context is <code>null</code>, then a
208: * <code>MalformedURLException</code>.</li>
209: * <li>If the context is not <code>null</code>, then the protocol is
210: * obtained from the context.</li>
211: * </ul>
212: * If the <code>spec</code> does specify a protocol:
213: * <ul>
214: * <li>If the context is <code>null</code>, or specifies a different
215: * protocol than the spec, the context is ignored.</li>
216: * <li>If the context is not <code>null</code> and specifies the same
217: * protocol as the specification, the properties of the new <code>URL</code>
218: * are obtained from the context.</li>
219: * </ul>
220: *
221: * @param context
222: * java.net.URL URL to use as context.
223: * @param spec
224: * java.lang.String a URL specification.
225: * @param handler
226: * java.net.URLStreamHandler a URLStreamHandler.
227: *
228: * @throws MalformedURLException
229: * if the spec could not be parsed as an URL
230: */
231: public URL(URL context, String spec, URLStreamHandler handler)
232: throws MalformedURLException {
233: if (handler != null) {
234: SecurityManager sm = System.getSecurityManager();
235: if (sm != null) {
236: sm.checkPermission(specifyStreamHandlerPermission);
237: }
238: strmHandler = handler;
239: }
240:
241: if (spec == null) {
242: throw new MalformedURLException();
243: }
244: spec = spec.trim();
245:
246: // The spec includes a protocol if it includes a colon character
247: // before the first occurrence of a slash character. Note that,
248: // "protocol" is the field which holds this URLs protocol.
249: int index;
250: try {
251: index = spec.indexOf(':');
252: } catch (NullPointerException e) {
253: throw new MalformedURLException(e.toString());
254: }
255: int startIPv6Addr = spec.indexOf('[');
256: if (index >= 0) {
257: if ((startIPv6Addr == -1) || (index < startIPv6Addr)) {
258: protocol = spec.substring(0, index);
259: // According to RFC 2396 scheme part should match
260: // the following expression:
261: // alpha *( alpha | digit | "+" | "-" | "." )
262: char c = protocol.charAt(0);
263: boolean valid = ('a' <= c && c <= 'z')
264: || ('A' <= c && c <= 'Z');
265: for (int i = 1; valid && (i < protocol.length()); i++) {
266: c = protocol.charAt(i);
267: valid = ('a' <= c && c <= 'z')
268: || ('A' <= c && c <= 'Z')
269: || ('0' <= c && c <= '9') || (c == '+')
270: || (c == '-') || (c == '.');
271: }
272: if (!valid) {
273: protocol = null;
274: index = -1;
275: } else {
276: // Ignore case in protocol names.
277: // Scheme is defined by ASCII characters.
278: protocol = Util.toASCIILowerCase(protocol);
279: }
280: }
281: }
282:
283: if (protocol != null) {
284: // If the context was specified, and it had the same protocol
285: // as the spec, then fill in the receiver's slots from the values
286: // in the context but still allow them to be over-ridden later
287: // by the values in the spec.
288: if (context != null
289: && protocol.equals(context.getProtocol())) {
290: String cPath = context.getPath();
291: if (cPath != null && cPath.startsWith("/")) { //$NON-NLS-1$
292: set(protocol, context.getHost(), context.getPort(),
293: context.getAuthority(), context
294: .getUserInfo(), cPath, context
295: .getQuery(), null);
296: }
297: if (strmHandler == null) {
298: strmHandler = context.strmHandler;
299: }
300: }
301: } else {
302: // If the spec did not include a protocol, then the context
303: // *must* be specified. Fill in the receiver's slots from the
304: // values in the context, but still allow them to be over-ridden
305: // by the values in the ("relative") spec.
306: if (context == null) {
307: throw new MalformedURLException(
308: org.apache.harmony.luni.util.Msg.getString(
309: "K00d8", spec)); //$NON-NLS-1$
310: }
311: set(context.getProtocol(), context.getHost(), context
312: .getPort(), context.getAuthority(), context
313: .getUserInfo(), context.getPath(), context
314: .getQuery(), null);
315: if (strmHandler == null) {
316: strmHandler = context.strmHandler;
317: }
318: }
319:
320: // If the stream handler has not been determined, set it
321: // to the default for the specified protocol.
322: if (strmHandler == null) {
323: setupStreamHandler();
324: if (strmHandler == null) {
325: throw new MalformedURLException(
326: org.apache.harmony.luni.util.Msg.getString(
327: "K00b3", protocol)); //$NON-NLS-1$
328: }
329: }
330:
331: // Let the handler parse the URL. If the handler throws
332: // any exception, throw MalformedURLException instead.
333: //
334: // Note: We want "index" to be the index of the start of the scheme
335: // specific part of the URL. At this point, it will be either
336: // -1 or the index of the colon after the protocol, so we
337: // increment it to point at either character 0 or the character
338: // after the colon.
339: try {
340: strmHandler.parseURL(this , spec, ++index, spec.length());
341: } catch (Exception e) {
342: throw new MalformedURLException(e.toString());
343: }
344:
345: if (port < -1) {
346: throw new MalformedURLException(
347: org.apache.harmony.luni.util.Msg.getString(
348: "K0325", port)); //$NON-NLS-1$
349: }
350: }
351:
352: /**
353: * Constructs a new URL instance using the arguments provided.
354: *
355: * @param protocol
356: * String the protocol for the URL.
357: * @param host
358: * String the name of the host.
359: * @param file
360: * the name of the resource.
361: *
362: * @throws MalformedURLException
363: * if the parameters do not represent a valid URL.
364: */
365: public URL(String protocol, String host, String file)
366: throws MalformedURLException {
367: this (protocol, host, -1, file, (URLStreamHandler) null);
368: }
369:
370: /**
371: * Constructs a new URL instance using the arguments provided.
372: *
373: * @param protocol
374: * String the protocol for the URL.
375: * @param host
376: * String the name of the host.
377: * @param port
378: * int the port number.
379: * @param file
380: * String the name of the resource.
381: *
382: * @throws MalformedURLException
383: * if the parameters do not represent a valid URL.
384: */
385: public URL(String protocol, String host, int port, String file)
386: throws MalformedURLException {
387: this (protocol, host, port, file, (URLStreamHandler) null);
388: }
389:
390: /**
391: * Constructs a new URL instance using the arguments provided.
392: *
393: * If the handler argument is non-null, a security check is made to verify
394: * that user-defined protocol handlers can be specified.
395: *
396: * @param protocol
397: * the protocol for the URL.
398: * @param host
399: * the name of the host.
400: * @param port
401: * the port number.
402: * @param file
403: * the name of the resource.
404: * @param handler
405: * the stream handler that this URL uses.
406: *
407: * @throws MalformedURLException
408: * if the parameters do not represent an URL.
409: */
410: public URL(String protocol, String host, int port, String file,
411: URLStreamHandler handler) throws MalformedURLException {
412: if (port < -1) {
413: throw new MalformedURLException(Msg
414: .getString("K0325", port)); //$NON-NLS-1$
415: }
416:
417: if (host != null
418: && host.indexOf(":") != -1 && host.charAt(0) != '[') { //$NON-NLS-1$
419: host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$
420: }
421:
422: if (protocol == null) {
423: throw new NullPointerException(Msg.getString(
424: "K00b3", "null")); //$NON-NLS-1$ //$NON-NLS-2$
425: }
426:
427: this .protocol = protocol;
428: this .host = host;
429: this .port = port;
430:
431: // Set the fields from the arguments. Handle the case where the
432: // passed in "file" includes both a file and a reference part.
433: int index = -1;
434: index = file.indexOf("#", file.lastIndexOf("/")); //$NON-NLS-1$ //$NON-NLS-2$
435: if (index >= 0) {
436: this .file = file.substring(0, index);
437: ref = file.substring(index + 1);
438: } else {
439: this .file = file;
440: }
441: fixURL(false);
442:
443: // Set the stream handler for the URL either to the handler
444: // argument if it was specified, or to the default for the
445: // receiver's protocol if the handler was null.
446: if (handler == null) {
447: setupStreamHandler();
448: if (strmHandler == null) {
449: throw new MalformedURLException(Msg.getString(
450: "K00b3", protocol)); //$NON-NLS-1$
451: }
452: } else {
453: SecurityManager sm = System.getSecurityManager();
454: if (sm != null) {
455: sm.checkPermission(specifyStreamHandlerPermission);
456: }
457: strmHandler = handler;
458: }
459: }
460:
461: void fixURL(boolean fixHost) {
462: int index;
463: if (host != null && host.length() > 0) {
464: authority = host;
465: if (port != -1) {
466: authority = authority + ":" + port; //$NON-NLS-1$
467: }
468: }
469: if (fixHost) {
470: if (host != null && (index = host.lastIndexOf('@')) > -1) {
471: userInfo = host.substring(0, index);
472: host = host.substring(index + 1);
473: } else {
474: userInfo = null;
475: }
476: }
477: if (file != null && (index = file.indexOf('?')) > -1) {
478: query = file.substring(index + 1);
479: path = file.substring(0, index);
480: } else {
481: query = null;
482: path = file;
483: }
484: }
485:
486: /**
487: * Sets the properties of this URL using the provided arguments. This method
488: * is used both within this class and by the <code>URLStreamHandler</code>
489: * code.
490: *
491: * @param protocol
492: * the new protocol.
493: * @param host
494: * the new host name.
495: * @param port
496: * the new port number.
497: * @param file
498: * the new file component.
499: * @param ref
500: * the new reference.
501: *
502: * @see URL
503: * @see URLStreamHandler
504: */
505: protected void set(String protocol, String host, int port,
506: String file, String ref) {
507: if (this .protocol == null) {
508: this .protocol = protocol;
509: }
510: this .host = host;
511: this .file = file;
512: this .port = port;
513: this .ref = ref;
514: hashCode = 0;
515: fixURL(true);
516: }
517:
518: /**
519: * Compares the argument to the receiver, and answers true if they represent
520: * the same URL. Two URLs are equal if they have the same file, host, port,
521: * protocol, and reference components.
522: *
523: * @param o
524: * the object to compare with this URL.
525: * @return <code>true</code> if the object is the same as this URL,
526: * <code>false</code> otherwise.
527: *
528: * @see #hashCode()
529: */
530: @Override
531: public boolean equals(Object o) {
532: if (o == null) {
533: return false;
534: }
535: if (this == o) {
536: return true;
537: }
538: if (this .getClass() != o.getClass()) {
539: return false;
540: }
541: return strmHandler.equals(this , (URL) o);
542: }
543:
544: /**
545: * Answers true if the receiver and the argument refer to the same file. All
546: * components except the reference are compared.
547: *
548: * @param otherURL
549: * URL to compare against.
550: * @return true if the same resource, false otherwise
551: */
552: public boolean sameFile(URL otherURL) {
553: return strmHandler.sameFile(this , otherURL);
554: }
555:
556: /**
557: * Answers a hash code for this URL object.
558: *
559: * @return the hashcode for hashtable indexing
560: */
561: @Override
562: public int hashCode() {
563: if (hashCode == 0) {
564: hashCode = strmHandler.hashCode(this );
565: }
566: return hashCode;
567: }
568:
569: /**
570: * Sets the receiver's stream handler to one which is appropriate for its
571: * protocol. Throws a MalformedURLException if no reasonable handler is
572: * available.
573: * <p>
574: * Note that this will overwrite any existing stream handler with the new
575: * one. Senders must check if the strmHandler is null before calling the
576: * method if they do not want this behavior (a speed optimization).
577: */
578:
579: void setupStreamHandler() {
580: // Check for a cached (previously looked up) handler for
581: // the requested protocol.
582: strmHandler = streamHandlers.get(protocol);
583: if (strmHandler != null) {
584: return;
585: }
586:
587: // If there is a stream handler factory, then attempt to
588: // use it to create the handler.
589: if (streamHandlerFactory != null) {
590: strmHandler = streamHandlerFactory
591: .createURLStreamHandler(protocol);
592: if (strmHandler != null) {
593: streamHandlers.put(protocol, strmHandler);
594: return;
595: }
596: }
597:
598: // Check if there is a list of packages which can provide handlers.
599: // If so, then walk this list looking for an applicable one.
600: String packageList = AccessController
601: .doPrivileged(new PriviAction<String>(
602: "java.protocol.handler.pkgs")); //$NON-NLS-1$
603: if (packageList != null) {
604: StringTokenizer st = new StringTokenizer(packageList, "|"); //$NON-NLS-1$
605: while (st.hasMoreTokens()) {
606: String className = st.nextToken()
607: + "." + protocol + ".Handler"; //$NON-NLS-1$ //$NON-NLS-2$
608:
609: try {
610: strmHandler = (URLStreamHandler) Class.forName(
611: className, true,
612: ClassLoader.getSystemClassLoader())
613: .newInstance();
614: if (strmHandler != null) {
615: streamHandlers.put(protocol, strmHandler);
616: }
617: return;
618: } catch (IllegalAccessException e) {
619: } catch (InstantiationException e) {
620: } catch (ClassNotFoundException e) {
621: }
622: }
623: }
624:
625: // No one else has provided a handler, so try our internal one.
626:
627: String className = "org.apache.harmony.luni.internal.net.www.protocol." + protocol //$NON-NLS-1$
628: + ".Handler"; //$NON-NLS-1$
629: try {
630: strmHandler = (URLStreamHandler) Class.forName(className)
631: .newInstance();
632: } catch (IllegalAccessException e) {
633: } catch (InstantiationException e) {
634: } catch (ClassNotFoundException e) {
635: }
636: if (strmHandler != null) {
637: streamHandlers.put(protocol, strmHandler);
638: }
639:
640: }
641:
642: /**
643: * Answers an Object representing the resource referenced by this URL.
644: *
645: * @return The object of the resource pointed by this URL.
646: *
647: * @throws IOException
648: * If an error occurred obtaining the content.
649: */
650: public final Object getContent() throws IOException {
651: return openConnection().getContent();
652: }
653:
654: /**
655: * Answers an Object representing the resource referenced by this URL.
656: *
657: * @param types
658: * The list of acceptable content types
659: * @return The object of the resource pointed by this URL, or null if the
660: * content does not match a specified content type.
661: *
662: * @throws IOException
663: * If an error occurred obtaining the content.
664: */
665: // Param not generic in spec
666: @SuppressWarnings("unchecked")
667: public final Object getContent(Class[] types) throws IOException {
668: return openConnection().getContent(types);
669: }
670:
671: /**
672: * Answers a stream for reading from this URL.
673: *
674: * @return a stream on the contents of the resource.
675: *
676: * @throws IOException
677: * if a stream could not be created.
678: */
679: public final InputStream openStream() throws java.io.IOException {
680: return openConnection().getInputStream();
681: }
682:
683: /**
684: * Creates a connection to this URL using the appropriate ProtocolHandler.
685: *
686: * @return The connection to this URL.
687: *
688: * @throws IOException
689: * if the connection to the URL is not possible.
690: */
691: public URLConnection openConnection() throws IOException {
692: return strmHandler.openConnection(this );
693: }
694:
695: /**
696: * Creates a URI related with this URL
697: *
698: * @return a URI related to this URL
699: * @throws URISyntaxException
700: * if this URL cannot format into URI
701: */
702: public URI toURI() throws URISyntaxException {
703: return new URI(toExternalForm());
704: }
705:
706: /**
707: * The method is the same as <code>openConnection()</code> except that it
708: * uses the <code>proxy</code> to establish a connection to this URL using
709: * appropriate ProtocolHandler.
710: *
711: * @return The connection to this URL.
712: * @param proxy
713: * the proxy which is used to make the connection
714: *
715: * @exception IOException
716: * thrown if an IO error occurs during connection
717: * establishment
718: * @exception SecurityException
719: * thrown if a security manager is installed and it denies
720: * the permission to connect to the proxy.
721: * @exception IllegalArgumentException
722: * thrown if the proxy is null or of an invalid type.
723: * @exception UnsupportedOperationException
724: * thrown if the protocol handler doesn't support this
725: * method.
726: */
727: public URLConnection openConnection(Proxy proxy) throws IOException {
728: if (null == proxy) {
729: throw new IllegalArgumentException(Msg.getString("K034c")); //$NON-NLS-1$
730: }
731: return strmHandler.openConnection(this , proxy);
732: }
733:
734: /**
735: * Answers a string containing a concise, human-readable description of the
736: * receiver.
737: *
738: * @return a printable representation for the receiver.
739: */
740: @Override
741: public String toString() {
742: return toExternalForm();
743: }
744:
745: /**
746: * Create and return the String representation of this URL.
747: *
748: * @return the external representation of this URL.
749: *
750: * @see #toString()
751: * @see URL
752: * @see URLStreamHandler#toExternalForm(URL)
753: */
754: public String toExternalForm() {
755: if (strmHandler == null) {
756: return "unknown protocol(" + protocol + ")://" + host + file; //$NON-NLS-1$ //$NON-NLS-2$
757: }
758: return strmHandler.toExternalForm(this );
759: }
760:
761: /**
762: * This method is called to restore the state of a URL object that has been
763: * serialized. The stream handler is determined from the URL's protocol.
764: *
765: * @param stream
766: * the stream to read from.
767: *
768: * @throws IOException
769: * if an IO Exception occurs while reading the stream or the
770: * handler can not be found.
771: */
772: private void readObject(java.io.ObjectInputStream stream)
773: throws java.io.IOException {
774: try {
775: stream.defaultReadObject();
776: if (host != null && authority == null) {
777: fixURL(true);
778: } else if (authority != null) {
779: int index;
780: if ((index = authority.lastIndexOf('@')) > -1) {
781: userInfo = authority.substring(0, index);
782: }
783: if (file != null && (index = file.indexOf('?')) > -1) {
784: query = file.substring(index + 1);
785: path = file.substring(0, index);
786: } else {
787: path = file;
788: }
789: }
790: setupStreamHandler();
791: if (strmHandler == null) {
792: throw new IOException(Msg.getString("K00b3", protocol)); //$NON-NLS-1$
793: }
794: } catch (ClassNotFoundException e) {
795: throw new IOException(e.toString());
796: }
797: }
798:
799: /**
800: * This method is called to write any non-transient, non-static variables
801: * into the output stream.
802: * <p>
803: * Note that, we really only need the readObject method but the spec that
804: * says readObject will be ignored if no writeObject is present.
805: *
806: * @param s
807: * the stream to write to.
808: *
809: * @throws IOException
810: * if an IO Exception occurs during the write.
811: */
812: private void writeObject(ObjectOutputStream s) throws IOException {
813: s.defaultWriteObject();
814: }
815:
816: /**
817: * Answers the file component of this URL.
818: *
819: * @return the receiver's file.
820: */
821: public String getFile() {
822: return file;
823: }
824:
825: /**
826: * Answers the host component of this URL.
827: *
828: * @return the receiver's host.
829: */
830: public String getHost() {
831: return host;
832: }
833:
834: /**
835: * Answers the port component of this URL.
836: *
837: * @return the receiver's port.
838: */
839: public int getPort() {
840: return port;
841: }
842:
843: /**
844: * Answers the protocol component of this URL.
845: *
846: * @return the receiver's protocol.
847: */
848: public String getProtocol() {
849: return protocol;
850: }
851:
852: /**
853: * Answers the reference component of this URL.
854: *
855: * @return the receiver's reference component.
856: */
857: public String getRef() {
858: return ref;
859: }
860:
861: /**
862: * Answers the query component of this URL.
863: *
864: * @return the receiver's query.
865: */
866: public String getQuery() {
867: return query;
868: }
869:
870: /**
871: * Answers the path component of this URL.
872: *
873: * @return the receiver's path.
874: */
875: public String getPath() {
876: return path;
877: }
878:
879: /**
880: * Answers the user info component of this URL.
881: *
882: * @return the receiver's user info.
883: */
884: public String getUserInfo() {
885: return userInfo;
886: }
887:
888: /**
889: * Answers the authority component of this URL.
890: *
891: * @return the receiver's authority.
892: */
893: public String getAuthority() {
894: return authority;
895: }
896:
897: /**
898: * Sets the properties of this URL using the provided arguments. This method
899: * is used both within this class and by the <code>URLStreamHandler</code>
900: * code.
901: *
902: * @param protocol
903: * the new protocol.
904: * @param host
905: * the new host name.
906: * @param port
907: * the new port number.
908: * @param authority
909: * the new authority.
910: * @param userInfo
911: * the new user info.
912: * @param path
913: * the new path component.
914: * @param query
915: * the new query.
916: * @param ref
917: * the new reference.
918: *
919: * @see URL
920: * @see URLStreamHandler
921: */
922: protected void set(String protocol, String host, int port,
923: String authority, String userInfo, String path,
924: String query, String ref) {
925: String file = path;
926: if (query != null && !query.equals("")) { //$NON-NLS-1$
927: if (file != null) {
928: file = file + "?" + query; //$NON-NLS-1$
929: } else {
930: file = "?" + query; //$NON-NLS-1$
931: }
932: }
933: set(protocol, host, port, file, ref);
934: this .authority = authority;
935: this .userInfo = userInfo;
936: this .path = path;
937: this .query = query;
938: }
939:
940: /**
941: * Returns the default port for this URL as defined by the URLStreamHandler.
942: *
943: * @return the default port for this URL
944: *
945: * @see URLStreamHandler#getDefaultPort
946: */
947: public int getDefaultPort() {
948: return strmHandler.getDefaultPort();
949: }
950: }
|