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:eionet.cr.util.URLUtil.java

/**
 * A cross-the-stream-to-get-water replacement for {@link java.net.URL#getHost()}.
 *
 * @param uri//from w w w . j  a v a  2s . c o m
 * @param dflt
 * @return String
 */
public static String extractUrlHost(String uri) {

    if (URLUtil.isURL(uri)) {
        String host = "";
        URL url;
        try {
            url = new URL(StringUtils.substringBefore(uri, "#"));
            host = uri.substring(0, uri.indexOf(url.getPath()));

        } catch (Exception ex) {
            // No need to throw or log it.
        }
        return host;
    } else {
        return null;
    }
}

From source file:de.uzk.hki.da.sb.restfulService.FileUtil.java

/**
 * <p><em>Title: </em></p>
 * <p>Description: Method to strip file name from url</p>
 * // ww  w  .  jav  a2  s  . c om
 * @param url
 * @return 
 */
public static String getRemoteFileName(String url) {
    URL rUrl = null;
    ;
    try {
        rUrl = new URL(url);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String rFilePath = rUrl.getPath();
    String rFileName = rUrl.getPath().substring(rFilePath.lastIndexOf("/"));
    return rFileName;
}

From source file:fr.gael.dhus.datastore.processing.ProcessingUtils.java

/**
 * Retrieve DrbCortex class from passed url.
 * @param url the url to retrieve the class.
 * @return the related class to the url.
 * @throws IOException if model is not reachable.
 * @throws UnsupportedOperationException if class cannot be found.
 *//*from  w w w . j  a  v  a2  s.co m*/
public static DrbCortexItemClass getClassFromUrl(URL url) throws IOException {
    return ProcessingUtils.getClassFromNode(ProcessingUtils.getNodeFromPath(url.getPath()));
}

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

/**
 *
 * Creates a temporary file in the specified directory for the given URL.
 *
 * @param aUrl/*from   ww w.  j a v a  2s. co m*/
 *            URL containing the file's name.
 * @param aDirectory
 *            String containing the path where the temporary file will be created
 * @return The temporary executable file
 *
 * @throws IOException
 *             If a file could not be created
 *
 * */

private static synchronized File getFileAsExecutable(URL aUrl, String aDirectory) throws IOException {

    return File.createTempFile(FilenameUtils.getBaseName(aUrl.getPath()), ".temp", new File(aDirectory));
}

From source file:io.seldon.importer.articles.FileItemAttributesImporter.java

public static String getUrlEncodedString(String input) {
    URL url = null;
    try {/*  w  w  w.  ja  va 2s . c o  m*/
        url = new URL(input);

        URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);

        String encoded = uri.toASCIIString();

        return encoded;

    } catch (MalformedURLException mue) {
        logger.error("Malformed url " + input);
        return null;
    } catch (URISyntaxException e) {
        logger.error("Failed to tranform url into uri ", e);
        return null;
    }
}

From source file:com.skcraft.launcher.util.HttpRequest.java

/**
 * URL may contain spaces and other nasties that will cause a failure.
 *
 * @param existing the existing URL to transform
 * @return the new URL, or old one if there was a failure
 *///from   ww  w  .  j  a va 2  s .  c om
private static URL reformat(URL existing) {
    try {
        URL url = new URL(existing.toString());
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        url = uri.toURL();
        return url;
    } catch (MalformedURLException e) {
        return existing;
    } catch (URISyntaxException e) {
        return existing;
    }
}

From source file:ee.ioc.cs.vsle.ccl.PackageClassLoader.java

/**
 * Helper method for getWebStartClasspath() method.
 * Returns a local URL of a jar for the classpath.
 * If jar is a system lib, it is ignored.
 * @param url/*from ww  w  .ja va 2 s  . c  om*/
 * @param systemLibs
 * @return
 */
private static URL getLocalJarURL(URL url, Collection<String> systemLibs) {

    String urlStrJar = getJarPath(url);

    //ignore if it is a system lib
    if (systemLibs.contains(urlStrJar))
        return null;

    InputStream inputStreamJar = null;
    File tempJar;
    try {
        //check if the file is already local
        if (url.getPath().startsWith("file:"))
            return new File(urlStrJar).toURI().toURL();

        JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
        inputStreamJar = new FileInputStream(jar.getName());

        String strippedName = urlStrJar;
        int dotIndex = strippedName.lastIndexOf('.');
        if (dotIndex >= 0) {
            strippedName = strippedName.substring(0, dotIndex);
            strippedName = strippedName.replace("/", File.separator);
            strippedName = strippedName.replace("\\", File.separator);
            int slashIndex = strippedName.lastIndexOf(File.separator);
            if (slashIndex >= 0) {
                strippedName = strippedName.substring(slashIndex + 1);
            }
        }
        tempJar = File.createTempFile(strippedName, ".jar");
        tempJar.deleteOnExit();
        FileUtils.copyInputStreamToFile(inputStreamJar, tempJar);
        return tempJar.toURI().toURL();
    } catch (Exception ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (inputStreamJar != null) {
                inputStreamJar.close();
            }
        } catch (IOException ioe) {
        }
    }
    return null;
}

