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 
48  private HttpMethod method;
49  private HttpHeaders headers;
50  private T body;
51  private Class responseBodyType;
52  private Map<String, String> queryParams;
53  private String path;
54 
63  public JHttpQuery<T> method(HttpMethod method) {
64  this.method = method;
65  return this;
66  }
67 
76  public JHttpQuery<T> get() {
77  return method(HttpMethod.GET);
78  }
79 
88  public JHttpQuery<T> post() {
89  return method(HttpMethod.POST);
90  }
91 
100  public JHttpQuery<T> put() {
101  return method(HttpMethod.PUT);
102  }
103 
112  public JHttpQuery<T> patch() {
113  return method(HttpMethod.PATCH);
114  }
115 
124  public JHttpQuery<T> delete() {
125  return method(HttpMethod.DELETE);
126  }
127 
136  public JHttpQuery<T> trace() {
137  return method(HttpMethod.TRACE);
138  }
139 
148  public JHttpQuery<T> head() {
149  return method(HttpMethod.HEAD);
150  }
151 
161  return method(HttpMethod.OPTIONS);
162  }
163 
177  public JHttpQuery<T> headers(HttpHeaders headers) {
178  this.headers = headers;
179  return this;
180  }
181 
195  public JHttpQuery<T> headers(Map<String, List<String>> headers) {
196  initHeadersIfNull();
197  this.headers.putAll(headers);
198  return this;
199  }
200 
212  public JHttpQuery<T> header(String key, List<String> values) {
213  initHeadersIfNull();
214  headers.put(key, values);
215  return this;
216  }
217 
229  public JHttpQuery<T> header(String key, String value) {
230  initHeadersIfNull();
231  headers.put(key, newArrayList(value));
232  return this;
233  }
234 
248  public JHttpQuery<T> cookies(Map<String, String> cookies) {
249  initHeadersIfNull();
250  cookies.entrySet().forEach(entry -> this.headers.add("Cookie", entry.getKey() + "=" + entry.getValue()));
251  return this;
252  }
253 
265  public JHttpQuery<T> cookie(String name, String value) {
266  initHeadersIfNull();
267  this.headers.add("Cookie", name + "=" + value);
268  return this;
269  }
270 
285  public JHttpQuery<T> body(T body) {
286  this.body = body;
287  return this;
288  }
289 
300  public JHttpQuery<T> responseBodyType(Class responseBodyType) {
301  this.responseBodyType = responseBodyType;
302  return this;
303  }
304 
319  public JHttpQuery<T> queryParams(Map<String, String> queryParams) {
320  this.queryParams = queryParams;
321  return this;
322  }
323 
333  public JHttpQuery<T> queryParam(String paramName, String paramValue) {
334  if (queryParams == null)
335  this.queryParams = new HashMap<>();
336  this.queryParams.put(paramName, paramValue);
337  return this;
338  }
339 
349  public JHttpQuery<T> path(String path) {
350  this.path = StringUtils.startsWith(path, "/") ? path : "/" + path;
351  return this;
352  }
353 
363  public JHttpQuery<T> path(String... paths) {
364  String path = StringUtils.join(paths, "/");
365  return path(path);
366  }
367 
378  public JHttpQuery<T> path(String formatString, Object... args) {
379  String path = String.format(formatString, args);
380  return path(path);
381  }
382 
383  public HttpMethod getMethod() {
384  return method;
385  }
386 
387  public T getBody() {
388  return body;
389  }
390 
391  public Class getResponseBodyType() {
392  return responseBodyType;
393  }
394 
395  public HttpHeaders getHeaders() {
396  return headers;
397  }
398 
399  public Map<String, String> getQueryParams() {
400  return queryParams;
401  }
402 
403  public String getPath() {
404  return path;
405  }
406 
407  private void initHeadersIfNull() {
408  if (this.headers == null)
409  this.headers = new HttpHeaders();
410  }
411 
412  @Override
413  public String toString() {
414  return "JHttpQuery{" +
415  "method=" + method +
416  ", headers=" + headers +
417  ", body=" + body +
418  ", responseBodyType=" + responseBodyType +
419  ", queryParams=" + queryParams +
420  ", path='" + path + '\'' +
421  '}';
422  }
423 
424  @Override
425  public boolean equals(Object o) {
426  if (this == o) return true;
427  if (o == null || getClass() != o.getClass()) return false;
428 
429  JHttpQuery<?> httpQuery = (JHttpQuery<?>) o;
430 
431  if (method != httpQuery.method) return false;
432  if (headers != null ? !headers.equals(httpQuery.headers) : httpQuery.headers != null) return false;
433  if (body != null ? !body.equals(httpQuery.body) : httpQuery.body != null) return false;
434  if (responseBodyType != null ? !responseBodyType.equals(httpQuery.responseBodyType) : httpQuery.responseBodyType != null)
435  return false;
436  if (queryParams != null ? !queryParams.equals(httpQuery.queryParams) : httpQuery.queryParams != null) return false;
437  return path != null ? path.equals(httpQuery.path) : httpQuery.path == null;
438 
439  }
440 
441  @Override
442  public int hashCode() {
443  int result = method.hashCode();
444  result = 31 * result + (headers != null ? headers.hashCode() : 0);
445  result = 31 * result + (body != null ? body.hashCode() : 0);
446  result = 31 * result + (responseBodyType != null ? responseBodyType.hashCode() : 0);
447  result = 31 * result + (queryParams != null ? queryParams.hashCode() : 0);
448  result = 31 * result + (path != null ? path.hashCode() : 0);
449  return result;
450  }
451 }