@Override @Nullable public String[] getParameterValues(String name) { String[] parameterValues = super.getParameterValues(name); List<String> formParam = this.formParameters.get(name); if (formParam == null) { return parameterValues; } if (parameterValues == null || getQueryString() == null) { return StringUtils.toStringArray(formParam); } else { List<String> result = new ArrayList<>(parameterValues.length + formParam.size()); result.addAll(Arrays.asList(parameterValues)); result.addAll(formParam); return StringUtils.toStringArray(result); } } }
private static <K,V> void copy(MultiValueMap<K,V> src, MultiValueMap<K,V> dst) { if (!src.isEmpty()) { src.entrySet().stream() .filter(entry -> !dst.containsKey(entry.getKey())) .forEach(entry -> dst.put(entry.getKey(), entry.getValue())); } } }
private boolean isMultipart(MultiValueMap<String, ?> map, @Nullable MediaType contentType) { if (contentType != null) { return MediaType.MULTIPART_FORM_DATA.includes(contentType); } for (String name : map.keySet()) { for (Object value : map.get(name)) { if (value != null && !(value instanceof String)) { return true; } } } return false; }
public DefaultServerHttpRequestBuilder(ServerHttpRequest original) { Assert.notNull(original, "ServerHttpRequest is required"); this.uri = original.getURI(); this.httpMethodValue = original.getMethodValue(); this.body = original.getBody(); this.httpHeaders = HttpHeaders.writableHttpHeaders(original.getHeaders()); this.cookies = new LinkedMultiValueMap<>(original.getCookies().size()); copyMultiValueMap(original.getCookies(), this.cookies); this.originalRequest = original; }
/** * Return an unmodifiable view of the specified multi-value map. * @param map the map for which an unmodifiable view is to be returned. * @return an unmodifiable view of the specified multi-value map. * @since 3.1 */ @SuppressWarnings("unchecked") public static <K, V> MultiValueMap<K, V> unmodifiableMultiValueMap(MultiValueMap<? extends K, ? extends V> map) { Assert.notNull(map, "'map' must not be null"); Map<K, List<V>> result = new LinkedHashMap<>(map.size()); map.forEach((key, value) -> { List<? extends V> values = Collections.unmodifiableList(value); result.put(key, (List<V>) values); }); Map<K, List<V>> unmodifiableMap = Collections.unmodifiableMap(result); return toMultiValueMap(unmodifiableMap); }
private static void parsePathParamValues(String input, Charset charset, MultiValueMap<String, String> output) { if (StringUtils.hasText(input)) { int index = input.indexOf('='); if (index != -1) { String name = input.substring(0, index); String value = input.substring(index + 1); for (String v : StringUtils.commaDelimitedListToStringArray(value)) { name = StringUtils.uriDecode(name, charset); if (StringUtils.hasText(name)) { output.add(name, StringUtils.uriDecode(v, charset)); } } } else { String name = StringUtils.uriDecode(input, charset); if (StringUtils.hasText(name)) { output.add(input, ""); } } } }
/** * Provide a request parameter identifying the request for this FlashMap. * @param name the expected parameter name (skipped if empty) * @param value the expected value (skipped if empty) */ public FlashMap addTargetRequestParam(String name, String value) { if (StringUtils.hasText(name) && StringUtils.hasText(value)) { this.targetRequestParams.add(name, value); } return this; }
@Override public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException { String key = exchange.getRequest().getQueryParams().getFirst(getParameterName()); if (!StringUtils.hasText(key)) { return MEDIA_TYPE_ALL_LIST; } key = formatKey(key); MediaType match = this.mediaTypes.get(key); if (match == null) { match = MediaTypeFactory.getMediaType("filename." + key) .orElseThrow(() -> { List<MediaType> supported = new ArrayList<>(this.mediaTypes.values()); return new NotAcceptableStatusException(supported); }); } this.mediaTypes.putIfAbsent(key, match); return Collections.singletonList(match); }
private MultiValueMap<String, Object> generateBody() { HttpHeaders fooHeaders = new HttpHeaders(); fooHeaders.setContentType(MediaType.TEXT_PLAIN); ClassPathResource fooResource = new ClassPathResource("org/springframework/http/codec/multipart/foo.txt"); HttpEntity<ClassPathResource> fooPart = new HttpEntity<>(fooResource, fooHeaders); HttpEntity<String> barPart = new HttpEntity<>("bar"); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("fooPart", fooPart); parts.add("barPart", barPart); return parts; }
@Nullable protected final String getCallbackParam(ServerHttpRequest request) { String query = request.getURI().getQuery(); MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams(); String value = params.getFirst("c"); if (StringUtils.isEmpty(value)) { return null; } String result = UriUtils.decode(value, StandardCharsets.UTF_8); return (CALLBACK_PARAM_PATTERN.matcher(result).matches() ? result : null); }
@Test // SPR-11970 public void fromUriStringNoPathWithReservedCharInQuery() { UriComponents result = UriComponentsBuilder.fromUriString("http://example.com?foo=bar@baz").build(); assertTrue(StringUtils.isEmpty(result.getUserInfo())); assertEquals("example.com", result.getHost()); assertTrue(result.getQueryParams().containsKey("foo")); assertEquals("bar@baz", result.getQueryParams().getFirst("foo")); }
@Override public MultiValueMap<String, String> read(@Nullable Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType != null && 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; }
private static MultiValueMap<String, String> parseQueryParams(URI uri) { MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); String query = uri.getRawQuery(); if (query != null) { Matcher matcher = QUERY_PATTERN.matcher(query); while (matcher.find()) { String name = UriUtils.decode(matcher.group(1), StandardCharsets.UTF_8); String eq = matcher.group(2); String value = matcher.group(3); if (value != null) { value = UriUtils.decode(value, StandardCharsets.UTF_8); } else { value = (StringUtils.hasLength(eq) ? "" : null); } queryParams.add(name, value); } } return queryParams; }
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader); if (result != null) { return result; } try { Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); result = new LinkedMultiValueMap<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); for (Map.Entry<?, ?> entry : properties.entrySet()) { String factoryClassName = ((String) entry.getKey()).trim(); for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { result.add(factoryClassName, factoryName.trim()); } } } cache.put(classLoader, result); return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }