Example usage for java.net URL getPath

List of usage examples for java.net URL getPath

Introduction

In this page you can find the example usage for java.net URL getPath.

Prototype

public String getPath() 

Source Link

Document

Gets the path part of this URL .

Usage

From source file:com.magnet.plugin.helpers.UrlParser.java

public static ParsedUrl parseUrl(String url) {
    List<PathPart> pathParts = new ArrayList<PathPart>();
    List<Query> queries = new ArrayList<Query>();
    ParsedUrl parsedUrl;//  ww w.  ja  v a 2 s. com
    String base;

    try {
        URL aURL = new URL(url);
        base = aURL.getAuthority();
        String protocol = aURL.getProtocol();
        parsedUrl = new ParsedUrl();
        parsedUrl.setPathWithEndingSlash(aURL.getPath().endsWith("/"));
        parsedUrl.setBase(protocol + "://" + base);
        List<NameValuePair> pairs = URLEncodedUtils.parse(aURL.getQuery(), Charset.defaultCharset());
        for (NameValuePair pair : pairs) {
            Query query = new Query(pair.getName(), pair.getValue());
            queries.add(query);
        }
        parsedUrl.setQueries(queries);

        String[] pathStrings = aURL.getPath().split("/");
        for (String pathPart : pathStrings) {
            Matcher m = PATH_PARAM_PATTERN.matcher(pathPart);
            if (m.find()) {
                String paramDef = m.group(1);
                String[] paramParts = paramDef.split(":");
                if (paramParts.length > 1) {
                    pathParts.add(new PathPart(paramParts[1].trim(), paramParts[0].trim()));
                } else {
                    pathParts.add(new PathPart(paramParts[0].trim()));
                }
            } else {
                if (!pathPart.isEmpty()) {
                    pathParts.add(new PathPart(pathPart));
                }
            }
        }
        parsedUrl.setPathParts(pathParts);
    } catch (Exception ex) {
        Logger.error(UrlParser.class, "Can't parse URL " + url);
        return null;
    }
    return parsedUrl;
}

From source file:io.lightlink.utils.ClasspathScanUtils.java

private static void findFromDirectory(URL packageURL, List<String> names) throws UnsupportedEncodingException {
    if (packageURL == null)
        return;/*from   ww  w  .  j a v  a2 s  .  c  o  m*/
    File folder = new File(URLDecoder.decode(packageURL.getPath(), "UTF-8"));

    Collection<File> files = FileUtils.listFiles(folder, null, true);
    int length = folder.getAbsolutePath().length();

    String entryName;
    if (files != null)
        for (File actual : files) {
            entryName = actual.getAbsolutePath().substring(length);
            names.add(entryName);
        }
}

From source file:com.magnet.plugin.r2m.helpers.UrlParser.java

public static ParsedUrl parseUrl(String url) {
    List<PathPart> pathParts = new ArrayList<PathPart>();
    List<Query> queries = new ArrayList<Query>();
    ParsedUrl parsedUrl;//from  w w  w.ja v  a2s  . c  o  m
    String base;

    try {
        URL aURL = new URL(url);
        base = aURL.getAuthority();
        String protocol = aURL.getProtocol();
        parsedUrl = new ParsedUrl();
        parsedUrl.setPathWithEndingSlash(aURL.getPath().endsWith("/"));
        parsedUrl.setBaseUrl(protocol + "://" + base);
        List<NameValuePair> pairs = URLEncodedUtils.parse(aURL.getQuery(), Charset.defaultCharset());
        for (NameValuePair pair : pairs) {
            Query query = new Query(pair.getName(), pair.getValue());
            queries.add(query);
        }
        parsedUrl.setQueries(queries);

        String[] pathStrings = aURL.getPath().split("/");
        for (String pathPart : pathStrings) {
            Matcher m = PATH_PARAM_PATTERN.matcher(pathPart);
            if (m.find()) {
                String paramDef = m.group(1);
                String[] paramParts = paramDef.split(":");
                if (paramParts.length > 1) {
                    pathParts.add(new PathPart(paramParts[1].trim(), paramParts[0].trim()));
                } else {
                    pathParts.add(new PathPart(paramParts[0].trim()));
                }
            } else {
                if (!pathPart.isEmpty()) {
                    pathParts.add(new PathPart(pathPart));
                }
            }
        }
        parsedUrl.setPathParts(pathParts);
    } catch (Exception ex) {
        Logger.error(UrlParser.class, R2MMessages.getMessage("CANNOT_PARSE_URL", url));
        return null;
    }
    return parsedUrl;
}

From source file:fr.ybonnel.simpleweb4j.MultipartIntegrationTest.java

private static String getProjectPath() {
    URL resource = MultipartIntegrationTest.class.getClassLoader().getResource("");
    assert resource != null;
    String path = resource.getPath();
    return path.substring(0, path.indexOf("target"));
}

