Search in sources :

Example 1 with HandlerMatchingMetadata

use of cn.taketoday.web.HandlerMatchingMetadata in project today-infrastructure by TAKETODAY.

the class RedirectViewUriTemplateTests method uriTemplateReuseCurrentRequestVars.

@Test
void uriTemplateReuseCurrentRequestVars() throws Exception {
    Map<String, Object> model = new HashMap<>();
    model.put("key1", "value1");
    model.put("name", "value2");
    model.put("key3", "value3");
    Map<String, String> currentRequestUriTemplateVars = new HashMap<>();
    currentRequestUriTemplateVars.put("var1", "v1");
    currentRequestUriTemplateVars.put("name", "v2");
    currentRequestUriTemplateVars.put("var3", "v3");
    HandlerMatchingMetadata metadata = new HandlerMatchingMetadata(context) {

        @Override
        public Map<String, String> getUriVariables() {
            return currentRequestUriTemplateVars;
        }
    };
    context.setMatchingMetadata(metadata);
    String url = "https://url.somewhere.com";
    RedirectView redirectView = new RedirectView(url + "/{key1}/{var1}/{name}");
    redirectView.renderMergedOutputModel(model, context);
    assertThat(this.response.getRedirectedUrl()).isEqualTo((url + "/value1/v1/value2?key3=value3"));
}
Also used : HashMap(java.util.HashMap) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 2 with HandlerMatchingMetadata

use of cn.taketoday.web.HandlerMatchingMetadata in project today-infrastructure by TAKETODAY.

the class ContentNegotiatingViewResolverTests method getMediaTypeAcceptHeaderWithProduces.

@Test
public void getMediaTypeAcceptHeaderWithProduces() throws Exception {
    HandlerMatchingMetadata matchingMetadata = new HandlerMatchingMetadata(requestContext);
    matchingMetadata.setProducibleMediaTypes(new MediaType[] { MediaType.APPLICATION_XHTML_XML });
    requestContext.setMatchingMetadata(matchingMetadata);
    request.addHeader("Accept", "text/html,application/xml;q=0.9,application/xhtml+xml,*/*;q=0.8");
    viewResolver.afterPropertiesSet();
    List<MediaType> result = viewResolver.getMediaTypes(requestContext);
    assertThat(result.get(0)).as("Invalid content type").isEqualTo(new MediaType("application", "xhtml+xml"));
}
Also used : MediaType(cn.taketoday.http.MediaType) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 3 with HandlerMatchingMetadata

use of cn.taketoday.web.HandlerMatchingMetadata in project today-infrastructure by TAKETODAY.

the class AbstractView method createMergedOutputModel.

/**
 * Creates a combined output Map (never {@code null}) that includes dynamic values and static attributes.
 * Dynamic values take precedence over static attributes.
 */
protected Map<String, Object> createMergedOutputModel(@Nullable Map<String, ?> model, RequestContext context) {
    // Consolidate static and dynamic model attributes.
    Map<String, Object> staticAttributes = getStaticAttributes();
    int size = model != null ? model.size() : 0;
    if (staticAttributes != null) {
        size += staticAttributes.size();
    }
    Map<String, String> pathVars = null;
    HandlerMatchingMetadata matchingMetadata = context.getMatchingMetadata();
    if (exposePathVariables && matchingMetadata != null) {
        pathVars = matchingMetadata.getUriVariables();
        size += pathVars.size();
    }
    Map<String, Object> mergedModel = CollectionUtils.newLinkedHashMap(size);
    if (CollectionUtils.isNotEmpty(staticAttributes)) {
        mergedModel.putAll(staticAttributes);
    }
    if (CollectionUtils.isNotEmpty(pathVars)) {
        mergedModel.putAll(pathVars);
    }
    if (CollectionUtils.isNotEmpty(model)) {
        mergedModel.putAll(model);
    }
    // Expose RequestContext?
    if (requestContextAttribute != null) {
        mergedModel.put(requestContextAttribute, context);
    }
    return mergedModel;
}
Also used : HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata)

Example 4 with HandlerMatchingMetadata

use of cn.taketoday.web.HandlerMatchingMetadata in project today-infrastructure by TAKETODAY.

the class ReactiveTypeHandlerTests method mediaTypes.

@Test
public void mediaTypes() throws Exception {
    // Media type from request
    this.servletRequest.addHeader("Accept", "text/event-stream");
    testSseResponse(true);
    // Media type from "produces" attribute
    Set<MediaType> types = Collections.singleton(MediaType.TEXT_EVENT_STREAM);
    HandlerMatchingMetadata matchingMetadata = new HandlerMatchingMetadata(webRequest);
    matchingMetadata.setProducibleMediaTypes(new MediaType[] { MediaType.TEXT_EVENT_STREAM });
    webRequest.setMatchingMetadata(matchingMetadata);
    testSseResponse(true);
    // No media type preferences
    testSseResponse(false);
}
Also used : MediaType(cn.taketoday.http.MediaType) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 5 with HandlerMatchingMetadata

use of cn.taketoday.web.HandlerMatchingMetadata in project today-infrastructure by TAKETODAY.

the class RequestMappingInfoHandlerMapping method handleMatch.

/**
 * Expose URI template variables, matrix variables, and producible media types in the request.
 */
@Override
protected void handleMatch(Match bestMatch, String lookupPath, RequestContext request) {
    super.handleMatch(bestMatch, lookupPath, request);
    RequestMappingInfo info = bestMatch.mapping;
    PathPatternsRequestCondition pathPatternsCondition = info.getPathPatternsCondition();
    HandlerMatchingMetadata matchingMetadata = new HandlerMatchingMetadata(bestMatch.getHandlerMethod(), lookupPath, request.getLookupPath(), CollectionUtils.firstElement(pathPatternsCondition.getPatterns()), getPatternParser());
    request.setMatchingMetadata(matchingMetadata);
    Set<MediaType> mediaTypes = info.getProducesCondition().getProducibleMediaTypes();
    if (!mediaTypes.isEmpty()) {
        matchingMetadata.setProducibleMediaTypes(mediaTypes.toArray(new MediaType[0]));
    }
}
Also used : PathPatternsRequestCondition(cn.taketoday.web.handler.condition.PathPatternsRequestCondition) MediaType(cn.taketoday.http.MediaType) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata)

Aggregations

HandlerMatchingMetadata (cn.taketoday.web.HandlerMatchingMetadata)19 Test (org.junit.jupiter.api.Test)12 HashMap (java.util.HashMap)7 MediaType (cn.taketoday.http.MediaType)6 RequestPath (cn.taketoday.http.server.RequestPath)4 RequestContext (cn.taketoday.web.RequestContext)4 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)4 PathPattern (cn.taketoday.web.util.pattern.PathPattern)4 PathPatternsRequestCondition (cn.taketoday.web.handler.condition.PathPatternsRequestCondition)2 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)2 MockServletContext (cn.taketoday.web.mock.MockServletContext)2 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)2 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 Map (java.util.Map)2 Properties (java.util.Properties)2 HttpOutputMessage (cn.taketoday.http.HttpOutputMessage)1