Example usage for org.springframework.util PathMatcher match

List of usage examples for org.springframework.util PathMatcher match

Introduction

In this page you can find the example usage for org.springframework.util PathMatcher match.

Prototype

boolean match(String pattern, String path);

Source Link

Document

Match the given path against the given pattern , according to this PathMatcher's matching strategy.

Usage

From source file:com.griddynamics.jagger.JaggerLauncher.java

private static List<String> discoverResources(URL directory, String[] includePatterns,
        String[] excludePatterns) {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
            new FileSystemResourceLoader());
    List<String> resourceNames = new ArrayList<String>();
    PathMatcher matcher = new AntPathMatcher();
    try {//from  w  w  w  .  j a v  a  2s. c  o  m
        for (String pattern : includePatterns) {
            Resource[] includeResources = resolver.getResources(directory.toString() + pattern);
            for (Resource resource : includeResources) {
                boolean isValid = true;
                for (String excludePattern : excludePatterns) {
                    if (matcher.match(excludePattern, resource.getFilename())) {
                        isValid = false;
                        break;
                    }
                }
                if (isValid) {
                    resourceNames.add(resource.getURI().toString());
                }
            }
        }
    } catch (IOException e) {
        throw new TechnicalException(e);
    }

    return resourceNames;
}

From source file:com.wavemaker.commons.util.IOUtils.java

/**
 * Copy from: file to file, directory to directory, file to directory.
 *
 * @param source File object representing a file or directory to copy from.
 * @param destination File object representing the target; can only represent a file if the source is a file.
 * @param includedPatterns the ant-style path pattern to be included. Null means that all resources are included.
 * @param excludedPatterns the ant-style path pattern to be excluded. Null means that no resources are excluded.
 * @throws IOException/*from w w  w . jav a2s .c  om*/
 */
public static void copy(File source, File destination, List<String> includedPatterns,
        List<String> excludedPatterns) throws IOException {

    if (!source.exists()) {
        throw new IOException("Can't copy from non-existent file: " + source.getAbsolutePath());
    } else {
        PathMatcher matcher = new AntPathMatcher();

        boolean skip = false;
        if (includedPatterns != null) {
            for (String pattern : includedPatterns) {
                if (matcher.match(pattern, source.getName())) {
                    break;
                }
            }
        }

        if (excludedPatterns != null) {
            for (String pattern : excludedPatterns) {
                if (matcher.match(pattern, source.getName())) {
                    skip = true;
                    break;
                }
            }
        }

        if (skip) {
            return;
        }
    }

    if (source.isDirectory()) {
        if (!destination.exists()) {
            FileUtils.forceMkdir(destination);
        }
        if (!destination.isDirectory()) {
            throw new IOException("Can't copy directory (" + source.getAbsolutePath() + ") to non-directory: "
                    + destination.getAbsolutePath());
        }

        File files[] = source.listFiles(new WMFileNameFilter());

        for (int i = 0; i < files.length; i++) {
            copy(files[i], new File(destination, files[i].getName()), includedPatterns, excludedPatterns);
        }
    } else if (source.isFile()) {
        if (destination.isDirectory()) {
            destination = new File(destination, source.getName());
        }

        InputStream in = new FileInputStream(source);
        OutputStream out = new FileOutputStream(destination);

        copy(in, out, true, true);
    } else {
        throw new IOException(
                "Don't know how to copy " + source.getAbsolutePath() + "; it's neither a directory nor a file");
    }
}

From source file:org.wallride.web.controller.guest.page.PageDescribeController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    BlogLanguage blogLanguage = (BlogLanguage) request
            .getAttribute(BlogLanguageMethodArgumentResolver.BLOG_LANGUAGE_ATTRIBUTE);
    if (blogLanguage == null) {
        Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
        blogLanguage = blog.getLanguage(blog.getDefaultLanguage());
    }/*from   w ww . ja v  a 2 s. c  om*/

    String path = urlPathHelper.getLookupPathForRequest(request);

    PathMatcher pathMatcher = new AntPathMatcher();
    if (!pathMatcher.match(PATH_PATTERN, path)) {
        throw new HttpNotFoundException();
    }

    Map<String, String> variables = pathMatcher.extractUriTemplateVariables(PATH_PATTERN, path);
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, variables);

    Page page = pageService.getPageByCode(variables.get("code"), blogLanguage.getLanguage());
    if (page == null) {
        page = pageService.getPageByCode(variables.get("code"), blogLanguage.getBlog().getDefaultLanguage());
    }
    if (page == null) {
        throw new HttpNotFoundException();
    }
    if (page.getStatus() != Post.Status.PUBLISHED) {
        throw new HttpNotFoundException();
    }

    return createModelAndView(page);
}

From source file:com.bstek.dorado.web.resolver.UriResolverMapping.java

