Example usage for org.springframework.util ResourceUtils FILE_URL_PREFIX

List of usage examples for org.springframework.util ResourceUtils FILE_URL_PREFIX

Introduction

In this page you can find the example usage for org.springframework.util ResourceUtils FILE_URL_PREFIX.

Prototype

String FILE_URL_PREFIX

To view the source code for org.springframework.util ResourceUtils FILE_URL_PREFIX.

Click Source Link

Document

URL prefix for loading from the file system: "file:".

Usage

From source file:com.gzj.tulip.load.vfs.JarFileObject.java

JarFileObject(FileSystemManager fs, URL url) throws FileNotFoundException, IOException {
    this.fs = fs;
    String urlString = url.toString();
    String entryName = urlString.substring(
            urlString.indexOf(ResourceUtils.JAR_URL_SEPARATOR) + ResourceUtils.JAR_URL_SEPARATOR.length());
    if (entryName.length() == 0) {
        this.root = this;
        int beginIndex = urlString.indexOf(ResourceUtils.FILE_URL_PREFIX)
                + ResourceUtils.FILE_URL_PREFIX.length();
        int endIndex = urlString.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
        this.jarFile = new JarFile(urlString.substring(beginIndex, endIndex));
    } else {/*from w ww . j ava2  s  .c om*/
        this.root = (JarFileObject) fs.resolveFile(urlString.substring(//
                0,
                urlString.indexOf(ResourceUtils.JAR_URL_SEPARATOR) + ResourceUtils.JAR_URL_SEPARATOR.length()));
        this.jarFile = root.jarFile;
    }
    this.entry = jarFile.getJarEntry(entryName);
    this.url = url;
    this.urlString = urlString;
    int indexSep = entryName.lastIndexOf('/');
    if (indexSep == -1) {
        this.fileName = new FileNameImpl(this, entryName);
    } else {
        if (entryName.endsWith("/")) {
            int index = entryName.lastIndexOf('/', entryName.length() - 2);
            this.fileName = new FileNameImpl(this, entryName.substring(index + 1, indexSep));
        } else {
            this.fileName = new FileNameImpl(this, entryName.substring(indexSep + 1));
        }
    }
}

From source file:com.github.jknack.handlebars.springmvc.SpringTemplateLoader.java

@Override
public String resolve(final String location) {
    String protocol = null;/*from   w  w  w  .  j a v  a2 s . c om*/
    if (location.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
        protocol = ResourceUtils.CLASSPATH_URL_PREFIX;
    } else if (location.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
        protocol = ResourceUtils.FILE_URL_PREFIX;
    }
    if (protocol == null) {
        return super.resolve(location);
    }
    return protocol + super.resolve(location.substring(protocol.length()));
}

From source file:spring.osgi.io.OsgiBundleResource.java

/**
 * Returns a <code>File</code> handle for this resource. This method does
 * a best-effort attempt to locate the bundle resource on the file system.
 * It is strongly recommended to use {@link #getInputStream()} method
 * instead which works no matter if the bundles are saved (in exploded form
 * or not) on the file system.//from w  w w . java  2 s . c om
 *
 * @return File handle to this resource
 * @throws java.io.IOException if the resource cannot be resolved as absolute file
 *                             path, i.e. if the resource is not available in a file system
 */
public File getFile() throws IOException {
    // locate the file inside the bundle only known prefixes
    if (searchType != OsgiResourceUtils.PREFIX_TYPE_UNKNOWN) {
        String bundleLocation = bundle.getLocation();
        int prefixIndex = bundleLocation.indexOf(ResourceUtils.FILE_URL_PREFIX);
        if (prefixIndex > -1) {
            bundleLocation = bundleLocation.substring(prefixIndex + ResourceUtils.FILE_URL_PREFIX.length());
        }
        File file = new File(bundleLocation, path);
        if (file.exists()) {
            return file;
        }
        // fall back to the URL discovery (just in case)
    }

    try {
        return ResourceUtils.getFile(getURI(), getDescription());
    } catch (IOException ioe) {
        throw (IOException) new FileNotFoundException(
                getDescription() + " cannot be resolved to absolute file path").initCause(ioe);
    }
}

From source file:org.activiti.rest.osgi.OsgiClassPathStore.java

private String toDocumentPath(final String resourcePath) {
    String documentPath = null;//from  w w  w  .  ja  v  a2 s  .com

    // check if this is a valid url (either a java URL or a Spring classpath prefix URL)
    try {
        final URL url = ResourceUtils.getURL(resourcePath);

        String urlString = resourcePath;

        // if the URL is a JAR url, trim off the reference to the JAR
        if (isJarURL(url)) {
            // find the URL to the jar file and split off the prefix portion that references the jar file
            String jarUrlString = extractJarFileURL(url).toExternalForm();

            final int x = urlString.indexOf(jarUrlString);
            if (x != -1) {
                urlString = urlString.substring(x + jarUrlString.length());

                // remove a prefix ! if it is found
                if (urlString.charAt(0) == '!') {
                    urlString = urlString.substring(1);
                }

                // remove a prefix / if it is found
                if (urlString.charAt(0) == '/') {
                    urlString = urlString.substring(1);
                }
            }
        }

        // if the url string starts with the classpath: prefix, remove it
        if (urlString.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
            urlString = urlString.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length());
        }

        // if the url string starts with the file: prefix, remove the storeDir path
        // this also remove the base path
        if (urlString.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
            if (storeDirs == null) {
                throw new WebScriptException("Unable to resolve a file: resource without a storeDir.");
            }
            for (int i = 0; i < this.storeDirs.length; i++) {
                if (urlString.startsWith(this.storeDirs[i])) {
                    urlString = urlString.substring(this.storeDirs[i].length());
                    break;
                }
            }
        }
        // handle the JBoss app-server virtual filesystem prefix
        else if (urlString.startsWith(VFSFILE_URL_PREFIX)) {
            if (storeDirs == null) {
                throw new WebScriptException("Unable to resolve a vfsfile: resource without a storeDir.");
            }
            for (int i = 0; i < this.storeDirs.length; i++) {
                if (urlString.startsWith(this.storeDirs[i])) {
                    urlString = urlString.substring(this.storeDirs[i].length() + 3); // to account for "vfs" prefix
                    break;
                }
            }
        } else {
            // now remove the class path store base path
            if (classPath != null && classPath.length() != 0) {
                // the url string should always contain the class path
                int idx = urlString.indexOf(classPath);
                if (idx >= 0) {
                    urlString = urlString.substring(idx + classPath.length());
                }

                // remove extra / at the front if found
                if (urlString.charAt(0) == '/') {
                    urlString = urlString.substring(1);
                }
            }
        }

        // what remains is the document path
        documentPath = urlString;
    } catch (FileNotFoundException fnfe) {
        if (logger.isWarnEnabled())
            logger.warn("Unable to determine document path for resource: " + resourcePath + " with base path "
                    + classPath, fnfe);
    } catch (MalformedURLException mue) {
        if (logger.isWarnEnabled())
            logger.warn("Unable to determine document path for resource: " + resourcePath + " with base path "
                    + classPath, mue);
    }

    return documentPath;
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Determine jar file references from the "java.class.path." manifest property and add them
 * to the given set of resources in the form of pointers to the root of the jar file content.
 * @param result the set of resources to add jar roots to
 * @since 4.3//  ww  w .  ja v a2 s  . c  om
 */