From source file:autoupdater.DownloadLatestZipFromRepo.java

/**
 * @param artifactURL the url of the artifact, if the url is the actual file archive(zip,tar.gz or jar) to download it will just download,
 * otherwise it will try to first retrieve artifactid-versionnumber.zip/tar.gz
 * and lastly try to retrieve artifactid-versionnumber.jar
 * @return the downloaded file/*from w w  w.  j  a  v  a  2  s .c o  m*/
 * @throws java.io.FileNotFoundException if there is not file to download at the url location
 */
public static File downloadJarFile(URL artifactURL, File destination) throws IOException {

    URL archiveURL;

    // get the archive url
    String folderName;
    if (artifactURL.getPath().contains(".zip")) {
        archiveURL = WebDAO.getUrlOfZippedVersion(artifactURL, ".zip", false);
        folderName = archiveURL.getFile().substring(archiveURL.getFile().lastIndexOf("/"),
                archiveURL.getFile().lastIndexOf(".zip"));
    } else {
        archiveURL = WebDAO.getUrlOfZippedVersion(artifactURL, ".tar.gz", false);
        if (archiveURL != null) {
            folderName = archiveURL.getFile().substring(archiveURL.getFile().lastIndexOf("/"),
                    archiveURL.getFile().lastIndexOf(".tar.gz"));
        }
    }

    // special fix for tools with separate versions for windows and unix, you don't do special fixes in a generic library
    /*
            if (folderName.endsWith("-windows")) {
    folderName = folderName.substring(0, folderName.indexOf("-windows"));
            } else if (folderName.endsWith("-mac_and_linux")) {
    folderName = folderName.substring(0, folderName.indexOf("-mac_and_linux"));
            }
    */

    if (!destination.exists()) {
        if (!destination.mkdirs()) {
            throw new IOException("could not make the directories needed to download the file in");
        }
    }

    // create an empty dummy file so that progress can be monitored
    downloadedFile = new File(destination,
            archiveURL.getFile().substring(archiveURL.getFile().lastIndexOf("/")));

    // download and unzip the file
    downloadedFile = fileDAO.writeStreamToDisk(archiveURL.openStream(),
            archiveURL.getFile().substring(archiveURL.getFile().lastIndexOf("/")), destination);
    return downloadedFile;
}

From source file:msi.gama.gui.swt.WorkspaceModelsManager.java

public static String getCurrentGamaStampString() {
    String gamaStamp = null;//from   w ww .  j  av a  2s .  co m
    try {
        URL urlRep = FileLocator.toFileURL(new URL("platform:/plugin/msi.gama.models/models/"));
        File modelsRep = new File(urlRep.getPath());
        long time = modelsRep.lastModified();
        gamaStamp = ".built_in_models_" + time;
        System.out.println("Version of the models in GAMA = " + gamaStamp);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return gamaStamp;
}

From source file:co.cask.common.security.server.ExternalAuthenticationServerSSLTest.java

@BeforeClass
public static void beforeClass() throws Exception {
    URL certUrl = ExternalAuthenticationServerSSLTest.class.getClassLoader().getResource("cert.jks");
    Assert.assertNotNull(certUrl);//from w  ww  .  java2s . c o  m

    String authHandlerConfigBase = Constants.AUTH_HANDLER_CONFIG_BASE;

    SecurityConfiguration cConf = SecurityConfiguration.create();
    cConf.set(Constants.SSL_ENABLED, "true");
    cConf.set(authHandlerConfigBase.concat("useLdaps"), "true");
    cConf.set(authHandlerConfigBase.concat("ldapsVerifyCertificate"), "false");
    cConf.set(Constants.AuthenticationServer.SSL_KEYSTORE_PATH, certUrl.getPath());
    configuration = cConf;

    String keystorePassword = cConf.get(Constants.AuthenticationServer.SSL_KEYSTORE_PASSWORD);
    KeyStoreKeyManager keyManager = new KeyStoreKeyManager(certUrl.getFile(), keystorePassword.toCharArray());
    SSLUtil sslUtil = new SSLUtil(keyManager, new TrustAllTrustManager());
    ldapListenerConfig = InMemoryListenerConfig.createLDAPSConfig("LDAP", InetAddress.getByName("127.0.0.1"),
            ldapPort, sslUtil.createSSLServerSocketFactory(), sslUtil.createSSLSocketFactory());

    setup();
}