Ejemplo n.º 1
0
        /*
         * Creates a new URL instance using the given arguments. The URL uses the
         * specified port instead of the default port for the given protocol.
         *
         * @param protocol
         *            the protocol of the new URL.
         * @param host
         *            the host name or IP address of the new URL.
         * @param port
         *            the specific port number of the URL. {@code -1} represents the
         *            default port of the protocol.
         * @param file
         *            the name of the resource.
         * @param handler
         *            the stream handler to be used by this URL.
         * @throws MalformedURLException
         *             if the combination of all arguments do not represent a valid
         *             URL or the protocol is invalid.
         * @throws SecurityException
         *             if {@code handler} is non-{@code null}, and a security
         *             manager is installed that disallows user-defined protocol
         *             handlers.
         */
        public URL(String protocol, String host, int port, String file,
                   URLStreamHandler handler)
        {// throws MalformedURLException {
            if (port < -1)
            {
                throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$
            }

            if (host != null && host.indexOf(":") != -1 && host.charAt(0) != '[')
            {                            //$NON-NLS-1$
                host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$
            }

            if (protocol == null)
            {
                throw new java.lang.NullPointerException("Unknown protocol: " + "null"); //$NON-NLS-1$ //$NON-NLS-2$
            }

            this.protocol = protocol;
            this.host     = host;
            this.port     = port;

            // Set the fields from the arguments. Handle the case where the
            // passed in "file" includes both a file and a reference part.
            int index = -1;

            index = file.indexOf("#", file.lastIndexOf("/")); //$NON-NLS-1$ //$NON-NLS-2$
            if (index >= 0)
            {
                this.file = file.substring(0, index);
                refJ      = file.substring(index + 1);
            }
            else
            {
                this.file = file;
            }
            fixURL(false);

            // Set the stream handler for the URL either to the handler
            // argument if it was specified, or to the default for the
            // receiver's protocol if the handler was null.
            if (handler == null)
            {
                setupStreamHandler();
                if (strmHandler == null)
                {
                    throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$
                }
            }
            else
            {
                java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager();
                if (sm != null)
                {
                    sm.checkPermission(specifyStreamHandlerPermission);
                }
                strmHandler = handler;
            }
        }
Ejemplo n.º 2
0
        /**
         * Sets the receiver's stream handler to one which is appropriate for its
         * protocol. Throws a MalformedURLException if no reasonable handler is
         * available.
         * <p/>
         * Note that this will overwrite any existing stream handler with the new
         * one. Senders must check if the strmHandler is null before calling the
         * method if they do not want this behavior (a speed optimization).
         */
        void setupStreamHandler()
        {
            String className = null;

            // Check for a cached (previously looked up) handler for
            // the requested protocol.
            strmHandler = streamHandlers.get(protocol);
            if (strmHandler != null)
            {
                return;
            }

            // If there is a stream handler factory, then attempt to
            // use it to create the handler.
            if (streamHandlerFactory != null)
            {
                strmHandler = streamHandlerFactory.createURLStreamHandler(protocol);
                if (strmHandler != null)
                {
                    streamHandlers.put(protocol, strmHandler);
                    return;
                }
            }

            // Check if there is a list of packages which can provide handlers.
            // If so, then walk this list looking for an applicable one.
            String packageList = java.security.AccessController
                    .doPrivileged(new PriviAction<String>(
                            "java.protocol.handler.pkgs")); //$NON-NLS-1$
            if (packageList != null)
            {
                java.util.StringTokenizer st = new java.util.StringTokenizer(packageList, "|"); //$NON-NLS-1$
                while (st.hasMoreTokens())
                {
                    className = st.nextToken() + "." + protocol + ".Handler"; //$NON-NLS-1$ //$NON-NLS-2$

                    try
                    {
                        strmHandler = (URLStreamHandler)java.lang.Class.forName(className,
                                true, java.lang.ClassLoader.getSystemClassLoader())
                                .newInstance();
                        if (strmHandler != null)
                        {
                            streamHandlers.put(protocol, strmHandler);
                        }
                        return;
                    }
                    catch (java.lang.IllegalAccessException e)
                    {
                    }
                    catch (java.lang.InstantiationException e)
                    {
                    }
                    catch (java.lang.ClassNotFoundException e)
                    {
                    }
                }
            }

            // No one else has provided a handler, so try our internal one.

            className = "org.apache.harmony.luni.internal.net.www.protocol." + protocol //$NON-NLS-1$
                    + ".Handler"; //$NON-NLS-1$
            try
            {
                strmHandler = (URLStreamHandler)java.lang.Class.forName(className)
                        .newInstance();
            }
            catch (java.lang.IllegalAccessException e)
            {
            }
            catch (java.lang.InstantiationException e)
            {
            }
            catch (java.lang.ClassNotFoundException e)
            {
            }
            if (strmHandler != null)
            {
                streamHandlers.put(protocol, strmHandler);
            }
        }
