Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.arextest.web.common.HttpUtils Maven / Gradle / Ivy
package com.arextest.web.common;
import com.google.gson.Gson;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Deprecated
@Slf4j
public class HttpUtils {
private HttpUtils() {
}
public static ResponseEntity post(String url, R request, Class responseClazz) {
return call(url, request, responseClazz, HttpMethod.POST, null, null, null);
}
public static ResponseEntity post(String url, R request, Class responseClazz,
Map headers) {
return call(url, request, responseClazz, HttpMethod.POST, null, headers, null);
}
public static ResponseEntity post(String url, R request, Class responseClazz,
String contentType,
Map headers, Integer timeout) {
return call(url, request, responseClazz, HttpMethod.POST, contentType, headers, timeout);
}
public static ResponseEntity get(String url, Class responseClazz) {
return call(url, null, responseClazz, HttpMethod.GET, null, null, null);
}
public static ResponseEntity get(String url, Class responseClazz, String contentType,
Map headers, Integer timeout) {
return call(url, null, responseClazz, HttpMethod.GET, contentType, headers, timeout);
}
public static ResponseEntity call(String url, R request, Class responseClazz,
HttpMethod httpMethod,
String contentType, Map headers, Integer timeout) {
ResponseEntity responseEntity = new ResponseEntity<>(HttpStatus.OK);
if (Strings.isBlank(url)) {
return responseEntity;
}
try {
RestTemplate restTemplate = new RestTemplate();
if (timeout != null && timeout > 0) {
HttpComponentsClientHttpRequestFactory httpRequestFactory =
new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(timeout);
restTemplate = new RestTemplate(httpRequestFactory);
}
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
httpHeaders.setAll(headers);
}
if (!Strings.isBlank(contentType)) {
httpHeaders.setContentType(MediaType.parseMediaType(contentType));
} else if (httpHeaders.getContentType() == null) {
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
}
HttpEntity requestEntity = new HttpEntity<>(request, httpHeaders);
responseEntity = restTemplate.exchange(url, httpMethod, requestEntity, responseClazz);
} catch (Exception e) {
LogUtils.error(LOGGER,
String.format("[HttpUtils error] url:%1$s, request:%2$s, exception:%3$s", url,
new Gson().toJson(request), e.toString()));
responseEntity = new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
}
return responseEntity;
}
}