Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JHttpEndpoint.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker.v2;
2 
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;
10 
11 import java.io.Serializable;
12 import java.net.MalformedURLException;
13 import java.net.URI;
14 import java.net.URL;
15 import java.util.Map;
16 import java.util.Objects;
17 
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;
24 
34 public class JHttpEndpoint implements Serializable {
35 
36  public static final Protocol DEF_PROTOCOL = Protocol.HTTP;
37 
41  public enum Protocol {
42  HTTP, HTTPS
43  }
44 
45  private Protocol protocol = DEF_PROTOCOL;
46  private String hostname;
47  private int port = HttpURL.DEFAULT_PORT;
48 
54  @SuppressWarnings("unused")
55  public JHttpEndpoint(URI uri) {
56  try {
57  URL url = uri.toURL();
58  } catch (MalformedURLException e) {
59  throw new IllegalArgumentException(e);
60  }
61 
62  if (equalsIgnoreCase(uri.getScheme(), HTTP.name())) {
63  this.protocol = HTTP;
64  this.port = HttpURL.DEFAULT_PORT;
65  } else if (equalsIgnoreCase(uri.getScheme(), HTTPS.name())) {
66  this.protocol = HTTPS;
67  this.port = HttpsURL.DEFAULT_PORT;
68  } else {
69  throw new IllegalArgumentException(format("Protocol of uri '%s' is unsupported!", uri));
70  }
71 
72  this.hostname = uri.getHost();
73  if (uri.getPort() > 0) {
74  this.port = uri.getPort();
75  }
76  }
77 
83  public JHttpEndpoint(Protocol protocol, String hostname, int port) {
84  Objects.requireNonNull(protocol);
85  this.protocol = protocol;
86 
87  if (org.springframework.util.StringUtils.isEmpty(hostname)) {
88  throw new IllegalArgumentException(format("hostname must non-empty. Provided value: %s", hostname));
89  }
90  this.hostname = hostname;
91 
92  if (port <= 0) {
93  throw new IllegalArgumentException(format("port number must be > 0. Provided value: %s", port));
94  }
95  this.port = port;
96  }
97 
102  public JHttpEndpoint(String hostname, int port) {
103  this(DEF_PROTOCOL, hostname, port);
104  }
105 
109  public JHttpEndpoint(String endpointURL) {
110  this(URI.create(endpointURL));
111  }
112 
114  return protocol;
115  }
116 
117  public String getHostname() {
118  return hostname;
119  }
120 
121  public int getPort() {
122  return port;
123  }
124 
129  public URI getURI() {
130  Preconditions.checkNotNull(hostname, "Hostname is null!");
131 
132  if (protocol == null) {
133  protocol = DEF_PROTOCOL;
134  }
135  return newInstance().scheme(protocol.name().toLowerCase()).host(hostname).port(port).build().toUri();
136  }
137 
143  public URI getURI(String path) {
144  URI oldUri = getURI();
145  if (StringUtils.isEmpty(path)) {
146  return oldUri;
147  }
148 
149  return fromUri(oldUri).path(path).build().toUri();
150  }
151 
157  public URI getURI(Map<String, String> queryParams) {
158  URI uri = getURI();
159  if (MapUtils.isEmpty(queryParams)) {
160  return uri;
161  }
162 
163  return appendParameters(uri, queryParams);
164  }
165 
172  public URI getURI(String path, Map<String, String> queryParams) {
173  URI uri = getURI(path);
174  if (MapUtils.isEmpty(queryParams)) {
175  return uri;
176  }
177 
178  return appendParameters(uri, queryParams);
179  }
180 
186  public static URI appendParameters(URI oldUri, Map<String, String> queryParams) {
187  MultiValueMap<String, String> localQueryParams = new LinkedMultiValueMap<>();
188  queryParams.entrySet().forEach(entry -> localQueryParams.add(entry.getKey(), entry.getValue()));
189 
190  return fromUri(oldUri).queryParams(localQueryParams).build().toUri();
191  }
192 
193  public static JHttpEndpoint copyOf(JHttpEndpoint jHttpEndpoint) {
194  if (jHttpEndpoint == null)
195  return null;
196  return new JHttpEndpoint(jHttpEndpoint.getURI());
197  }
198 
199  @Override
200  public String toString() {
201  return "JHttpEndpoint{" +
202  "protocol=" + protocol +
203  ", hostname='" + hostname + '\'' +
204  ", port=" + port +
205  '}';
206  }
207 }