@Override
protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {
    Map<String, Object> handlerMap = getHandlerMap();
    Object handler = handlerMap.get(urlPath);
    String bestPatternMatch = urlPath;
    String pathWithinMapping = urlPath;

    if (handler == null) {
        PathMatcher pathMatcher = getPathMatcher();
        for (Iterator<String> it = handlerMap.keySet().iterator(); it.hasNext();) {
            String registeredPath = it.next();
            if (pathMatcher.match(registeredPath, urlPath)) {
                request.setAttribute("originalUrlPath", urlPath);
                handler = handlerMap.get(registeredPath);
                bestPatternMatch = registeredPath;
                pathWithinMapping = pathMatcher.extractPathWithinPattern(bestPatternMatch, urlPath);
                break;
            }// ww  w  .  j a  v  a 2  s.co  m
        }
    }

    if (handler != null) {
        PathMatcher pathMatcher = getPathMatcher();
        Map<String, String> uriTemplateVariables = new LinkedHashMap<String, String>();
        uriTemplateVariables.putAll(pathMatcher.extractUriTemplateVariables(bestPatternMatch, urlPath));

        if (logger.isDebugEnabled()) {
            logger.debug("URI Template variables for request [" + urlPath + "] are " + uriTemplateVariables);
        }
        handler = buildPathExposingHandler(handler, bestPatternMatch, pathWithinMapping, uriTemplateVariables);
    }

    return handler;
}

From source file:org.devefx.httpmapper.spring.handler.MappedListener.java

/**
 * Returns {@code true} if the listener applies to the given request path.
 * @param lookupPath the current request path
 * @param pathMatcher a path matcher for path pattern matching
 *//*from  w ww .  j a  v  a 2s. c o  m*/
public boolean matches(String lookupPath, PathMatcher pathMatcher) {
    PathMatcher pathMatcherToUse = (this.pathMatcher != null) ? this.pathMatcher : pathMatcher;
    if (this.excludePatterns != null) {
        for (String pattern : this.excludePatterns) {
            if (pathMatcherToUse.match(pattern, lookupPath)) {
                return false;
            }
        }
    }
    if (this.includePatterns == null) {
        return true;
    } else {
        for (String pattern : this.includePatterns) {
            if (pathMatcherToUse.match(pattern, lookupPath)) {
                return true;
            }
        }
        return false;
    }
}

From source file:com.ziroom.common.filter.AuthenticationFilter.java

public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
        final FilterChain filterChain) throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;
    final HttpSession session = request.getSession(false);
    final Assertion assertion = session != null ? (Assertion) session.getAttribute(CONST_CAS_ASSERTION) : null;

    if (assertion != null) {
        filterChain.doFilter(request, response);
        return;/*from   w  w  w .  java2 s .  co m*/
    }

    //excludeFile filter
    String requestStr = request.getRequestURL().toString();
    this.log.debug("requestStr-->" + requestStr);
    PathMatcher matcher = new AntPathMatcher();
    if (excludePathsArray != null) {
        for (String excludePath : excludePathsArray) {
            boolean flag = matcher.match(excludePath, requestStr);
            if (!flag) {
                flag = requestStr.indexOf(excludePath) > 0;
            }
            if (flag) {
                this.log.debug("??" + excludePath);
                filterChain.doFilter(request, response);
                return;
            }
        }
    }

    final String serviceUrl = constructServiceUrl(request, response);
    final String ticket = CommonUtils.safeGetParameter(request, getArtifactParameterName());
    final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);

    if (CommonUtils.isNotBlank(ticket) || wasGatewayed) {
        filterChain.doFilter(request, response);
        return;
    }

    final String modifiedServiceUrl;

    log.debug("no ticket and no assertion found");
    if (this.gateway) {
        log.debug("setting gateway attribute in session");
        modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl);
    } else {
        modifiedServiceUrl = serviceUrl;
    }

    if (log.isDebugEnabled()) {
        log.debug("Constructed service url: " + modifiedServiceUrl);
    }

    final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl,
            getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);

    if (log.isDebugEnabled()) {
        log.debug("redirecting to \"" + urlToRedirectTo + "\"");
    }

    response.sendRedirect(urlToRedirectTo);
}

From source file:com.wavemaker.common.util.IOUtils.java

/**
 * Copy from: file to file, directory to directory, file to directory.
 * //from  www. j  av  a 2s.c  o  m
 * @param source File object representing a file or directory to copy from.
 * @param destination File object representing the target; can only represent a file if the source is a file.
 * @param includedPatterns the ant-style path pattern to be included. Null means that all resources are included.
 * @param excludedPatterns the ant-style path pattern to be excluded. Null means that no resources are excluded.
 * @throws IOException
 */