Ejemplo n.º 3
0
        /**
         * Creates a new URL instance using the given arguments. The URL uses the
         * specified port instead of the default port for the given protocol.
         *
         * @param protocol
         *            the protocol of the new URL.
         * @param host
         *            the host name or IP address of the new URL.
         * @param port
         *            the specific port number of the URL. {@code -1} represents the
         *            default port of the protocol.
         * @param file
         *            the name of the resource.
         * @param handler
         *            the stream handler to be used by this URL.
         * @throws MalformedURLException
         *             if the combination of all arguments do not represent a valid
         *             URL or the protocol is invalid.
         * @throws SecurityException
         *             if {@code handler} is non-{@code null}, and a security
         *             manager is installed that disallows user-defined protocol
         *             handlers.
         */
        public URL(String protocol, String host, int port, String file,
                URLStreamHandler handler)
        {
            // throws MalformedURLException {
            if (port < -1)
            {
                throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$
            }

            if (host != null && host.indexOf(":") != -1 && host.charAt(0) != '[')
            { //$NON-NLS-1$
                host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$
            }

            if (protocol == null)
            {
                throw new java.lang.NullPointerException("Unknown protocol: " + "null"); //$NON-NLS-1$ //$NON-NLS-2$
            }

            this.protocol = protocol;
            this.host = host;
            this.port = port;

            // Set the fields from the arguments. Handle the case where the
            // passed in "file" includes both a file and a reference part.
            int index = -1;
            index = file.indexOf("#", file.lastIndexOf("/")); //$NON-NLS-1$ //$NON-NLS-2$
            if (index >= 0)
            {
                this.file = file.substring(0, index);
                refJ = file.substring(index + 1);
            }
            else
            {
                this.file = file;
            }
            fixURL(false);

            // Set the stream handler for the URL either to the handler
            // argument if it was specified, or to the default for the
            // receiver's protocol if the handler was null.
            if (handler == null)
            {
                setupStreamHandler();
                if (strmHandler == null)
                {
                    throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$
                }
            }
            else
            {
                java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager();
                if (sm != null)
                {
                    sm.checkPermission(specifyStreamHandlerPermission);
                }
                strmHandler = handler;
            }
        }
Ejemplo n.º 4
0
        /**
         * Creates a new URL to the specified resource {@code spec}. This URL is
         * relative to the given {@code context}. The {@code handler} will be used
         * to parse the URL string representation. If this argument is {@code null}
         * the default {@code URLStreamHandler} will be used. If the protocol of the
         * parsed URL does not match with the protocol of the context URL, then the
         * newly created URL is absolute and bases only on the given URL represented
         * by {@code spec}. Otherwise the protocol is defined by the context URL.
         *
         * @param context
         *            the URL which is used as the context.
         * @param spec
         *            the URL string representation which has to be parsed.
         * @param handler
         *            the specific stream handler to be used by this URL.
         * @throws MalformedURLException
         *             if the given string {@code spec} could not be parsed as a URL
         *             or an invalid protocol has been found.
         */
        public URL(URL context, String spec, URLStreamHandler handler)
        {
            //throws MalformedURLException {
            if (handler != null)
            {
                java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager();
                if (sm != null)
                {
                    sm.checkPermission(specifyStreamHandlerPermission);
                }
                strmHandler = handler;
            }

            if (spec == null)
            {
                throw new MalformedURLException();
            }
            spec = spec.trim();

            // The spec includes a protocol if it includes a colon character
            // before the first occurrence of a slash character. Note that,
            // "protocol" is the field which holds this URLs protocol.
            int index;
            try
            {
                index = spec.indexOf(':');
            }
            catch (java.lang.NullPointerException e)
            {
                throw new MalformedURLException(e.toString());
            }
            int startIPv6Addr = spec.indexOf('[');
            if (index >= 0)
            {
                if ((startIPv6Addr == -1) || (index < startIPv6Addr))
                {
                    protocol = spec.substring(0, index);
                    // According to RFC 2396 scheme part should match
                    // the following expression:
                    // alpha *( alpha | digit | "+" | "-" | "." )
                    char c = protocol.charAt(0);
                    bool valid = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
                    for (int i = 1; valid && (i < protocol.length()); i++)
                    {
                        c = protocol.charAt(i);
                        valid = ('a' <= c && c <= 'z') ||
                                ('A' <= c && c <= 'Z') ||
                                ('0' <= c && c <= '9') ||
                                (c == '+') ||
                                (c == '-') ||
                                (c == '.');
                    }
                    if (!valid)
                    {
                        protocol = null;
                        index = -1;
                    }
                    else
                    {
                        // Ignore case in protocol names.
                        // Scheme is defined by ASCII characters.
                        protocol = Util.toASCIILowerCase(protocol);
                    }
                }
            }

            if (protocol != null)
            {
                // If the context was specified, and it had the same protocol
                // as the spec, then fill in the receiver's slots from the values
                // in the context but still allow them to be over-ridden later
                // by the values in the spec.
                if (context != null && protocol.equals(context.getProtocol()))
                {
                    String cPath = context.getPath();
                    if (cPath != null && cPath.startsWith("/"))
                    { //$NON-NLS-1$
                        set(protocol, context.getHost(), context.getPort(), context
                                .getAuthority(), context.getUserInfo(), cPath,
                                context.getQuery(), null);
                    }
                    if (strmHandler == null)
                    {
                        strmHandler = context.strmHandler;
                    }
                }
            }
            else
            {
                // If the spec did not include a protocol, then the context
                // *must* be specified. Fill in the receiver's slots from the
                // values in the context, but still allow them to be over-ridden
                // by the values in the ("relative") spec.
                if (context == null)
                {
                    throw new MalformedURLException("Protocol not found: " + spec); //$NON-NLS-1$
                }
                set(context.getProtocol(), context.getHost(), context.getPort(),
                        context.getAuthority(), context.getUserInfo(), context
                                .getPath(), context.getQuery(), null);
                if (strmHandler == null)
                {
                    strmHandler = context.strmHandler;
                }
            }

            // If the stream handler has not been determined, set it
            // to the default for the specified protocol.
            if (strmHandler == null)
            {
                setupStreamHandler();
                if (strmHandler == null)
                {
                    throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$
                }
            }

            // Let the handler parse the URL. If the handler throws
            // any exception, throw MalformedURLException instead.
            //
            // Note: We want "index" to be the index of the start of the scheme
            // specific part of the URL. At this point, it will be either
            // -1 or the index of the colon after the protocol, so we
            // increment it to point at either character 0 or the character
            // after the colon.
            try
            {
                strmHandler.parseURL(this, spec, ++index, spec.length());
            }
            catch (Exception e)
            {
                throw new MalformedURLException(e.toString());
            }

            if (port < -1)
            {
                throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$
            }
        }
