Example usage for java.net URLConnection getURL

List of usage examples for java.net URLConnection getURL

Introduction

In this page you can find the example usage for java.net URLConnection getURL.

Prototype

public URL getURL() 

Source Link

Document

Returns the value of this URLConnection 's URL field.

Usage

From source file:eionet.cr.util.URLUtil.java

/**
 *
 * @param urlConnection//from  www. ja va2  s .  c o m
 */
public static void disconnect(URLConnection urlConnection) {

    if (urlConnection != null && urlConnection instanceof HttpURLConnection) {
        try {
            ((HttpURLConnection) urlConnection).disconnect();
        } catch (Exception e) {
            LOGGER.warn("Error when disconnection from " + urlConnection.getURL() + ": " + e.toString());
        }
    }
}

From source file:org.springframework.ide.eclipse.boot.wizard.github.auth.BasicAuthCredentials.java

@Override
public void apply(URLConnection conn) {
    try {/*from ww  w.  ja v a  2 s .  co m*/
        if (matchHost(conn.getURL().getHost())) {
            conn.setRequestProperty("Authorization", computeAuthString());
        }
    } catch (UnsupportedEncodingException e) {
        //Shouldn't really be possible...
        BootWizardActivator.log(e);
    }
}

From source file:org.springframework.ide.eclipse.wizard.gettingstarted.github.auth.BasicAuthCredentials.java

@Override
public void apply(URLConnection conn) {
    try {//  w  w  w .  j ava 2 s.co  m
        if (matchHost(conn.getURL().getHost())) {
            conn.setRequestProperty("Authorization", computeAuthString());
        }
    } catch (UnsupportedEncodingException e) {
        //Shouldn't really be possible...
        WizardPlugin.log(e);
    }
}

From source file:com.occamlab.te.parsers.ImageParser.java

