Jagger
 All Classes Namespaces Files Functions Variables 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 
43 @SuppressWarnings("unused")
44 public class JHttpQuery<T> implements Serializable {
45 
46  private HttpMethod method;
47  private HttpHeaders headers;
48  private T body;
49  private Class responseBodyType;
50  private Map<String, String> queryParams;
51  private String path;
52 
61  public JHttpQuery<T> method(HttpMethod method) {
62  this.method = method;
63  return this;
64  }
65 
74  public JHttpQuery<T> get() {
75  return method(HttpMethod.GET);
76  }
77 
86  public JHttpQuery<T> post() {
87  return method(HttpMethod.POST);
88  }
89 
98  public JHttpQuery<T> put() {
99  return method(HttpMethod.PUT);
100  }
101 
110  public JHttpQuery<T> patch() {
111  return method(HttpMethod.PATCH);
112  }
113 
122  public JHttpQuery<T> delete() {
123  return method(HttpMethod.DELETE);
124  }
125 
134  public JHttpQuery<T> trace() {
135  return method(HttpMethod.TRACE);
136  }
137 
146  public JHttpQuery<T> head() {
147  return method(HttpMethod.HEAD);
148  }
149 
159  return method(HttpMethod.OPTIONS);
160  }
161 
175  public JHttpQuery<T> headers(HttpHeaders headers) {
176  this.headers = headers;
177  return this;
178  }
179 
193  public JHttpQuery<T> headers(Map<String, List<String>> headers) {
194  initHeadersIfNull();
195  this.headers.putAll(headers);
196  return this;
197  }
198 
210  public JHttpQuery<T> header(String key, List<String> values) {
211  initHeadersIfNull();
212  headers.put(key, values);
213  return this;
214  }
215 
227  public JHttpQuery<T> header(String key, String value) {
228  initHeadersIfNull();
229  headers.put(key, newArrayList(value));
230  return this;
231  }
232 
246  public JHttpQuery<T> cookies(Map<String, String> cookies) {
247  initHeadersIfNull();
248  cookies.entrySet().forEach(entry -> this.headers.add("Cookie", entry.getKey() + "=" + entry.getValue()));
249  return this;
250  }
251 
263  public JHttpQuery<T> cookie(String name, String value) {
264  initHeadersIfNull();
265  this.headers.add("Cookie", name + "=" + value);
266  return this;
267  }
268 
283  public JHttpQuery<T> body(T body) {
284  this.body = body;
285  return this;
286  }
287 
298  public JHttpQuery<T> responseBodyType(Class responseBodyType) {
299  this.responseBodyType = responseBodyType;
300  return this;
301  }
302 
317  public JHttpQuery<T> queryParams(Map<String, String> queryParams) {
318  this.queryParams = queryParams;
319  return this;
320  }
321 
331  public JHttpQuery<T> queryParam(String paramName, String paramValue) {
332  if (queryParams == null)
333  this.queryParams = new HashMap<>();
334  this.queryParams.put(paramName, paramValue);
335  return this;
336  }
337 
347  public JHttpQuery<T> path(String path) {
348  this.path = StringUtils.startsWith(path, "/") ? path : "/" + path;
349  return this;
350  }
351 
361  public JHttpQuery<T> path(String... paths) {
362  String path = StringUtils.join(paths, "/");
363  return path(path);
364  }
365 
376  public JHttpQuery<T> path(String formatString, Object... args) {
377  String path = String.format(formatString, args);
378  return path(path);
379  }
380 
381  public HttpMethod getMethod() {
382  return method;
383  }
384 
385  public T getBody() {
386  return body;
387  }
388 
389  public Class getResponseBodyType() {
390  return responseBodyType;
391  }
392 
393  public HttpHeaders getHeaders() {
394  return headers;
395  }
396 
397  public Map<String, String> getQueryParams() {
398  return queryParams;
399  }
400 
401  public String getPath() {
402  return path;
403  }
404 
405  private void initHeadersIfNull() {
406  if (this.headers == null)
407  this.headers = new HttpHeaders();
408  }
409 
410  @Override
411  public String toString() {
412  return "JHttpQuery{" +
413  "method=" + method +
414  ", headers=" + headers +
415  ", body=" + body +
416  ", responseBodyType=" + responseBodyType +
417  ", queryParams=" + queryParams +
418  ", path='" + path + '\'' +
419  '}';
420  }
421 
422  @Override
423  public boolean equals(Object o) {
424  if (this == o) return true;
425  if (o == null || getClass() != o.getClass()) return false;
426 
427  JHttpQuery<?> httpQuery = (JHttpQuery<?>) o;
428 
429  if (method != httpQuery.method) return false;
430  if (headers != null ? !headers.equals(httpQuery.headers) : httpQuery.headers != null) return false;
431  if (body != null ? !body.equals(httpQuery.body) : httpQuery.body != null) return false;
432  if (responseBodyType != null ? !responseBodyType.equals(httpQuery.responseBodyType) : httpQuery.responseBodyType != null)
433  return false;
434  if (queryParams != null ? !queryParams.equals(httpQuery.queryParams) : httpQuery.queryParams != null) return false;
435  return path != null ? path.equals(httpQuery.path) : httpQuery.path == null;
436 
437  }
438 
439  @Override
440  public int hashCode() {
441  int result = method.hashCode();
442  result = 31 * result + (headers != null ? headers.hashCode() : 0);
443  result = 31 * result + (body != null ? body.hashCode() : 0);
444  result = 31 * result + (responseBodyType != null ? responseBodyType.hashCode() : 0);
445  result = 31 * result + (queryParams != null ? queryParams.hashCode() : 0);
446  result = 31 * result + (path != null ? path.hashCode() : 0);
447  return result;
448  }
449 }