From source file:com.asual.summer.core.util.ResourceUtils.java

/**
  * Tries to convert the specified URL to a file object. If this fails,
  * <b>null</b> is returned.
  */* w w w  . j  a  va 2  s.c  o  m*/
  * @param url the URL
  * @return the resulting file object
  */
@SuppressWarnings("deprecation")
public static File fileFromURL(URL url) {
    if (PROTOCOL_FILE.equals(url.getProtocol())) {
        return new File(URLDecoder.decode(url.getPath()));
    } else {
        return null;
    }
}

From source file:com.singular.utils.FileUtils.java

/**
 * Taken from apache hadoop project.//from w ww .  ja v a 2s  . co  m
 * Apache 2.0 license.
 *
 * @param className
 * @return
 */
public static String findContainingJar(Class className) {
    ClassLoader loader = className.getClassLoader();
    String classFile = className.getName().replaceAll("\\.", "/") + ".class";

    try {
        for (Enumeration itr = loader.getResources(classFile); itr.hasMoreElements();) {
            URL url = (URL) itr.nextElement();

            if ("jar".equals(url.getProtocol())) {
                String toReturn = url.getPath();
                if (toReturn.startsWith("file:")) {
                    toReturn = toReturn.substring("file:".length());
                }
                toReturn = toReturn.replaceAll("\\+", "%2B");
                toReturn = URLDecoder.decode(toReturn, "UTF-8");
                return toReturn.replaceAll("!.*$", "");
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return null;
}

From source file:com.netflix.nicobar.core.utils.ClassPathUtils.java

/**
 * Find the jar containing the given resource.
 *
 * @param jarUrl URL that came from a jar that needs to be parsed
 * @return {@link Path} to the Jar containing the resource.
 */// www  .  j  a va  2s  .c om
public static Path getJarPathFromUrl(URL jarUrl) {
    try {
        String pathString = jarUrl.getPath();
        // for Jar URL, the path is in the form of: file:/path/to/groovy/myJar.jar!/path/to/resource/myResource.txt
        int endIndex = pathString.lastIndexOf("!");
        return Paths.get(new URI(pathString.substring(0, endIndex)));
    } catch (URISyntaxException e) {
        throw new IllegalStateException("Unsupported URL syntax: " + jarUrl.getPath(), e);
    }
}

From source file:com.evanzeimet.queryinfo.QueryInfoTestUtils.java

public static String getFormattedJson(Class<?> clazz, String filename) throws IOException, QueryInfoException {
    URL url = clazz.getResource(filename);

    if (url == null) {
        String message = String.format("Resource not found at [%s]", filename);
        throw new QueryInfoException(message);
    }/* ww w.jav a2 s .  com*/

    File file = new File(url.getPath());
    String rawContents = FileUtils.readFileToString(file);

    return dosToUnix(rawContents);
}

From source file:ml.shifu.guagua.hadoop.util.HDPUtils.java

/**
 * Find a real file that contains file name in class path.
 * /*from w  ww  . ja v a  2 s  . c om*/
 * @param file
 *            name
 * @return real file name
 */
public static String findContainingFile(String fileName) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
        Enumeration<URL> itr = null;
        // Try to find the class in registered jars
        if (loader instanceof URLClassLoader) {
            itr = ((URLClassLoader) loader).findResources(fileName);
        }
        // Try system classloader if not URLClassLoader or no resources found in URLClassLoader
        if (itr == null || !itr.hasMoreElements()) {
            itr = loader.getResources(fileName);
        }
        for (; itr.hasMoreElements();) {
            URL url = (URL) itr.nextElement();
            if ("file".equals(url.getProtocol())) {
                String toReturn = url.getPath();
                if (toReturn.startsWith("file:")) {
                    toReturn = toReturn.substring("file:".length());
                }
                // URLDecoder is a misnamed class, since it actually decodes
                // x-www-form-urlencoded MIME type rather than actual
                // URL encoding (which the file path has). Therefore it would
                // decode +s to ' 's which is incorrect (spaces are actually
                // either unencoded or encoded as "%20"). Replace +s first, so
                // that they are kept sacred during the decoding process.
                toReturn = toReturn.replaceAll("\\+", "%2B");
                toReturn = URLDecoder.decode(toReturn, "UTF-8");
                return toReturn.replaceAll("!.*$", "");
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return null;
}

From source file:ie.wombat.rt.fireeagle.CookieConsumer.java

/** Reconstruct the requested URL path, complete with query string (if any). */
private static String getRequestPath(HttpServletRequest request) throws MalformedURLException {

    URL url = new URL(OAuthServlet.getRequestURL(request));
    StringBuilder path = new StringBuilder(url.getPath());
    String queryString = url.getQuery();
    if (queryString != null) {
        path.append("?").append(queryString);
    }//from   www .ja v a 2 s  .  c  o  m
    return path.toString();
}