Example usage for org.springframework.util AntPathMatcher match

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

Introduction

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

Prototype

@Override
    public boolean match(String pattern, String path) 

Source Link

Usage

From source file:springfox.documentation.builders.PathSelectors.java

/**
 * Predicate that evaluates the supplied ant pattern
 *
 * @param antPattern - ant Pattern/*from  w w w .  j a  v  a2 s  .  c  om*/
 * @return predicate that matches a particular ant pattern
 */
public static Predicate<String> ant(final String antPattern) {
    return new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            AntPathMatcher matcher = new AntPathMatcher();
            return matcher.match(antPattern, input);
        }
    };
}

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 www  .  j a  v  a  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.microsoftopentechnologies.windowsazurestorage.WAStorageClient.java

/**
 * Determines whether the path is an exact match to any of the provided
 * patterns/*from  w  ww  .  j a va2  s . com*/
 *
 * @param path
 * @param patterns
 * @return
 */
private static boolean isExactMatch(String path, String[] patterns) {
    AntPathMatcher matcher = new AntPathMatcher();
    for (String pattern : patterns) {
        if (matcher.match(pattern, path)) {
            return true;
        }
    }
    return false;
}

From source file:reconf.server.services.security.SecurityAccessDecisionManager.java

@Override
public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
        throws AccessDeniedException, InsufficientAuthenticationException {
    if (!(object instanceof FilterInvocation)) {
        return;//from ww w  .j  av a 2  s . co  m
    }
    if (authentication == null) {
        return;
    }
    FilterInvocation filterInvocation = (FilterInvocation) object;

    String url = filterInvocation.getRequestUrl();
    if (url.endsWith("/")) {
        url = StringUtils.substringBeforeLast(url, "/");
    }

    AntPathMatcher antMatcher = new AntPathMatcher();
    if (antMatcher.match("/crud/product", url)) {
        if (userDetailsManager.userExists(authentication.getName())) {
            return;
        }
    }
    if (antMatcher.match("/crud/product/{product}", url)) {
        if (continueToProduct(authentication, antMatcher, "/crud/product/{product}", url)) {
            return;
        }
    }
    if (antMatcher.match("/crud/product/{product}/**", url)) {
        if (continueToProduct(authentication, antMatcher, "/crud/product/{product}/**", url)) {
            return;
        }
    }
    if (antMatcher.match("/crud/user", url)) {
        if (userDetailsManager.userExists(authentication.getName())) {
            return;
        }
    }
    if (antMatcher.match("/crud/user/**", url)) {
        if (ApplicationSecurity.isRoot(authentication)) {
            return;
        }
    }
    throw new AccessDeniedException("Forbidden");
}

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());
        }/*from w  w  w . j  a v  a2 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:com.apporiented.hermesftp.usermanager.model.PermissionData.java

/**
 * Fills the placeholders in the path template and checks if the passed path matches the
 * template.//  w w  w  .j  a  v a 2  s.co m
 *
 * @param checkPath The path to check.
 * @param ftproot The ftp root folder.
 * @param username The username.
 * @return True, if the path matches the configured pattern.
 * @throws FtpConfigException Error on reading or processing a configuration file.
 */
public boolean matches(String checkPath, String ftproot, String username) throws FtpConfigException {
    if (checkPath == null) {
        return false;
    }
    AntPathMatcher pathMatcher = new AntPathMatcher();
    String antPath = replacePlaceholders(ftproot, username);
    antPath = FilenameUtils.normalizeNoEndSeparator(antPath);
    antPath = FilenameUtils.separatorsToUnix(antPath);
    checkPath = FilenameUtils.normalizeNoEndSeparator(checkPath);
    checkPath = FilenameUtils.separatorsToUnix(checkPath);
    return pathMatcher.match(antPath, checkPath);

}

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

/**
 * Check if resource delta only contains static resources
 *//*from   w  w w . j a  va  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();
        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:org.eclipse.virgo.ide.runtime.internal.core.ServerPublishOperation.java

/**
 * Check if resource delta only contains static resources
 *///ww w. j av a2s .  co 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.att.ajsc.csilogging.util.UtilLib.java

public static String getInput(String pathinfoArr[], int arrLength, String componentType, String pathInfo) {
    Set<String> endpointSet = null;
    /*/*from   ww  w .j  a  v  a  2s.c  om*/
     * if (componentType.equalsIgnoreCase("rest")) { endpointSet =
     * DME2Helper.restletEndpointSet; } else { endpointSet =
     * DME2Helper.serviceEndpointSet; }
     */
    HashSet<String> setBasedArrLenth = new HashSet<String>();
    HashMap setBasedCharMap = new HashMap();
    HashSet<String> setBasedValues = new HashSet<String>();
    AntPathMatcher pathMatcher = new AntPathMatcher();

    String inputBasedonLength[];
    int globalvalue = 0;
    for (String s : endpointSet) {
        int dif = StringUtils.getLevenshteinDistance(pathInfo, s);

        if (globalvalue == 0 || globalvalue > dif) {
            globalvalue = dif;
            setBasedCharMap.put(globalvalue, s);
        }

        inputBasedonLength = s.split("\\/");
        int i = inputBasedonLength.length;
        if (arrLength == i) {
            setBasedArrLenth.add(s);
        }
    }

    String inputBasedOnValues[];
    for (String s1 : setBasedArrLenth) {
        inputBasedOnValues = s1.split("\\/");

        int j = 1;
        while (compareValues(pathinfoArr[j], inputBasedOnValues[j])) {
            j++;
            if (j >= arrLength) {
                break;
            }
        }
        if (j == arrLength) {
            setBasedValues.add(s1);
        }
    }
    String input = "";

    if (setBasedValues.size() == 1) {
        for (String s2 : setBasedValues) {
            input = s2;
        }
    } else {
        for (String s2 : setBasedValues) {
            if (pathMatcher.match(pathInfo, s2)) {
                input = s2;
            }
        }
    }
    if (input.isEmpty()) {
        input = (String) setBasedCharMap.get(globalvalue);

    }
    return "/" + componentType + input;
}

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

/**
 * {@inheritDoc}//from  w ww .  j a  v a  2 s . 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;
}