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"));
}
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"));
}
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;
}
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);
}
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]));
}
}
Aggregations