Java Code Examples for java.net.URLConnection#getURL()

The following examples show how to use java.net.URLConnection#getURL() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 2
/**
 * Returns the URL.
 *
 * @return null if the class file could not be obtained. 
 */
@Override
public URL find(String classname) {
    try {
        URLConnection con = openClassfile0(classname);
        InputStream is = con.getInputStream();
        if (is != null) {
            is.close();
            return con.getURL();
        }
    }
    catch (IOException e) {}
    return null; 
}
 
Example 3
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 4
Source Project: jdk8u60   File: plain.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 5
Source Project: materialup   File: CookieManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prior to opening a URLConnection, calling this method will set all unexpired cookies that match the path or subpaths for thi
 * underlying URL The connection MUST NOT have been opened method or an IOException will be thrown.
 *
 * @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown
 * @throws java.io.IOException Thrown if conn has already been opened.
 */
public void setCookies(URLConnection conn) throws IOException {

    // let's determine the domain and path to retrieve the appropriate cookies
    URL url = conn.getURL();
    String domain = getDomainFromHost(url.getHost());
    String path = url.getPath();

    Map domainStore = (Map) store.get(domain);
    if (domainStore == null)
        return;
    StringBuffer cookieStringBuffer = new StringBuffer();

    Iterator cookieNames = domainStore.keySet().iterator();
    while (cookieNames.hasNext()) {
        String cookieName = (String) cookieNames.next();
        Map cookie = (Map) domainStore.get(cookieName);
        // check cookie to ensure path matches and cookie is not expired
        // if all is cool, add cookie to header string
        if (comparePaths((String) cookie.get(PATH), path) && isNotExpired((String) cookie.get(EXPIRES))) {
            cookieStringBuffer.append(cookieName);
            cookieStringBuffer.append("=");
            cookieStringBuffer.append((String) cookie.get(cookieName));
            if (cookieNames.hasNext())
                cookieStringBuffer.append(SET_COOKIE_SEPARATOR);
        }
    }
    try {
        conn.setRequestProperty(COOKIE, cookieStringBuffer.toString());
    } catch (java.lang.IllegalStateException ise) {
        IOException ioe = new IOException("Illegal State! Cookies cannot be set on a URLConnection that is already connected. " + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect().");
        throw ioe;
    }
}
 
Example 6
Source Project: android-discourse   File: CookieManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prior to opening a URLConnection, calling this method will set all unexpired cookies that match the path or subpaths for thi
 * underlying URL The connection MUST NOT have been opened method or an IOException will be thrown.
 *
 * @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown
 * @throws java.io.IOException Thrown if conn has already been opened.
 */
public void setCookies(URLConnection conn) throws IOException {

    // let's determine the domain and path to retrieve the appropriate cookies
    URL url = conn.getURL();
    String domain = getDomainFromHost(url.getHost());
    String path = url.getPath();

    Map domainStore = (Map) store.get(domain);
    if (domainStore == null)
        return;
    StringBuffer cookieStringBuffer = new StringBuffer();

    Iterator cookieNames = domainStore.keySet().iterator();
    while (cookieNames.hasNext()) {
        String cookieName = (String) cookieNames.next();
        Map cookie = (Map) domainStore.get(cookieName);
        // check cookie to ensure path matches and cookie is not expired
        // if all is cool, add cookie to header string
        if (comparePaths((String) cookie.get(PATH), path) && isNotExpired((String) cookie.get(EXPIRES))) {
            cookieStringBuffer.append(cookieName);
            cookieStringBuffer.append("=");
            cookieStringBuffer.append((String) cookie.get(cookieName));
            if (cookieNames.hasNext())
                cookieStringBuffer.append(SET_COOKIE_SEPARATOR);
        }
    }
    try {
        conn.setRequestProperty(COOKIE, cookieStringBuffer.toString());
    } catch (java.lang.IllegalStateException ise) {
        IOException ioe = new IOException("Illegal State! Cookies cannot be set on a URLConnection that is already connected. " + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect().");
        throw ioe;
    }
}
 
Example 7
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 8
@Override
public long getLastModified() throws IOException {
    URLConnection con = getResourceURLConnection(name, classLoader);
    if (con == null) {
        return -1;
    }
    try {
        long lastModified = con.getLastModified();
        resolvedURL = con.getURL();
        LOGGER.debug("{} last modified: {}", name, lastModified);
        return lastModified;
    } finally {
        closeURLConnection(con);
    }
}
 
Example 9
public URL getURL(String name, ClassLoader classLoader) throws IOException {
    URLConnection con = getResourceURLConnection(name, classLoader);
    if (con == null) {
        return null;
    }
    try {
        return con.getURL();
    } finally {
        closeURLConnection(con);
    }
}
 
Example 10
Source Project: hottub   File: plain.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 11
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 12
Source Project: openjdk-8   File: plain.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 13
Source Project: jdk8u_jdk   File: plain.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns one of several object types (this set may change in future
 * versions):
 * 1) instance of Thread:
 *    Invoke the thread to launch an external viewer.
 * 2) instance of InputStream:
 *    Bring up the "Save to disk" dialog page to allow the content
 *    to be saved to disk.
 * 3) instance of InputStreamImageSource:
 *    Load the image into HotJava in an image viewer page.
 * 4) instance of String:
 *    Go to a new page with the string as the plain text content
 *    of that page.
 */
public Object getContent(URLConnection uc) {
    try {
        InputStream is = uc.getInputStream();
        StringBuffer sb = new StringBuffer();
        int c;

        sb.append("[Content of " + uc.getURL() + "]\n\n");
        sb.append("[This opening message brought to you by your plain/text\n");
        sb.append("content handler. To remove this content handler, delete the\n");
        sb.append("COM.foo.content.text directory from your class path and\n");
        sb.append("the java.content.handler.pkgs property from your HotJava\n");
        sb.append("properties file.]\n");
        sb.append("----------------------------------------------------------------\n\n");

        // Read the characters from the source, accumulate them into the string buffer.
        // (Not the most efficient, but simplest for this example.)
        while ((c = is.read()) >= 0) {
            sb.append((char)c);
        }

        // Tidy up
        is.close();

        // Return the resulting string to our client (we're case 4 above)
        return sb.toString();
    } catch (IOException e) {
        // For any exception, just return an indication of what went wrong.
        return "Problem reading document: " + uc.getURL();
    }
}
 
Example 14
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 15
Source Project: Onosendai   File: HttpHelper.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public R handleStream (final URLConnection connection, final InputStream is, final int contentLength) throws IOException {
	this.url = connection.getURL();
	this.contentLength = contentLength;
	return this.delagate.handleStream(connection, is, contentLength);
}
 
Example 16
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 17
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 18
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 19
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 20
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}