1 package com.griddynamics.jagger.invoker.v2;
3 import com.google.common.base.Preconditions;
4 import org.apache.commons.collections.MapUtils;
5 import org.apache.commons.lang.StringUtils;
6 import org.springframework.util.LinkedMultiValueMap;
7 import org.springframework.util.MultiValueMap;
9 import java.io.Serializable;
10 import java.net.MalformedURLException;
12 import java.net.URISyntaxException;
16 import static com.griddynamics.jagger.invoker.v2.JHttpEndpoint.Protocol.HTTP;
17 import static com.griddynamics.jagger.invoker.v2.JHttpEndpoint.Protocol.HTTPS;
18 import static java.lang.String.format;
19 import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
20 import static org.springframework.web.util.UriComponentsBuilder.fromUri;
21 import static org.springframework.web.util.UriComponentsBuilder.newInstance;
30 @SuppressWarnings(
"unused")
41 private String hostname;
42 private int port = 80;
51 URL url = uri.toURL();
52 }
catch (MalformedURLException e) {
53 throw new IllegalArgumentException(e);
56 if (equalsIgnoreCase(uri.getScheme(), HTTP.name())) {
58 }
else if (equalsIgnoreCase(uri.getScheme(), HTTPS.name())) {
59 this.protocol = HTTPS;
61 throw new IllegalArgumentException(format(
"Protocol of uri '%s' is unsupported!", uri));
64 this.hostname = uri.getHost();
65 this.port = uri.getPort() < 0 ? 80 : uri.getPort();
74 this.protocol = protocol;
75 this.hostname = hostname;
84 this.hostname = hostname;
93 this(
new URI(endpointURL));
113 Preconditions.checkNotNull(hostname,
"Hostname is null!");
115 if (protocol == null) {
118 return newInstance().scheme(protocol.name().toLowerCase()).host(hostname).port(port).build().toUri();
127 URI oldUri = getURI();
128 if (StringUtils.isEmpty(path)) {
132 return fromUri(oldUri).path(path).build().toUri();
140 public URI
getURI(Map<String, String> queryParams) {
142 if (MapUtils.isEmpty(queryParams)) {
146 return appendParameters(uri, queryParams);
155 public URI
getURI(String path, Map<String, String> queryParams) {
156 URI uri = getURI(path);
157 if (MapUtils.isEmpty(queryParams)) {
161 return appendParameters(uri, queryParams);
170 MultiValueMap<String, String> localQueryParams =
new LinkedMultiValueMap<>();
171 queryParams.entrySet().forEach(entry -> localQueryParams.add(entry.getKey(), entry.getValue()));
173 return fromUri(oldUri).queryParams(localQueryParams).build().toUri();
178 return "JHttpEndpoint{" +
179 "protocol=" + protocol +
180 ", hostname='" + hostname +
'\'' +