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============================================
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.eclipse.jetty.util.security.Password;
52 import org.json.simple.JSONObject;
53 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
54 import org.onap.portalapp.portal.scheduler.client.HttpBasicClient;
55 import org.onap.portalapp.portal.scheduler.client.HttpsBasicClient;
56 import org.onap.portalapp.portal.scheduler.restobjects.RestObject;
57 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.springframework.http.HttpStatus;
59 import org.springframework.stereotype.Service;
60 import org.springframework.web.client.HttpClientErrorException;
63 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
65 private static final String PASSWORD_IS_EMPTY = "Password is Empty";
67 private static Client client = null;
69 private MultivaluedHashMap<String, Object> commonHeaders;
72 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
74 public SchedulerRestInterface() {
78 public void initRestClient() {
79 logger.debug(EELFLoggerDelegate.debugLogger, "Starting to initialize rest client");
81 final String username;
82 final String password;
84 String methodName = "initRestClient";
85 /* Setting user name based on properties */
86 String retrievedUsername = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
87 if (retrievedUsername.isEmpty()) {
90 username = retrievedUsername;
93 /* Setting password based on properties */
94 String retrievedPassword = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
95 if (retrievedPassword.isEmpty()) {
96 password = StringUtils.EMPTY;
98 if (retrievedPassword.contains("OBF:")) {
99 password = Password.deobfuscate(retrievedPassword);
101 password = retrievedPassword;
105 if (StringUtils.isBlank(password)) {
106 throw new CredentialException(PASSWORD_IS_EMPTY);
108 } catch (Exception ex) {
109 logger.error(EELFLoggerDelegate.errorLogger, "Unable to initialize rest client", ex);
111 String authString = username + ":" + password;
112 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
113 String authStringEnc = new String(authEncBytes);
115 commonHeaders = new MultivaluedHashMap<String, Object>();
116 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
119 if (!username.isEmpty()) {
121 client = HttpBasicClient.getClient();
124 client = HttpsBasicClient.getClient();
126 } catch (Exception e) {
127 logger.debug(EELFLoggerDelegate.debugLogger, "Unable to initialize rest client");
130 logger.debug(EELFLoggerDelegate.debugLogger, "Client Initialized");
134 @SuppressWarnings("unchecked")
135 public <T> void Get(T t, String sourceId, String path,
136 org.onap.portalapp.portal.scheduler.restobjects.RestObject<T> restObject) throws Exception {
138 String methodName = "Get";
139 String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
141 logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR GET : ", url);
145 final Response cres = client.target(url).request().accept("application/json").headers(commonHeaders).get();
147 int status = cres.getStatus();
148 restObject.setStatusCode(status);
150 t = (T) cres.readEntity(t.getClass());
152 restObject.set(null);
156 } catch (HttpClientErrorException e) {
157 String message = String.format(
158 " HttpClientErrorException: Exception For the POST . MethodName: %s, Url: %s", methodName,url);
159 logger.error(EELFLoggerDelegate.errorLogger, message, e);
160 EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
161 } catch (Exception e) {
162 String message = String.format(
163 "Exception For the POST . MethodName: %s, Url: %s", methodName,url);
165 logger.error(EELFLoggerDelegate.errorLogger, message, e);
166 EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
174 @SuppressWarnings("unchecked")
175 public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
177 String methodName = "Post";
178 String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
179 logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR POST : "+ url);
185 // Change the content length
186 final Response cres = client.target(url).request().accept("application/json").headers(commonHeaders)
187 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
189 if (cres.getEntity() != null) {
190 t = (T) cres.readEntity(t.getClass());
197 int status = cres.getStatus();
198 restObject.setStatusCode(status);
200 if (status >= 200 && status <= 299) {
201 String message = String.format(
202 " REST api POST was successful!", methodName);
203 logger.debug(EELFLoggerDelegate.debugLogger, message);
206 String message = String.format(
207 " FAILED with http status . MethodName: %s, Status: %s, Url: %s", methodName,status,url);
208 logger.debug(EELFLoggerDelegate.debugLogger, message);
211 } catch (HttpClientErrorException e) {
212 String message = String.format(
213 " HttpClientErrorException: Exception For the POST . MethodName: %s, Url: %s", methodName,url);
214 logger.error(EELFLoggerDelegate.errorLogger, message, e);
215 EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
216 } catch (Exception e) {
217 String message = String.format(
218 " HttpClientErrorException: Exception For the POST . MethodName: %s, Url: %s", methodName,url);
219 logger.error(EELFLoggerDelegate.errorLogger, message, e);
220 EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
226 public void logRequest(JSONObject requestDetails) {
229 @SuppressWarnings("unchecked")
230 public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
232 String methodName = "Delete";
234 Response cres = null;
239 url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
241 cres = client.target(url).request().accept("application/json").headers(commonHeaders)
243 .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke();
244 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
245 // .delete(Entity.entity(r, MediaType.APPLICATION_JSON));
247 int status = cres.getStatus();
248 restObject.setStatusCode(status);
249 if (cres.getEntity() != null) {
250 t = (T) cres.readEntity(t.getClass());
254 } catch (HttpClientErrorException e) {
255 logger.error(EELFLoggerDelegate.errorLogger, " HttpClientErrorException:Exception For the Delete",
257 EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
258 } catch (Exception e) {
259 logger.error(EELFLoggerDelegate.errorLogger, "Exception For the Delete", methodName, url, e);
260 EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
265 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
266 return clazz.newInstance();