Example usage for org.springframework.util MultiValueMap get

List of usage examples for org.springframework.util MultiValueMap get

Introduction

In this page you can find the example usage for org.springframework.util MultiValueMap get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:org.springframework.web.multipart.commons.CommonsMultipartResolverTests.java

private void doTestFiles(MultipartHttpServletRequest request) throws IOException {
    Set<String> fileNames = new HashSet<>();
    Iterator<String> fileIter = request.getFileNames();
    while (fileIter.hasNext()) {
        fileNames.add(fileIter.next());//from   w w  w .  j  ava 2 s .c o  m
    }
    assertEquals(3, fileNames.size());
    assertTrue(fileNames.contains("field1"));
    assertTrue(fileNames.contains("field2"));
    assertTrue(fileNames.contains("field2x"));
    CommonsMultipartFile file1 = (CommonsMultipartFile) request.getFile("field1");
    CommonsMultipartFile file2 = (CommonsMultipartFile) request.getFile("field2");
    CommonsMultipartFile file2x = (CommonsMultipartFile) request.getFile("field2x");

    Map<String, MultipartFile> fileMap = request.getFileMap();
    assertEquals(3, fileMap.size());
    assertTrue(fileMap.containsKey("field1"));
    assertTrue(fileMap.containsKey("field2"));
    assertTrue(fileMap.containsKey("field2x"));
    assertEquals(file1, fileMap.get("field1"));
    assertEquals(file2, fileMap.get("field2"));
    assertEquals(file2x, fileMap.get("field2x"));

    MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
    assertEquals(3, multiFileMap.size());
    assertTrue(multiFileMap.containsKey("field1"));
    assertTrue(multiFileMap.containsKey("field2"));
    assertTrue(multiFileMap.containsKey("field2x"));
    List<MultipartFile> field1Files = multiFileMap.get("field1");
    assertEquals(2, field1Files.size());
    assertTrue(field1Files.contains(file1));
    assertEquals(file1, multiFileMap.getFirst("field1"));
    assertEquals(file2, multiFileMap.getFirst("field2"));
    assertEquals(file2x, multiFileMap.getFirst("field2x"));

    assertEquals("type1", file1.getContentType());
    assertEquals("type2", file2.getContentType());
    assertEquals("type2", file2x.getContentType());
    assertEquals("field1.txt", file1.getOriginalFilename());
    assertEquals("field2.txt", file2.getOriginalFilename());
    assertEquals("field2x.txt", file2x.getOriginalFilename());
    assertEquals("text1", new String(file1.getBytes()));
    assertEquals("text2", new String(file2.getBytes()));
    assertEquals(5, file1.getSize());
    assertEquals(5, file2.getSize());
    assertTrue(file1.getInputStream() instanceof ByteArrayInputStream);
    assertTrue(file2.getInputStream() instanceof ByteArrayInputStream);
    File transfer1 = new File("C:/transfer1");
    file1.transferTo(transfer1);
    File transfer2 = new File("C:/transfer2");
    file2.transferTo(transfer2);
    assertEquals(transfer1, ((MockFileItem) file1.getFileItem()).writtenFile);
    assertEquals(transfer2, ((MockFileItem) file2.getFileItem()).writtenFile);

}

From source file:org.springframework.web.servlet.support.AbstractFlashMapManager.java

/**
 * Whether the given FlashMap matches the current request.
 * Uses the expected request path and query parameters saved in the FlashMap.
 *///from w w w. j  av  a 2 s .c  o  m
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
    String expectedPath = flashMap.getTargetRequestPath();
    if (expectedPath != null) {
        String requestUri = getUrlPathHelper().getOriginatingRequestUri(request);
        if (!requestUri.equals(expectedPath) && !requestUri.equals(expectedPath + "/")) {
            return false;
        }
    }
    MultiValueMap<String, String> actualParams = getOriginatingRequestParams(request);
    MultiValueMap<String, String> expectedParams = flashMap.getTargetRequestParams();
    for (String expectedName : expectedParams.keySet()) {
        List<String> actualValues = actualParams.get(expectedName);
        if (actualValues == null) {
            return false;
        }
        for (String expectedValue : expectedParams.get(expectedName)) {
            if (!actualValues.contains(expectedValue)) {
                return false;
            }
        }
    }
    return true;
}

From source file:org.springframework.web.servlet.support.DefaultFlashMapManager.java

/**
 * Whether the given FlashMap matches the current request.
 * The default implementation uses the target request path and query params 
 * saved in the FlashMap./*w  w w .  j av a2  s.  c  o m*/
 */
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
    if (flashMap.getTargetRequestPath() != null) {
        String requestUri = this.urlPathHelper.getOriginatingRequestUri(request);
        if (!requestUri.equals(flashMap.getTargetRequestPath())
                && !requestUri.equals(flashMap.getTargetRequestPath() + "/")) {
            return false;
        }
    }
    MultiValueMap<String, String> targetParams = flashMap.getTargetRequestParams();
    for (String paramName : targetParams.keySet()) {
        for (String targetValue : targetParams.get(paramName)) {
            if (!ObjectUtils.containsElement(request.getParameterValues(paramName), targetValue)) {
                return false;
            }
        }
    }
    return true;
}

