2  * ============LICENSE_START==========================================
 
   4  * ===================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ===================================================================
 
   8  * Unless otherwise specified, all software contained herein is licensed
 
   9  * under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this software except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *             http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * Unless otherwise specified, all documentation contained herein is licensed
 
  22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 
  23  * you may not use this documentation except in compliance with the License.
 
  24  * You may obtain a copy of the License at
 
  26  *             https://creativecommons.org/licenses/by/4.0/
 
  28  * Unless required by applicable law or agreed to in writing, documentation
 
  29  * distributed under the License is distributed on an "AS IS" BASIS,
 
  30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  31  * See the License for the specific language governing permissions and
 
  32  * limitations under the License.
 
  34  * ============LICENSE_END============================================
 
  36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  38 package org.onap.portalapp.portal.scheduler;
 
  40 import java.util.Collections;
 
  42 import javax.security.auth.login.CredentialException;
 
  43 import javax.ws.rs.client.Client;
 
  44 import javax.ws.rs.client.Entity;
 
  45 import javax.ws.rs.core.MediaType;
 
  46 import javax.ws.rs.core.MultivaluedHashMap;
 
  47 import javax.ws.rs.core.Response;
 
  49 import org.apache.commons.codec.binary.Base64;
 
  50 import org.apache.commons.lang.StringUtils;
 
  51 import org.drools.compiler.lang.DRL5Expressions.neg_operator_key_return;
 
  52 import org.eclipse.jetty.util.security.Password;
 
  53 import org.json.simple.JSONObject;
 
  54 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
 
  55 import org.onap.portalapp.portal.scheduler.client.HttpBasicClient;
 
  56 import org.onap.portalapp.portal.scheduler.client.HttpsBasicClient;
 
  57 import org.onap.portalapp.portal.scheduler.restobjects.RestObject;
 
  58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  59 import org.springframework.http.HttpStatus;
 
  60 import org.springframework.stereotype.Service;
 
  61 import org.springframework.web.client.HttpClientErrorException;
 
  64 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
 
  66         private static final String PASSWORD_IS_EMPTY = "Password is Empty";
 
  68         private static Client client = null;
 
  70         private MultivaluedHashMap<String, Object> commonHeaders;
 
  73         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
 
  75         public SchedulerRestInterface() {
 
  79         public void initRestClient() {
 
  80                 logger.debug(EELFLoggerDelegate.debugLogger, "Starting to initialize rest client");
 
  82                 final String username;
 
  83                 final String password;
 
  85                 String methodName = "initRestClient";
 
  86                 /* Setting user name based on properties */
 
  87                 String retrievedUsername = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
 
  88                 if (retrievedUsername.isEmpty()) {
 
  91                         username = retrievedUsername;
 
  94                 /* Setting password based on properties */
 
  95                 String retrievedPassword = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
 
  96                 if (retrievedPassword.isEmpty()) {
 
  97                         password = StringUtils.EMPTY;
 
  99                         if (retrievedPassword.contains("OBF:")) {
 
 100                                 password = Password.deobfuscate(retrievedPassword);
 
 102                                 password = retrievedPassword;
 
 106                         if (StringUtils.isBlank(password)) {
 
 107                                 throw new CredentialException(PASSWORD_IS_EMPTY); 
 
 109                 } catch (Exception ex) {
 
 110                         logger.error(EELFLoggerDelegate.errorLogger, "Unable to initialize rest client", ex);
 
 112                 String authString = username + ":" + password;
 
 113                 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
 
 114                 String authStringEnc = new String(authEncBytes);
 
 116                 commonHeaders = new MultivaluedHashMap<String, Object>();
 
 117                 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
 
 120                         if (!username.isEmpty()) {
 
 122                                 client = HttpBasicClient.getClient();
 
 125                                 client = HttpsBasicClient.getClient();
 
 127                 } catch (Exception e) {
 
 128                         logger.debug(EELFLoggerDelegate.debugLogger, "Unable to initialize rest client");
 
 131                 logger.debug(EELFLoggerDelegate.debugLogger, "Client Initialized");
 
 135         @SuppressWarnings("unchecked")
 
 136         public <T> void Get(T t, String sourceId, String path,
 
 137                         org.onap.portalapp.portal.scheduler.restobjects.RestObject<T> restObject) throws Exception {
 
 139                 String methodName = "Get";
 
 140                 String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
 
 142                 logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR GET : ", url);
 
 146                         final Response cres = client.target(url).request().accept("application/json").headers(commonHeaders).get();
 
 148                         int status = cres.getStatus();
 
 149                         restObject.setStatusCode(status);
 
 151                         t = (T) cres.readEntity(t.getClass());
 
 153                                 restObject.set(null);
 
 157                 } catch (HttpClientErrorException e) {
 
 158                         String message = String.format(
 
 159                                         " HttpClientErrorException: Exception For the POST  . MethodName: %s, Url: %s", methodName,url);
 
 160                         logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 161                         EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
 
 162                 } catch (Exception e) {
 
 163                         String message = String.format(
 
 164                                         "Exception For the POST . MethodName: %s, Url: %s", methodName,url);
 
 166                         logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 167                         EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
 
 175         @SuppressWarnings("unchecked")
 
 176         public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
 
 178                 String methodName = "Post";
 
 179                 String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
 
 180                 logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR POST : "+ url);
 
 186                         // Change the content length
 
 187                         final Response cres = client.target(url).request().accept("application/json").headers(commonHeaders)
 
 188                                         .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
 
 190                         if (cres.getEntity() != null) {
 
 191                                 t = (T) cres.readEntity(t.getClass());
 
 198                         int status = cres.getStatus();
 
 199                         restObject.setStatusCode(status);
 
 201                         if (status >= 200 && status <= 299) {
 
 202                                 String message = String.format(
 
 203                                                 " REST api POST was successful!", methodName);
 
 204                                 logger.debug(EELFLoggerDelegate.debugLogger, message);
 
 207                                 String message = String.format(
 
 208                                                 " FAILED with http status  . MethodName: %s, Status: %s, Url: %s", methodName,status,url);
 
 209                                 logger.debug(EELFLoggerDelegate.debugLogger, message);
 
 212                 } catch (HttpClientErrorException e) {
 
 213                         String message = String.format(
 
 214                                         " HttpClientErrorException: Exception For the POST  . MethodName: %s, Url: %s", methodName,url);
 
 215                         logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 216                         EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
 
 217                 } catch (Exception e) {
 
 218                         String message = String.format(
 
 219                                         " HttpClientErrorException: Exception For the POST  . MethodName: %s, Url: %s", methodName,url);
 
 220                         logger.error(EELFLoggerDelegate.errorLogger, message, e);
 
 221                         EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
 
 227         public void logRequest(JSONObject requestDetails) {
 
 230         @SuppressWarnings("unchecked")
 
 231         public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
 
 233                 String methodName = "Delete";
 
 235                 Response cres = null;
 
 240                         url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
 
 242                         cres = client.target(url).request().accept("application/json").headers(commonHeaders)
 
 244                                         .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke();
 
 245                         // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
 
 246                         // .delete(Entity.entity(r, MediaType.APPLICATION_JSON));
 
 248                         int status = cres.getStatus();
 
 249                         restObject.setStatusCode(status);
 
 250                         if (cres.getEntity() != null) {
 
 251                                 t = (T) cres.readEntity(t.getClass());
 
 255                 } catch (HttpClientErrorException e) {
 
 256                         logger.error(EELFLoggerDelegate.errorLogger, " HttpClientErrorException:Exception For the Delete",
 
 258                         EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
 
 259                 } catch (Exception e) {
 
 260                         logger.error(EELFLoggerDelegate.errorLogger, "Exception For the Delete", methodName, url, e);
 
 261                         EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
 
 266         public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
 
 267                 return clazz.newInstance();