Example usage for java.net URI toURL

List of usage examples for java.net URI toURL

Introduction

In this page you can find the example usage for java.net URI toURL.

Prototype

public URL toURL() throws MalformedURLException 

Source Link

Document

Constructs a URL from this URI.

Usage

From source file:eu.eubrazilcc.lvl.core.util.UrlUtils.java

/**
 * Returns a list of name-value pairs as built from the URI's query portion.
 * @param uri - input URI/*w  w w.  j a v  a  2s  . co m*/
 * @return a list of name-value pairs as built from the URI's query portion.
 * @throws IllegalArgumentException If a malformed URI occurs.
 */
public static Map<String, String> getQueryParams(final URI uri) {
    checkArgument(uri != null, "Uninitialized URI");
    try {
        final URI nUri = uri.normalize();
        final URL url = nUri.isAbsolute() ? nUri.toURL()
                : new URL(new URL("http://example.org/"), nUri.toString());
        final String query = new URL(URLDecoder.decode(url.toString(), defaultCharset().name())).getQuery();
        final Map<String, String> map = newHashMap();
        final List<NameValuePair> pairs = URLEncodedUtils.parse(query, defaultCharset());
        final Iterator<NameValuePair> iterator = pairs.iterator();
        while (iterator.hasNext()) {
            final NameValuePair pair = iterator.next();
            map.put(pair.getName(), pair.getValue());
        }
        return map;
    } catch (MalformedURLException | UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Malformed URI", e);
    }
}

From source file:com.nadmm.airports.utils.NetworkUtils.java

public static boolean doHttpGet(Context context, String host, int port, String path, String query, File file,
        ResultReceiver receiver, Bundle result, Class<? extends FilterInputStream> filter) throws Exception {
    URI uri = new URI("http", null, host, port, path, query, null);
    return doHttpGet(context, uri.toURL(), file, receiver, result, filter);
}

From source file:org.davidmason.zayf.rest.ServerProxyImpl.java

public static URI getRestURI(URI baseURI) {
    try {//from www  . ja  v  a 2 s .  co m
        return new URL(baseURI.toURL(), RESOURCE_PREFIX).toURI();
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java

public static String doGet(String urlStr) {
    URLConnection connection = null;
    try {/*ww  w.j  a va  2s. co  m*/
        URL url = new URL(urlStr);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        url = uri.toURL();

        // Open the connection
        connection = url.openConnection();
    } catch (Exception e) {
    }

    try {
        InputStream in = connection.getInputStream();

        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "utf-8");
        String theString = writer.toString();
        return theString;
    } catch (Exception e) {
    }
    return "";
}

From source file:com.hemou.android.util.StrUtils.java

public static String toUri(String url, Object... urlVariables) {
    try {//from   ww  w .  j a v  a 2s .  c o m
        UriTemplate uriTemplate = new UriTemplate(url);
        URI expanded = uriTemplate.expand(urlVariables);
        return expanded.toURL().toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return url;
}

From source file:org.apache.ode.axis2.util.AxisUtils.java

public static void applySecurityPolicy(AxisService service, String policy_file)
        throws IllegalArgumentException {
    URI policyUri = new File(policy_file).toURI();
    if (log.isDebugEnabled())
        log.debug("Applying security policy: " + policyUri);
    try {//from  w w  w  .j a  v a  2 s .  c o m
        InputStream policyStream = policyUri.toURL().openStream();
        try {
            Policy policyDoc = PolicyEngine.getPolicy(policyStream);
            // Neethi parser is really dumb.
            // In case of parsing error, the exception is printed out and swallowed. Null is returned.
            if (policyDoc == null) {
                String msg = "Failed to parse policy: " + policy_file
                        + ". Due to Neethi limitations the reason can't be provided. See stacktraces in standard output (not logs)";
                log.error(msg);
                throw new IllegalArgumentException(msg);
            }
            service.getPolicyInclude().addPolicyElement(PolicyInclude.AXIS_SERVICE_POLICY, policyDoc);
            // make sure the proper modules are engaged, if they are available
            engageModules(service, "rampart", "rahas");
        } finally {
            policyStream.close();
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Exception while parsing policy: " + policyUri, e);
    }
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtils.java

/**
 * Returns a local {@link File} for the specified {@link LocalizeResource}. If the specified {@link LocalizeResource}
 * points to a local file already, it simply returns a {@link File} object for it. If it is not local, then the
 * method will try to download the resource to a local temporary file and return it.
 *
 * @param resource the {@link LocalizeResource} for which a local file is requested
 * @param tempDir the {@link File directory} to download the file to if it is a remote file
 * @return a local {@link File} for the specified resource
 *///  w w  w.  j ava 2s. c o  m
private static File getFileToLocalize(LocalizeResource resource, File tempDir) throws IOException {
    URI uri = resource.getURI();
    if ("file".equals(uri.getScheme())) {
        // Local file. Just return a File object for the file.
        return new File(uri.getPath());
    }
    URL url = uri.toURL();
    String name = new File(uri.getPath()).getName();
    File tempFile = new File(tempDir, name);
    Files.copy(Resources.newInputStreamSupplier(url), tempFile);
    return tempFile;
}

From source file:ArchiveUtil.java

/**
 * Reads the contents of the given file into a byte array.
 *//*from  ww w  .j  a v  a 2  s.  c o  m*/
public static byte[] readBytes(URI file) throws IOException {
    return readBytes(file.toURL());
}

From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java

public static String doPost(String serviceUrl, String queryString) {
    URLConnection connection = null;
    try {//from w w  w . ja  v a  2s . co  m

        URL url = new URL(serviceUrl);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        url = uri.toURL();
        // Open the connection
        connection = url.openConnection();
        connection.setDoInput(true);
        connection.setUseCaches(false); // Disable caching the document
        connection.setDoOutput(true); // Triggers POST.
        connection.setRequestProperty("Content-Type", "text/html");

        OutputStreamWriter writer = null;

        log.info("About to write");
        try {
            if (null != connection.getOutputStream()) {
                writer = new OutputStreamWriter(connection.getOutputStream());
                writer.write(queryString); // Write POST query

            } else {
                log.warn("connection Null");
            }
            // string.
        } catch (ConnectException ex) {
            log.warn("Exception : " + ex);
            // ex.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception lg) {
                    log.warn("Exception lg: " + lg.toString());
                    //lg.printStackTrace();
                }
            }
        }

        InputStream in = connection.getInputStream();

        //            StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "utf-8");
        String theString = writer.toString();
        return theString;
    } catch (Exception e) {
        //e.printStackTrace();
        log.warn("Error URL " + e.toString());
        return "";
    }
}

From source file:org.entrystore.rowstore.RowStoreApplication.java

public static String getVersion() {
    if (VERSION == null) {
        URI versionFile = getConfigurationURI("VERSION.txt");
        try {//  ww w  .j  ava  2s. co m
            log.debug("Reading version number from " + versionFile);
            VERSION = readFirstLine(versionFile.toURL());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        if (VERSION == null) {
            VERSION = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
    }
    return VERSION;
}