1 package com.griddynamics.jagger.invoker.v2;
3 import org.apache.commons.lang.StringUtils;
4 import org.springframework.http.HttpHeaders;
5 import org.springframework.http.HttpMethod;
7 import java.io.Serializable;
8 import java.util.HashMap;
12 import static com.google.common.collect.Lists.newArrayList;
45 @SuppressWarnings(
"unused")
46 public class JHttpQuery<T> implements Serializable {
47 public static final JHttpQuery EMPTY_QUERY = null;
49 private HttpMethod method;
50 private HttpHeaders headers;
52 private Class responseBodyType;
53 private Map<String, String> queryParams;
78 return method(HttpMethod.GET);
90 return method(HttpMethod.POST);
102 return method(HttpMethod.PUT);
114 return method(HttpMethod.PATCH);
126 return method(HttpMethod.DELETE);
138 return method(HttpMethod.TRACE);
150 return method(HttpMethod.HEAD);
162 return method(HttpMethod.OPTIONS);
179 this.headers = headers;
198 this.headers.putAll(headers);
215 headers.put(key, values);
232 headers.put(key, newArrayList(value));
251 cookies.entrySet().forEach(entry -> this.headers.add(
"Cookie", entry.getKey() +
"=" + entry.getValue()));
268 this.headers.add(
"Cookie", name +
"=" + value);
302 this.responseBodyType = responseBodyType;
321 this.queryParams = queryParams;
335 if (queryParams == null)
336 this.queryParams =
new HashMap<>();
337 this.queryParams.
put(paramName, paramValue);
351 this.path = StringUtils.startsWith(path,
"/") ? path :
"/" + path;
365 String path = StringUtils.join(paths,
"/");
380 String path = String.format(formatString, args);
393 return responseBodyType;
408 private void initHeadersIfNull() {
409 if (this.headers == null)
410 this.headers =
new HttpHeaders();
413 public static JHttpQuery
copyOf(JHttpQuery jHttpQuery) {
414 if (jHttpQuery == null)
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());
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;
439 return "JHttpQuery{" +
441 ", headers=" + headers +
443 ", responseBodyType=" + responseBodyType +
444 ", queryParams=" + queryParams +
445 ", path='" + path +
'\'' +
451 if (
this == o)
return true;
452 if (o == null || getClass() != o.getClass())
return false;
454 JHttpQuery<?> httpQuery = (JHttpQuery<?>) o;
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)
461 if (queryParams != null ? !queryParams.equals(httpQuery.queryParams) : httpQuery.queryParams != null)
return false;
462 return path != null ? path.equals(httpQuery.path) : httpQuery.path == null;
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);