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.aurel.track.util.PluginUtils.java

/**
 * Transforms the given URL which may contain escaping characters (like %20 for space)
 * to an URL which may conatin illegal URL characters (like space) but is valid for creating a file based on the resulting URL
 * The escape characters will be changed back to characters that are illegal in URLs (like space) because the file could be
 * constructed just in such a way in some servlet containers like Tomcat5.5
 * @param url/*  w ww.j  a  v  a  2s . c o m*/
 * @return
 */
public static URL createValidFileURL(URL url) {
    File file;
    URL fileUrl;
    if (url == null) {
        return null;
    }
    if (url.getPath() != null) {
        file = new File(url.getPath());
        //we have no escape characters or the servlet container can deal with escape characters
        if (file.exists()) {
            //valid url
            return url;
        }
    }
    //valid file through URI?
    //see Bug ID:  4466485 (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4466485)
    URI uri = null;
    try {
        //get rid of spaces
        uri = new URI(url.toString());
    } catch (URISyntaxException e1) {
    }
    if (uri == null) {
        return null;
    }
    if (uri.getPath() != null) {
        //the decoded path component of this URI
        file = new File(uri.getPath());
        if (file.exists()) {
            try {
                //file.toURL() does not automatically escape characters that are illegal in URLs
                fileUrl = file.toURL(); // revert, problems with Windows?
                if (fileUrl != null) {
                    return fileUrl;
                }
            } catch (MalformedURLException e) {
            }
        }
    }
    //back to URL from URI
    try {
        url = uri.toURL();
    } catch (MalformedURLException e) {
    }
    if (url != null && url.getPath() != null) {
        file = new File(url.getPath());
        if (file.exists()) {
            //valid url
            return url;
        }
    }
    return null;
}

From source file:com.esri.gpt.framework.http.crawl.HttpCrawlRequest.java

private String getRelativePath() throws MalformedURLException {
    URL u = new URL(getUrl());
    return String.format("%s%s%s", u.getPath() != null ? u.getPath() : "/",
            u.getQuery() != null ? "?" + u.getQuery() : "", u.getRef() != null ? "#" + u.getRef() : "");
}

From source file:com.textuality.keybase.lib.prover.HackerNews.java

@Override
public String getPresenceLabel() throws KeybaseException {
    String answer = mProof.getServiceUrl();
    try {//from w ww  .j a  va2 s.  com
        URL u = new URL(answer);
        answer = u.getHost() + u.getPath() + '?' + u.getQuery();
    } catch (MalformedURLException e) {
        answer = super.getPresenceLabel();
    }
    return answer;
}

From source file:it.uniud.ailab.dcore.wrappers.external.OpenNlpBootstrapperAnnotator.java

/**
 * Checks if the database entry for the POStagger are local or web resources
 * and downloads the online ones.// w  w w. ja va 2s .  c o m
 *
 */
private static void prepareModels() {

    Map<String, String> correctPaths = new HashMap<>();

    for (Map.Entry e : databasePaths.entrySet()) {

        String entryKey = (String) e.getKey();
        String entryValue = (String) e.getValue();

        try {

            URL url = new URL((String) e.getValue());
            // if we're dealing with a local file, then
            // we don't care and continue.
            if (isLocalFile(url)) {
                Logger.getLogger(OpenNlpBootstrapperAnnotator.class.getName()).log(Level.INFO,
                        "Using {0} as local path...", e.getValue());
                continue;
            }

            // Download the new file and put it in a local folder
            String newFileName = FileSystem.getDistillerTmpPath().concat(FileSystem.getSeparator())
                    .concat("OpenNLPmodels").concat(FileSystem.getSeparator()).concat(url.getPath()
                            .substring(url.getPath().lastIndexOf("/") + 1, url.getPath().length()));
            ;

            // Check if the file already exists (i.e. we have probably
            // downloaded it before). If exists, then we're happy and 
            // don't download anything
            File f = new File(newFileName);
            if (f.exists()) {
                //LOG.log(Level.INFO, "Using {0} as local cache...", newFileName);
                correctPaths.put(entryKey, f.getCanonicalPath());
                continue;
            }

            Logger.getLogger(OpenNlpBootstrapperAnnotator.class.getName()).log(Level.INFO,
                    "Downloading model from {0}...", e.getValue());
            FileUtils.copyURLToFile(url, f);
            Logger.getLogger(OpenNlpBootstrapperAnnotator.class.getName()).log(Level.INFO,
                    "OpenNLP database saved in {0}", f.getCanonicalPath());

            correctPaths.put(entryKey, f.getAbsolutePath());

        } catch (MalformedURLException ex) {
            //LOG.log(Level.INFO, "Using {0} as local path...", e.getValue());
        } catch (IOException ex) {
            //LOG.log(Level.SEVERE, "Savefile error", ex);
            throw new AnnotationException(new OpenNlpBootstrapperAnnotator(),
                    "Failed to download " + e.getValue(), ex);
        } finally {

            // if something went wrong, put the default value.
            if (!correctPaths.containsKey(entryKey)) {
                correctPaths.put(entryKey, entryValue);
            }
        }
    }

    // update the old map with the new values
    databasePaths.clear();
    databasePaths.putAll(correctPaths);

}