public static void copy(File source, File destination, List<String> includedPatterns,
        List<String> excludedPatterns) throws IOException {

    if (!source.exists()) {
        throw new IOException("Can't copy from non-existent file: " + source.getAbsolutePath());
    } else {
        PathMatcher matcher = new AntPathMatcher();

        boolean skip = false;
        if (includedPatterns != null) {
            for (String pattern : includedPatterns) {
                if (matcher.match(pattern, source.getName())) {
                    break;
                }
            }
        }

        if (excludedPatterns != null) {
            for (String pattern : excludedPatterns) {
                if (matcher.match(pattern, source.getName())) {
                    skip = true;
                    break;
                }
            }
        }

        if (skip) {
            return;
        }
    }

    if (source.isDirectory()) {
        if (!destination.exists()) {
            FileUtils.forceMkdir(destination);
        }
        if (!destination.isDirectory()) {
            throw new IOException("Can't copy directory (" + source.getAbsolutePath() + ") to non-directory: "
                    + destination.getAbsolutePath());
        }

        File files[] = source.listFiles(new java.io.FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.indexOf("#") == -1 && name.indexOf("~") == -1;
            }
        });

        for (int i = 0; i < files.length; i++) {
            copy(files[i], new File(destination, files[i].getName()), includedPatterns, excludedPatterns);
        }
    } else if (source.isFile()) {
        if (destination.isDirectory()) {
            destination = new File(destination, source.getName());
        }

        InputStream in = new FileInputStream(source);
        OutputStream out = new FileOutputStream(destination);

        copy(in, out);

        in.close();
        out.close();

    } else {
        throw new IOException(
                "Don't know how to copy " + source.getAbsolutePath() + "; it's neither a directory nor a file");
    }
}

From source file:org.eclipse.gemini.blueprint.io.OsgiBundleResourcePatternResolver.java

/**
 * Searches for the given pattern inside the imported bundle. This translates to pattern matching on the imported
 * packages.//from ww w .ja va 2s.c  om
 * 
 * @param importedBundle imported bundle
 * @param path path used for pattern matching
 * @param foundPaths collection of found results
 */
@SuppressWarnings("unchecked")
private void findImportedBundleMatchingResource(final ImportedBundle importedBundle, String rootPath,
        String path, final Collection<String> foundPaths) throws IOException {

    final boolean trace = logger.isTraceEnabled();

    String[] packages = importedBundle.getImportedPackages();

    if (trace)
        logger.trace("Searching path [" + path + "] on imported pkgs " + ObjectUtils.nullSafeToString(packages)
                + "...");

    final boolean startsWithSlash = rootPath.startsWith(FOLDER_SEPARATOR);

    for (int i = 0; i < packages.length; i++) {
        // transform the package name into a path
        String pkg = packages[i].replace(DOT, SLASH) + SLASH;

        if (startsWithSlash) {
            pkg = FOLDER_SEPARATOR + pkg;
        }

        final PathMatcher matcher = getPathMatcher();
        // if the imported package matches the path
        if (matcher.matchStart(path, pkg)) {
            Bundle bundle = importedBundle.getBundle();
            // 1. look at the Bundle jar root
            Enumeration<String> entries = bundle.getEntryPaths(pkg);
            while (entries != null && entries.hasMoreElements()) {
                String entry = entries.nextElement();
                if (startsWithSlash)
                    entry = FOLDER_SEPARATOR + entry;

                if (matcher.match(path, entry)) {
                    if (trace)
                        logger.trace("Found entry [" + entry + "]");
                    foundPaths.add(entry);
                }
            }
            // 2. Do a Bundle-Classpath lookup (since the jar might use a different classpath)
            Collection<String> cpMatchingPaths = findBundleClassPathMatchingPaths(bundle, path);
            foundPaths.addAll(cpMatchingPaths);
        }
    }
}

From source file:org.impalaframework.web.servlet.ResourceServlet.java

private boolean matchesCompressedMimeTypes(String mimeType) {
    PathMatcher pathMatcher = new AntPathMatcher();
    Iterator<String> compressedMimeTypesIt = compressedMimeTypes.iterator();
    while (compressedMimeTypesIt.hasNext()) {
        String compressedMimeType = compressedMimeTypesIt.next();
        if (pathMatcher.match(compressedMimeType, mimeType)) {
            return true;
        }/*from w  w w .j  a  v a2  s .  com*/
    }
    return false;
}

From source file:org.impalaframework.web.servlet.ResourceServlet.java

private boolean isAllowed(String resourcePath) {
    if (resourcePath.matches(protectedPath)) {
        return false;
    }/* w  w w. j  a v  a  2  s .co m*/
    PathMatcher pathMatcher = new AntPathMatcher();
    Iterator<String> allowedResourcePathsIt = allowedResourcePaths.iterator();
    while (allowedResourcePathsIt.hasNext()) {
        String pattern = allowedResourcePathsIt.next();
        if (pathMatcher.match(pattern, resourcePath)) {
            return true;
        }
    }
    return false;
}