List of usage examples for java.net URL getPath
public String getPath()
From source file:it.cnr.icar.eric.common.Utility.java
public static String getURLPathFromURI(String uri) throws URISyntaxException { String path = null;/*from www . ja va 2 s . com*/ try { URL url = new URL(uri); path = url.getPath(); } catch (MalformedURLException e) { //Not a URL. Convert to URN String urn = fixURN(uri); path = getURLPathFromURN(urn); } return path; }
From source file:edu.umn.cs.spatialHadoop.core.SpatialSite.java
private static String findContainingJar(Class my_class) { ClassLoader loader = my_class.getClassLoader(); String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; try {/* www. ja v a2 s . co m*/ for (Enumeration<URL> itr = loader.getResources(class_file); 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 = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:net.sourceforge.lept4j.util.LoadLibs.java
/** * Extracts Leptonica resources to temp folder. * * @param dirname resource location// w ww .jav a 2 s. c o m * @return target location */ public static File extractNativeResources(String dirname) { File targetTempDir = null; try { targetTempDir = new File(LEPT4J_TEMP_DIR, dirname); URL leptResourceUrl = LoadLibs.class.getResource(dirname.startsWith("/") ? dirname : "/" + dirname); if (leptResourceUrl == null) { return null; } URLConnection urlConnection = leptResourceUrl.openConnection(); /** * Either load from resources from jar or project resource folder. */ if (urlConnection instanceof JarURLConnection) { copyJarResourceToDirectory((JarURLConnection) urlConnection, targetTempDir); } else { FileUtils.copyDirectory(new File(leptResourceUrl.getPath()), targetTempDir); } } catch (Exception e) { logger.log(Level.WARNING, e.getMessage(), e); } return targetTempDir; }
From source file:com.comcast.cmb.common.util.AuthUtil.java
private static String constructV2DataToSign(URL url, Map<String, String> parameters) throws UnsupportedEncodingException { StringBuilder sb = new StringBuilder(); sb.append("POST").append("\n"); sb.append(normalizeURL(url)).append("\n"); sb.append(normalizeResourcePath(url.getPath())).append("\n"); sb.append(normalizeQueryString(parameters)); return sb.toString(); }
From source file:msi.gama.application.workspace.WorkspaceModelsManager.java
public static String getCurrentGamaStampString() { String gamaStamp = null;//from w ww . jav a 2s.c om try { final URL tmpURL = new URL("platform:/plugin/msi.gama.models/models/"); final URL new_url = FileLocator.resolve(tmpURL); final String path_s = new_url.getPath().replaceFirst("^/(.:/)", "$1"); final java.nio.file.Path normalizedPath = Paths.get(path_s).normalize(); final File modelsRep = normalizedPath.toFile(); // loading file from URL Path is not a good idea. There are some bugs // File modelsRep = new File(urlRep.getPath()); final long time = modelsRep.lastModified(); gamaStamp = ".built_in_models_" + time; System.out.println(">GAMA version " + WorkspaceModelsManager.BUILTIN_VERSION + " loading..."); System.out.println(">GAMA models library version: " + gamaStamp); } catch (final IOException e) { e.printStackTrace(); } return gamaStamp; }
From source file:com.t3.persistence.FileUtil.java
/** * Given a URL this method determines the content type of the URL (if possible) and * then returns a Reader with the appropriate character encoding. * //www . jav a 2 s. com * @param url the source of the data stream * @return String representing the data * @throws IOException */ public static InputStream getURLAsInputStream(URL url) throws IOException { InputStream is = null; URLConnection conn = null; // We're assuming character here, but it could be bytes. Perhaps we should // check the MIME type returned by the network server? conn = url.openConnection(); if (log.isDebugEnabled()) { String type = URLConnection.guessContentTypeFromName(url.getPath()); log.debug("result from guessContentTypeFromName(" + url.getPath() + ") is " + type); type = getContentType(conn.getInputStream()); log.debug("result from getContentType(" + url.getPath() + ") is " + type); } is = conn.getInputStream(); return is; }
From source file:net.antidot.semantic.rdf.rdb2rdf.r2rml.tools.R2RMLToolkit.java
/** * The URL-safe version of a string is obtained by an URL encoding to a * string value./* w ww . j a va2 s . co m*/ * * @throws R2RMLDataError * @throws MalformedURLException */ public static String getURLSafeVersion(String value) throws R2RMLDataError { if (value == null) return null; URL url = null; try { url = new URL(value); } catch (MalformedURLException mue) { // This template should be not a url : no encoding return value; } // No exception raised, this template is a valid url : perform // percent-encoding try { java.net.URI uri = new java.net.URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef()); String result = uri.toURL().toString(); // Percent encoding : complete with no supported char in this // treatment result = result.replaceAll("\\,", "%2C"); return result; } catch (URISyntaxException e) { throw new R2RMLDataError("[R2RMLToolkit:getIRISafeVersion] This value " + value + " can not be percent-encoded because " + e.getMessage()); } catch (MalformedURLException e) { throw new R2RMLDataError("[R2RMLToolkit:getIRISafeVersion] This value " + value + " can not be percent-encoded because " + e.getMessage()); } }
From source file:com.amazon.speech.speechlet.authentication.SpeechletRequestSignatureVerifier.java
/** * Verifies the signing certificate chain URL and returns a {@code URL} object. * * @param signingCertificateChainUrl/*from ww w. j a v a 2s. c om*/ * the external form of the URL * @return the URL * @throws CertificateException * if the URL is malformed or contains an invalid hostname, an unsupported protocol, * or an invalid port (if specified) */ static URL getAndVerifySigningCertificateChainUrl(final String signingCertificateChainUrl) throws CertificateException { try { URL url = new URI(signingCertificateChainUrl).normalize().toURL(); // Validate the hostname if (!VALID_SIGNING_CERT_CHAIN_URL_HOST_NAME.equalsIgnoreCase(url.getHost())) { throw new CertificateException(String.format( "SigningCertificateChainUrl [%s] does not contain the required hostname" + " of [%s]", signingCertificateChainUrl, VALID_SIGNING_CERT_CHAIN_URL_HOST_NAME)); } // Validate the path prefix String path = url.getPath(); if (!path.startsWith(VALID_SIGNING_CERT_CHAING_URL_PATH_PREFIX)) { throw new CertificateException(String.format( "SigningCertificateChainUrl path [%s] is invalid. Expecting path to " + "start with [%s]", signingCertificateChainUrl, VALID_SIGNING_CERT_CHAING_URL_PATH_PREFIX)); } // Validate the protocol String urlProtocol = url.getProtocol(); if (!VALID_SIGNING_CERT_CHAIN_PROTOCOL.equalsIgnoreCase(urlProtocol)) { throw new CertificateException( String.format("SigningCertificateChainUrl [%s] contains an unsupported protocol [%s]", signingCertificateChainUrl, urlProtocol)); } // Validate the port uses the default of 443 for HTTPS if explicitly defined in the URL int urlPort = url.getPort(); if ((urlPort != UNSPECIFIED_SIGNING_CERT_CHAIN_URL_PORT_VALUE) && (urlPort != url.getDefaultPort())) { throw new CertificateException( String.format("SigningCertificateChainUrl [%s] contains an invalid port [%d]", signingCertificateChainUrl, urlPort)); } return url; } catch (IllegalArgumentException | MalformedURLException | URISyntaxException ex) { throw new CertificateException( String.format("SigningCertificateChainUrl [%s] is malformed", signingCertificateChainUrl), ex); } }
From source file:gate.DocumentFormat.java
/** * Given a URL, this method returns all the 'file extensions' for the file * part of the URL. For this purposes, a 'file extension' is any sequence of * .-separated tokens (such as .gate.xml.gz). The order the extensions are * returned in is from the most specific (longest) to the most generic * (shortest) one, e.g. [.gate.xml.gz, .xml.gz, .gz]. *///from ww w . j av a 2 s . c o m private static List<String> getFileSuffixes(URL url) { List<String> res = new LinkedList<String>(); if (url != null) { // get the file name from the URL String fileName = url.getPath(); int pos = fileName.lastIndexOf('/'); if (pos > 0) fileName = fileName.substring(pos); pos = fileName.indexOf('.', 1); while (pos > 0 && pos < fileName.length() - 1) { res.add(fileName.substring(pos + 1)); pos = fileName.indexOf('.', pos + 1); } } return res; }
From source file:com.egt.core.util.Utils.java
public static String getAttachedFileSimpleName(URL url) { return getAttachedFileSimpleName(url.getPath()); }