Java Code Examples for org.springframework.util.MultiValueMap#forEach()

The following examples show how to use org.springframework.util.MultiValueMap#forEach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormHttpMessageWriter.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected String serializeForm(MultiValueMap<String, String> formData, Charset charset) {
	StringBuilder builder = new StringBuilder();
	formData.forEach((name, values) ->
			values.forEach(value -> {
				try {
					if (builder.length() != 0) {
						builder.append('&');
					}
					builder.append(URLEncoder.encode(name, charset.name()));
					if (value != null) {
						builder.append('=');
						builder.append(URLEncoder.encode(value, charset.name()));
					}
				}
				catch (UnsupportedEncodingException ex) {
					throw new IllegalStateException(ex);
				}
			}));
	return builder.toString();
}
 
Example 2
Source File: FormHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected String serializeForm(MultiValueMap<String, Object> formData, Charset charset) {
	StringBuilder builder = new StringBuilder();
	formData.forEach((name, values) ->
			values.forEach(value -> {
				try {
					if (builder.length() != 0) {
						builder.append('&');
					}
					builder.append(URLEncoder.encode(name, charset.name()));
					if (value != null) {
						builder.append('=');
						builder.append(URLEncoder.encode(String.valueOf(value), charset.name()));
					}
				}
				catch (UnsupportedEncodingException ex) {
					throw new IllegalStateException(ex);
				}
			}));

	return builder.toString();
}
 
Example 3
Source File: FormHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected String serializeForm(MultiValueMap<String, Object> formData, Charset charset) {
	StringBuilder builder = new StringBuilder();
	formData.forEach((name, values) ->
			values.forEach(value -> {
				try {
					if (builder.length() != 0) {
						builder.append('&');
					}
					builder.append(URLEncoder.encode(name, charset.name()));
					if (value != null) {
						builder.append('=');
						builder.append(URLEncoder.encode(String.valueOf(value), charset.name()));
					}
				}
				catch (UnsupportedEncodingException ex) {
					throw new IllegalStateException(ex);
				}
			}));

	return builder.toString();
}
 
Example 4
Source File: FormHttpMessageConverter.java    From onetwo with Apache License 2.0 6 votes vote down vote up
protected String serializeForm(MultiValueMap<String, String> formData, Charset charset) {
	StringBuilder builder = new StringBuilder();
	formData.forEach((name, values) ->
			values.forEach(value -> {
				try {
					if (builder.length() != 0) {
						builder.append('&');
					}
					builder.append(URLEncoder.encode(name, charset.name()));
					if (value != null) {
						builder.append('=');
						builder.append(URLEncoder.encode(value, charset.name()));
					}
				}
				catch (UnsupportedEncodingException ex) {
					throw new IllegalStateException(ex);
				}
			}));

	return builder.toString();
}
 
Example 5
Source File: WebMvcStompEndpointRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return a handler mapping with the mapped ViewControllers.
 */
public AbstractHandlerMapping getHandlerMapping() {
	Map<String, Object> urlMap = new LinkedHashMap<>();
	for (WebMvcStompWebSocketEndpointRegistration registration : this.registrations) {
		MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
		mappings.forEach((httpHandler, patterns) -> {
			for (String pattern : patterns) {
				urlMap.put(pattern, httpHandler);
			}
		});
	}
	WebSocketHandlerMapping hm = new WebSocketHandlerMapping();
	hm.setUrlMap(urlMap);
	hm.setOrder(this.order);
	if (this.urlPathHelper != null) {
		hm.setUrlPathHelper(this.urlPathHelper);
	}
	return hm;
}
 
Example 6
Source File: UrlPathHelper.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Decode the given matrix variables via {@link #decodeRequestString} unless
 * {@link #setUrlDecode} 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 {@link #getLookupPathForRequest(HttpServletRequest)}.
 * @param request current HTTP request
 * @param vars the 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());
		vars.forEach((key, values) -> {
			for (String value : values) {
				decodedVars.add(key, decodeInternal(request, value));
			}
		});
		return decodedVars;
	}
}
 
