use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class ReactorServerHttpRequest method initCookies.
@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
DefaultMultiValueMap<String, HttpCookie> cookies = MultiValueMap.fromLinkedHashMap();
for (Map.Entry<CharSequence, Set<Cookie>> entry : request.cookies().entrySet()) {
CharSequence name = entry.getKey();
for (Cookie cookie : entry.getValue()) {
HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value());
cookies.add(name.toString(), httpCookie);
}
}
return cookies;
}
use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class Jackson2ObjectMapperBuilder method configure.
/**
* Configure an existing {@link ObjectMapper} instance with this builder's
* settings. This can be applied to any number of {@code ObjectMappers}.
*
* @param objectMapper the ObjectMapper to configure
*/
public void configure(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
DefaultMultiValueMap<Object, Module> modulesToRegister = MultiValueMap.fromLinkedHashMap();
if (this.findModulesViaServiceLoader) {
for (Module module : ObjectMapper.findModules(this.moduleClassLoader)) {
registerModule(module, modulesToRegister);
}
} else if (this.findWellKnownModules) {
registerWellKnownModulesIfAvailable(modulesToRegister);
}
if (this.modules != null) {
for (Module module : modules) {
registerModule(module, modulesToRegister);
}
}
if (this.moduleClasses != null) {
for (Class<? extends Module> moduleClass : this.moduleClasses) {
registerModule(BeanUtils.newInstance(moduleClass), modulesToRegister);
}
}
List<Module> modules = new ArrayList<>();
for (List<Module> nestedModules : modulesToRegister.values()) {
modules.addAll(nestedModules);
}
objectMapper.registerModules(modules);
if (this.dateFormat != null) {
objectMapper.setDateFormat(this.dateFormat);
}
if (this.locale != null) {
objectMapper.setLocale(this.locale);
}
if (this.timeZone != null) {
objectMapper.setTimeZone(this.timeZone);
}
if (this.annotationIntrospector != null) {
objectMapper.setAnnotationIntrospector(this.annotationIntrospector);
}
if (this.propertyNamingStrategy != null) {
objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
}
if (this.defaultTyping != null) {
objectMapper.setDefaultTyping(this.defaultTyping);
}
if (this.serializationInclusion != null) {
objectMapper.setDefaultPropertyInclusion(this.serializationInclusion);
}
if (this.filters != null) {
objectMapper.setFilterProvider(this.filters);
}
this.mixIns.forEach(objectMapper::addMixIn);
if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
SimpleModule module = new SimpleModule();
addSerializers(module);
addDeserializers(module);
objectMapper.registerModule(module);
}
this.visibilities.forEach(objectMapper::setVisibility);
for (Map.Entry<Object, Boolean> entry : features.entrySet()) {
Object feature = entry.getKey();
Boolean enabled = entry.getValue();
configureFeature(objectMapper, feature, enabled);
}
customizeDefaultFeatures(objectMapper);
if (this.handlerInstantiator != null) {
objectMapper.setHandlerInstantiator(this.handlerInstantiator);
} else if (this.applicationContext != null) {
objectMapper.setHandlerInstantiator(new BeanFactoryHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
}
if (this.configurer != null) {
this.configurer.accept(objectMapper);
}
}
use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class MultipartBodyBuilderTests method builder.
@Test
public void builder() {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
MultiValueMap<String, String> multipartData = new DefaultMultiValueMap<>();
multipartData.add("form field", "form value");
builder.part("key", multipartData).header("foo", "bar");
Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
builder.part("logo", logo).header("baz", "qux");
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.add("foo", "bar");
HttpEntity<String> entity = new HttpEntity<>("body", entityHeaders);
builder.part("entity", entity).header("baz", "qux");
Publisher<String> publisher = Flux.just("foo", "bar", "baz");
builder.asyncPart("publisherClass", publisher, String.class).header("baz", "qux");
builder.asyncPart("publisherPtr", publisher, new TypeReference<String>() {
}).header("baz", "qux");
MultiValueMap<String, HttpEntity<?>> result = builder.build();
assertThat(result.size()).isEqualTo(5);
HttpEntity<?> resultEntity = result.getFirst("key");
assertThat(resultEntity).isNotNull();
assertThat(resultEntity.getBody()).isEqualTo(multipartData);
assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
resultEntity = result.getFirst("logo");
assertThat(resultEntity).isNotNull();
assertThat(resultEntity.getBody()).isEqualTo(logo);
assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
resultEntity = result.getFirst("entity");
assertThat(resultEntity).isNotNull();
assertThat(resultEntity.getBody()).isEqualTo("body");
assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
resultEntity = result.getFirst("publisherClass");
assertThat(resultEntity).isNotNull();
assertThat(resultEntity.getBody()).isEqualTo(publisher);
assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.fromClass(String.class));
assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
resultEntity = result.getFirst("publisherPtr");
assertThat(resultEntity).isNotNull();
assertThat(resultEntity.getBody()).isEqualTo(publisher);
assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.fromClass(String.class));
assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class AbstractDataBinderParameterResolver method resolveName.
@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext context) throws Exception {
final int parameterNameLength = name.length();
// prepare property values
final Map<String, String[]> parameters = context.getParameters();
final DefaultMultiValueMap<String, PropertyValue> propertyValues = new DefaultMultiValueMap<>();
for (final Map.Entry<String, String[]> entry : parameters.entrySet()) {
final String[] paramValues = entry.getValue();
if (ObjectUtils.isNotEmpty(paramValues)) {
final String requestParameterName = entry.getKey();
// users[key].userName=TODAY&users[key].age=20
if (requestParameterName.startsWith(name) && requestParameterName.charAt(parameterNameLength) == '[') {
// userList[0].name '.' 's index
final int separatorIndex = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(requestParameterName);
final String property = requestParameterName.substring(separatorIndex + 1);
final int closeKey = requestParameterName.indexOf(']');
final String key = requestParameterName.substring(parameterNameLength + 1, closeKey);
final PropertyValue propertyValue = new PropertyValue(property, paramValues[0]);
propertyValues.add(key, propertyValue);
}
}
}
return doBind(propertyValues, resolvable);
}
use of cn.taketoday.core.DefaultMultiValueMap in project today-framework by TAKETODAY.
the class FormHttpMessageConverterTests method writeMultipartCharset.
@Test
public void writeMultipartCharset() throws Exception {
MultiValueMap<String, Object> parts = new DefaultMultiValueMap<>();
Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
parts.add("logo", logo);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
MediaType contentType = outputMessage.getHeaders().getContentType();
Map<String, String> parameters = contentType.getParameters();
assertThat(parameters).containsOnlyKeys("boundary");
this.converter.setCharset(StandardCharsets.ISO_8859_1);
outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
parameters = outputMessage.getHeaders().getContentType().getParameters();
assertThat(parameters).containsOnlyKeys("boundary", "charset");
assertThat(parameters).containsEntry("charset", "ISO-8859-1");
}
Aggregations