Search in sources :

Example 1 with AntPathMatcher

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"));
}
Also used : AntPathMatcher(org.springframework.util.AntPathMatcher) Test(org.junit.Test)

Example 2 with AntPathMatcher

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;
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) PathMatcher(org.springframework.util.PathMatcher) AntPathMatcher(org.springframework.util.AntPathMatcher) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ArrayList(java.util.ArrayList) AntPathMatcher(org.springframework.util.AntPathMatcher)

Example 3 with AntPathMatcher

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);
}
Also used : AntPathMatcher(org.springframework.util.AntPathMatcher)

Example 4 with AntPathMatcher

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;
}
Also used : AuthenticationCredentialsNotFoundException(org.springframework.security.authentication.AuthenticationCredentialsNotFoundException) AntPathMatcher(org.springframework.util.AntPathMatcher)

Example 5 with AntPathMatcher

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"));
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) AntPathMatcher(org.springframework.util.AntPathMatcher) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

AntPathMatcher (org.springframework.util.AntPathMatcher)35 Test (org.junit.jupiter.api.Test)13 SimpleRouteMatcher (org.springframework.util.SimpleRouteMatcher)11 Test (org.junit.Test)5 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)5 ByteArrayDecoder (org.springframework.core.codec.ByteArrayDecoder)4 ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 Message (org.springframework.messaging.Message)3 DefaultMetadataExtractor (org.springframework.messaging.rsocket.DefaultMetadataExtractor)3 RSocketStrategies (org.springframework.messaging.rsocket.RSocketStrategies)3 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 PathMatcher (org.springframework.util.PathMatcher)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)2