All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.biz.web.client.QueryParamsUrlTemplateHandler Maven / Gradle / Ivy

There is a newer version: 3.3.x.20241003.RELEASE
Show newest version
package org.springframework.biz.web.client;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplateHandler;
import org.springframework.web.util.UriUtils;

public class QueryParamsUrlTemplateHandler implements UriTemplateHandler {

	private boolean strictEncoding;
	
	/**
	 * Whether to encode characters outside the unreserved set as defined in
	 * RFC 3986 Section 2.
	 * This ensures a URI variable value will not contain any characters with a
	 * reserved purpose.
	 * 

By default this is set to {@code false} in which case only characters * illegal for the given URI component are encoded. For example when expanding * a URI variable into a path segment the "/" character is illegal and * encoded. The ";" character however is legal and not encoded even though * it has a reserved purpose. * @param strictEncoding whether to perform strict encoding */ public void setStrictEncoding(boolean strictEncoding) { this.strictEncoding = strictEncoding; } /** * Whether to strictly encode any character outside the unreserved set. * @return Whether to strictly encode any character outside the unreserved set. */ public boolean isStrictEncoding() { return this.strictEncoding; } @Override public URI expand(String uriTemplate, Object... uriVariables) { return null; } @Override public URI expand(String uriTemplate, Map uriVariables) { UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uriTemplate); for (Map.Entry varEntry : uriVariables.entrySet()) { builder.queryParam(varEntry.getKey(), varEntry.getValue()); } return builder.build(uriVariables); } protected UriComponents expandAndEncode(UriComponentsBuilder builder, Map uriVariables) { if (!isStrictEncoding()) { return builder.buildAndExpand(uriVariables).encode(); } else { Map encodedUriVars = UriUtils.encodeUriVariables(uriVariables); return builder.buildAndExpand(encodedUriVars); } } protected UriComponents expandAndEncode(UriComponentsBuilder builder, Object[] uriVariables) { if (!isStrictEncoding()) { return builder.buildAndExpand(uriVariables).encode(); } else { Object[] encodedUriVars = UriUtils.encodeUriVariables(uriVariables); return builder.buildAndExpand(encodedUriVars); } } protected URI createUri(UriComponents uriComponents) { try { // Avoid further encoding (in the case of strictEncoding=true) return new URI(uriComponents.toUriString()); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not create URI object: " + ex.getMessage(), ex); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy