Search in sources :

Example 1 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-boot by spring-projects.

the class MetricsFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    StopWatch stopWatch = createStopWatchIfNecessary(request);
    String path = new UrlPathHelper().getPathWithinApplication(request);
    int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
    try {
        chain.doFilter(request, response);
        status = getStatus(response);
    } finally {
        if (!request.isAsyncStarted()) {
            if (response.isCommitted()) {
                status = getStatus(response);
            }
            stopWatch.stop();
            request.removeAttribute(ATTRIBUTE_STOP_WATCH);
            recordMetrics(request, path, status, stopWatch.getTotalTimeMillis());
        }
    }
}
Also used : UrlPathHelper(org.springframework.web.util.UrlPathHelper) StopWatch(org.springframework.util.StopWatch)

Example 2 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class ResourceUrlProvider method getLookupPathIndex.

private int getLookupPathIndex(HttpServletRequest request) {
    UrlPathHelper pathHelper = getUrlPathHelper();
    String requestUri = pathHelper.getRequestUri(request);
    String lookupPath = pathHelper.getLookupPathForRequest(request);
    return requestUri.indexOf(lookupPath);
}
Also used : UrlPathHelper(org.springframework.web.util.UrlPathHelper)

Example 3 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class RequestContext method initContext.

/**
	 * Initialize this context with the given request, using the given model attributes for Errors retrieval.
	 * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
	 * locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
	 * @param request current HTTP request
	 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
	 * fallback to root WebApplicationContext)
	 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
	 * for Errors retrieval)
	 * @see #getFallbackLocale
	 * @see #getFallbackTheme
	 * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
	 * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
	 */
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, Map<String, Object> model) {
    this.request = request;
    this.response = response;
    this.model = model;
    // Fetch WebApplicationContext, either from DispatcherServlet or the root context.
    // ServletContext needs to be specified to be able to fall back to the root context!
    this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (this.webApplicationContext == null) {
        this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
        if (this.webApplicationContext == null) {
            throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " + "request and no ContextLoaderListener registered?");
        }
    }
    // Determine locale to use for this RequestContext.
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    if (localeResolver instanceof LocaleContextResolver) {
        LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
        this.locale = localeContext.getLocale();
        if (localeContext instanceof TimeZoneAwareLocaleContext) {
            this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
        }
    } else if (localeResolver != null) {
        // Try LocaleResolver (we're within a DispatcherServlet request).
        this.locale = localeResolver.resolveLocale(request);
    }
    // Try JSTL fallbacks if necessary.
    if (this.locale == null) {
        this.locale = getFallbackLocale();
    }
    if (this.timeZone == null) {
        this.timeZone = getFallbackTimeZone();
    }
    // Determine default HTML escape setting from the "defaultHtmlEscape"
    // context-param in web.xml, if any.
    this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
    // Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
    // context-param in web.xml, if any.
    this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
    this.urlPathHelper = new UrlPathHelper();
    if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
        this.requestDataValueProcessor = this.webApplicationContext.getBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
    }
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) UrlPathHelper(org.springframework.web.util.UrlPathHelper) LocaleContextResolver(org.springframework.web.servlet.LocaleContextResolver)

Example 4 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class DelegatingWebMvcConfigurationTests method configurePathMatch.

@Test
public void configurePathMatch() throws Exception {
    final PathMatcher pathMatcher = mock(PathMatcher.class);
    final UrlPathHelper pathHelper = mock(UrlPathHelper.class);
    List<WebMvcConfigurer> configurers = new ArrayList<>();
    configurers.add(new WebMvcConfigurerAdapter() {

        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            configurer.setUseRegisteredSuffixPatternMatch(true).setUseTrailingSlashMatch(false).setUrlPathHelper(pathHelper).setPathMatcher(pathMatcher);
        }
    });
    delegatingConfig.setConfigurers(configurers);
    RequestMappingHandlerMapping handlerMapping = delegatingConfig.requestMappingHandlerMapping();
    assertNotNull(handlerMapping);
    assertEquals("PathMatchConfigurer should configure RegisteredSuffixPatternMatch", true, handlerMapping.useRegisteredSuffixPatternMatch());
    assertEquals("PathMatchConfigurer should configure SuffixPatternMatch", true, handlerMapping.useSuffixPatternMatch());
    assertEquals("PathMatchConfigurer should configure TrailingSlashMatch", false, handlerMapping.useTrailingSlashMatch());
    assertEquals("PathMatchConfigurer should configure UrlPathHelper", pathHelper, handlerMapping.getUrlPathHelper());
    assertEquals("PathMatchConfigurer should configure PathMatcher", pathMatcher, handlerMapping.getPathMatcher());
}
Also used : PathMatcher(org.springframework.util.PathMatcher) ArrayList(java.util.ArrayList) UrlPathHelper(org.springframework.web.util.UrlPathHelper) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Test(org.junit.Test)

Example 5 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method handleMatchMatrixVariablesDecoding.

@Test
public void handleMatchMatrixVariablesDecoding() {
    MockHttpServletRequest request;
    UrlPathHelper urlPathHelper = new UrlPathHelper();
    urlPathHelper.setUrlDecode(false);
    urlPathHelper.setRemoveSemicolonContent(false);
    this.handlerMapping.setUrlPathHelper(urlPathHelper);
    request = new MockHttpServletRequest();
    handleMatch(request, "/path{filter}", "/path;mvar=a%2fb");
    MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "filter");
    Map<String, String> uriVariables = getUriTemplateVariables(request);
    assertNotNull(matrixVariables);
    assertEquals(Collections.singletonList("a/b"), matrixVariables.get("mvar"));
    assertEquals(";mvar=a/b", uriVariables.get("filter"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Test(org.junit.Test)

Aggregations

UrlPathHelper (org.springframework.web.util.UrlPathHelper)32 Test (org.junit.jupiter.api.Test)10 PathMatcher (org.springframework.util.PathMatcher)5 Map (java.util.Map)4 Test (org.junit.Test)4 MultiValueMap (org.springframework.util.MultiValueMap)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)3 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)3 Collections (java.util.Collections)2 List (java.util.List)2 BiConsumer (java.util.function.BiConsumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 BDDMockito.given (org.mockito.BDDMockito.given)2