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.capitalone.dashboard.client.RestClient Maven / Gradle / Ivy
package com.capitalone.dashboard.client;
import com.capitalone.dashboard.util.Encryption;
import com.capitalone.dashboard.util.EncryptionException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestOperations;
import java.nio.charset.StandardCharsets;
import java.util.Map;
@Component
public class RestClient {
private static final Log LOG = LogFactory.getLog(RestClient.class);
private final RestOperations restOperations;
@Autowired
public RestClient (RestOperationsSupplier restOperationsSupplier) {
this .restOperations = restOperationsSupplier.get();
}
private ResponseEntity makeRestCallPost (String url, HttpEntity httpEntity) {
long start = System.currentTimeMillis();
ResponseEntity response;
String status = null ;
try {
response = restOperations.exchange(url, HttpMethod.POST, httpEntity, String.class);
status = response.getStatusCode().toString();
} catch (HttpStatusCodeException e) {
status = e.getStatusCode().toString();
LOG.info("status=" + status + ", requestBody=" + httpEntity.getBody());
throw e;
} finally {
long end = System.currentTimeMillis();
LOG.info("makeRestCall op=POST url=" + url + ", status=" + status + " duration=" + (end - start));
}
return response;
}
public ResponseEntity makeRestCallPost (String url, HttpHeaders headers, JSONObject body) {
if (headers==null ) headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity(body, headers);
return this .makeRestCallPost(url, httpEntity);
}
public ResponseEntity makeRestCallPost (String url, HttpHeaders headers, String body) {
HttpEntity httpEntity = new HttpEntity(body, headers);
return this .makeRestCallPost(url, httpEntity);
}
public ResponseEntity makeRestCallPost (String url, JSONObject body) {
if (restOperations == null ) { return null ; }
return this .makeRestCallPost(url, (HttpHeaders)null , body);
}
public ResponseEntity makeRestCallPost (String url, String headerKey, String token, JSONObject body) {
if (restOperations == null ) { return null ; }
HttpHeaders headers = null ;
if (StringUtils.isNotEmpty(headerKey) && StringUtils.isNotEmpty(token)) {
headers = createHeaders(headerKey, token);
}
return this .makeRestCallPost(url, headers, body);
}
public ResponseEntity makeRestCallPost (String url, RestUserInfo userInfo, JSONObject body) {
if (restOperations == null ) { return null ; }
HttpHeaders headers = null ;
if ((userInfo != null )) {
headers = createHeaders(userInfo.getFormattedString());
}
return this .makeRestCallPost(url, headers, body);
}
public ResponseEntity makeRestCallGet (String url, HttpHeaders headers) throws RestClientException {
long start = System.currentTimeMillis();
ResponseEntity response;
String status = null ;
try {
HttpEntity entity = headers==null ?null :new HttpEntity(headers);
response = restOperations.exchange(url, HttpMethod.GET, entity, String.class);
status = response.getStatusCode().toString();
} catch (HttpStatusCodeException e) {
status = e.getStatusCode().toString();
throw e;
} catch (Exception e) {
status = e.getClass().getCanonicalName();
throw e;
} finally {
long end = System.currentTimeMillis();
LOG.info("makeRestCall op=GET url=" + url + " status=" + status + " duration=" + (end - start));
}
return response;
}
public ResponseEntity makeRestCallGet (String url) throws RestClientException {
if (restOperations == null ) { return null ; }
return this .makeRestCallGet(url, (HttpHeaders)null );
}
public ResponseEntity makeRestCallGet (String url, String headerKey, String token) throws RestClientException {
if (restOperations == null ) { return null ; }
HttpHeaders headers = null ;
if (StringUtils.isNotEmpty(headerKey) && StringUtils.isNotEmpty(token)) {
headers = createHeaders(headerKey, token);
}
return this .makeRestCallGet(url, headers);
}
public ResponseEntity makeRestCallGet (String url, RestUserInfo userInfo) throws RestClientException {
if (restOperations == null ) { return null ; }
HttpHeaders headers = null ;
if (userInfo != null && StringUtils.isNotEmpty(userInfo.getFormattedString())) {
headers = createHeaders(userInfo.getFormattedString());
}
return this .makeRestCallGet(url, headers);
}
protected HttpHeaders createHeaders (RestUserInfo restUserInfo) {
return createHeaders(restUserInfo.getFormattedString());
}
protected HttpHeaders createHeaders (String user) {
byte [] encodedAuth = Base64.encodeBase64(user.getBytes(StandardCharsets.US_ASCII));
String authHeader = "Basic " + new String(encodedAuth);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization" , authHeader);
return headers;
}
protected HttpHeaders createHeaders (String key, String token) {
String authHeader = key.trim() + " " + token.trim();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization" , authHeader);
return headers;
}
public JSONObject parseAsObject (ResponseEntity response) throws ParseException {
if (response == null ) { return new JSONObject(); }
return (JSONObject) new JSONParser().parse(response.getBody());
}
public JSONArray parseAsArray (ResponseEntity response) throws ParseException {
return (JSONArray) new JSONParser().parse(response.getBody());
}
public JSONArray getArray (JSONObject json, String key) {
if (json == null ) { return new JSONArray(); }
if (json.get(key) == null ) { return new JSONArray(); }
return (JSONArray) json.get(key);
}
public String getString (Object obj, String key) {
if (obj == null ) { return "" ; }
if (obj instanceof Map) {
Map map = (Map) obj;
Object value = map.get(key);
return (value == null ) ? "" : value.toString();
} else if (obj instanceof JSONObject) {
JSONObject json = (JSONObject) obj;
Object value = json.get(key);
return (value == null ) ? "" : value.toString();
}
return "" ;
}
public Integer getInteger (Object obj, String key) throws NumberFormatException {
return NumberUtils.toInt(getString(obj, key));
}
public Object getAsObject (Object obj, String key) {
if (obj == null ) { return null ; }
if (obj instanceof Map) {
Map map = (Map) obj;
return map.get(key);
} else if (obj instanceof JSONObject) {
JSONObject json = (JSONObject) obj;
return json.get(key);
}
return null ;
}
public boolean getBoolean (Object obj, String key) {
if (obj == null ) { return false ; }
if (obj instanceof Map) {
Map map = (Map) obj;
return (Boolean) map.get(key);
} else if (obj instanceof JSONObject) {
JSONObject json = (JSONObject) obj;
return (Boolean) json.get(key);
}
return false ;
}
public Long getLong (Object obj, String key) throws NumberFormatException {
return NumberUtils.toLong(getString(obj, key));
}
public static String decryptString (String string, String key) {
if (StringUtils.isEmpty(string)) { return "" ; }
String result = "" ;
try {
result = Encryption.decryptString(string, key);
} catch (EncryptionException e) {
LOG.error(e.getMessage());
}
return result;
}
}