protected void addClassPathManifestEntries(Set<Resource> result) {
    try {
        String javaClassPathProperty = System.getProperty("java.class.path");
        for (String path : StringUtils.delimitedListToStringArray(javaClassPathProperty,
                System.getProperty("path.separator"))) {
            try {
                String filePath = new File(path).getAbsolutePath();
                int prefixIndex = filePath.indexOf(':');
                if (prefixIndex == 1) {
                    // Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection
                    filePath = StringUtils.capitalize(filePath);
                }
                UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX
                        + ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
                // Potentially overlapping with URLClassLoader.getURLs() result above!
                if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) {
                    result.add(jarResource);
                }
            } catch (MalformedURLException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Cannot search for matching files underneath [" + path
                            + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                }
            }
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to evaluate 'java.class.path' manifest entries: " + ex);
        }
    }
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Check whether the given file path has a duplicate but differently structured entry
 * in the existing result, i.e. with or without a leading slash.
 * @param filePath the file path (with or without a leading slash)
 * @param result the current result/*from  w  ww  .  j  a  v  a 2  s . co  m*/
 * @return {@code true} if there is a duplicate (i.e. to ignore the given file path),
 * {@code false} to proceed with adding a corresponding resource to the current result
 */
private boolean hasDuplicate(String filePath, Set<Resource> result) {
    if (result.isEmpty()) {
        return false;
    }
    String duplicatePath = (filePath.startsWith("/") ? filePath.substring(1) : "/" + filePath);
    try {
        return result.contains(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX
                + duplicatePath + ResourceUtils.JAR_URL_SEPARATOR));
    } catch (MalformedURLException ex) {
        // Ignore: just for testing against duplicate.
        return false;
    }
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Resolve the given jar file URL into a JarFile object.
 *//*from   www  . j  ava 2 s  .  c om*/
protected JarFile getJarFile(String jarFileUrl) throws IOException {
    if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
        try {
            return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart());
        } catch (URISyntaxException ex) {
            // Fallback for URLs that are not valid URIs (should hardly ever happen).
            return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length()));
        }
    } else {
        return new JarFile(jarFileUrl);
    }
}

From source file:org.springframework.extensions.config.source.UrlConfigSource.java

private URL extractJarFileURL(final URL jarUrl) {
    String urlFile = jarUrl.getFile();
    int separatorIndex = urlFile.indexOf(JarConfigSource.JAR_PATH_SEPARATOR);
    if (separatorIndex == -1) {
        // support for JBoss VFS filesystem JAR files URLs
        urlFile = jarUrl.toString();/*from  ww  w.ja  va 2s .  c  om*/
        separatorIndex = urlFile.indexOf(VFSJAR_URL_SEPARATOR);
        if (separatorIndex != -1) {
            // offset index to account for .jar suffix
            separatorIndex += 4;
        }
    }
    if (separatorIndex != -1) {
        String jarFile = urlFile.substring(0, separatorIndex);
        try {
            return new URL(jarFile);
        } catch (MalformedURLException ex) {
            // BEA WebLogic Server 12 fix, we know that there is jar separator and zip protocol
            if (jarUrl.getProtocol().equals(ResourceUtils.URL_PROTOCOL_ZIP)) {
                try {
                    return new URL(ResourceUtils.FILE_URL_PREFIX + jarFile);
                } catch (MalformedURLException mue) {
                    return jarUrl;
                }
            } else {
                return jarUrl;
            }
        }
    } else {
        return jarUrl;
    }
}

From source file:org.springframework.extensions.webscripts.ClassPathStore.java

/**
 * Converts a resource path back to a document path.
 * //w ww.j  a  v  a2 s  .co m
 * A document path is relative to the base path of the store.  It is what users of the store pass in when
 * they call the methods of the store.
 * 
 * A resource path includes the base path and is descriptive of the resource relative to the root of the
 * resource tree.
 * 
 * @param resourcePath String
 * 
 * @return document path
 */