From source file:org.springframework.web.util.UrlPathHelper.java

/**
 * Decode the given matrix variables via
 * {@link #decodeRequestString(HttpServletRequest, String)} unless
 * {@link #setUrlDecode(boolean)} is set to {@code true} in which case it is
 * assumed the URL path from which the variables were extracted is already
 * decoded through a call to/*from w  w w.  j  a v a  2 s. com*/
 * {@link #getLookupPathForRequest(HttpServletRequest)}.
 * @param request current HTTP request
 * @param vars URI variables extracted from the URL path
 * @return the same Map or a new Map instance
 */
public MultiValueMap<String, String> decodeMatrixVariables(HttpServletRequest request,
        MultiValueMap<String, String> vars) {

    if (this.urlDecode) {
        return vars;
    } else {
        MultiValueMap<String, String> decodedVars = new LinkedMultiValueMap<>(vars.size());
        for (String key : vars.keySet()) {
            for (String value : vars.get(key)) {
                decodedVars.add(key, decodeInternal(request, value));
            }
        }
        return decodedVars;
    }
}

From source file:org.springframework.ws.wsdl.wsdl11.provider.AbstractPortTypesProvider.java

private void createOperations(Definition definition, PortType portType) throws WSDLException {
    MultiValueMap<String, Message> operations = new LinkedMultiValueMap<String, Message>();
    for (Iterator<?> iterator = definition.getMessages().values().iterator(); iterator.hasNext();) {
        Message message = (Message) iterator.next();
        String operationName = getOperationName(message);
        if (StringUtils.hasText(operationName)) {
            operations.add(operationName, message);
        }//  w  ww . j  a v a2  s.c  o  m
    }
    if (operations.isEmpty() && logger.isWarnEnabled()) {
        logger.warn("No operations were created, make sure the WSDL contains messages");
    }
    for (String operationName : operations.keySet()) {
        Operation operation = definition.createOperation();
        operation.setName(operationName);
        List<Message> messages = operations.get(operationName);
        for (Message message : messages) {
            if (isInputMessage(message)) {
                Input input = definition.createInput();
                input.setMessage(message);
                populateInput(definition, input);
                operation.setInput(input);
            } else if (isOutputMessage(message)) {
                Output output = definition.createOutput();
                output.setMessage(message);
                populateOutput(definition, output);
                operation.setOutput(output);
            } else if (isFaultMessage(message)) {
                Fault fault = definition.createFault();
                fault.setMessage(message);
                populateFault(definition, fault);
                operation.addFault(fault);
            }
        }
        operation.setStyle(getOperationType(operation));
        operation.setUndefined(false);
        if (logger.isDebugEnabled()) {
            logger.debug("Adding operation [" + operation.getName() + "] to port type [" + portType.getQName()
                    + "]");
        }
        portType.addOperation(operation);
    }
}

From source file:org.venice.beachfront.bfapi.services.OAuthServiceTests.java

@Test
public void testRequestAccessTokenSuccess() throws UserException {
    String mockAuthCode = "mock-auth-code-123";
    String mockAccessToken = "mock-access-token-321";

    AccessTokenResponseBody mockResponse = Mockito.mock(AccessTokenResponseBody.class);
    Mockito.when(mockResponse.getAccessToken()).thenReturn(mockAccessToken);

    Mockito.when(this.restTemplate.exchange(Mockito.eq(this.oauthTokenUrl), Mockito.eq(HttpMethod.POST),
            Mockito.any(), Mockito.eq(AccessTokenResponseBody.class)))
            .then(new Answer<ResponseEntity<AccessTokenResponseBody>>() {
                @Override// w  w  w  .  jav a2  s  . c  o  m
                public ResponseEntity<AccessTokenResponseBody> answer(InvocationOnMock invocation) {
                    HttpEntity<MultiValueMap<String, String>> entity = invocation.getArgumentAt(2,
                            HttpEntity.class);
                    MultiValueMap<String, String> body = entity.getBody();
                    assertEquals(mockAuthCode, body.get("code").get(0));
                    assertEquals("authorization_code", body.get("grant_type").get(0));
                    assertEquals(oauthService.getOauthRedirectUri(), body.get("redirect_uri").get(0));
                    return new ResponseEntity<AccessTokenResponseBody>(mockResponse, HttpStatus.OK);
                }
            });

    String receivedAccessToken = this.oauthService.requestAccessToken(mockAuthCode);
    assertEquals(mockAccessToken, receivedAccessToken);
}