public static Document parse(URLConnection uc, Element instruction, PrintWriter logger) {
    Document doc = null;/*from   w w w  .  j  a  v  a2 s.  com*/
    InputStream is = null;
    try {
        is = uc.getInputStream();
        doc = parse(is, instruction, logger);
    } catch (Exception e) {
        String msg = String.format("Failed to parse %s resource from %s \n %s", uc.getContentType(),
                uc.getURL(), e.getMessage());
        jlogger.warning(msg);
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return doc;
}

From source file:org.orbeon.oxf.util.NetUtils.java

/**
 * Get the last modification date of an open URLConnection.
 *
 * This handles the (broken at some point in the Java libraries) case of the file: protocol.
 *
 * @return last modified timestamp "as is"
 *///from w w w  .  j  a v a 2s.  c  o  m
public static long getLastModified(URLConnection urlConnection) {
    try {
        long lastModified = urlConnection.getLastModified();
        if (lastModified == 0 && "file".equals(urlConnection.getURL().getProtocol()))
            lastModified = new File(
                    URLDecoder.decode(urlConnection.getURL().getFile(), STANDARD_PARAMETER_ENCODING))
                            .lastModified();
        return lastModified;
    } catch (UnsupportedEncodingException e) {
        // Should not happen as we are using a required encoding
        throw new OXFException(e);
    }
}

From source file:org.springframework.ide.eclipse.gettingstarted.github.auth.BasicAuthCredentials.java

@Override
public void apply(URLConnection conn) {
    try {//ww  w .j  ava2s .  c  o m
        if (matchHost(conn.getURL().getHost())) {
            conn.setRequestProperty("Authorization", computeAuthString());
        }
    } catch (UnsupportedEncodingException e) {
        //Shouldn't really be possible...
        GettingStartedActivator.log(e);
    }
}

From source file:org.apache.pulsar.client.impl.auth.AuthenticationAthenz.java

private PrivateKey loadPrivateKey(String privateKeyURL) {
    PrivateKey privateKey = null;
    try {//w  ww .  j  a  v a  2  s.c o  m
        URLConnection urlConnection = new URL(privateKeyURL).openConnection();
        String protocol = urlConnection.getURL().getProtocol();
        if ("data".equals(protocol) && !APPLICATION_X_PEM_FILE.equals(urlConnection.getContentType())) {
            throw new IllegalArgumentException(
                    "Unsupported media type or encoding format: " + urlConnection.getContentType());
        }
        String keyData = CharStreams.toString(new InputStreamReader((InputStream) urlConnection.getContent()));
        privateKey = Crypto.loadPrivateKey(keyData);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Invalid privateKey format", e);
    } catch (CryptoException | InstantiationException | IllegalAccessException | IOException e) {
        privateKey = null;
    }
    return privateKey;
}

From source file:com.romeikat.datamessie.core.base.service.download.AbstractDownloader.java

protected String getResponseUrl(final URLConnection urlConnection) throws IOException {
    if (!(urlConnection instanceof HttpURLConnection)) {
        return urlConnection.getURL().toExternalForm();
    }// ww  w  .  ja v  a  2 s  .c om
    final HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
    httpUrlConnection.setInstanceFollowRedirects(false);
    final String responseUrl = httpUrlConnection.getHeaderField("Location");
    return responseUrl;
}

From source file:com.dawsonloudon.videoplayer.VideoPlayer.java

private void playVideo(String url) throws IOException {
    if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/")
            || url.contains("youtu.be/")) {
        //support for google / bitly / tinyurl / youtube shortens
        URLConnection con = new URL(url).openConnection();
        con.connect();/*from   w w  w .ja va 2s.  c o m*/
        InputStream is = con.getInputStream();
        //new redirected url
        url = con.getURL().toString();
        is.close();
    }

    // Create URI
    Uri uri = Uri.parse(url);

    Intent intent = null;
    // Check to see if someone is trying to play a YouTube page.
    if (url.contains(YOU_TUBE)) {
        // If we don't do it this way you don't have the option for youtube
        uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
        if (isYouTubeInstalled()) {
            intent = new Intent(Intent.ACTION_VIEW, uri);
        } else {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id=com.google.android.youtube"));
        }
    } else if (url.contains(ASSETS)) {
        // get file path in assets folder
        String filepath = url.replace(ASSETS, "");
        // get actual filename from path as command to write to internal storage doesn't like folders
        String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length());

        // Don't copy the file if it already exists
        File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename);
        if (!fp.exists()) {
            this.copy(filepath, filename);
        }

        // change uri to be to the new file in internal storage
        uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename);

        // Display video player
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    } else {
        // Display video player
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    }

    this.cordova.getActivity().startActivity(intent);
}

From source file:com.phonegap.plugins.videoplayer.VideoPlayer.java

private void playVideo(String url) throws IOException {

    if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/")
            || url.contains("youtu.be/")) {
        //support for google / bitly / tinyurl / youtube shortens
        URLConnection con = new URL(url).openConnection();
        con.connect();//from  w ww  .j a va  2 s.  c om
        InputStream is = con.getInputStream();
        //new redirected url
        url = con.getURL().toString();
        is.close();
    }

    // Create URI
    Uri uri = Uri.parse(url);

    Intent intent = null;
    // Check to see if someone is trying to play a YouTube page.
    if (url.contains(YOU_TUBE)) {
        // If we don't do it this way you don't have the option for youtube
        uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
        if (isYouTubeInstalled()) {
            intent = new Intent(Intent.ACTION_VIEW, uri);
        } else {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id=com.google.android.youtube"));
        }
    } else if (url.contains(ASSETS)) {
        // get file path in assets folder
        String filepath = url.replace(ASSETS, "");
        // get actual filename from path as command to write to internal storage doesn't like folders
        String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length());

        // Don't copy the file if it already exists
        File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename);
        if (!fp.exists()) {
            this.copy(filepath, filename);
        }

        // change uri to be to the new file in internal storage
        uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename);

        // Display video player
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    } else {
        // Display video player
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    }

    this.cordova.getActivity().startActivity(intent);
}