Example 7
Source File: MatrixVariableMapMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
@Override
public Object resolveArgumentValue(MethodParameter parameter, BindingContext bindingContext,
		ServerWebExchange exchange) {

	Map<String, MultiValueMap<String, String>> matrixVariables =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(annotation != null, "No MatrixVariable annotation");
	String pathVariable = annotation.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
Example 8
Source File: ExtSpringEncoder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
	public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException {
		if(requestBody==null){
			super.encode(requestBody, bodyType, request);
			return ;
		}
		if(GET_METHOD.equalsIgnoreCase(request.method()) && requestBody!=null){
//			Map<String, Object> map = beanToMapConvertor.toFlatMap(requestBody);
//			MultiValueMap<String, String> map = RestUtils.toMultiValueStringMap(requestBody);
			MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
			
			// 使用ConsumableValuePutter增强对注解处理
			/*getParamsConvertor.flatObject("", requestBody, (k, v, ctx)->{
				map.add(k, v);
			});*/
			getParamsConvertor.flatObject("", requestBody, new ConsumableValuePutter((k, v) -> {
				map.add(k, v);
			}));
			map.forEach((name, value)->{
				if(value!=null){
					request.query(name, value.toArray(new String[0]));
				}
			});
			return ;
		}
		Object convertedRequestBody = convertRequestBodyIfNecessary(requestBody, request);
		super.encode(convertedRequestBody, bodyType, request);
	}
 
Example 9
Source File: MavenMetadataGenerator.java    From artifactory-resource with Apache License 2.0 5 votes vote down vote up
private void generate(Directory root, File pomFile, boolean generateChecksums) {
	String name = StringUtils.getFilename(pomFile.getName());
	String extension = StringUtils.getFilenameExtension(pomFile.getName());
	String prefix = name.substring(0, name.length() - extension.length() - 1);
	File[] files = pomFile.getParentFile().listFiles((f) -> include(f, prefix));
	MultiValueMap<File, MavenCoordinates> fileCoordinates = new LinkedMultiValueMap<>();
	for (File file : files) {
		String rootPath = StringUtils.cleanPath(root.getFile().getPath());
		String relativePath = StringUtils.cleanPath(file.getPath()).substring(rootPath.length() + 1);
		fileCoordinates.add(file.getParentFile(), MavenCoordinates.fromPath(relativePath));
	}
	fileCoordinates.forEach((file, coordinates) -> writeMetadata(file, coordinates, generateChecksums));
}
 
Example 10
Source File: ReadHeaderRestController.java    From tutorials with MIT License 5 votes vote down vote up
@GetMapping("/multiValue")
public ResponseEntity<String> multiValue(@RequestHeader MultiValueMap<String, String> headers) {
    headers.forEach((key, value) -> {
        LOG.info(String.format("Header '%s' = %s", key, value.stream().collect(Collectors.joining("|"))));
    });
    
    return new ResponseEntity<String>(String.format("Listed %d headers", headers.size()), HttpStatus.OK);
}
 
Example 11
Source File: MatrixVariableMapMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest request, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, MultiValueMap<String, String>> matrixVariables =
			(Map<String, MultiValueMap<String, String>>) request.getAttribute(
					HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVariable = ann.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
Example 12
Source File: BodyInserters.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private MultipartInserter withInternal(MultiValueMap<String, ?> values) {
	values.forEach((key, valueList) -> {
		for (Object value : valueList) {
			this.builder.part(key, value);
		}
	});
	return this;
}
 
Example 13
Source File: MockHttpServletRequestBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void addRequestParams(MockHttpServletRequest request, MultiValueMap<String, String> map) {
	map.forEach((key, values) -> values.forEach(value -> {
		value = (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null);
		request.addParameter(UriUtils.decode(key, StandardCharsets.UTF_8), value);
	}));
}
 
Example 14
Source File: JettyHeadersAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void addAll(MultiValueMap<String, String> values) {
	values.forEach(this::addAll);
}
 
Example 15
Source File: UndertowHeadersAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void addAll(MultiValueMap<String, String> values) {
	values.forEach((key, list) -> this.headers.addAll(HttpString.tryFromString(key), list));
}
 
Example 16
Source File: TomcatHeadersAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void addAll(MultiValueMap<String, String> values) {
	values.forEach(this::addAll);
}
 
Example 17
Source File: DefaultServerHttpRequestBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static <K, V> void copyMultiValueMap(MultiValueMap<K,V> source, MultiValueMap<K,V> target) {
	source.forEach((key, value) -> target.put(key, new LinkedList<>(value)));
}
 
Example 18
Source File: NettyHeadersAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void addAll(MultiValueMap<String, String> values) {
	values.forEach(this.headers::add);
}
 
Example 19
Source File: TomcatHeadersAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void addAll(MultiValueMap<String, String> values) {
	values.forEach(this::addAll);
}
 
Example 20
Source File: StompHeaders.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void addAll(MultiValueMap<String, String> values) {
	values.forEach(this::addAll);
}