Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JHttpQuery.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker.v2;
2 
3 import org.apache.commons.lang.StringUtils;
4 import org.springframework.http.HttpHeaders;
5 import org.springframework.http.HttpMethod;
6 
7 import java.io.Serializable;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 
12 import static com.google.common.collect.Lists.newArrayList;
13 
14 
45 @SuppressWarnings("unused")
46 public class JHttpQuery<T> implements Serializable {
47  public static final JHttpQuery EMPTY_QUERY = null;
48 
49  private HttpMethod method;
50  private HttpHeaders headers;
51  private T body;
52  private Class responseBodyType;
53  private Map<String, String> queryParams;
54  private String path;
55 
64  public JHttpQuery<T> method(HttpMethod method) {
65  this.method = method;
66  return this;
67  }
68 
77  public JHttpQuery<T> get() {
78  return method(HttpMethod.GET);
79  }
80 
89  public JHttpQuery<T> post() {
90  return method(HttpMethod.POST);
91  }
92 
101  public JHttpQuery<T> put() {
102  return method(HttpMethod.PUT);
103  }
104 
113  public JHttpQuery<T> patch() {
114  return method(HttpMethod.PATCH);
115  }
116 
125  public JHttpQuery<T> delete() {
126  return method(HttpMethod.DELETE);
127  }
128 
137  public JHttpQuery<T> trace() {
138  return method(HttpMethod.TRACE);
139  }
140 
149  public JHttpQuery<T> head() {
150  return method(HttpMethod.HEAD);
151  }
152 
162  return method(HttpMethod.OPTIONS);
163  }
164 
178  public JHttpQuery<T> headers(HttpHeaders headers) {
179  this.headers = headers;
180  return this;
181  }
182 
196  public JHttpQuery<T> headers(Map<String, List<String>> headers) {
197  initHeadersIfNull();
198  this.headers.putAll(headers);
199  return this;
200  }
201 
213  public JHttpQuery<T> header(String key, List<String> values) {
214  initHeadersIfNull();
215  headers.put(key, values);
216  return this;
217  }
218 
230  public JHttpQuery<T> header(String key, String value) {
231  initHeadersIfNull();
232  headers.put(key, newArrayList(value));
233  return this;
234  }
235 
249  public JHttpQuery<T> cookies(Map<String, String> cookies) {
250  initHeadersIfNull();
251  cookies.entrySet().forEach(entry -> this.headers.add("Cookie", entry.getKey() + "=" + entry.getValue()));
252  return this;
253  }
254 
266  public JHttpQuery<T> cookie(String name, String value) {
267  initHeadersIfNull();
268  this.headers.add("Cookie", name + "=" + value);
269  return this;
270  }
271 
286  public JHttpQuery<T> body(T body) {
287  this.body = body;
288  return this;
289  }
290 
301  public JHttpQuery<T> responseBodyType(Class responseBodyType) {
302  this.responseBodyType = responseBodyType;
303  return this;
304  }
305 
320  public JHttpQuery<T> queryParams(Map<String, String> queryParams) {
321  this.queryParams = queryParams;
322  return this;
323  }
324 
334  public JHttpQuery<T> queryParam(String paramName, String paramValue) {
335  if (queryParams == null)
336  this.queryParams = new HashMap<>();
337  this.queryParams.put(paramName, paramValue);
338  return this;
339  }
340 
350  public JHttpQuery<T> path(String path) {
351  this.path = StringUtils.startsWith(path, "/") ? path : "/" + path;
352  return this;
353  }
354 
364  public JHttpQuery<T> path(String... paths) {
365  String path = StringUtils.join(paths, "/");
366  return path(path);
367  }
368 
379  public JHttpQuery<T> path(String formatString, Object... args) {
380  String path = String.format(formatString, args);
381  return path(path);
382  }
383 
384  public HttpMethod getMethod() {
385  return method;
386  }
387 
388  public T getBody() {
389  return body;
390  }
391 
392  public Class getResponseBodyType() {
393  return responseBodyType;
394  }
395 
396  public HttpHeaders getHeaders() {
397  return headers;
398  }
399 
400  public Map<String, String> getQueryParams() {
401  return queryParams;
402  }
403 
404  public String getPath() {
405  return path;
406  }
407 
408  private void initHeadersIfNull() {
409  if (this.headers == null)
410  this.headers = new HttpHeaders();
411  }
412 
413  public static JHttpQuery copyOf(JHttpQuery jHttpQuery) {
414  if (jHttpQuery == null)
415  return null;
416 
417  JHttpQuery copy = new JHttpQuery()
418  .body(jHttpQuery.getBody())
419  .queryParams(jHttpQuery.getQueryParams())
420  .path(jHttpQuery.getPath())
421  .responseBodyType(jHttpQuery.getResponseBodyType())
422  .headers(jHttpQuery.getHeaders());
423 
424  switch (jHttpQuery.getMethod()) {
425  case DELETE: copy.delete(); break;
426  case GET: copy.get(); break;
427  case HEAD: copy.head(); break;
428  case OPTIONS: copy.options(); break;
429  case PATCH: copy.patch(); break;
430  case POST: copy.post(); break;
431  case PUT: copy.put(); break;
432  case TRACE: copy.trace(); break;
433  }
434  return copy;
435  }
436 
437  @Override
438  public String toString() {
439  return "JHttpQuery{" +
440  "method=" + method +
441  ", headers=" + headers +
442  ", body=" + body +
443  ", responseBodyType=" + responseBodyType +
444  ", queryParams=" + queryParams +
445  ", path='" + path + '\'' +
446  '}';
447  }
448 
449  @Override
450  public boolean equals(Object o) {
451  if (this == o) return true;
452  if (o == null || getClass() != o.getClass()) return false;
453 
454  JHttpQuery<?> httpQuery = (JHttpQuery<?>) o;
455 
456  if (method != httpQuery.method) return false;
457  if (headers != null ? !headers.equals(httpQuery.headers) : httpQuery.headers != null) return false;
458  if (body != null ? !body.equals(httpQuery.body) : httpQuery.body != null) return false;
459  if (responseBodyType != null ? !responseBodyType.equals(httpQuery.responseBodyType) : httpQuery.responseBodyType != null)
460  return false;
461  if (queryParams != null ? !queryParams.equals(httpQuery.queryParams) : httpQuery.queryParams != null) return false;
462  return path != null ? path.equals(httpQuery.path) : httpQuery.path == null;
463 
464  }
465 
466  @Override
467  public int hashCode() {
468  int result = method.hashCode();
469  result = 31 * result + (headers != null ? headers.hashCode() : 0);
470  result = 31 * result + (body != null ? body.hashCode() : 0);
471  result = 31 * result + (responseBodyType != null ? responseBodyType.hashCode() : 0);
472  result = 31 * result + (queryParams != null ? queryParams.hashCode() : 0);
473  result = 31 * result + (path != null ? path.hashCode() : 0);
474  return result;
475  }
476 }