⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionmanager.java

📁 EasyJF信息发布全部源代码!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**
     * Adds a cookie to the cookie jar.
     * @param cookie The cookie to add.
     * @param domain The domain to use in case the cookie has no domain attribute.
     */
    public void setCookie (Cookie cookie, String domain)
    {
        String path;
        Vector cookies;
        Cookie probe;
        boolean found; // flag if a cookie with current name is already there

        if (null != cookie.getDomain ())
            domain = cookie.getDomain ();
        path = cookie.getPath ();
        if (null == mCookieJar)
            mCookieJar = new Hashtable (); // turn on cookie processing
        cookies = (Vector)mCookieJar.get (domain);
        if (null != cookies)
        {
            found = false;
            for (int j = 0; j < cookies.size (); j++)
            {
                probe = (Cookie)cookies.elementAt (j);
                if (probe.getName ().equalsIgnoreCase (cookie.getName ()))
                {
                    // we keep paths sorted most specific to least
                    if (probe.getPath ().equals (path))
                    {
                        cookies.setElementAt (cookie, j); // replace
                        found = true; // cookie found, set flag
                        break;
                    }
                    else if (path.startsWith (probe.getPath ()))
                    {
                        cookies.insertElementAt (cookie, j);
                        found = true; // cookie found, set flag
                        break;
                    }
                }
            }
            if (!found)
                // there's no cookie with the current name, therefore it's added
                // at the end of the list (faster then inserting at the front)
                cookies.addElement (cookie);
        }
        else
        {   // new cookie list needed
            cookies = new Vector ();
            cookies.addElement (cookie);
            mCookieJar.put (domain, cookies);
        }
    }

    /**
     * Get the monitoring object, if any.
     * @return Returns the monitor, or null if none has been assigned.
     */
    public ConnectionMonitor getMonitor ()
    {
        return (mMonitor);
    }

    /**
     * Set the monitoring object.
     * @param monitor The monitor to set.
     */
    public void setMonitor (ConnectionMonitor monitor)
    {
        mMonitor = monitor;
    }

    /**
     * Opens a connection using the given url.
     * @param url The url to open.
     * @return The connection.
     * @exception ParserException if an i/o exception occurs accessing the url.
     */
    public URLConnection openConnection (URL url)
        throws
            ParserException
    {
        Properties sysprops;
        Hashtable properties;
        Enumeration enumeration;
        String key;
        String value;
        String set = null; // old proxySet value
        String host = null; // old proxyHost value
        String port = null; // old proxyPort value
        String host2 = null; // old http.proxyHost value
        String port2 = null; // old http.proxyPort value
        HttpURLConnection http;
        String auth;
        String encoded;
        URLConnection ret;

        try
        {
            try
            {
            		
                // set up for proxy
                if ((null != getProxyHost ()) && (0 != getProxyPort ()))
                {
                    sysprops = System.getProperties ();
                    set = (String)sysprops.put ("proxySet", "true");
                    host = (String)sysprops.put ("proxyHost", getProxyHost ());
                    port = (String)sysprops.put ("proxyPort",
                        Integer.toString (getProxyPort ()));
                    // see http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html
                    host2 = (String)sysprops.put ("http.proxyHost",
                        getProxyHost ());
                    port2 = (String)sysprops.put ("http.proxyPort",
                        Integer.toString (getProxyPort ()));
                    System.setProperties (sysprops);                    
                }
    
                // open the connection... but don't connect yet
                ret = url.openConnection ();
              /* ret.setRequestProperty("Content-type", "multipart/form-data;boundary=" + BOUNDARY);
       	       ret.setRequestProperty("Referer", "http://www.omg.org/mda/");
       	      ret.setRequestProperty("Cache-Control", "no-cache");*/
       	
                if (ret instanceof HttpURLConnection)
                {
                    http = (HttpURLConnection)ret;
                    
                    // set the fixed request properties
                    properties = getRequestProperties ();
                    if (null != properties)
                        for (enumeration = properties.keys ();
                                enumeration.hasMoreElements ();)
                        {
                            key = (String)enumeration.nextElement ();
                            value = (String)properties.get (key);
                            ret.setRequestProperty (key, value);
                        }

                    // set the proxy name and password
                    if ((null != getProxyUser ())
                        && (null != getProxyPassword ()))
                    {                    	
                        auth = getProxyUser () + ":" + getProxyPassword ();                        
                        encoded ="Basic "+ encode (auth.getBytes("ISO-8859-1"));                        
                        ret.setRequestProperty ("Proxy-Authorization",encoded);
                    }
                    
                    // set the URL name and password
                    if ((null != getUser ()) && (null != getPassword ()))
                    {
                        auth = getUser () + ":" + getPassword ();
                        encoded = encode (auth.getBytes("ISO-8859-1"));
                        ret.setRequestProperty ("Authorization",
                            "Basic " + encoded);
                    }
    
                    // set the cookies based on the url
                    addCookies (ret);

                    if (null != getMonitor ())
                        getMonitor ().preConnect (http);
                }
                else
                    http = null;

                try
                {
                    ret.connect ();
                    
                    if (null != http)
                    {
                        if (null != getMonitor ())
                            getMonitor ().postConnect (http);
    
                        parseCookies (ret);
                    }
                }
                catch (UnknownHostException uhe)
                {
                    int message = (int)(Math.random () * FOUR_OH_FOUR.length);
                    throw new ParserException (FOUR_OH_FOUR[message], uhe);
                }
                catch (IOException ioe)
                {
                    throw new ParserException (ioe.getMessage (), ioe);
                }
            }
            finally
            {
                if ((null != getProxyHost ()) && (0 != getProxyPort ()))
                {
                    sysprops = System.getProperties ();
                    if (null != set)
                        sysprops.put ("proxySet", set);
                    else
                        sysprops.remove ("proxySet");
                    if (null != host)
                        sysprops.put ("proxyHost", host);
                    else
                        sysprops.remove ("proxyHost");
                    if (null != port)
                        sysprops.put ("proxyPort", port);
                    else
                        sysprops.remove ("proxyPort");
                    if (null != host2)
                        sysprops.put ("http.proxyHost", host2);
                    else
                        sysprops.remove ("http.proxyHost");
                    if (null != port2)
                        sysprops.put ("http.proxyPort", port2);
                    else
                        sysprops.remove ("http.proxyPort");
                    System.setProperties (sysprops);
                }
            }
        }
        catch (IOException ioe)
        {
            String msg = "Error in opening a connection to "
                + url.toExternalForm ();
            ParserException ex = new ParserException (msg, ioe);
            throw ex;
        }

        return (ret);
    }

    /**
     * Encodes a byte array into BASE64 in accordance with
     * <a href="http://www.faqs.org/rfcs/rfc2045.html">RFC 2045</a>.
     * @param array The bytes to convert.
     * @return A BASE64 encoded string.
     */
    public final static String encode (byte[] array)
    {
        int last; // last byte
        int count; // character count
        int separators; // line separator count
        int length; // length of returned string
        char[] encoded; // encoded characters
        int left; // bytes left
        int end;
        int block; // encoding buffer
        int r; // shift count
        int n; // byte to encode
        int index; // index into output array
        String ret;

        if ((null != array) && (0 != array.length))
        {
            last = array.length - 1;
            count = (last / 3 + 1) << 2;
            separators = (count - 1) / 76;
            length = count + separators;
            encoded = new char[length];
            index = 0;
            separators = 0;
            for (int i = 0; i <= last; i += 3)
            {
                left = last - i;
                end = (left > 1 ? 2 : left);
    
                // collect 1 to 3 bytes to encode
                block = 0;
                r = 16;
                for (int j = 0; j <= end; j++)
                {
                    n = array[i + j];
                    block += (n < 0 ? n + 256 : n) << r;
                    r -= 8;
                }
    
                // encode into 2-4 chars padding with '=' if no data left
                encoded[index++] = BASE64_CHAR_TABLE[(block >>> 18) & 0x3f];
                encoded[index++] = BASE64_CHAR_TABLE[(block >>> 12) & 0x3f];
                encoded[index++] = left > 0 ?
                    BASE64_CHAR_TABLE[(block >>> 6) & 0x3f] :
                    '=';
                encoded[index++] = left > 1 ?
                    BASE64_CHAR_TABLE[block & 0x3f] :
                    '=';
    
                if ((0 == (index - separators) % 76) && (index < length))
                {
                    encoded[index++] = '\n';
                    separators += 1;
                }
            }
            ret = new String (encoded);
        }
        else
            ret = "";

        return (ret);
    }

    /**
     * Turn spaces into %20.
     * ToDo: make this more generic
     * (see RFE #1010593 provide URL encoding/decoding utilities).
     * @param url The url containing spaces.
     * @return The URL with spaces as %20 sequences.
     */
    public String fixSpaces (String url)
    {
        int index;
        int length;
        char ch;
        StringBuffer buffer;

        index = url.indexOf (' ');
        if (-1 != index)
        {
            length = url.length ();
            buffer = new StringBuffer (length * 3);
            buffer.append (url.substring (0, index));
            for (int i = index; i < length; i++)
            {
                ch = url.charAt (i);
                if (ch==' ')
                    buffer.append ("%20");
                else
                    buffer.append (ch);
            }
            url = buffer.toString ();
        }

        return (url);
    }

    /**
     * Opens a connection based on a given string.
     * The string is either a file, in which case <code>file://localhost</code>
     * is prepended to a canonical path derived from the string, or a url that
     * begins with one of the known protocol strings, i.e. <code>http://</code>.
     * Embedded spaces are silently converted to %20 sequences.
     * @param string The name of a file or a url.
     * @return The connection.
     * @exception ParserException if the string is not a valid url or file.
     */
    public URLConnection openConnection (String string)
        throws
            ParserException
    {
        final String prefix = "file://localhost";
        String resource;
        URL url;
        StringBuffer buffer;
        URLConnection ret;

        try
        {
            url = new URL (fixSpaces (string));
            ret =  openConnection (url);
        }
        catch (MalformedURLException murle)
        {   // try it as a file
            try
            {
                File file = new File (string);
                resource = file.getCanonicalPath ();
                buffer = new StringBuffer (prefix.length ()
                    + resource.length ());
                buffer.append (prefix);
                if (!resource.startsWith ("/"))
                    buffer.append ("/");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -