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.httpclient.HttpURL;
6 import org.apache.commons.httpclient.HttpsURL;
7 import org.apache.commons.lang.StringUtils;
8 import org.springframework.util.LinkedMultiValueMap;
9 import org.springframework.util.MultiValueMap;
11 import java.io.Serializable;
12 import java.net.MalformedURLException;
16 import java.util.Objects;
18 import static com.griddynamics.jagger.invoker.v2.JHttpEndpoint.Protocol.HTTP;
19 import static com.griddynamics.jagger.invoker.v2.JHttpEndpoint.Protocol.HTTPS;
20 import static java.lang.String.format;
21 import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
22 import static org.springframework.web.util.UriComponentsBuilder.fromUri;
23 import static org.springframework.web.util.UriComponentsBuilder.newInstance;
44 private String hostname;
45 private int port = HttpURL.DEFAULT_PORT;
52 @SuppressWarnings(
"unused")
55 URL url = uri.toURL();
56 }
catch (MalformedURLException e) {
57 throw new IllegalArgumentException(e);
60 if (equalsIgnoreCase(uri.getScheme(), HTTP.name())) {
62 this.port = HttpURL.DEFAULT_PORT;
63 }
else if (equalsIgnoreCase(uri.getScheme(), HTTPS.name())) {
64 this.protocol = HTTPS;
65 this.port = HttpsURL.DEFAULT_PORT;
67 throw new IllegalArgumentException(format(
"Protocol of uri '%s' is unsupported!", uri));
70 this.hostname = uri.getHost();
71 if (uri.getPort() > 0) {
72 this.port = uri.getPort();
82 Objects.requireNonNull(protocol);
83 this.protocol = protocol;
85 if (org.springframework.util.StringUtils.isEmpty(hostname)) {
86 throw new IllegalArgumentException(format(
"hostname must non-empty. Provided value: %s", hostname));
88 this.hostname = hostname;
91 throw new IllegalArgumentException(format(
"port number must be > 0. Provided value: %s", port));
108 this(URI.create(endpointURL));
128 Preconditions.checkNotNull(hostname,
"Hostname is null!");
130 if (protocol == null) {
133 return newInstance().scheme(protocol.name().toLowerCase()).host(hostname).port(port).build().toUri();
143 if (StringUtils.isEmpty(path)) {
147 return fromUri(oldUri).path(path).build().toUri();
155 public URI
getURI(Map<String, String> queryParams) {
157 if (MapUtils.isEmpty(queryParams)) {
170 public URI
getURI(String path, Map<String, String> queryParams) {
172 if (MapUtils.isEmpty(queryParams)) {
185 MultiValueMap<String, String> localQueryParams =
new LinkedMultiValueMap<>();
186 queryParams.entrySet().forEach(entry -> localQueryParams.add(entry.getKey(), entry.getValue()));
188 return fromUri(oldUri).queryParams(localQueryParams).build().toUri();
193 return "JHttpEndpoint{" +
194 "protocol=" + protocol +
195 ", hostname='" + hostname +
'\'' +