summaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main
diff options
context:
space:
mode:
authorOfir Sonsino <os0695@intl.att.com>2018-08-30 08:36:26 +0000
committerGerrit Code Review <gerrit@onap.org>2018-08-30 08:36:26 +0000
commite81a3e864cc302e1c902af6f4ed0d51d30ad1f54 (patch)
tree0aaad53c02608b1edc4fad1d65acbeb1d3b23266 /vid-app-common/src/main
parentf0ee794cbe8322e314c6dc8c2184d6544cbb5362 (diff)
parent296599aa5977fb047d9d0619ec3e463c031b0b49 (diff)
Merge "Using Generic client in SchedulerRestInterface"
Diffstat (limited to 'vid-app-common/src/main')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java151
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/utils/Logging.java10
2 files changed, 66 insertions, 95 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java
index d66ed4947..a4c5b00a8 100644
--- a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java
+++ b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java
@@ -1,23 +1,22 @@
package org.onap.vid.scheduler;
import com.att.eelf.configuration.EELFLogger;
-import org.apache.commons.codec.binary.Base64;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import io.joshworks.restclient.http.HttpResponse;
import org.eclipse.jetty.util.security.Password;
-import org.onap.vid.aai.util.HttpClientMode;
-import org.onap.vid.aai.util.HttpsAuthClient;
-import org.onap.vid.client.HttpBasicClient;
-import org.onap.vid.exceptions.GenericUncheckedException;
-import org.onap.vid.utils.Logging;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.util.SystemProperties;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.onap.vid.client.SyncRestClient;
+import org.onap.vid.client.SyncRestClientInterface;
+import org.onap.vid.exceptions.GenericUncheckedException;
+import org.onap.vid.utils.Logging;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.Response;
+import java.util.Base64;
import java.util.Collections;
+import java.util.Map;
import java.util.function.Function;
import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
@@ -25,126 +24,88 @@ import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
@Service
public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
- private Client client = null;
- private Function<String, String> propertyGetter;
-
- @Autowired
- HttpsAuthClient httpsAuthClient;
-
- private MultivaluedHashMap<String, Object> commonHeaders;
-
- /** The logger. */
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
+ private SyncRestClientInterface syncRestClient;
+ private Function<String, String> propertyGetter;
+ private Map<String, String> commonHeaders;
- public SchedulerRestInterface(){
+ public SchedulerRestInterface() {
this.propertyGetter = SystemProperties::getProperty;
}
- public SchedulerRestInterface(Function<String, String> propertyGetter){
+ public SchedulerRestInterface(Function<String, String> propertyGetter) {
this.propertyGetter = propertyGetter;
}
public void initRestClient() {
logger.info("Starting to initialize rest client ");
+ String authStringEnc = calcEncodedAuthString();
- final String username;
- final String password;
-
- /*Setting user name based on properties*/
- String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
- if(retrievedUsername.isEmpty()) {
- username = "";
- } else {
- username = retrievedUsername;
- }
-
- /*Setting password based on properties*/
- String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
- if(retrievedPassword.isEmpty()) {
- password = "";
- } else {
- if (retrievedPassword.contains("OBF:")) {
- password = Password.deobfuscate(retrievedPassword);
- } else {
- password = retrievedPassword;
- }
- }
-
- String authString = username + ":" + password;
-
- byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
- String authStringEnc = new String(authEncBytes);
+ commonHeaders = Maps.newHashMap();
+ commonHeaders.put("Authorization", "Basic " + authStringEnc);
- commonHeaders = new MultivaluedHashMap<> ();
- commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc));
-
- try {
- if ( !username.isEmpty() ) {
- client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
- }
- else {
-
- client = HttpBasicClient.getClient();
- }
- } catch (Exception e) {
- logger.error(" <== Unable to initialize rest client ", e);
- }
+ syncRestClient = new SyncRestClient();
logger.info("\t<== Client Initialized \n");
}
- public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) {
-
- String methodName = "Get";
- String url = String.format("%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
+ public <T> void Get(T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
initRestClient();
+ String methodName = "Get";
+ String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
- final Response cres = client.target(url)
- .request()
- .accept("application/json")
- .headers(commonHeaders)
- .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
- .get();
- Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
- int status = cres.getStatus();
- restObject.setStatusCode (status);
+ Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
+ .putAll(commonHeaders)
+ .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
+ final HttpResponse<T> response = ((HttpResponse<T>) syncRestClient.get(url, requestHeaders,
+ Collections.emptyMap(), t.getClass()));
+ Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response);
+ int status = response.getStatus();
+ restObject.setStatusCode(status);
if (status == 200) {
- t = (T) cres.readEntity(t.getClass());
+ t = response.getBody();
restObject.set(t);
} else {
- throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status,url));
+ throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status, url));
}
-
}
public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
-
initRestClient();
-
- String url = String.format( "%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
+ String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
- Response cres = client.target(url)
- .request()
- .accept("application/json")
- .headers(commonHeaders)
- .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
- .delete();
- Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
-
- int status = cres.getStatus();
+ Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
+ .putAll(commonHeaders)
+ .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
+ final HttpResponse<T> response = (HttpResponse<T>) syncRestClient.delete(url, requestHeaders, t.getClass());
+
+ Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response);
+
+ int status = response.getStatus();
restObject.setStatusCode(status);
- t = (T) cres.readEntity(t.getClass());
+ t = response.getBody();
restObject.set(t);
-
}
- public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
- {
+ public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
return clazz.newInstance();
}
+ private String calcEncodedAuthString() {
+ String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
+ final String username = retrievedUsername.isEmpty() ? "" : retrievedUsername;
+
+ String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
+ final String password = retrievedPassword.isEmpty() ? "" : getDeobfuscatedPassword(retrievedPassword);
+
+ return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
+ }
+
+ private static String getDeobfuscatedPassword(String password) {
+ return password.contains("OBF:") ? Password.deobfuscate(password) : password;
+ }
}
diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
index e0ee6fbf0..3ac905884 100644
--- a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
+++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.collect.ImmutableList;
+import io.joshworks.restclient.http.HttpResponse;
import org.apache.commons.lang3.StringUtils;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
@@ -101,6 +102,15 @@ public class Logging {
}
}
+ public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) {
+ try {
+ logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.getBody());
+ }
+ catch (ProcessingException | IllegalStateException e) {
+ logger.debug("Received {} {} Status: {} . Failed to read response", method.name(), url, response.getStatus());
+ }
+ }
+
public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) {
logResponse(logger, method, url, response, String.class);
}