Jagger
 All Classes Namespaces Files Functions Variables 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 
32 public class JHttpEndpoint implements Serializable {
33 
34  public static final Protocol DEF_PROTOCOL = Protocol.HTTP;
35 
39  public enum Protocol {
40  HTTP, HTTPS
41  }
42 
43  private Protocol protocol = DEF_PROTOCOL;
44  private String hostname;
45  private int port = HttpURL.DEFAULT_PORT;
46 
52  @SuppressWarnings("unused")
53  public JHttpEndpoint(URI uri) {
54  try {
55  URL url = uri.toURL();
56  } catch (MalformedURLException e) {
57  throw new IllegalArgumentException(e);
58  }
59 
60  if (equalsIgnoreCase(uri.getScheme(), HTTP.name())) {
61  this.protocol = HTTP;
62  this.port = HttpURL.DEFAULT_PORT;
63  } else if (equalsIgnoreCase(uri.getScheme(), HTTPS.name())) {
64  this.protocol = HTTPS;
65  this.port = HttpsURL.DEFAULT_PORT;
66  } else {
67  throw new IllegalArgumentException(format("Protocol of uri '%s' is unsupported!", uri));
68  }
69 
70  this.hostname = uri.getHost();
71  if (uri.getPort() > 0) {
72  this.port = uri.getPort();
73  }
74  }
75 
81  public JHttpEndpoint(Protocol protocol, String hostname, int port) {
82  Objects.requireNonNull(protocol);
83  this.protocol = protocol;
84 
85  if (org.springframework.util.StringUtils.isEmpty(hostname)) {
86  throw new IllegalArgumentException(format("hostname must non-empty. Provided value: %s", hostname));
87  }
88  this.hostname = hostname;
89 
90  if (port <= 0) {
91  throw new IllegalArgumentException(format("port number must be > 0. Provided value: %s", port));
92  }
93  this.port = port;
94  }
95 
100  public JHttpEndpoint(String hostname, int port) {
101  this(DEF_PROTOCOL, hostname, port);
102  }
103 
107  public JHttpEndpoint(String endpointURL) {
108  this(URI.create(endpointURL));
109  }
110 
112  return protocol;
113  }
114 
115  public String getHostname() {
116  return hostname;
117  }
118 
119  public int getPort() {
120  return port;
121  }
122 
127  public URI getURI() {
128  Preconditions.checkNotNull(hostname, "Hostname is null!");
129 
130  if (protocol == null) {
131  protocol = DEF_PROTOCOL;
132  }
133  return newInstance().scheme(protocol.name().toLowerCase()).host(hostname).port(port).build().toUri();
134  }
135 
141  public URI getURI(String path) {
142  URI oldUri = getURI();
143  if (StringUtils.isEmpty(path)) {
144  return oldUri;
145  }
146 
147  return fromUri(oldUri).path(path).build().toUri();
148  }
149 
155  public URI getURI(Map<String, String> queryParams) {
156  URI uri = getURI();
157  if (MapUtils.isEmpty(queryParams)) {
158  return uri;
159  }
160 
161  return appendParameters(uri, queryParams);
162  }
163 
170  public URI getURI(String path, Map<String, String> queryParams) {
171  URI uri = getURI(path);
172  if (MapUtils.isEmpty(queryParams)) {
173  return uri;
174  }
175 
176  return appendParameters(uri, queryParams);
177  }
178 
184  public static URI appendParameters(URI oldUri, Map<String, String> queryParams) {
185  MultiValueMap<String, String> localQueryParams = new LinkedMultiValueMap<>();
186  queryParams.entrySet().forEach(entry -> localQueryParams.add(entry.getKey(), entry.getValue()));
187 
188  return fromUri(oldUri).queryParams(localQueryParams).build().toUri();
189  }
190 
191  @Override
192  public String toString() {
193  return "JHttpEndpoint{" +
194  "protocol=" + protocol +
195  ", hostname='" + hostname + '\'' +
196  ", port=" + port +
197  '}';
198  }
199 }