Ejemplo n.º 5
0
        /*
         * Sets the receiver's stream handler to one which is appropriate for its
         * protocol. Throws a MalformedURLException if no reasonable handler is
         * available.
         * <p/>
         * Note that this will overwrite any existing stream handler with the new
         * one. Senders must check if the strmHandler is null before calling the
         * method if they do not want this behavior (a speed optimization).
         */
        void setupStreamHandler()
        {
            String className = null;

            // Check for a cached (previously looked up) handler for
            // the requested protocol.
            strmHandler = streamHandlers.get(protocol);
            if (strmHandler != null)
            {
                return;
            }

            // If there is a stream handler factory, then attempt to
            // use it to create the handler.
            if (streamHandlerFactory != null)
            {
                strmHandler = streamHandlerFactory.createURLStreamHandler(protocol);
                if (strmHandler != null)
                {
                    streamHandlers.put(protocol, strmHandler);
                    return;
                }
            }

            // Check if there is a list of packages which can provide handlers.
            // If so, then walk this list looking for an applicable one.
            String packageList = java.security.AccessController
                                 .doPrivileged(new PriviAction <String>(
                                                   "java.protocol.handler.pkgs")); //$NON-NLS-1$

            if (packageList != null)
            {
                java.util.StringTokenizer st = new java.util.StringTokenizer(packageList, "|"); //$NON-NLS-1$
                while (st.hasMoreTokens())
                {
                    className = st.nextToken() + "." + protocol + ".Handler"; //$NON-NLS-1$ //$NON-NLS-2$

                    try
                    {
                        strmHandler = (URLStreamHandler)java.lang.Class.forName(className,
                                                                                true, java.lang.ClassLoader.getSystemClassLoader())
                                      .newInstance();
                        if (strmHandler != null)
                        {
                            streamHandlers.put(protocol, strmHandler);
                        }
                        return;
                    }
                    catch (java.lang.IllegalAccessException e)
                    {
                    }
                    catch (java.lang.InstantiationException e)
                    {
                    }
                    catch (java.lang.ClassNotFoundException e)
                    {
                    }
                }
            }

            // No one else has provided a handler, so try our internal one.

            className = "org.apache.harmony.luni.internal.net.www.protocol." + protocol //$NON-NLS-1$
                        + ".Handler";                                                   //$NON-NLS-1$
            try
            {
                strmHandler = (URLStreamHandler)java.lang.Class.forName(className)
                              .newInstance();
            }
            catch (java.lang.IllegalAccessException e)
            {
            }
            catch (java.lang.InstantiationException e)
            {
            }
            catch (java.lang.ClassNotFoundException e)
            {
            }
            if (strmHandler != null)
            {
                streamHandlers.put(protocol, strmHandler);
            }
        }
