summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerRestInterface.java
blob: 6b4319edea4b8e272e7174fc60d5739bfadbe204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
package org.onap.portalapp.portal.scheduler;

import java.util.Collections;
import java.util.Date;

import javax.security.auth.login.CredentialException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.impl.ResponseImpl;
import org.eclipse.jetty.util.security.Password;
import org.json.simple.JSONObject;
import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
import org.onap.portalapp.portal.logging.logic.EPLogUtil;
import org.onap.portalapp.portal.scheduler.restobjects.RestObject;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
import org.onap.portalsdk.core.onboarding.util.CipherUtil;
import org.onap.portalsdk.core.onboarding.util.KeyConstants;
import org.onap.portalsdk.core.onboarding.util.KeyProperties;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;

import lombok.NoArgsConstructor;

@SuppressWarnings("MalformedFormatString")
@Service
@NoArgsConstructor
public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
	private static final String APPLICATION_JSON = "application/json";
	private static final String PASSWORD_IS_EMPTY = "Password is Empty";
	private static final String HTTP_CLIENT_ERROR = " HttpClientErrorException: Exception For the POST  ."
			+ " MethodName: %APPLICATION_JSON, Url: %APPLICATION_JSON";

	private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
	private static WebClient client = null;
	private static Gson gson = null;

	private MultivaluedHashMap<String, String> commonHeaders;

	private static void init() {
		logger.debug(EELFLoggerDelegate.debugLogger, "initializing");
		GsonBuilder builder = new GsonBuilder();

		// Register an adapter to manage the date types as long values
		builder.registerTypeAdapter(Date.class,
				(JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));

		gson = builder.create();
	}

	public void initRestClient(String URI) {
		logger.debug(EELFLoggerDelegate.debugLogger, "Starting to initialize rest client");

		init();

		final String username;
		String password;

		/* Setting user name based on properties */
		String retrievedUsername = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
		if (retrievedUsername.isEmpty()) {
			username = "";
		} else {
			username = retrievedUsername;
		}

		/* Setting password based on properties */
		String retrievedPassword = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
		if (retrievedPassword.isEmpty()) {
			password = StringUtils.EMPTY;
		} else {

			try {
				password = CipherUtil.decryptPKC(retrievedPassword,
						KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
			} catch (CipherUtilException e) {
				logger.error(EELFLoggerDelegate.errorLogger, "failed to decrypt; Using as is", e);
				password = retrievedPassword;
			}

		}
		try {
			if (StringUtils.isBlank(password)) {
				throw new CredentialException(PASSWORD_IS_EMPTY);
			}
		} catch (Exception ex) {
			logger.error(EELFLoggerDelegate.errorLogger, "Unable to initialize rest client", ex);
		}
		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 = HttpBasicClient.getClient();
		// } else {
		//
		// client = HttpsBasicClient.getClient();
		// }
		// } catch (Exception e) {
		// logger.debug(EELFLoggerDelegate.debugLogger, "Unable to initialize rest
		// client",e.getMessage());
		//
		// }

		client = WebClient.create(URI);
		client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
		// client.path("");
		client.headers(commonHeaders);

		logger.debug(EELFLoggerDelegate.debugLogger, "Client Initialized");

	}
	
	@SuppressWarnings("unchecked")
	public <T> void Get(T t, String sourceId, String path,
			org.onap.portalapp.portal.scheduler.restobjects.RestObject<T> restObject) {

		String methodName = "Get";
		String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;

		logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR GET : ", url);
		try {
			initRestClient(url);

			// final Response cres =
			// client.target(url).request().accept(APPLICATION_JSON).headers(commonHeaders).get();
			final ResponseImpl cres = (ResponseImpl) client.get();

			logger.debug(EELFLoggerDelegate.debugLogger, "The implemenation class of Response : ",
					cres.getClass().getName());
			int status = cres.getStatus();
			restObject.setStatusCode(status);

			if (cres.getEntity() != null) {
				try {
					String str = (cres).readEntity(String.class);
					if (t.getClass().isAssignableFrom(String.class)) {
						t = (T) str;

					} else {
						t = (T) gson.fromJson(str, t.getClass());
					}

				} catch (Exception e) {
					EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e);
				}
			} else {
				t = null;
				restObject.set(null);
			}

			if ("".equals(t)) {
				restObject.set(null);
			} else {
				restObject.set(t);
			}
		} catch (HttpClientErrorException e) {
			String message = String.format(HTTP_CLIENT_ERROR, methodName, url);
			logger.error(EELFLoggerDelegate.errorLogger, message, e);
			EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
		} catch (Exception e) {
			String message = String.format(
					"Exception For the POST . MethodName: %APPLICATION_JSON, Url: %APPLICATION_JSON", methodName, url);

			logger.error(EELFLoggerDelegate.errorLogger, message, e);
			EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());

			throw e;

		}

	}

	@SuppressWarnings("unchecked")
	public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) {

		String methodName = "Post";
		String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
		logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR POST : " + url);

		try {

			initRestClient(url);

			// Change the content length
			final ResponseImpl cres = (ResponseImpl) client.post(requestDetails.toJSONString());

			if (cres != null && cres.getEntity() != null) {

				try {
					String str = (cres).readEntity(String.class);
					if (t.getClass().isAssignableFrom(String.class)) {
						t = (T) str;

					} else {
						t = (T) gson.fromJson(str, t.getClass());
					}

				} catch (Exception e) {
					EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e);
				}
				restObject.set(t);
			} else {
				restObject.set(null);
			}

			int status = cres != null ? cres.getStatus() : 0;
			restObject.setStatusCode(status);

			if (status >= 200 && status <= 299) {
				String message = String.format(" REST api POST was successful!", methodName);
				logger.debug(EELFLoggerDelegate.debugLogger, message);

			} else {
				String message = String.format(
						" FAILED with http status  . MethodName: %APPLICATION_JSON, Status: %APPLICATION_JSON, Url: %APPLICATION_JSON",
						methodName, status, url);
				logger.debug(EELFLoggerDelegate.debugLogger, message);
			}

		} catch (HttpClientErrorException e) {
			String message = String.format(HTTP_CLIENT_ERROR, methodName, url);
			logger.error(EELFLoggerDelegate.errorLogger, message, e);
			EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value());
		} catch (Exception e) {
			String message = String.format(HTTP_CLIENT_ERROR, methodName, url);
			logger.error(EELFLoggerDelegate.errorLogger, message, e);
			EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value());
			throw e;
		}
	}

	@Override
	public void logRequest(JSONObject requestDetails) {
		throw new UnsupportedOperationException();
	}

}