use of org.springframework.util.AntPathMatcher in project spring-framework by spring-projects.
the class PathPatternMatcherTests method extractUriTemplateVariables_spr15264.
@Test
public void extractUriTemplateVariables_spr15264() {
PathPattern pp = new PathPatternParser().parse("/{foo}");
assertTrue(pp.matches("/abc"));
assertFalse(pp.matches("/"));
assertFalse(pp.matches("//"));
checkCapture("/{foo}", "/abc", "foo", "abc");
pp = new PathPatternParser().parse("/{foo}/{bar}");
assertTrue(pp.matches("/abc/def"));
assertFalse(pp.matches("//def"));
assertFalse(pp.matches("//"));
pp = parse("/{foo}/boo");
assertTrue(pp.matches("/abc/boo"));
assertTrue(pp.matches("/a/boo"));
assertFalse(pp.matches("//boo"));
pp = parse("/{foo}*");
assertTrue(pp.matches("/abc"));
assertFalse(pp.matches("/"));
checkCapture("/{word:[a-z]*}", "/abc", "word", "abc");
pp = parse("/{word:[a-z]*}");
assertFalse(pp.matches("/1"));
assertTrue(pp.matches("/a"));
assertFalse(pp.matches("/"));
// Two captures mean we use a RegexPathElement
pp = new PathPatternParser().parse("/{foo}{bar}");
assertTrue(pp.matches("/abcdef"));
assertFalse(pp.matches("/"));
assertFalse(pp.matches("//"));
checkCapture("/{foo:[a-z][a-z]}{bar:[a-z]}", "/abc", "foo", "ab", "bar", "c");
// Only patterns not capturing variables cannot match against just /
pp = new PathPatternParser().parse("/****");
assertTrue(pp.matches("/abcdef"));
assertTrue(pp.matches("/"));
assertTrue(pp.matches("//"));
// Confirming AntPathMatcher behaviour:
assertFalse(new AntPathMatcher().match("/{foo}", "/"));
assertTrue(new AntPathMatcher().match("/{foo}", "/a"));
assertTrue(new AntPathMatcher().match("/{foo}{bar}", "/a"));
assertFalse(new AntPathMatcher().match("/{foo}*", "/"));
assertTrue(new AntPathMatcher().match("/*", "/"));
assertFalse(new AntPathMatcher().match("/*{foo}", "/"));
Map<String, String> vars = new AntPathMatcher().extractUriTemplateVariables("/{foo}{bar}", "/a");
assertEquals("a", vars.get("foo"));
assertEquals("", vars.get("bar"));
}
use of org.springframework.util.AntPathMatcher in project spring-framework by spring-projects.
the class InterceptorRegistryTests method getInterceptorsForPath.
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
PathMatcher pathMatcher = new AntPathMatcher();
List<HandlerInterceptor> result = new ArrayList<>();
for (Object interceptor : this.registry.getInterceptors()) {
if (interceptor instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
result.add(mappedInterceptor.getInterceptor());
}
} else if (interceptor instanceof HandlerInterceptor) {
result.add((HandlerInterceptor) interceptor);
} else {
fail("Unexpected interceptor type: " + interceptor.getClass().getName());
}
}
return result;
}
use of org.springframework.util.AntPathMatcher in project vorto by eclipse.
the class ModelGenerationController method getExtractPath.
private String getExtractPath(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();
return apm.extractPathWithinPattern(bestMatchPattern, path);
}
use of org.springframework.util.AntPathMatcher in project tuerauf-backend-java by dsteinkopf.
the class AppsecretChecker method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String urlPattern = request.getContextPath() + FrontendAPIRestController.FRONTEND_URL_PATTERN;
boolean isFrontendRequest = new AntPathMatcher().match(urlPattern, request.getRequestURI());
if (!isFrontendRequest) {
return true;
}
Assert.hasText(appsecret, "property tuerauf.appsecret is not configured.");
String appsecretParam = request.getParameter("appsecret");
if (!appsecret.equals(appsecretParam)) {
logger.info("URL param appsecret is missing or wrong: {}", appsecretParam);
throw new AuthenticationCredentialsNotFoundException("URL param appsecret is missing or wrong: " + appsecretParam);
}
return true;
}
use of org.springframework.util.AntPathMatcher in project spring-integration by spring-projects.
the class HttpRequestHandlingMessagingGatewayWithPathMappingTests method withoutPayloadExpressionPointingToUriVariables.
@SuppressWarnings("unchecked")
@Test
public void withoutPayloadExpressionPointingToUriVariables() throws Exception {
DirectChannel echoChannel = new DirectChannel();
echoChannel.subscribe(message -> {
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(message);
});
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setMethod("POST");
request.setContentType("text/plain");
request.setParameter("foo", "bar");
request.setContent("hello".getBytes());
String requestURI = "/fname/bill/lname/clinton";
// See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch
Map<String, String> uriTemplateVariables = new AntPathMatcher().extractUriTemplateVariables("/fname/{f}/lname/{l}", requestURI);
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
request.setRequestURI(requestURI);
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setBeanFactory(mock(BeanFactory.class));
RequestMapping requestMapping = new RequestMapping();
requestMapping.setPathPatterns("/fname/{f}/lname/{l}");
gateway.setRequestMapping(requestMapping);
gateway.setRequestChannel(echoChannel);
gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables"));
gateway.afterPropertiesSet();
gateway.start();
Object result = gateway.doHandleRequest(request, response);
assertThat(result, instanceOf(Message.class));
assertEquals("bill", ((Map<String, Object>) ((Message<?>) result).getPayload()).get("f"));
}
Aggregations