List of usage examples for java.net URL getFile
public String getFile()
From source file:com.denimgroup.threadfix.cli.util.TestUtils.java
public static String getScanPath() { URL scanFileUrl = TestUtils.class.getClassLoader().getResource("testfire.xml"); assert scanFileUrl != null : "Please make the sample scan testfire.xml available as a resource."; return scanFileUrl.getFile(); }
From source file:com.stratio.es.ESJavaRDDFT.java
/** * Imports dataset/*from w ww . ja v a 2s . c om*/ * * @throws java.io.IOException */ private static void dataSetImport() throws IOException, ExecutionException, IOException, InterruptedException, ParseException { JSONParser parser = new JSONParser(); URL url = Resources.getResource(DATA_SET_NAME); Object obj = parser.parse(new FileReader(url.getFile())); JSONObject jsonObject = (JSONObject) obj; IndexResponse responseBook = client.prepareIndex(ES_INDEX_BOOK, ES_TYPE_INPUT, "id") .setSource(jsonObject.toJSONString()).execute().actionGet(); String json2 = "{" + "\"message\":\"" + MESSAGE_TEST + "\"" + "}"; IndexResponse response2 = client.prepareIndex(ES_INDEX_MESSAGE, ES_TYPE_MESSAGE).setCreate(true) .setSource(json2).setReplicationType(ReplicationType.ASYNC).execute().actionGet(); String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; IndexResponse response = client.prepareIndex(ES_INDEX, ES_TYPE).setCreate(true).setSource(json).execute() .actionGet(); String index = response.getIndex(); String _type = response.getType(); String _id = response.getId(); try { CountResponse countResponse = client.prepareCount(ES_INDEX).setTypes(ES_TYPE).execute().actionGet(); SearchResponse searchResponse = client.prepareSearch(ES_INDEX_BOOK).setTypes(ES_TYPE_INPUT).execute() .actionGet(); //searchResponse.getHits().hits(); //assertEquals(searchResponse.getCount(), 1); } catch (AssertionError | Exception e) { cleanup(); e.printStackTrace(); } }
From source file:com.predic8.membrane.core.ws.relocator.Relocator.java
public static String getNewLocation(String addr, String protocol, String host, int port) { try {//from ww w .j a v a2s.c o m URL oldURL = new URL(addr); if (port == -1) { return new URL(protocol, host, oldURL.getFile()).toString(); } if ("http".equals(protocol) && port == 80) port = -1; if ("https".equals(protocol) && port == 443) port = -1; return new URL(protocol, host, port, oldURL.getFile()).toString(); } catch (MalformedURLException e) { log.error("", e); } return ""; }
From source file:com.worldline.easycukes.commons.helpers.FileHelper.java
/** * Downloads some content from an URL to a specific directory * * @param from a {@link String} representation of the URL on which the * content should be downloaded * @param to the path on which the content should be downloaded * @throws IOException if anything's going wrong while downloading the content to * the specified directory *//* w w w. j av a 2 s .c o m*/ public static void download(@NonNull String from, @NonNull String to) throws IOException { final URL url = new URL(from); log.debug("Downloading from: " + url.toString() + " to: " + to.toString()); final String zipFilePath = to + url.getFile().substring(url.getFile().lastIndexOf('/')); FileUtils.copyURLToFile(url, new File(zipFilePath), 30000, 300000); }
From source file:Main.java
public static File getClassFile(Class clazz) { URL path = clazz.getResource(clazz.getName().substring(clazz.getName().lastIndexOf("") + 1) + ".classs"); if (path == null) { String name = clazz.getName().replaceAll("[.]", "/"); path = clazz.getResource("/" + name + ".class"); }/*from w ww. j ava 2s . c o m*/ return new File(path.getFile()); }
From source file:Main.java
/** * Make a URL from the given string.//from w w w.jav a 2 s .co m * * <p> * If the string is a properly formatted file URL, then the file * portion will be made canonical. * * <p> * If the string is an invalid URL then it will be converted into a * file URL. * * @param urlspec The string to construct a URL for. * @param relativePrefix The string to prepend to relative file * paths, or null to disable prepending. * @return A URL for the given string. * * @throws MalformedURLException Could not make a URL for the given string. */ public static URL toURL(String urlspec, final String relativePrefix) throws MalformedURLException { urlspec = urlspec.trim(); URL url; try { url = new URL(urlspec); if (url.getProtocol().equals("file")) { url = makeURLFromFilespec(url.getFile(), relativePrefix); } } catch (Exception e) { // make sure we have a absolute & canonical file url try { url = makeURLFromFilespec(urlspec, relativePrefix); } catch (IOException n) { // // jason: or should we rethrow e? // throw new MalformedURLException(n.toString()); } } return url; }
From source file:cz.lbenda.common.ClassLoaderHelper.java
/** Stream of class which is in given packages * @param basePackage base package/*from ww w . j a v a 2 s . c om*/ * @param classLoader class loader where is classes found * @return stream of class names */ public static List<String> classInPackage(String basePackage, ClassLoader classLoader) { List<String> result = new ArrayList<>(); try { for (Enumeration<URL> resources = classLoader.getResources(basePackage.replace(".", "/")); resources .hasMoreElements();) { URL url = resources.nextElement(); if (String.valueOf(url).startsWith("file:")) { File file = new File(url.getFile()); int prefixLength = url.getFile().length() - basePackage.length(); List<File> files = subClasses(file); files.stream().forEach(f -> { String fName = f.getAbsolutePath(); result.add(fName.substring(prefixLength, fName.length() - 6).replace("/", ".")); }); } else { URLConnection urlCon = url.openConnection(); if (urlCon instanceof JarURLConnection) { JarURLConnection jarURLConnection = (JarURLConnection) urlCon; try (JarFile jar = jarURLConnection.getJarFile()) { jar.stream().forEach(entry -> { String entryName = entry.getName(); if (entryName.endsWith(".class") && entryName.startsWith(basePackage)) { result.add(entryName.substring(0, entryName.length() - 6)); } }); } } } } } catch (IOException e) { throw new RuntimeException(e); } return result; }
From source file:com.stratio.deep.aerospike.AerospikeJavaRDDFT.java
/** * Imports dataset.//from w ww . j av a 2s . c o m * * @throws java.io.IOException */ private static void dataSetImport() throws IOException, ParseException { URL url = Resources.getResource(DATA_SET_NAME); JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader(url.getFile())); JSONObject jsonObject = (JSONObject) obj; String id = (String) jsonObject.get("id"); JSONObject metadata = (JSONObject) jsonObject.get("metadata"); JSONArray cantos = (JSONArray) jsonObject.get("cantos"); Key key = new Key(NAMESPACE_ENTITY, SET_NAME, id); Bin binId = new Bin("id", id); Bin binMetadata = new Bin("metadata", metadata); Bin binCantos = new Bin("cantos", cantos); aerospike.put(null, key, binId, binMetadata, binCantos); aerospike.createIndex(null, NAMESPACE_ENTITY, SET_NAME, "id_idx", "id", IndexType.STRING); Key key2 = new Key(NAMESPACE_CELL, SET_NAME, 3); Bin bin_id = new Bin("_id", "3"); Bin bin_number = new Bin("number", 3); Bin bin_text = new Bin("message", "new message test"); aerospike.put(null, key2, bin_id, bin_number, bin_text); aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "num_idx", "number", IndexType.NUMERIC); aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "_id_idx", "_id", IndexType.STRING); }
From source file:com.us.util.FileHelper.java
/** * //from w w w .j av a 2 s . c o m * * @param packName ?? * @param fileName ?? * @return */ public static InputStream getFileInPackage(String packName, String fileName) { try { String packdir = packName.replace('.', '/'); Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader().getResources(packdir); while (dirs.hasMoreElements()) { URL url = dirs.nextElement(); if (url.getProtocol().equals("file")) { String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); return new FileInputStream(filePath + "/" + fileName); } else if (url.getProtocol().equals("jar")) { return FileHelper.class.getClassLoader() .getResourceAsStream(packdir.concat("/").concat(fileName)); } } } catch (IOException e) { throw UsMsgException.newInstance(String.format(":%s.%s", packName, fileName), e); } return null; }
From source file:ee.ioc.cs.vsle.ccl.PackageClassLoader.java
/** * Returns a path for a jar from a given url. *///from w w w.j a v a 2s. c om private static String getJarPath(URL url) { String urlString = url.getFile(); int idx; if ((idx = urlString.indexOf("!/")) >= 0) { urlString = urlString.substring(0, idx); } if (urlString.startsWith("file:")) { return urlString.substring(5); } return urlString; }