Search in sources :

Example 26 with MultiValueMap

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"));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 27 with MultiValueMap

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);
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) HttpEntity(cn.taketoday.http.HttpEntity) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) RestTemplate(cn.taketoday.web.client.RestTemplate) ServletContext(jakarta.servlet.ServletContext) File(java.io.File) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 28 with MultiValueMap

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;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) URL(java.net.URL)

Example 29 with MultiValueMap

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;
}
Also used : Charset(java.nio.charset.Charset) List(java.util.List) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 30 with MultiValueMap

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));
}
Also used : ClientHttpConnector(cn.taketoday.http.client.reactive.ClientHttpConnector) ClientCodecConfigurer(cn.taketoday.http.codec.ClientCodecConfigurer) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) CollectionUtils(cn.taketoday.util.CollectionUtils) JettyClientHttpConnector(cn.taketoday.http.client.reactive.JettyClientHttpConnector) ClassUtils(cn.taketoday.util.ClassUtils) UriBuilderFactory(cn.taketoday.web.util.UriBuilderFactory) ArrayList(java.util.ArrayList) ReactorClientHttpConnector(cn.taketoday.http.client.reactive.ReactorClientHttpConnector) LinkedHashMap(java.util.LinkedHashMap) Consumer(java.util.function.Consumer) List(java.util.List) HttpHeaders(cn.taketoday.http.HttpHeaders) Nullable(cn.taketoday.lang.Nullable) Map(java.util.Map) HttpComponentsClientHttpConnector(cn.taketoday.http.client.reactive.HttpComponentsClientHttpConnector) MultiValueMap(cn.taketoday.core.MultiValueMap) DefaultUriBuilderFactory(cn.taketoday.web.util.DefaultUriBuilderFactory) JdkClientHttpConnector(cn.taketoday.http.client.reactive.JdkClientHttpConnector) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpConnector(cn.taketoday.http.client.reactive.ClientHttpConnector) JettyClientHttpConnector(cn.taketoday.http.client.reactive.JettyClientHttpConnector) ReactorClientHttpConnector(cn.taketoday.http.client.reactive.ReactorClientHttpConnector) HttpComponentsClientHttpConnector(cn.taketoday.http.client.reactive.HttpComponentsClientHttpConnector) JdkClientHttpConnector(cn.taketoday.http.client.reactive.JdkClientHttpConnector)

Aggregations

MultiValueMap (cn.taketoday.core.MultiValueMap)46 Test (org.junit.jupiter.api.Test)27 Map (java.util.Map)17 List (java.util.List)13 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)12 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)10 Nullable (cn.taketoday.lang.Nullable)9 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 HttpHeaders (cn.taketoday.http.HttpHeaders)8 MultipartBodyBuilder (cn.taketoday.http.client.MultipartBodyBuilder)8 MethodParameter (cn.taketoday.core.MethodParameter)7 Resource (cn.taketoday.core.io.Resource)6 MatrixParam (cn.taketoday.web.annotation.MatrixParam)5 ArrayList (java.util.ArrayList)5 BeanMetadata (cn.taketoday.beans.BeanMetadata)4 PropertyValue (cn.taketoday.beans.PropertyValue)4 PropertyValues (cn.taketoday.beans.PropertyValues)4 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)4 ResolvableType (cn.taketoday.core.ResolvableType)4 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4