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;
46 private String hostname;
47 private int port = HttpURL.DEFAULT_PORT;
54 @SuppressWarnings(
"unused")
57 URL url = uri.toURL();
58 }
catch (MalformedURLException e) {
59 throw new IllegalArgumentException(e);
62 if (equalsIgnoreCase(uri.getScheme(), HTTP.name())) {
64 this.port = HttpURL.DEFAULT_PORT;
65 }
else if (equalsIgnoreCase(uri.getScheme(), HTTPS.name())) {
66 this.protocol = HTTPS;
67 this.port = HttpsURL.DEFAULT_PORT;
69 throw new IllegalArgumentException(format(
"Protocol of uri '%s' is unsupported!", uri));
72 this.hostname = uri.getHost();
73 if (uri.getPort() > 0) {
74 this.port = uri.getPort();
84 Objects.requireNonNull(protocol);
85 this.protocol = protocol;
87 if (org.springframework.util.StringUtils.isEmpty(hostname)) {
88 throw new IllegalArgumentException(format(
"hostname must non-empty. Provided value: %s", hostname));
90 this.hostname = hostname;
93 throw new IllegalArgumentException(format(
"port number must be > 0. Provided value: %s", port));
110 this(URI.create(endpointURL));
130 Preconditions.checkNotNull(hostname,
"Hostname is null!");
132 if (protocol == null) {
135 return newInstance().scheme(protocol.name().toLowerCase()).host(hostname).port(port).build().toUri();
145 if (StringUtils.isEmpty(path)) {
149 return fromUri(oldUri).path(path).build().toUri();
157 public URI
getURI(Map<String, String> queryParams) {
159 if (MapUtils.isEmpty(queryParams)) {
172 public URI
getURI(String path, Map<String, String> queryParams) {
174 if (MapUtils.isEmpty(queryParams)) {
187 MultiValueMap<String, String> localQueryParams =
new LinkedMultiValueMap<>();
188 queryParams.entrySet().forEach(entry -> localQueryParams.add(entry.getKey(), entry.getValue()));
190 return fromUri(oldUri).queryParams(localQueryParams).build().toUri();
194 if (jHttpEndpoint == null)
201 return "JHttpEndpoint{" +
202 "protocol=" + protocol +
203 ", hostname='" + hostname +
'\'' +