From source file:org.pentaho.springdm.extender.PentahoOsgiBundleXmlApplicationContext.java

@Override
protected Resource getResourceByPath(String path) {
    int index = path.indexOf("plugin:");
    if (index != 0) {
        return super.getResourceByPath(path);
    }/*from  w  ww . j a  v a 2s.c om*/

    path = path.substring("plugin:".length());
    String pluginPath = "";
    try {
        Enumeration<URL> resources = getBundle().getResources("/");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            if (!url.getPath().endsWith("-INF")) {
                pluginPath = url.getPath();
                break;
            }
        }
    } catch (IOException e) {
        log.error("Error getting resources by path", e);
    }

    Resource resourceByPath = super.getResourceByPath("classpath:" + pluginPath + "/" + path);
    if (resourceByPath != null) {
        return resourceByPath;
    }
    return null;
}

From source file:com.brightcove.com.zartan.verifier.video.VideoStillVerifier.java

@ZartanCheck(value = "VideoStill is a jpg")
public ResultEnum assertVideoStillTypeCorrect(UploadData upData) throws Throwable {
    URL u = getStillUrl(upData);
    assertTrue("type should be jpg of " + u.getPath(), u.getPath().endsWith("jpg"));
    return ResultEnum.PASS;
}

From source file:com.confighub.api.server.filters.UrlRewriteFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;//from   ww w. jav  a 2s .  c  o  m
    }

    String url = ((HttpServletRequest) request).getRequestURL().toString().toLowerCase();
    URL aUrl = new URL(url);
    String path = aUrl.getPath();

    if (path.startsWith("/email-verification") || path.startsWith("/passwordReset")) {
        request.getRequestDispatcher("/index.html").forward(request, response);
        return;
    }

    if (Utils.isBlank(path) || path.equals("/") || path.equals("/index.html") || path.startsWith("/rest")) {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            request.getRequestDispatcher("/404.html").forward(request, response);
        }
        return;
    }

    if (path.startsWith("/r/") || path.startsWith("/account/") || path.contains("edit/file/")) {
        request.getRequestDispatcher("/index.html").forward(request, response);
        return;
    }

    boolean hasExt = Utils.isBlank(FilenameUtils.getExtension(url));

    if (!hasExt) {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            request.getRequestDispatcher("/404.html").forward(request, response);
        }
        return;
    }

    request.getRequestDispatcher("/index.html").forward(request, response);
}

From source file:com.thoughtworks.go.domain.materials.tfs.TfsSDKCommandBuilderTest.java

private URL getLog4jJarFromClasspath() throws URISyntaxException {
    URLClassLoader classLoader = (URLClassLoader) this.getClass().getClassLoader();
    for (URL u : classLoader.getURLs()) {
        String jarPath = u.getPath();
        if (jarPath.endsWith(".jar") && jarPath.contains("log4j")) {
            return u;
        }/* w w  w .j  a v  a2  s  .  co  m*/
    }
    return null;
}

From source file:net.duckling.ddl.service.url.ContainerURLConstructor.java

private String getBasePath(String baseUrl) {
    String basePath;/*from ww  w . j a v  a  2  s  .  c o m*/
    try {
        URL url = new URL(baseUrl);
        basePath = url.getPath();
    } catch (MalformedURLException e) {
        basePath = "/ddl";
    }
    return basePath;
}

From source file:net.ontopia.infoset.content.FileContentStoreTest.java

public File getTestDirectory() {
    String clsUri = getClass().getName().replace('.', '/') + ".class";
    URL url = getClass().getClassLoader().getResource(clsUri);
    String clsPath = url.getPath().replaceAll("%20", " ");
    File root = new File(clsPath.substring(0, clsPath.length() - clsUri.length()));
    return new File(root.getParentFile(), "test-data");
}