private String toDocumentPath(final String resourcePath) {
    String documentPath = null;

    // check if this is a valid url (either a java URL or a Spring classpath prefix URL)
    try {
        final URL url = ResourceUtils.getURL(resourcePath);

        String urlString = resourcePath;

        // if the URL is a JAR url, trim off the reference to the JAR
        if (isJarURL(url)) {
            // find the URL to the jar file and split off the prefix portion that references the jar file
            String jarUrlString = extractJarFileURL(url).toExternalForm();

            final int x = urlString.indexOf(jarUrlString);
            if (x != -1) {
                urlString = urlString.substring(x + jarUrlString.length());

                // remove a prefix ! if it is found
                if (urlString.charAt(0) == '!') {
                    urlString = urlString.substring(1);
                }

                // remove a prefix / if it is found
                if (urlString.charAt(0) == '/') {
                    urlString = urlString.substring(1);
                }
            }
        }

        // if the url string starts with the classpath: prefix, remove it
        if (urlString.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
            urlString = urlString.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length());
        }

        // if the url string starts with the file: prefix, remove the storeDir path
        // this also remove the base path
        if (urlString.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
            if (storeDirs == null) {
                throw new WebScriptException("Unable to resolve a file: resource without a storeDir.");
            }
            for (int i = 0; i < this.storeDirs.length; i++) {
                if (urlString.startsWith(this.storeDirs[i])) {
                    urlString = urlString.substring(this.storeDirs[i].length());
                    break;
                }
            }
        }
        // handle the JBoss app-server virtual filesystem prefix
        else if (urlString.startsWith(VFSFILE_URL_PREFIX)) {
            if (storeDirs == null) {
                throw new WebScriptException("Unable to resolve a vfsfile: resource without a storeDir.");
            }
            for (int i = 0; i < this.storeDirs.length; i++) {
                // handle VFS files in expanded WARs
                if (urlString.startsWith(this.storeDirs[i], 3)) {
                    urlString = urlString.substring(this.storeDirs[i].length() + 3); // to account for "vfs" prefix
                    break;
                }
                // handle VFS files in other classpath dirs
                else if (urlString.startsWith(this.storeDirs[i])) {
                    urlString = urlString.substring(this.storeDirs[i].length());
                    break;
                }
            }
        } else {
            // now remove the class path store base path
            if (classPath != null && classPath.length() != 0) {
                // the url string should always start with the class path
                if (urlString.startsWith(classPath)) {
                    urlString = urlString.substring(classPath.length());
                }
            }
        }

        // remove extra / at the front if found
        if (urlString.charAt(0) == '/') {
            urlString = urlString.substring(1);
        }

        // what remains is the document path
        documentPath = urlString;
    } catch (FileNotFoundException fnfe) {
        if (logger.isWarnEnabled())
            logger.warn("Unable to determine document path for resource: " + resourcePath + " with base path "
                    + classPath, fnfe);
    } catch (MalformedURLException mue) {
        if (logger.isWarnEnabled())
            logger.warn("Unable to determine document path for resource: " + resourcePath + " with base path "
                    + classPath, mue);
    }

    return documentPath;
}

From source file:org.springframework.web.context.support.ServletContextResourcePatternResolver.java

/**
 * Extract entries from the given jar by pattern.
 * @param jarFilePath the path to the jar file
 * @param entryPattern the pattern for jar entries to match
 * @param result the Set of matching Resources to add to
 *//*from  w  w  w.  j a  v a 2  s.  c  om*/
private void doRetrieveMatchingJarEntries(String jarFilePath, String entryPattern, Set<Resource> result) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching jar file [" + jarFilePath + "] for entries matching [" + entryPattern + "]");
    }
    try {
        JarFile jarFile = new JarFile(jarFilePath);
        try {
            for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
                JarEntry entry = entries.nextElement();
                String entryPath = entry.getName();
                if (getPathMatcher().match(entryPattern, entryPath)) {
                    result.add(new UrlResource(ResourceUtils.URL_PROTOCOL_JAR, ResourceUtils.FILE_URL_PREFIX
                            + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
                }
            }
        } finally {
            jarFile.close();
        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Cannot search for matching resources in jar file [" + jarFilePath
                    + "] because the jar cannot be opened through the file system", ex);
        }
    }
}