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.lang.StringUtils;
6 import org.springframework.util.LinkedMultiValueMap;
7 import org.springframework.util.MultiValueMap;
8 
9 import java.io.Serializable;
10 import java.net.MalformedURLException;
11 import java.net.URI;
12 import java.net.URISyntaxException;
13 import java.net.URL;
14 import java.util.Map;
15 
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;
22 
30 @SuppressWarnings("unused")
31 public class JHttpEndpoint implements Serializable {
32 
36  public enum Protocol {
37  HTTP, HTTPS
38  }
39 
40  private Protocol protocol = HTTP;
41  private String hostname;
42  private int port = 80;
43 
49  public JHttpEndpoint(URI uri) {
50  try {
51  URL url = uri.toURL();
52  } catch (MalformedURLException e) {
53  throw new IllegalArgumentException(e);
54  }
55 
56  if (equalsIgnoreCase(uri.getScheme(), HTTP.name())) {
57  this.protocol = HTTP;
58  } else if (equalsIgnoreCase(uri.getScheme(), HTTPS.name())) {
59  this.protocol = HTTPS;
60  } else {
61  throw new IllegalArgumentException(format("Protocol of uri '%s' is unsupported!", uri));
62  }
63 
64  this.hostname = uri.getHost();
65  this.port = uri.getPort() < 0 ? 80 : uri.getPort();
66  }
67 
73  public JHttpEndpoint(Protocol protocol, String hostname, int port) {
74  this.protocol = protocol;
75  this.hostname = hostname;
76  this.port = port;
77  }
78 
83  public JHttpEndpoint(String hostname, int port) {
84  this.hostname = hostname;
85  this.port = port;
86  }
87 
92  public JHttpEndpoint(String endpointURL) throws URISyntaxException {
93  this(new URI(endpointURL));
94  }
95 
96  public Protocol getProtocol() {
97  return protocol;
98  }
99 
100  public String getHostname() {
101  return hostname;
102  }
103 
104  public int getPort() {
105  return port;
106  }
107 
112  public URI getURI() {
113  Preconditions.checkNotNull(hostname, "Hostname is null!");
114 
115  if (protocol == null) {
116  protocol = HTTP;
117  }
118  return newInstance().scheme(protocol.name().toLowerCase()).host(hostname).port(port).build().toUri();
119  }
120 
126  public URI getURI(String path) {
127  URI oldUri = getURI();
128  if (StringUtils.isEmpty(path)) {
129  return oldUri;
130  }
131 
132  return fromUri(oldUri).path(path).build().toUri();
133  }
134 
140  public URI getURI(Map<String, String> queryParams) {
141  URI uri = getURI();
142  if (MapUtils.isEmpty(queryParams)) {
143  return uri;
144  }
145 
146  return appendParameters(uri, queryParams);
147  }
148 
155  public URI getURI(String path, Map<String, String> queryParams) {
156  URI uri = getURI(path);
157  if (MapUtils.isEmpty(queryParams)) {
158  return uri;
159  }
160 
161  return appendParameters(uri, queryParams);
162  }
163 
169  public static URI appendParameters(URI oldUri, Map<String, String> queryParams) {
170  MultiValueMap<String, String> localQueryParams = new LinkedMultiValueMap<>();
171  queryParams.entrySet().forEach(entry -> localQueryParams.add(entry.getKey(), entry.getValue()));
172 
173  return fromUri(oldUri).queryParams(localQueryParams).build().toUri();
174  }
175 
176  @Override
177  public String toString() {
178  return "JHttpEndpoint{" +
179  "protocol=" + protocol +
180  ", hostname='" + hostname + '\'' +
181  ", port=" + port +
182  '}';
183  }
184 }