Search in sources :

Example 1 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project geode by apache.

the class ClientHttpRequestJUnitTest method testCreateRequestEntityForPut.

@Test
@SuppressWarnings("unchecked")
public void testCreateRequestEntityForPut() throws Exception {
    final Link expectedLink = new Link("put", toUri("http://host.domain.com:8080/app/libraries/{name}/books/{isbn}"), HttpMethod.PUT);
    final ClientHttpRequest request = new ClientHttpRequest(expectedLink);
    assertEquals(expectedLink, request.getLink());
    final MultiValueMap<String, Object> expectedRequestParameters = new LinkedMultiValueMap<String, Object>(4);
    expectedRequestParameters.add("year", "1979");
    request.addHeaderValues(HttpHeader.ACCEPT.getName(), MediaType.TEXT_XML_VALUE);
    request.addHeaderValues(HttpHeader.CONTENT_TYPE.getName(), MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    request.addParameterValues("year", expectedRequestParameters.getFirst("year"));
    final HttpEntity<MultiValueMap<String, Object>> requestEntity = (HttpEntity<MultiValueMap<String, Object>>) request.createRequestEntity();
    assertNotNull(requestEntity);
    assertNotNull(requestEntity.getHeaders());
    assertEquals(Collections.singletonList(MediaType.TEXT_XML), requestEntity.getHeaders().getAccept());
    assertEquals(MediaType.APPLICATION_FORM_URLENCODED, requestEntity.getHeaders().getContentType());
    assertEquals(expectedRequestParameters, requestEntity.getBody());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Link(org.apache.geode.management.internal.web.domain.Link) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 2 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-framework by spring-projects.

the class FormHttpMessageConverter method read.

@Override
public MultiValueMap<String, String> read(Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    MediaType contentType = inputMessage.getHeaders().getContentType();
    Charset charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
    String body = StreamUtils.copyToString(inputMessage.getBody(), charset);
    String[] pairs = StringUtils.tokenizeToStringArray(body, "&");
    MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length);
    for (String pair : pairs) {
        int idx = pair.indexOf('=');
        if (idx == -1) {
            result.add(URLDecoder.decode(pair, charset.name()), null);
        } else {
            String name = URLDecoder.decode(pair.substring(0, idx), charset.name());
            String value = URLDecoder.decode(pair.substring(idx + 1), charset.name());
            result.add(name, value);
        }
    }
    return result;
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset)

Example 3 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-framework by spring-projects.

the class RxNettyServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
    for (String name : this.request.getCookies().keySet()) {
        for (Cookie cookie : this.request.getCookies().get(name)) {
            HttpCookie httpCookie = new HttpCookie(name, cookie.value());
            cookies.add(name, httpCookie);
        }
    }
    return cookies;
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpCookie(org.springframework.http.HttpCookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Example 4 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-framework by spring-projects.

the class UndertowServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
    for (String name : this.exchange.getRequestCookies().keySet()) {
        Cookie cookie = this.exchange.getRequestCookies().get(name);
        HttpCookie httpCookie = new HttpCookie(name, cookie.getValue());
        cookies.add(name, httpCookie);
    }
    return cookies;
}
Also used : Cookie(io.undertow.server.handlers.Cookie) HttpCookie(org.springframework.http.HttpCookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Example 5 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-framework by spring-projects.

the class CommonsFileUploadSupport method parseFileItems.

/**
	 * Parse the given List of Commons FileItems into a Spring MultipartParsingResult,
	 * containing Spring MultipartFile instances and a Map of multipart parameter.
	 * @param fileItems the Commons FileIterms to parse
	 * @param encoding the encoding to use for form fields
	 * @return the Spring MultipartParsingResult
	 * @see CommonsMultipartFile#CommonsMultipartFile(org.apache.commons.fileupload.FileItem)
	 */
protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String encoding) {
    MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>();
    Map<String, String[]> multipartParameters = new HashMap<>();
    Map<String, String> multipartParameterContentTypes = new HashMap<>();
    // Extract multipart files and multipart parameters.
    for (FileItem fileItem : fileItems) {
        if (fileItem.isFormField()) {
            String value;
            String partEncoding = determineEncoding(fileItem.getContentType(), encoding);
            if (partEncoding != null) {
                try {
                    value = fileItem.getString(partEncoding);
                } catch (UnsupportedEncodingException ex) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not decode multipart item '" + fileItem.getFieldName() + "' with encoding '" + partEncoding + "': using platform default");
                    }
                    value = fileItem.getString();
                }
            } else {
                value = fileItem.getString();
            }
            String[] curParam = multipartParameters.get(fileItem.getFieldName());
            if (curParam == null) {
                // simple form field
                multipartParameters.put(fileItem.getFieldName(), new String[] { value });
            } else {
                // array of simple form fields
                String[] newParam = StringUtils.addStringToArray(curParam, value);
                multipartParameters.put(fileItem.getFieldName(), newParam);
            }
            multipartParameterContentTypes.put(fileItem.getFieldName(), fileItem.getContentType());
        } else {
            // multipart file field
            CommonsMultipartFile file = createMultipartFile(fileItem);
            multipartFiles.add(file.getName(), file);
            if (logger.isDebugEnabled()) {
                logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() + " bytes with original filename [" + file.getOriginalFilename() + "], stored " + file.getStorageDescription());
            }
        }
    }
    return new MultipartParsingResult(multipartFiles, multipartParameters, multipartParameterContentTypes);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MultipartFile(org.springframework.web.multipart.MultipartFile) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)402 Test (org.junit.Test)152 HttpHeaders (org.springframework.http.HttpHeaders)125 MultiValueMap (org.springframework.util.MultiValueMap)91 Test (org.junit.jupiter.api.Test)88 HttpEntity (org.springframework.http.HttpEntity)59 List (java.util.List)42 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)40 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)37 HashMap (java.util.HashMap)36 MediaType (org.springframework.http.MediaType)34 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)33 URI (java.net.URI)32 Map (java.util.Map)31 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)30 ArrayList (java.util.ArrayList)27 UUID (java.util.UUID)27 lombok.val (lombok.val)27 IOException (java.io.IOException)26 Assert (org.junit.Assert)25