diff options
author | koblosz <sandra.koblosz@nokia.com> | 2018-08-20 16:42:57 +0200 |
---|---|---|
committer | koblosz <sandra.koblosz@nokia.com> | 2018-08-21 09:28:10 +0200 |
commit | d47fd3bfc9aa456091c356122d149166e853f953 (patch) | |
tree | f61315b2d6b716d54414b43d458e02fc969011d3 /vid-app-common/src/main/java/org/onap | |
parent | 6447784fdd770e8a2b4ef2c52ba206d35c0fd325 (diff) |
VID-266 added stub server for testing
Change-Id: I39b8fca2df4c45123e2308142dfbb43c737f7707
Issue-ID: VID-266
Signed-off-by: Koblosz, Sandra <sandra.koblosz@nokia.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java | 231 |
1 files changed, 121 insertions, 110 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 abd03f3e0..9a7522b52 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 @@ -18,123 +18,134 @@ import javax.ws.rs.client.Client; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.Response; import java.util.Collections; +import java.util.function.Function; import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; @Service public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { - private Client client = null; - - @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"); - - public void initRestClient() { - logger.info("Starting to initialize rest client "); - - final String username; - final String password; - + 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"); + + @Autowired + public SchedulerRestInterface(){ + this.propertyGetter = SystemProperties::getProperty; + } + + public SchedulerRestInterface(Function<String, String> propertyGetter){ + this.propertyGetter = propertyGetter; + } + + public void initRestClient() { + logger.info("Starting to initialize rest client "); + + final String username; + final String password; + /*Setting user name based on properties*/ - String retrievedUsername = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL); - if(retrievedUsername.isEmpty()) { - username = ""; - } else { - username = retrievedUsername; - } - + String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL); + if(retrievedUsername.isEmpty()) { + username = ""; + } else { + username = retrievedUsername; + } + /*Setting password based on properties*/ - String retrievedPassword = SystemProperties.getProperty(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 = new MultivaluedHashMap<String, Object> (); - 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); - } - - 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 = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path; - initRestClient(); - 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); - - if (status == 200) { - t = (T) cres.readEntity(t.getClass()); - restObject.set(t); - - } else { - throw new GenericUncheckedException(methodName + " with status="+ status + ", url= " + url ); - } - - } - - public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) { - - initRestClient(); - - String url = SystemProperties.getProperty(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(); - restObject.setStatusCode(status); - - t = (T) cres.readEntity(t.getClass()); - restObject.set(t); - - } - - public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException - { - return clazz.newInstance(); - } + 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 = 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); + } + + 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); + initRestClient(); + 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); + + if (status == 200) { + t = (T) cres.readEntity(t.getClass()); + restObject.set(t); + + } else { + 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); + 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(); + restObject.setStatusCode(status); + + t = (T) cres.readEntity(t.getClass()); + restObject.set(t); + + } + + public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException + { + return clazz.newInstance(); + } } |