use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class MapToMapConverterTests method multiValueMapToMultiValueMap.
@Test
@SuppressWarnings("unchecked")
void multiValueMapToMultiValueMap() throws Exception {
DefaultConversionService.addDefaultConverters(conversionService);
MultiValueMap<String, Integer> source = MultiValueMap.fromLinkedHashMap();
source.put("a", Arrays.asList(1, 2, 3));
source.put("b", Arrays.asList(4, 5, 6));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size()).isEqualTo(2);
assertThat(converted.get("a")).isEqualTo(Arrays.asList("1", "2", "3"));
assertThat(converted.get("b")).isEqualTo(Arrays.asList("4", "5", "6"));
}
use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class TomcatServletWebServerFactoryTests method nonExistentUploadDirectoryIsCreatedUponMultipartUpload.
@Test
void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() throws IOException, URISyntaxException {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
factory.addInitializers((servletContext) -> {
servletContextReference.set(servletContext);
Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getParts();
}
});
servlet.addMapping("/upload");
servlet.setMultipartConfig(new MultipartConfigElement((String) null));
});
this.webServer = factory.getWebServer();
this.webServer.start();
File temp = (File) servletContextReference.get().getAttribute(ServletContext.TEMPDIR);
FileSystemUtils.deleteRecursively(temp);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = HttpHeaders.create();
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class TodayStrategies method loadStrategies.
private static MultiValueMap<String, String> loadStrategies(ClassLoader classLoader) {
MultiValueMap<String, String> strategies = strategiesCache.get(classLoader);
if (strategies != null) {
return strategies;
}
synchronized (strategiesCache) {
strategies = strategiesCache.get(classLoader);
if (strategies == null) {
log.debug("Detecting strategies location '{}'", STRATEGIES_LOCATION);
strategies = MultiValueMap.fromLinkedHashMap();
try {
Enumeration<URL> urls = classLoader.getResources(STRATEGIES_LOCATION);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = new Properties();
try (InputStream inputStream = url.openStream()) {
properties.load(inputStream);
}
log.debug("Reading strategies file '{}'", url);
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key != null && value != null) {
String strategyKey = key.toString();
// split as string list
List<String> strategyValues = StringUtils.splitAsList(value.toString());
for (String strategyValue : strategyValues) {
// trim whitespace
strategyValue = strategyValue.trim();
if (StringUtils.isNotEmpty(strategyValue)) {
strategies.add(strategyKey, strategyValue);
}
}
}
}
}
strategiesCache.put(classLoader, strategies);
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to load strategies from location [" + STRATEGIES_LOCATION + "]", ex);
}
}
}
return strategies;
}
use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class UriUtils method encodeQueryParams.
/**
* Encode the query parameters from the given {@code MultiValueMap} with UTF-8.
* <p>This can be used with {@link UriComponentsBuilder#queryParams(MultiValueMap)}
* when building a URI from an already encoded template.
* <pre class="code">{@code
* MultiValueMap<String, String> params = new DefaultMultiValueMap<>(2);
* // add to params...
*
* ServletUriComponentsBuilder.fromCurrentRequest()
* .queryParams(UriUtils.encodeQueryParams(params))
* .build(true)
* .toUriString();
* }</pre>
*
* @param params the parameters to encode
* @return a new {@code MultiValueMap} with the encoded names and values
*/
public static MultiValueMap<String, String> encodeQueryParams(MultiValueMap<String, String> params) {
Charset charset = StandardCharsets.UTF_8;
MultiValueMap<String, String> result = MultiValueMap.fromLinkedHashMap(params.size());
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
for (String value : entry.getValue()) {
result.add(encodeQueryParam(entry.getKey(), charset), encodeQueryParam(value, charset));
}
}
return result;
}
use of cn.taketoday.core.MultiValueMap in project today-framework by TAKETODAY.
the class DefaultWebClientBuilder method build.
@Override
public WebClient build() {
ClientHttpConnector connectorToUse = (this.connector != null ? this.connector : initConnector());
ExchangeFunction exchange = (this.exchangeFunction == null ? ExchangeFunctions.create(connectorToUse, initExchangeStrategies()) : this.exchangeFunction);
ExchangeFunction filteredExchange = (this.filters != null ? this.filters.stream().reduce(ExchangeFilterFunction::andThen).map(filter -> filter.apply(exchange)).orElse(exchange) : exchange);
HttpHeaders defaultHeaders = copyDefaultHeaders();
MultiValueMap<String, String> defaultCookies = copyDefaultCookies();
return new DefaultWebClient(filteredExchange, initUriBuilderFactory(), defaultHeaders, defaultCookies, this.defaultRequest, new DefaultWebClientBuilder(this));
}
Aggregations