Example usage for org.springframework.util MultiValueMap add

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

Introduction

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

Prototype

void add(K key, @Nullable V value);

Source Link

Document

Add the given single value to the current list of values for the given key.

Usage

From source file:org.springframework.social.slack.api.impl.ChatTemplate.java

@Override
public SlackMessageResponse postMessage(String channel, String text, String username, boolean asUser,
        boolean link_names, List<SlackAttachment> attachments, boolean unfurl_links, boolean unfurl_media,
        String icon_url, String icon_emoji) throws SlackException {
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("channel", channel);
    map.add("text", text);
    map.add("username", username);
    map.add("as_user", String.valueOf(asUser));

    if (link_names) {
        map.add("link_names", "1");
        map.add("parse", "full");
    } else {/*from   w  w w. j av  a2s .  c o  m*/
        map.add("parse", "none");
    }

    if (attachments != null && !attachments.isEmpty()) {
        try {
            map.add("attachments", new ObjectMapper().writeValueAsString(attachments));
        } catch (JsonProcessingException e) {
            throw new SlackException(e);
        }
    }

    map.add("unfurl_links", String.valueOf(unfurl_links));
    map.add("unfurl_media", String.valueOf(unfurl_media));
    map.add("icon_url", icon_url);
    map.add("icon_emoji", icon_emoji);

    SlackMessageResponse slackResponse = post("/chat.postMessage", map, SlackMessageResponse.class);
    return slackResponse;

}

From source file:org.springframework.social.slack.api.impl.ChatTemplate.java

@Override
public SlackMessageResponse postMessage(String message, String channelNameOrId, String usernameOrBotName) {
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("channel", channelNameOrId);
    map.add("text", message);
    map.add("username", usernameOrBotName);
    map.add("as_user", "false");
    map.add("unfurl_links", "true");

    SlackMessageResponse slackResponse = post("/chat.postMessage", map, SlackMessageResponse.class);
    return slackResponse;

}

From source file:org.springframework.social.slack.api.impl.ChatTemplate.java

@Override
public SlackMessageResponse deleteMessage(String timestamp, String channelNameOrId, boolean asUser) {
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("ts", timestamp);
    map.add("channel", channelNameOrId);
    map.add("as_user", Boolean.toString(asUser));

    SlackMessageResponse slackResponse = get("/chat.delete", map, SlackMessageResponse.class);
    return slackResponse;

}

From source file:org.springframework.social.slack.api.impl.ChatTemplate.java

@Override
public SlackMessageResponse updateMessage(String timestamp, String channelNameOrId, boolean asUser,
        String message) {//from  w w  w .ja  v a 2  s  .c om
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("ts", timestamp);
    map.add("channel", channelNameOrId);
    map.add("text", message);
    map.add("as_user", String.valueOf(asUser));

    SlackMessageResponse slackResponse = post("/chat.update", map, SlackMessageResponse.class);
    return slackResponse;
}

From source file:org.springframework.social.slack.api.impl.ChatTemplate.java

@Override
public SlackMessageResponse updateMessage(String timestamp, String channelNameOrId, boolean asUser,
        String message, List<SlackAttachment> attachments, boolean linkNames) throws SlackException {

    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("ts", timestamp);
    map.add("channel", channelNameOrId);
    map.add("text", message);
    map.add("as_user", String.valueOf(asUser));

    if (attachments != null && !attachments.isEmpty()) {
        try {/*  w w w.  jav a2  s  .co  m*/
            map.add("attachments", new ObjectMapper().writeValueAsString(attachments));
        } catch (JsonProcessingException e) {
            throw new SlackException(e);
        }
    }

    if (linkNames) {
        map.add("link_names", "1");
        map.add("parse", "full");
    } else {
        map.add("parse", "none");
    }

    SlackMessageResponse slackResponse = post("/chat.update", map, SlackMessageResponse.class);
    return slackResponse;

}

From source file:org.springframework.social.tencentWeibo.api.impl.TencentWeiboTemplate.java

protected MultiValueMap<String, String> insertOAuthInfor(MultiValueMap<String, String> paramsList) {
    paramsList.add("access_token", accessToken);
    paramsList.add("oauth_consumer_key", app_key);
    paramsList.add("openid", openid);
    paramsList.add("clientip", localIP);
    paramsList.add("oauth_version", oauth_version);
    paramsList.add("scope", scope);
    return paramsList;
}

From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java

private Map<String, ?> resolveRequestParamMap(Class<? extends Map<?, ?>> mapType, NativeWebRequest webRequest) {
    Map<String, String[]> parameterMap = webRequest.getParameterMap();
    if (MultiValueMap.class.isAssignableFrom(mapType)) {
        MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(parameterMap.size());
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            for (String value : entry.getValue()) {
                result.add(entry.getKey(), value);
            }/*from   ww w  .  j a  va  2 s  .  c  o  m*/
        }
        return result;
    } else {
        Map<String, String> result = new LinkedHashMap<String, String>(parameterMap.size());
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            if (entry.getValue().length > 0) {
                result.put(entry.getKey(), entry.getValue()[0]);
            }
        }
        return result;
    }
}

From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java

private Map<String, ?> resolveRequestHeaderMap(Class<? extends Map<?, ?>> mapType,
        NativeWebRequest webRequest) {/*from   www  .jav  a  2s .  co  m*/
    if (MultiValueMap.class.isAssignableFrom(mapType)) {
        MultiValueMap<String, String> result;
        if (HttpHeaders.class.isAssignableFrom(mapType)) {
            result = new HttpHeaders();
        } else {
            result = new LinkedMultiValueMap<String, String>();
        }
        for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) {
            String headerName = iterator.next();
            for (String headerValue : webRequest.getHeaderValues(headerName)) {
                result.add(headerName, headerValue);
            }
        }
        return result;
    } else {
        Map<String, String> result = new LinkedHashMap<String, String>();
        for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) {
            String headerName = iterator.next();
            String headerValue = webRequest.getHeader(headerName);
            result.put(headerName, headerValue);
        }
        return result;
    }
}

From source file:org.springframework.web.client.RestTemplateIntegrationTests.java

@Test
public void multipart() throws UnsupportedEncodingException {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);

    template.postForLocation(baseUrl + "/multipart", parts);
}

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

/**
 * 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)
 *//*  w w w . ja v  a  2s .c  o  m*/
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);
            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();
            }
            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);
}