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

The following examples show how to use java.net.URLConnection#getURL() . 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 check out the related API usage on the sidebar.
Example 1
Source File: MCRXMLResource.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@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 2
Source File: plain.java    From jdk8u_jdk with 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 3
Source File: plain.java    From openjdk-8 with 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 4
Source File: plain.java    From openjdk-8-source with 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 File: plain.java    From hottub with 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 6
Source File: MCRXMLResource.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
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 7
Source File: plain.java    From jdk8u-dev-jdk with 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 8
Source File: CookieManager.java    From android-discourse with 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 9
Source File: CookieManager.java    From materialup with 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 10
Source File: plain.java    From jdk8u60 with 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
Source File: plain.java    From TencentKona-8 with 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 12
Source File: URLClassPath.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 13
Source File: plain.java    From dragonwell8_jdk with 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
Source File: URLImageSource.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 15
Source File: URLImageSource.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 16
Source File: URLImageSource.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 17
Source File: HttpHelper.java    From Onosendai with 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 18
Source File: URLImageSource.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 19
Source File: URLImageSource.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}
 
Example 20
Source File: URLImageSource.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public URLImageSource(URLConnection uc) {
    this(uc.getURL(), uc);
}