Example usage for java.net MalformedURLException initCause

List of usage examples for java.net MalformedURLException initCause

Introduction

In this page you can find the example usage for java.net MalformedURLException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:org.eclipse.skalli.commons.URLUtils.java

/**
 * Converts a given string into a corresponding URL.
 * <p>/*from w  ww . j  av  a 2  s  . c o m*/
 * Encodes path and/or query parts of the given string according to
 * {@link URI#URI(String, String, String, int, String, String, String)}.
 * For example, blanks in the path are converted to <tt>%20</tt>.
 *
 * @param s  the string to convert to an URL.
 * @return  an URL, or <code>null</code> if the string is <code>null</code>, empty or whitespace.
 *
 * @throws MalformedURLException  if the given string is not a valid URL and cannot be
 * "sanitized" to yield a valid URL even after proper encoding of its parts.
 */
public static URL stringToURL(String s) throws MalformedURLException {
    if (StringUtils.isBlank(s)) {
        return null;
    }
    URI uri = null;
    try {
        uri = new URI(s);
    } catch (URISyntaxException e) {
        URL url = new URL(s);
        try {
            uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                    url.getQuery(), url.getRef());
        } catch (URISyntaxException e1) {
            MalformedURLException e2 = new MalformedURLException(e1.getMessage());
            e2.initCause(e1);
            throw e2;
        }
    }
    return new URL(uri.toASCIIString());
}

From source file:android.databinding.tool.store.LayoutFileParser.java

public static File urlToFile(URL url) throws MalformedURLException {
    try {/*ww  w.ja va2s.  c o m*/
        return new File(url.toURI());
    } catch (IllegalArgumentException e) {
        MalformedURLException ex = new MalformedURLException(e.getLocalizedMessage());
        ex.initCause(e);
        throw ex;
    } catch (URISyntaxException e) {
        return new File(url.getPath());
    }
}

From source file:org.lockss.util.UrlUtil.java

private static MalformedURLException newMalformedURLException(Throwable cause) {

    MalformedURLException ex = new MalformedURLException();
    ex.initCause(cause);
    return ex;//from   w  w  w.  ja  va2 s. c  o m
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.resources.ResourceUtils.java

/**
 * Resolve a location (which can be many things) to an URL. If the location starts with
 * {@code classpath:} the location is interpreted as a classpath location. Otherwise it is tried
 * as a URL, file and at last UIMA resource. If the location is treated as a classpath or file
 * location, an URL is only returned if the target exists. If it is an URL, it is possible that
 * the target may not actually exist./* w w  w.java2s  .c om*/
 *
 * @param aLocation
 *            a location (classpath, URL, file or UIMA resource location).
 * @param aClassLoader
 *            the class loader to be used for classpath URLs.
 * @param aContext
 *            a UIMA context.
 * @return the resolved URL.
 * @throws IOException
 *             if the target could not be found.
 */
public static URL resolveLocation(String aLocation, ClassLoader aClassLoader, UimaContext aContext)
        throws IOException {
    // if we have a caller, we use it's classloader
    ClassLoader classLoader = aClassLoader;
    if (classLoader == null) {
        classLoader = ResourceUtils.class.getClassLoader();
    }

    // If a location starts with "classpath:"
    String prefixClasspath = "classpath:";
    if (aLocation.startsWith(prefixClasspath)) {
        String cpLocation = aLocation.substring(prefixClasspath.length());
        if (cpLocation.startsWith("/")) {
            cpLocation = cpLocation.substring(1);
        }
        URL url = classLoader.getResource(cpLocation);

        if (url == null) {
            throw new FileNotFoundException("No file found at [" + aLocation + "]");
        }
        return url;
    }

    // If it is a true well-formed URL, we assume that it is just that.
    try {
        return new URL(aLocation);
    } catch (MalformedURLException e) {
        // Ok - was not an URL.
    }

    // Otherwise we try if it is a file.
    File file = new File(aLocation);
    if (file.exists()) {
        return file.toURI().toURL();
    }

    // Otherwise we look into the context (if there was one)
    if (aContext != null) {
        Exception ex = null;
        URL url = null;
        try {
            url = aContext.getResourceURL(aLocation);
        } catch (ResourceAccessException e) {
            ex = e;
        }
        if (url == null) {
            FileNotFoundException e = new FileNotFoundException("No file found at [" + aLocation + "]");
            if (ex != null) {
                e.initCause(ex);
            }
            throw e;
        }
        return url;
    }

    // Otherwise bail out
    throw new FileNotFoundException("No file found at [" + aLocation + "]");
}

From source file:org.lockss.plugin.silverchair.PostHttpClientUrlConnection.java

java.net.MalformedURLException newMalformedURLException(String msg, Throwable cause) {
    java.net.MalformedURLException e = new java.net.MalformedURLException(msg);
    e.initCause(cause);
    return e;// ww  w. j  a v a  2s. c  o  m
}

From source file:edu.mit.mobile.android.locast.net.NetworkClient.java

private void setBaseUrl(String baseUrlString) throws MalformedURLException {
    final URL baseUrl = new URL(baseUrlString);
    try {//from  w  w  w . j av  a  2  s .  c  om
        mBaseUrl = baseUrl.toURI();

        mAuthScope = new AuthScope(mBaseUrl.getHost(), mBaseUrl.getPort());

    } catch (final URISyntaxException e) {
        final MalformedURLException me = new MalformedURLException(e.getLocalizedMessage());
        me.initCause(e);
        throw me;
    }
}