Example usage for org.springframework.util AntPathMatcher AntPathMatcher

List of usage examples for org.springframework.util AntPathMatcher AntPathMatcher

Introduction

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

Prototype

public AntPathMatcher() 

Source Link

Document

Create a new instance with the #DEFAULT_PATH_SEPARATOR .

Usage

From source file:org.springjutsu.validation.util.RequestUtils.java

/**
 * Identifies the view name pattern which best matches
 * the current request URL (path within handler mapping).
 * @param candidateViewNames The view name patterns to test
 * @param controllerPaths Possible request mapping prefixes 
 * from a controller-level RequestMapping annotation 
 * @param request the current request//from   w  ww .ja va 2s  .  c  o  m
 * @return the best matching view name.
 */
public static String findFirstMatchingRestPath(String[] candidateViewNames, String[] controllerPaths,
        HttpServletRequest request) {

    String pathWithinHandlerMapping = removeLeadingAndTrailingSlashes(
            (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));

    AntPathMatcher antPathMatcher = new AntPathMatcher();
    for (String candidatePath : candidateViewNames) {
        if ((controllerPaths == null || candidatePath.startsWith("/")) && antPathMatcher
                .match(removeLeadingAndTrailingSlashes(candidatePath), pathWithinHandlerMapping)) {
            return candidatePath;
        } else if (controllerPaths != null) {
            for (String controllerPath : controllerPaths) {
                String testPath = (controllerPath + "/" + candidatePath).replace("//", "/");
                if (antPathMatcher.match(removeLeadingAndTrailingSlashes(testPath), pathWithinHandlerMapping)) {
                    return candidatePath;
                }
            }
        }
    }
    return null;
}

From source file:com.cloudbees.plugins.credentials.domains.PathSpecification.java

/**
 * {@inheritDoc}// www.j  a  va2s  .  c  o  m
 */
@NonNull
@Override
public Result test(@NonNull DomainRequirement requirement) {
    if (requirement instanceof PathRequirement) {
        final AntPathMatcher matcher = new AntPathMatcher();
        String path = ((PathRequirement) requirement).getPath();
        if (!caseSensitive) {
            path = path.toLowerCase();
        }
        if (includes != null) {
            boolean isInclude = false;
            for (String include : includes.split(",")) {
                include = Util.fixEmptyAndTrim(include);
                if (include == null) {
                    continue;
                }
                if (!caseSensitive) {
                    include = include.toLowerCase();
                }
                if (matcher.isPattern(include) ? matcher.match(include, path)
                        : StringUtils.equals(include, path)) {
                    isInclude = true;
                    break;
                }
            }
            if (!isInclude) {
                return Result.NEGATIVE;
            }
        }
        if (excludes != null) {
            boolean isExclude = false;
            for (String exclude : excludes.split(",")) {
                exclude = Util.fixEmptyAndTrim(exclude);
                if (exclude == null) {
                    continue;
                }
                if (!caseSensitive) {
                    exclude = exclude.toLowerCase();
                }
                if (matcher.isPattern(exclude) ? matcher.match(exclude, path)
                        : StringUtils.equals(exclude, path)) {
                    isExclude = true;
                    break;
                }
            }
            if (isExclude) {
                return Result.NEGATIVE;
            }
        }
        return Result.PARTIAL;
    }
    return Result.UNKNOWN;
}

From source file:com.bennavetta.appsite.webapi.ContentController.java

/**
 * Given a request that was matched against a @RequestMapping method, extract a trailing path from it.
 * For example, given a mapping of '/foo/**' and a request of '/foo/bar/baz', this would return 'bar/baz'.
 * @param request the matched request/*from   w  w  w  . j  a v  a 2 s. com*/
 * @return the final path component
 * @see http://stackoverflow.com/questions/3686808/spring-3-requestmapping-get-path-value
 */
public static String extractPathFromPattern(HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    return finalPath;
}

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;// w w  w .  ja  v a2s  .  com
    }

    //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:org.wallride.web.controller.guest.article.ArticleIndexController.java

private String extractPathFromPattern(final HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    return finalPath;
}

From source file:com.codenvy.cas.adaptors.ldap.services.DefaultLdapRegisteredServiceMapper.java

/**
 * Gets the registered service by id that would either match an ant or regex pattern.
 *
 * @param id the id/* w  ww .j  a v a2  s .  c  om*/
 * @return the registered service
 */
