List of usage examples for java.net URL getPath
public String getPath()
From source file:eu.eubrazilcc.lvl.storage.UrlShortenerTest.java
@BeforeClass public static void setUp() throws IOException { final File file = new File( concat(DEFAULT_LOCATION, TEST_CONFIG_ROOT + separator + "etc" + separator + REST_SERVICE_CONFIG)); if (file.canRead()) { final List<URL> urls = newArrayList(getDefaultConfiguration()); for (final ListIterator<URL> it = urls.listIterator(); it.hasNext();) { final URL url = it.next(); if (url.getPath().endsWith(REST_SERVICE_CONFIG)) { it.remove();//from ww w .j a v a 2 s . com it.add(toURLs(new File[] { file })[0]); } } CONFIG_MANAGER.setup(urls); hasToClean = true; } }
From source file:Main.java
public static String realUrl(String target) { try {// www . j ava 2 s. com URL url = new URL(target); String protocol = url.getProtocol(); String host = url.getHost(); String path = url.getPath(); String query = url.getQuery(); path = URLEncoder.encode(path, "utf-8").replace("%3A", ":").replace("%2B", "+").replace("%2C", ",") .replace("%5E", "^").replace("%2F", "/").replace("%21", "!").replace("%24", "$") .replace("%25", "%").replace("%26", "&").replace("%28", "(").replace("%29", ")") .replace("%40", "@").replace("%60", "`"); // .replace("", "#"); // not support. StringBuilder urlBuild = new StringBuilder(protocol).append("://").append(host).append(path); if (query != null) urlBuild.append("?").append(query); return urlBuild.toString(); } catch (IOException e) { return target; } }
From source file:com.basho.contact.parser.ContactParserTest.java
public static String loadResource(String name) throws Exception { ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL url = loader.getResource(name); File f = new File(url.getPath()); return org.apache.commons.io.FileUtils.readFileToString(f); }
From source file:com.gc.iotools.fmt.base.TestUtils.java
public static String[] listFilesExcludingExtension(final String[] forbidden) throws IOException { final URL fileURL = TestUtils.class.getResource("/testFiles"); String filePath = URLDecoder.decode(fileURL.getPath(), "UTF-8"); final File dir = new File(filePath); final String[] files = dir.list(); final Collection<String> goodFiles = new Vector<String>(); if (!filePath.endsWith(File.separator)) { filePath = filePath + File.separator; }/*from www .ja v a 2 s. c o m*/ for (final String file : files) { boolean insert = true; for (final String extForbidden : forbidden) { insert &= !(file.endsWith(extForbidden)); } if (insert) { goodFiles.add(filePath + file); } } return goodFiles.toArray(new String[goodFiles.size()]); }
From source file:com.gc.iotools.fmt.base.TestUtils.java
/** * @deprecated/*from ww w . j av a 2 s . c o m*/ * @see FileUtils#iterate(); * @param allowed * @return * @throws IOException */ @Deprecated public static String[] listFilesIncludingExtension(final String[] allowed) throws IOException { final URL fileURL = TestUtils.class.getResource("/testFiles"); String filePath = URLDecoder.decode(fileURL.getPath(), "UTF-8"); final File dir = new File(filePath); final String[] files = dir.list(); final Collection<String> goodFiles = new Vector<String>(); if (!filePath.endsWith(File.separator)) { filePath = filePath + File.separator; } for (final String file : files) { for (final String element : allowed) { if (file.endsWith(element)) { goodFiles.add(filePath + file); } } } return goodFiles.toArray(new String[goodFiles.size()]); }
From source file:com.streamsets.datacollector.cluster.TarFileCreator.java
private static void addClasspath(String prefix, TarOutputStream out, List<URL> urls) throws IOException { if (urls != null) { for (URL url : urls) { File file = new File(url.getPath()); String name = file.getName(); if (name.endsWith(".jar")) { out.putNextEntry(new TarEntry(file, prefix + "/" + file.getName())); BufferedInputStream src = new BufferedInputStream(new FileInputStream(file), 65536); IOUtils.copy(src, out);//from www . ja v a 2 s . c o m src.close(); out.flush(); } } } }
From source file:zipkin.sparkstreaming.job.ZipkinSparkStreamingConfiguration.java
static String pathToJar(Class<?> type) { URL jarFile = type.getProtectionDomain().getCodeSource().getLocation(); try {/*from w w w . ja v a2 s. c o m*/ return URLDecoder.decode(jarFile.getPath(), "UTF-8"); } catch (UnsupportedEncodingException e) { return null; } }
From source file:com.autentia.common.util.ejb.JBossUtils.java
/** * Devuelve el nombre del ear en el que se encuentra la clase <code>clazz</code>. "" si no est en un ear. * <p>//from w w w .j a v a 2 s . co m * Este mtodo esta probado para JBoss 4.2.2GA. * * @param clazz clase que se est buscando. * @return el nombre del ear en el que se encuentra la clase <code>clazz</code>. "" si no est en un ear. */ public static String getEarName(Class<?> clazz) { String cn = File.separator + clazz.getCanonicalName(); cn = cn.replace('.', File.separatorChar); cn += ".class"; final URL url = Thread.currentThread().getContextClassLoader().getResource(cn); final String path = url.getPath(); if (log.isDebugEnabled()) { log.debug(clazz.getCanonicalName() + " is in path: " + path); } final int indexOfEar = path.indexOf(".ear"); final int indexOfExclamationChar = path.indexOf("!"); if (indexOfEar > -1 && indexOfExclamationChar > -1 && indexOfEar < indexOfExclamationChar) { // JBoss despliega los ear en un directorio temporal del estilo: .../tmp34545nombreDelEar.ear-contents/... int beginTempDir = path.lastIndexOf(File.separatorChar, indexOfEar); int i = beginTempDir; boolean reachedDigit = false; while (i < indexOfEar) { if (Character.isDigit(path.charAt(i))) { reachedDigit = true; } else if (reachedDigit) { break; } i++; } final String prefix = path.substring(i, indexOfEar) + File.separator; log.debug(clazz.getCanonicalName() + " is inside ear: " + prefix); return prefix; } log.debug(clazz.getCanonicalName() + " is not inside an ear."); return ""; }
From source file:com.threadswarm.imagefeedarchiver.FeedUtils.java
/** * Returns a hierarchical {@code URI} constructed from individual components * of the supplied {@code urlString} argument. * <p>/*from w ww . ja va2 s.c om*/ * The {@code urlString} argument is first used to instantiate a {@code URL} * which in turn is used to construct a {@code URI} based on the individual * components of the former. This more robust then simply calling {@code URL.toURI()}. * * @param urlString the {@code String} based representation of a URL * @return a {@code URI} constructed from the individual URL components * @throws URISyntaxException if a valid {@code URI} cannot be constructed from the supplied {@code urlString} argument * @throws MalformedURLException if the {@code urlString} cannot be used to instantiate a {@code URL} */ public static URI getUriFromUrlString(String urlString) throws URISyntaxException, MalformedURLException { URL url = new URL(urlString); return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); }
From source file:com.cloudera.sqoop.util.JdbcUrl.java
/** * @return the database name from the connect string, which is typically the * 'path' component, or null if we can't. *///from ww w . j a v a2 s. c o m public static String getDatabaseName(String connectString) { try { String sanitizedString = null; int schemeEndOffset = connectString.indexOf("://"); if (-1 == schemeEndOffset) { // couldn't find one? try our best here. sanitizedString = "http://" + connectString; LOG.warn("Could not find database access scheme in connect string " + connectString); } else { sanitizedString = "http" + connectString.substring(schemeEndOffset); } URL connectUrl = new URL(sanitizedString); String databaseName = connectUrl.getPath(); if (null == databaseName) { return null; } // This is taken from a 'path' part of a URL, which may have leading '/' // characters; trim them off. while (databaseName.startsWith("/")) { databaseName = databaseName.substring(1); } return databaseName; } catch (MalformedURLException mue) { LOG.error("Malformed connect string URL: " + connectString + "; reason is " + mue.toString()); return null; } }