List of usage examples for org.springframework.util StringUtils cleanPath
public static String cleanPath(String path)
From source file:com.taobao.itest.listener.ResourceLocationProcessingUtil.java
public static String[] modifyLocations(Class<?> clazz, String... locations) { String[] modifiedLocations = new String[locations.length]; for (int i = 0; i < locations.length; i++) { String path = locations[i]; if (path.startsWith("/")) { modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path; } else if (!ResourcePatternUtils.isUrl(path)) { modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + "/" + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + "/" + path); } else {//from ww w.ja v a 2 s . c o m modifiedLocations[i] = StringUtils.cleanPath(path); } } return modifiedLocations; }
From source file:org.shept.util.JarResourceCopier.java
public void initializeResources(ServletContext context) { String destPath = StringUtils.cleanPath(context.getRealPath(getTargetPath())); if (files != null && files.length > 0) { for (int i = 0; i < files.length; i++) { String source = StringUtils.applyRelativePath(getSourcePath(), files[i]); // surprise surprise you can't use StringUtils.applyRelativePath here this will cut off the last part of destPath JarUtils.copyResourcesOnce(new ClassPathResource(source), destPath, destPath + "/" + files[i]); }/*from w ww. j a v a 2 s .co m*/ } else { JarUtils.copyResourcesOnce(new ClassPathResource(getSourcePath()), destPath); } }
From source file:ratpack.spring.config.RatpackProperties.java
static Path resourceToPath(URL resource) { Objects.requireNonNull(resource, "Resource URL cannot be null"); URI uri;// w ww.j a v a2 s . co m try { uri = resource.toURI(); } catch (URISyntaxException e) { throw new IllegalArgumentException("Could not extract URI", e); } String scheme = uri.getScheme(); if (scheme.equals("file")) { String path = uri.toString().substring("file:".length()); if (path.contains("//")) { path = StringUtils.cleanPath(path.replace("//", "")); } return Paths.get(new FileSystemResource(path).getFile().toURI()); } if (!scheme.equals("jar")) { throw new IllegalArgumentException("Cannot convert to Path: " + uri); } String s = uri.toString(); int separator = s.indexOf("!/"); String entryName = s.substring(separator + 2); URI fileURI = URI.create(s.substring(0, separator)); FileSystem fs; try { fs = FileSystems.newFileSystem(fileURI, Collections.<String, Object>emptyMap()); return fs.getPath(entryName); } catch (IOException e) { throw new IllegalArgumentException("Could not create file system for resource: " + resource, e); } }
From source file:com.excilys.ebi.utils.spring.log.logback.test.LogbackConfigurerTestExecutionListener.java
/** * Generates the default classpath resource location based on the supplied * class.// www .jav a 2 s. com * <p> * For example, if the supplied class is <code>com.example.MyTest</code>, * the generated location will be a string with a value of * "classpath:/com/example/<code><suffix></code>", where * <code><suffix></code> is the value of the * {@link #getResourceName() resource name} string. * <p> * Subclasses can override this method to implement a different * <em>default location generation</em> strategy. * * @param clazz * the class for which the default locations are to be generated * @return an array of default application context resource locations * @see #getResourceSuffix() */ private String generateDefaultLocation(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); return ResourceUtils.CLASSPATH_URL_PREFIX + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + "/" + getResourceName()); }
From source file:org.eclipse.gemini.blueprint.test.internal.util.jar.JarCreator.java
/** * Small utility method used for determining the file name by striping the * root path from the file full path./*from ww w .ja v a 2 s . c o m*/ * * @param rootPath * @param resource * @return */ private String determineRelativeName(String rootPath, Resource resource) { try { String path = StringUtils.cleanPath(resource.getURL().toExternalForm()); return path.substring(path.indexOf(rootPath) + rootPath.length()); } catch (IOException ex) { throw (RuntimeException) new IllegalArgumentException("illegal resource " + resource.toString()) .initCause(ex); } }
From source file:spring.osgi.io.OsgiBundleResource.java
/** * Constructs a new <code>OsgiBundleResource</code> instance. * * @param bundle OSGi bundle used by this resource * @param path resource path inside the bundle. *//*from w w w.jav a 2s . co m*/ public OsgiBundleResource(Bundle bundle, String path) { Assert.notNull(bundle, "Bundle must not be null"); this.bundle = bundle; // check path Assert.notNull(path, "Path must not be null"); this.path = StringUtils.cleanPath(path); this.searchType = OsgiResourceUtils.getSearchType(this.path); switch (this.searchType) { case OsgiResourceUtils.PREFIX_TYPE_NOT_SPECIFIED: pathWithoutPrefix = path; break; case OsgiResourceUtils.PREFIX_TYPE_BUNDLE_SPACE: pathWithoutPrefix = path.substring(BUNDLE_URL_PREFIX.length()); break; case OsgiResourceUtils.PREFIX_TYPE_BUNDLE_JAR: pathWithoutPrefix = path.substring(BUNDLE_JAR_URL_PREFIX.length()); break; case OsgiResourceUtils.PREFIX_TYPE_CLASS_SPACE: pathWithoutPrefix = path.substring(ResourceLoader.CLASSPATH_URL_PREFIX.length()); break; // prefix unknown so the path will be resolved outside the context default: pathWithoutPrefix = null; } }
From source file:com.agileapes.couteau.maven.resource.ClassPathScanningClassProvider.java
private Class resolveClass(Resource resource, String basePackage) throws IOException, ClassNotFoundException { final String clazzCleanPath = StringUtils.cleanPath(resource.getURL().getPath()); final String clazzPathWithoutExtension = StringUtils.stripFilenameExtension(clazzCleanPath); final String resourcePackageLikeStr = ClassUtils.convertResourcePathToClassName(clazzPathWithoutExtension); final String className = resourcePackageLikeStr.substring(resourcePackageLikeStr.lastIndexOf(basePackage)); return ClassUtils.forName(className, classLoader); }
From source file:com.excilys.ebi.utils.spring.log.logback.test.LogbackConfigurerTestExecutionListener.java
/** * Generate a modified version of the supplied location and returns it. * <p>//w w w. ja va 2 s .co m * A plain path, e.g. "context.xml", will be treated as a * classpath resource from the same package in which the specified class is * defined. A path starting with a slash is treated as a fully qualified * class path location, e.g.: "/com/example/whatever/foo.xml". A * path which references a URL (e.g., a path prefixed with * {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:}, * {@link ResourceUtils#FILE_URL_PREFIX file:}, <code>http:</code>, etc.) * will be added to the results unchanged. * <p> * Subclasses can override this method to implement a different * <em>location modification</em> strategy. * * @param clazz * the class with which the locations are associated * @param locations * the resource location to be modified * @return the modified application context resource location */ protected String modifyLocation(Class<?> clazz, String location) { String modifiedLocation = null; if (location.startsWith("/")) { modifiedLocation = ResourceUtils.CLASSPATH_URL_PREFIX + location; } else if (!ResourcePatternUtils.isUrl(location)) { modifiedLocation = ResourceUtils.CLASSPATH_URL_PREFIX + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + "/" + location); } else { modifiedLocation = StringUtils.cleanPath(location); } return modifiedLocation; }
From source file:org.eclipse.gemini.blueprint.test.internal.util.jar.JarCreator.java
/** * Resolve the jar content based on its path. Will return a map containing * the entries relative to the jar root path as keys and Spring Resource * pointing to the actual resources as values. It will also determine the * packages contained by this package./*from ww w.j av a 2 s. co m*/ * * @return */ public Map resolveContent() { Resource[][] resources = resolveResources(); URL rootURL; String rootP = getRootPath(); try { rootURL = new URL(rootP); } catch (MalformedURLException ex) { throw (RuntimeException) new IllegalArgumentException("illegal root path given " + rootP).initCause(ex); } String rootPath = StringUtils.cleanPath(rootURL.getPath()); // remove duplicates Map entries = new TreeMap(); // save contained bundle packages containedPackages.clear(); // empty stream used for folders Resource folderResource = new ByteArrayResource(new byte[0]); // add folder entries also for (int i = 0; i < resources.length; i++) { for (int j = 0; j < resources[i].length; j++) { String relativeName = determineRelativeName(rootPath, resources[i][j]); // be consistent when adding resources to jar if (!relativeName.startsWith("/")) relativeName = "/" + relativeName; entries.put(relativeName, resources[i][j]); // look for class entries if (relativeName.endsWith(CLASS_EXT)) { // determine package (exclude first char) String clazzName = relativeName.substring(1, relativeName.length() - CLASS_EXT.length()) .replace('/', '.'); // remove class name int index = clazzName.lastIndexOf('.'); if (index > 0) clazzName = clazzName.substring(0, index); // add it to the collection containedPackages.add(clazzName); } String token = relativeName; // get folder and walk up to the root if (addFolders) { // add META-INF entries.put("/META-INF/", folderResource); int slashIndex; // stop at root folder while ((slashIndex = token.lastIndexOf('/')) > 1) { // add the folder with trailing / entries.put(token.substring(0, slashIndex + 1), folderResource); // walk the tree token = token.substring(0, slashIndex); } // add root folder //entries.put("/", folderResource); } } } if (log.isTraceEnabled()) log.trace("The following packages were discovered in the bundle: " + containedPackages); return entries; }
From source file:com.civilizer.web.handler.ResourceHttpRequestHandler.java
/** * Validates the given path: returns {@code true} if the given path is not a valid resource path. * <p>The default implementation rejects paths containing "WEB-INF" or "META-INF" as well as paths * with relative paths ("../") that result in access of a parent directory. * @param path the path to validate//from w ww . jav a 2 s.c om * @return {@code true} if the path has been recognized as invalid, {@code false} otherwise */ protected boolean isInvalidPath(String path) { return (path.contains("WEB-INF") || path.contains("META-INF") || StringUtils.cleanPath(path).startsWith("..")); }