private AbstractRegisteredService getRegisteredService(@NotNull final String id) {
    if (isValidRegexPattern(id)) {
        return new RegexRegisteredService();
    }

    if (new AntPathMatcher().isPattern(id)) {
        return new RegisteredServiceImpl();
    }
    return null;
}

From source file:org.springmodules.validation.bean.conf.namespace.XmlBasedValidatorBeanDefinitionParser.java

protected List createResources(Element resourcesDefinition) {
    String dirName = resourcesDefinition.getAttribute(DIR_ATTR);
    final String pattern = resourcesDefinition.getAttribute(PATTERN_ATTR);
    final AntPathMatcher matcher = new AntPathMatcher();
    FileFilter filter = new FileFilter() {
        public boolean accept(File file) {
            return matcher.match(pattern, file.getName());
        }/*ww w . j a v  a 2  s. c o m*/
    };
    List resources = new ArrayList();
    for (Iterator files = new FileIterator(dirName, filter); files.hasNext();) {
        File file = (File) files.next();
        resources.add(new FileSystemResource(file));
    }
    return resources;
}

From source file:org.eclipse.virgo.ide.runtime.internal.core.ServerPublishOperation.java

/**
 * Check if resource delta only contains static resources
 *///  w  w w  .  jav a 2  s.c  o m
private boolean onlyStaticResources(IModuleResourceDelta delta, Set<IModuleFile> files) {
    if (delta.getModuleResource() instanceof IModuleFolder) {
        for (IModuleResourceDelta child : delta.getAffectedChildren()) {
            if (!onlyStaticResources(child, files)) {
                return false;
            }
        }
        return true;
    } else {
        if (delta.getModuleResource() instanceof IModuleFile) {
            files.add((IModuleFile) delta.getModuleResource());
        }
        String name = delta.getModuleResource().getName();

        // make that configurable
        if (name.endsWith(".xml")) {
            IFile file = (IFile) delta.getModuleResource().getAdapter(IFile.class);
            // check for spring context xml files first but exclude
            if (!checkIfSpringConfigurationFile(file)) {
                return false;
            }
        }
        boolean isStatic = false;
        // Check the configuration options for static resources
        AntPathMatcher matcher = new AntPathMatcher();
        for (String pattern : StringUtils
                .delimitedListToStringArray(ServerUtils.getServer(server).getStaticFilenamePatterns(), ",")) {
            if (pattern.startsWith("!") && matcher.match(pattern.substring(1), name)) {
                isStatic = false;
            } else if (matcher.match(pattern, name)) {
                isStatic = true;
            }
        }
        return isStatic;
    }
}

From source file:com.vmware.vfabric.ide.eclipse.tcserver.internal.core.TcPublisher.java

/**
 * Check if resource delta only contains static resources
 *///ww  w  .ja  v a  2  s.c om
private boolean onlyStaticResources(IModuleResourceDelta delta, Set<IModuleFile> files) {
    if (delta.getModuleResource() instanceof IModuleFolder) {
        for (IModuleResourceDelta child : delta.getAffectedChildren()) {
            if (!onlyStaticResources(child, files)) {
                return false;
            }
        }
        return true;
    } else {
        if (delta.getModuleResource() instanceof IModuleFile) {
            files.add((IModuleFile) delta.getModuleResource());
        }
        String name = delta.getModuleResource().getName();

        // make that configurable
        if (name.endsWith(".xml")) {
            IFile file = (IFile) delta.getModuleResource().getAdapter(IFile.class);
            // check for spring context xml files first but exclude
            if (!checkIfSpringConfigurationFile(file)) {
                return false;
            }
        }
        boolean isStatic = false;
        // Check the configuration options for static resources
        AntPathMatcher matcher = new AntPathMatcher();
        TcServer tcServer = (TcServer) server.getServer().loadAdapter(TcServer.class, null);
        for (String pattern : StringUtils.splitByWholeSeparator(tcServer.getStaticFilenamePatterns(), ",")) {
            if (pattern.startsWith("!") && matcher.match(pattern.substring(1), name)) {
                isStatic = false;
            } else if (matcher.match(pattern, name)) {
                isStatic = true;
            }
        }
        return isStatic;
    }
}

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  ww  w.j a  v  a2  s. com
 */
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");
    }
}