List of usage examples for java.net URL getPath
public String getPath()
From source file:com.centeractive.ws.builder.MessageComplianceTest.java
public static String getContent(String folderPath, String fileName) { URL fileUrl = ResourceUtils.getResourceWithAbsolutePackagePath(folderPath, fileName); File file = null;/*from w w w . j a v a2 s . co m*/ try { file = new File(fileUrl.toURI()); } catch (URISyntaxException e) { file = new File(fileUrl.getPath()); } try { return FileUtils.readFileToString(file); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:ml.shifu.guagua.hadoop.util.HDPUtils.java
@SuppressWarnings("rawtypes") public static String findContainingJar(Class my_class) { ClassLoader loader = my_class.getClassLoader(); if (loader == null) { loader = Thread.currentThread().getContextClassLoader(); }//ww w . j av a 2s. co m String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; try { Enumeration<URL> itr = null; // Try to find the class in registered jars if (loader instanceof URLClassLoader) { itr = ((URLClassLoader) loader).findResources(class_file); } // Try system classloader if not URLClassLoader or no resources found in URLClassLoader if (itr == null || !itr.hasMoreElements()) { itr = loader.getResources(class_file); } for (; 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()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:com.centeractive.ws.builder.ServiceComplianceTest.java
public static Definition getDefinition(URL wsdlUrl) throws WSDLException { WSDLReader reader = new WSDLReaderImpl(); Definition definition = reader.readWSDL(wsdlUrl.getPath()); return definition; }
From source file:com.amazonaws.codepipeline.jenkinsplugin.CompressionTools.java
public static Path resolveWorkspacePath(final File workspace, final String outputPath) throws FileNotFoundException { Path path = null;/* ww w . j av a 2 s.c o m*/ if (workspace != null) { if (!outputPath.contains(workspace.getAbsolutePath())) { path = Paths.get(workspace.getAbsolutePath(), outputPath); } else { path = Paths.get(outputPath); } } else { final String attemptPath; final URL tmp = CompressionTools.class.getClassLoader().getResource(""); if (tmp != null) { attemptPath = tmp.getPath(); } else { attemptPath = ""; } if (attemptPath != null) { path = Paths.get(attemptPath); } } if (path == null) { throw new FileNotFoundException("Could not resolve path for " + outputPath); } path.resolve(outputPath); return path; }
From source file:de.qaware.cloud.deployer.commons.config.util.FileUtil.java
/** * Reads the content of the file with the specified filename into a string. * * @param filename The name of the file whose content will be returned. * @return The content of the file.// w w w. ja v a 2s.com * @throws ResourceConfigException If a problem with the file occurs. */ public static String readFileContent(String filename) throws ResourceConfigException { if (filename == null || filename.isEmpty()) { throw new ResourceConfigException( COMMONS_MESSAGE_BUNDLE.getMessage("DEPLOYER_COMMONS_ERROR_INVALID_FILENAME")); } URL filepath = FileUtil.class.getResource(filename); if (filepath == null) { throw new ResourceConfigException( COMMONS_MESSAGE_BUNDLE.getMessage("DEPLOYER_COMMONS_ERROR_MISSING_FILE", filename)); } File file = new File(filepath.getPath()); return readFileContent(file); }
From source file:com.asakusafw.testdriver.DirectIoUtil.java
private static <T> DataModelSourceFactory load0(DataModelDefinition<T> definition, HadoopFileFormat<? super T> format, URL source) throws IOException, InterruptedException { List<String> segments = Arrays.stream(source.getPath().split("/")) //$NON-NLS-1$ .map(String::trim).filter(s -> s.isEmpty() == false).collect(Collectors.toList()); String name;/*from w w w . j av a 2 s .c o m*/ if (segments.isEmpty()) { name = "testing.file"; //$NON-NLS-1$ } else { name = segments.get(segments.size() - 1); } Path tmpdir = Files.createTempDirectory("asakusa-"); //$NON-NLS-1$ try (InputStream in = source.openStream()) { Path target = tmpdir.resolve(name); Files.copy(in, target); return load0(definition, format, target.toFile()); } finally { File dir = tmpdir.toFile(); if (FileUtils.deleteQuietly(dir) == false && dir.exists()) { LOG.warn(MessageFormat.format("failed to delete a temporary file: {0}", tmpdir)); } } }
From source file:com.qwazr.utils.HtmlUtils.java
public final static String urlHostPathWrapReduce(String url, int maxSize) { URL u; try {/*from w w w.j a va 2 s.c o m*/ u = new URL(url); } catch (MalformedURLException e) { return url; } String path = StringUtils.fastConcat(u.getHost(), '/', u.getPath()); String[] frags = StringUtils.split(path, '/'); if (frags.length < 2) return path; int startPos = 1; int endPos = frags.length - 2; StringBuilder sbStart = new StringBuilder(frags[0]); StringBuilder sbEnd = new StringBuilder(frags[frags.length - 1]); int length = sbStart.length() + sbEnd.length(); for (;;) { boolean bHandled = false; if (startPos != -1 && startPos < endPos) { if (frags[startPos].length() + length < maxSize) { sbStart.append('/'); sbStart.append(frags[startPos++]); bHandled = true; } } if (endPos != -1 && endPos > startPos) { if (frags[endPos].length() + length < maxSize) { sbEnd.insert(0, '/'); sbEnd.insert(0, frags[endPos--]); bHandled = true; } } if (!bHandled) break; } return StringUtils.fastConcat(sbStart, "//", sbEnd); }
From source file:com.app.test.BaseTestCase.java
protected static void setUpProperties() throws Exception { Class<?> clazz = BaseTestCase.class; URL resource = clazz.getResource("/test-config.properties"); PropertiesUtil.loadConfigurationProperties(resource.getPath()); }
From source file:com.spotify.docker.client.DockerConfigReaderTest.java
private static Path getWindowsPath(final String path) { final URL resource = DockerConfigReaderTest.class.getResource("/" + path); return Paths.get(resource.getPath().substring(1)); }
From source file:io.lightlink.utils.ClasspathScanUtils.java
public static File getFileFromResource(String rootPackage, String resource) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String resourceName = rootPackage.replace('.', '/') + resource; URL url = classLoader.getResource(resourceName); if (url == null) throw new IllegalArgumentException("Cannot find :" + resourceName + " " + resource); return new File(URLDecoder.decode(url.getPath())); }