Ejemplo n.º 1
0
  /**
   * Same as {@link #openConnection()}, except that the connection will be made through the
   * specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter
   * and make a normal connection.
   *
   * <p>Invoking this method preempts the system's default ProxySelector settings.
   *
   * @param proxy the Proxy through which this connection will be made. If direct connection is
   *     desired, Proxy.NO_PROXY should be specified.
   * @return a <code>URLConnection</code> to the URL.
   * @exception IOException if an I/O exception occurs.
   * @exception SecurityException if a security manager is present and the caller doesn't have
   *     permission to connect to the proxy.
   * @exception IllegalArgumentException will be thrown if proxy is null, or proxy has the wrong
   *     type
   * @exception UnsupportedOperationException if the subclass that implements the protocol handler
   *     doesn't support this method.
   * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
   * @see java.net.URLConnection
   * @see java.net.URLStreamHandler#openConnection(java.net.URL, java.net.Proxy)
   * @since 1.5
   */
  public URLConnection openConnection(Proxy proxy) throws java.io.IOException {
    if (proxy == null) {
      throw new IllegalArgumentException("proxy can not be null");
    }

    SecurityManager sm = System.getSecurityManager();
    if (proxy.type() != Proxy.Type.DIRECT && sm != null) {
      InetSocketAddress epoint = (InetSocketAddress) proxy.address();
      if (epoint.isUnresolved()) sm.checkConnect(epoint.getHostName(), epoint.getPort());
      else sm.checkConnect(epoint.getAddress().getHostAddress(), epoint.getPort());
    }
    return handler.openConnection(this, proxy);
  }
Ejemplo n.º 2
0
 /**
  * Constructs a string representation of this <code>URL</code>. The string is created by calling
  * the <code>toExternalForm</code> method of the stream protocol handler for this object.
  *
  * @return a string representation of this object.
  * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
  */
 public String toExternalForm() {
   return handler.toExternalForm(this);
 }
Ejemplo n.º 3
0
 /**
  * Returns a {@link java.net.URLConnection URLConnection} instance that represents a connection to
  * the remote object referred to by the {@code URL}.
  *
  * <p>A new instance of {@linkplain java.net.URLConnection URLConnection} is created every time
  * when invoking the {@linkplain java.net.URLStreamHandler#openConnection(URL)
  * URLStreamHandler.openConnection(URL)} method of the protocol handler for this URL.
  *
  * <p>It should be noted that a URLConnection instance does not establish the actual network
  * connection on creation. This will happen only when calling {@linkplain
  * java.net.URLConnection#connect() URLConnection.connect()}.
  *
  * <p>If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized
  * URLConnection subclass belonging to one of the following packages or one of their subpackages:
  * java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For
  * example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be
  * returned.
  *
  * @return a {@link java.net.URLConnection URLConnection} linking to the URL.
  * @exception IOException if an I/O exception occurs.
  * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  */
 public URLConnection openConnection() throws java.io.IOException {
   return handler.openConnection(this);
 }
Ejemplo n.º 4
0
  /**
   * Creates an integer suitable for hash table indexing.
   *
   * <p>The hash code is based upon all the URL components relevant for URL comparison. As such,
   * this operation is a blocking operation.
   *
   * <p>
   *
   * @return a hash code for this <code>URL</code>.
   */
  public synchronized int hashCode() {
    if (hashCode != -1) return hashCode;

    hashCode = handler.hashCode(this);
    return hashCode;
  }
Ejemplo n.º 5
0
 /**
  * Compares two URLs, excluding the fragment component.
  *
  * <p>Returns <code>true</code> if this <code>URL</code> and the <code>other</code> argument are
  * equal without taking the fragment component into consideration.
  *
  * @param other the <code>URL</code> to compare against.
  * @return <code>true</code> if they reference the same remote object; <code>false</code>
  *     otherwise.
  */
 public boolean sameFile(URL other) {
   return handler.sameFile(this, other);
 }
Ejemplo n.º 6
0
 /**
  * Gets the default port number of the protocol associated with this <code>URL</code>. If the URL
  * scheme or the URLStreamHandler for the URL do not define a default port number, then -1 is
  * returned.
  *
  * @return the port number
  * @since 1.4
  */
 public int getDefaultPort() {
   return handler.getDefaultPort();
 }
Ejemplo n.º 7
0
  /**
   * Compares this URL for equality with another object.
   *
   * <p>If the given object is not a URL then this method immediately returns <code>false</code>.
   *
   * <p>Two URL objects are equal if they have the same protocol, reference equivalent hosts, have
   * the same port number on the host, and the same file and fragment of the file.
   *
   * <p>Two hosts are considered equivalent if both host names can be resolved into the same IP
   * addresses; else if either host name can't be resolved, the host names must be equal without
   * regard to case; or both host names equal to null.
   *
   * <p>Since hosts comparison requires name resolution, this operation is a blocking operation.
   *
   * <p>Note: The defined behavior for <code>equals</code> is known to be inconsistent with virtual
   * hosting in HTTP.
   *
   * @param obj the URL to compare against.
   * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
   */
  public boolean equals(Object obj) {
    if (!(obj instanceof URL)) return false;
    URL u2 = (URL) obj;

    return handler.equals(this, u2);
  }
Ejemplo n.º 8
0
 /**
  * Returns a String representing this URL. Identical to toExternalForm(). The value returned is
  * created by the protocol handler's toExternalForm method. Overrides Object.toString()
  *
  * @return A string for this URL
  */
 public String toString() {
   // Identical to toExternalForm().
   return ph.toExternalForm(this);
 }