Ejemplo n.º 6
0
        /*
         * Creates a new URL to the specified resource {@code spec}. This URL is
         * relative to the given {@code context}. The {@code handler} will be used
         * to parse the URL string representation. If this argument is {@code null}
         * the default {@code URLStreamHandler} will be used. If the protocol of the
         * parsed URL does not match with the protocol of the context URL, then the
         * newly created URL is absolute and bases only on the given URL represented
         * by {@code spec}. Otherwise the protocol is defined by the context URL.
         *
         * @param context
         *            the URL which is used as the context.
         * @param spec
         *            the URL string representation which has to be parsed.
         * @param handler
         *            the specific stream handler to be used by this URL.
         * @throws MalformedURLException
         *             if the given string {@code spec} could not be parsed as a URL
         *             or an invalid protocol has been found.
         */
        public URL(URL context, String spec, URLStreamHandler handler)
        {//throws MalformedURLException {
            if (handler != null)
            {
                java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager();
                if (sm != null)
                {
                    sm.checkPermission(specifyStreamHandlerPermission);
                }
                strmHandler = handler;
            }

            if (spec == null)
            {
                throw new MalformedURLException();
            }
            spec = spec.trim();

            // The spec includes a protocol if it includes a colon character
            // before the first occurrence of a slash character. Note that,
            // "protocol" is the field which holds this URLs protocol.
            int index;

            try
            {
                index = spec.indexOf(':');
            }
            catch (java.lang.NullPointerException e)
            {
                throw new MalformedURLException(e.toString());
            }
            int startIPv6Addr = spec.indexOf('[');

            if (index >= 0)
            {
                if ((startIPv6Addr == -1) || (index < startIPv6Addr))
                {
                    protocol = spec.substring(0, index);
                    // According to RFC 2396 scheme part should match
                    // the following expression:
                    // alpha *( alpha | digit | "+" | "-" | "." )
                    char c     = protocol.charAt(0);
                    bool valid = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
                    for (int i = 1; valid && (i < protocol.length()); i++)
                    {
                        c     = protocol.charAt(i);
                        valid = ('a' <= c && c <= 'z') ||
                                ('A' <= c && c <= 'Z') ||
                                ('0' <= c && c <= '9') ||
                                (c == '+') ||
                                (c == '-') ||
                                (c == '.');
                    }
                    if (!valid)
                    {
                        protocol = null;
                        index    = -1;
                    }
                    else
                    {
                        // Ignore case in protocol names.
                        // Scheme is defined by ASCII characters.
                        protocol = Util.toASCIILowerCase(protocol);
                    }
                }
            }

            if (protocol != null)
            {
                // If the context was specified, and it had the same protocol
                // as the spec, then fill in the receiver's slots from the values
                // in the context but still allow them to be over-ridden later
                // by the values in the spec.
                if (context != null && protocol.equals(context.getProtocol()))
                {
                    String cPath = context.getPath();
                    if (cPath != null && cPath.startsWith("/"))
                    { //$NON-NLS-1$
                        set(protocol, context.getHost(), context.getPort(), context
                            .getAuthority(), context.getUserInfo(), cPath,
                            context.getQuery(), null);
                    }
                    if (strmHandler == null)
                    {
                        strmHandler = context.strmHandler;
                    }
                }
            }
            else
            {
                // If the spec did not include a protocol, then the context
                // *must* be specified. Fill in the receiver's slots from the
                // values in the context, but still allow them to be over-ridden
                // by the values in the ("relative") spec.
                if (context == null)
                {
                    throw new MalformedURLException("Protocol not found: " + spec); //$NON-NLS-1$
                }
                set(context.getProtocol(), context.getHost(), context.getPort(),
                    context.getAuthority(), context.getUserInfo(), context
                    .getPath(), context.getQuery(), null);
                if (strmHandler == null)
                {
                    strmHandler = context.strmHandler;
                }
            }

            // If the stream handler has not been determined, set it
            // to the default for the specified protocol.
            if (strmHandler == null)
            {
                setupStreamHandler();
                if (strmHandler == null)
                {
                    throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$
                }
            }

            // Let the handler parse the URL. If the handler throws
            // any exception, throw MalformedURLException instead.
            //
            // Note: We want "index" to be the index of the start of the scheme
            // specific part of the URL. At this point, it will be either
            // -1 or the index of the colon after the protocol, so we
            // increment it to point at either character 0 or the character
            // after the colon.
            try
            {
                strmHandler.parseURL(this, spec, ++index, spec.length());
            }
            catch (Exception e)
            {
                throw new MalformedURLException(e.toString());
            }

            if (port < -1)
            {
                throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$
            }
        }