Ejemplo n.º 9
0
  /**
   * Creates a URL by parsing the given spec with the specified handler within a specified context.
   * If the handler is null, the parsing occurs as with the two argument constructor.
   *
   * @param context the context in which to parse the specification.
   * @param spec the <code>String</code> to parse as a URL.
   * @param handler the stream handler for the URL.
   * @exception MalformedURLException if no protocol is specified, or an unknown protocol is found,
   *     or <tt>spec</tt> is <tt>null</tt>.
   * @exception SecurityException if a security manager exists and its <code>checkPermission</code>
   *     method doesn't allow specifying a stream handler.
   * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
   * @see java.net.URLStreamHandler
   * @see java.net.URLStreamHandler#parseURL(java.net.URL, java.lang.String, int, int)
   */
  public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException {
    String original = spec;
    int i, limit, c;
    int start = 0;
    String newProtocol = null;
    boolean aRef = false;
    boolean isRelative = false;

    // Check for permission to specify a handler
    if (handler != null) {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
        checkSpecifyHandler(sm);
      }
    }

    try {
      limit = spec.length();
      while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
        limit--; // eliminate trailing whitespace
      }
      while ((start < limit) && (spec.charAt(start) <= ' ')) {
        start++; // eliminate leading whitespace
      }

      if (spec.regionMatches(true, start, "url:", 0, 4)) {
        start += 4;
      }
      if (start < spec.length() && spec.charAt(start) == '#') {
        /* we're assuming this is a ref relative to the context URL.
         * This means protocols cannot start w/ '#', but we must parse
         * ref URL's like: "hello:there" w/ a ':' in them.
         */
        aRef = true;
      }
      for (i = start; !aRef && (i < limit) && ((c = spec.charAt(i)) != '/'); i++) {
        if (c == ':') {

          String s = spec.substring(start, i).toLowerCase();
          if (isValidProtocol(s)) {
            newProtocol = s;
            start = i + 1;
          }
          break;
        }
      }

      // Only use our context if the protocols match.
      protocol = newProtocol;
      if ((context != null)
          && ((newProtocol == null) || newProtocol.equalsIgnoreCase(context.protocol))) {
        // inherit the protocol handler from the context
        // if not specified to the constructor
        if (handler == null) {
          handler = context.handler;
        }

        // If the context is a hierarchical URL scheme and the spec
        // contains a matching scheme then maintain backwards
        // compatibility and treat it as if the spec didn't contain
        // the scheme; see 5.2.3 of RFC2396
        if (context.path != null && context.path.startsWith("/")) newProtocol = null;

        if (newProtocol == null) {
          protocol = context.protocol;
          authority = context.authority;
          userInfo = context.userInfo;
          host = context.host;
          port = context.port;
          file = context.file;
          path = context.path;
          isRelative = true;
        }
      }

      if (protocol == null) {
        throw new MalformedURLException("no protocol: " + original);
      }

      // Get the protocol handler if not specified or the protocol
      // of the context could not be used
      if (handler == null && (handler = getURLStreamHandler(protocol)) == null) {
        throw new MalformedURLException("unknown protocol: " + protocol);
      }

      this.handler = handler;

      i = spec.indexOf('#', start);
      if (i >= 0) {
        ref = spec.substring(i + 1, limit);
        limit = i;
      }

      /*
       * Handle special case inheritance of query and fragment
       * implied by RFC2396 section 5.2.2.
       */
      if (isRelative && start == limit) {
        query = context.query;
        if (ref == null) {
          ref = context.ref;
        }
      }

      handler.parseURL(this, spec, start, limit);

    } catch (MalformedURLException e) {
      throw e;
    } catch (Exception e) {
      MalformedURLException exception = new MalformedURLException(e.getMessage());
      exception.initCause(e);
      throw exception;
    }
  }
Ejemplo n.º 10
0
 /**
  * Tests whether or not another URL refers to the same "file" as this one. This will be true if
  * and only if the passed object is not null, is a URL, and matches all fields but the ref (ie,
  * protocol, host, port, and file);
  *
  * @param url The URL object to test with
  * @return true if URL matches this URL's file, false otherwise
  */
 public boolean sameFile(URL url) {
   return ph.sameFile(this, url);
 }
Ejemplo n.º 11
0
 /**
  * Returns a URLConnection object that represents a connection to the remote object referred to by
  * the URL. The URLConnection is created by calling the openConnection() method of the protocol
  * handler
  *
  * @return A URLConnection for this URL
  * @exception IOException If an error occurs
  */
 public URLConnection openConnection() throws IOException {
   return ph.openConnection(this);
 }
Ejemplo n.º 12
0
 /**
  * Returns a hashcode computed by the URLStreamHandler of this URL
  *
  * @return The hashcode for this URL.
  */
 public int hashCode() {
   if (hashCode != 0) return hashCode; // Use cached value if available.
   else return ph.hashCode(this);
 }
Ejemplo n.º 13
0
 /**
  * Returns the default port of the URL. If the StreamHandler for the URL protocol does not define
  * a default port it returns -1.
  *
  * @return The default port of the current protocol.
  */
 public int getDefaultPort() {
   return ph.getDefaultPort();
 }
Ejemplo n.º 14
0
  /**
   * Test another URL for equality with this one. This will be true only if the argument is non-null
   * and all of the fields in the URL's match exactly (ie, protocol, host, port, file, and ref).
   * Overrides Object.equals(), implemented by calling the equals method of the handler.
   *
   * @param obj The URL to compare with
   * @return true if the URL is equal, false otherwise
   */
  public boolean equals(Object obj) {
    if (!(obj instanceof URL)) return false;

    return ph.equals(this, (URL) obj);
  }