summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/restful/client/SharedContextRestClient.java
blob: f017a7df01e11da899dce814c07760b05e6f601e (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file 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.
 * ================================================================================
 */
package org.openecomp.portalsdk.core.restful.client;

import java.net.URI;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.utils.URIBuilder;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.onboarding.crossapi.PortalApiConstants;
import org.openecomp.portalsdk.core.onboarding.crossapi.PortalApiProperties;
import org.openecomp.portalsdk.core.restful.domain.SharedContext;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Provides convenience methods to use the shared-context service at Portal.
 * This hides all JSON; instead it accepts and returns Java objects.
 * Usage caveats (repeated from superclass):
 * <OL>
 * <LI>Must be auto-wired by Spring, because this in turn auto-wires a data
 * access service to read application credentials from the FN_APP table.
 * <LI>If HTTP access is used and the server uses a self-signed certificate, the
 * local trust store must be extended appropriately. The HTTP client throws
 * exceptions if the JVM cannot validate the server certificate.
 * </OL>
 */
@Component
public class SharedContextRestClient extends PortalRestClientBase {

	@Autowired
	SystemProperties systemProperties;
	
	EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextRestClient.class);

	/**
	 * Reusable JSON (de)serializer
	 */
	private final ObjectMapper mapper = new ObjectMapper();

	/**
	 * Gets the shared-context value for the specified context ID and key.
	 * 
	 * @param contextId
	 *            An Ecomp Portal session ID
	 * @param key
	 *            Key for the shared-context entry; e.g., "lastName"
	 * @return SharedContext object; null if not found.
	 * @throws Exception
	 */
	public SharedContext getContextValue(String contextId, String key) throws Exception {
		HttpStatusAndResponse hsr = getContext("get", contextId, key);
		logger.info(EELFLoggerDelegate.debugLogger, "getSharedContext: resp is " + hsr);
		if (hsr == null) {
			logger.error(EELFLoggerDelegate.applicationLogger, "getContextValue: unexpected null response");
			return null;
		}
		SharedContext jsonObj = null;
		try {
			jsonObj = mapper.readValue(hsr.getResponse(), SharedContext.class);
		} catch (JsonMappingException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "getContextValue: failed to map response onto object" + ex.getMessage());
		} catch (JsonParseException ex) {
			logger.info(EELFLoggerDelegate.applicationLogger, "getContextValue: failed to parse response" + ex.getMessage());
		}
		if (jsonObj != null && jsonObj.getResponse() != null)
			return null;
		return jsonObj;
	}

	/**
	 * Gets user information for the specified context ID.
	 * 
	 * @param contextId
	 *            An Ecomp Portal session ID
	 * @return List of SharedContext objects corresponding to the following
	 *         keys: USER_FIRST_NAME, USER_LAST_NAME, USER_EMAIL and
	 *         USER_ORGUSERID; empty if none were found; null if an error happens.
	 * @throws Exception
	 */
	public List<SharedContext> getUserContext(String contextId) throws Exception {
		HttpStatusAndResponse hsr = getContext("get_user", contextId, null);
		logger.info(EELFLoggerDelegate.debugLogger, "getUserContext: resp is " + hsr);
		if (hsr == null) {
			logger.error(EELFLoggerDelegate.applicationLogger, "getUserContext: unexpected null response");
			return null;
		}
		List<SharedContext> jsonList = null;
		try {
			TypeReference<List<SharedContext>> typeRef = new TypeReference<List<SharedContext>>() {
			};
			jsonList = mapper.readValue(hsr.getResponse(), typeRef);
		} catch (JsonMappingException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "getUserContext: failed to map response onto object" + ex.getMessage());
		} catch (JsonParseException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "getUserContext: failed to parse response" + ex.getMessage());
		}
		return jsonList;
	}

	/**
	 * Checks whether a shared-context entry exists for the specified context ID
	 * and key.
	 * 
	 * @param contextId
	 *            An Ecomp Portal session ID
	 * @param key
	 *            Key for the shared-context entry; e.g., "lastName"
	 * @return True if the object exists, false otherwise; null on error.
	 * @throws Exception
	 */
	public Boolean checkSharedContext(String contextId, String key) throws Exception {
		HttpStatusAndResponse hsr = getContext("check", contextId, key);
		logger.info(EELFLoggerDelegate.debugLogger, "checkSharedContext: resp is " + hsr);
		if (hsr == null) {
			logger.error(EELFLoggerDelegate.applicationLogger, "checkSharedContext: unexpected null response");
			return null;
		}
		String response = null;
		try {
			SharedContext jsonObj = mapper.readValue(hsr.getResponse(), SharedContext.class);
			response = jsonObj.getResponse();
		} catch (JsonMappingException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "checkSharedContext: failed to map response onto object" + ex.getMessage());
		} catch (JsonParseException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "checkSharedContext: failed to parse response" + ex.getMessage());
		}
		if (response == null)
			return null;
		return ("exists".equals(response));
	}

	/**
	 * Removes a shared-context entry with the specified context ID and key.
	 * 
	 * @param contextId
	 *            An Ecomp Portal session ID
	 * @param key
	 *            Key for the shared-context entry; e.g., "lastName"
	 * @return True if the entry was removed, false otherwise; null on error.
	 * @throws Exception
	 */
	public Boolean removeSharedContext(String contextId, String key) throws Exception {
		HttpStatusAndResponse hsr = getContext("remove", contextId, key);
		logger.info(EELFLoggerDelegate.debugLogger, "removeSharedContext: resp is " + hsr);
		if (hsr == null) {
			logger.error(EELFLoggerDelegate.applicationLogger, "removeSharedContext: unexpected null response");
			return null;
		}
		SharedContext jsonObj = null;
		try {
			jsonObj = mapper.readValue(hsr.getResponse(), SharedContext.class);
		} catch (JsonMappingException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "removeSharedContext: failed to map response onto object" + ex.getMessage());
		} catch (JsonParseException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "removeSharedContext: failed to parse response" + ex.getMessage());
		}
		if (jsonObj == null)
			return null;
		String response = jsonObj.getResponse();
		return ("removed".equals(response));
	}

	/**
	 * Clears the shared context for the specified context ID; i.e., removes all
	 * key-value pairs.
	 * 
	 * @param contextId
	 *            An Ecomp Portal session ID
	 * @return Number of key-value pairs removed; -1 if not found or any
	 *         problems occur.
	 * @throws Exception
	 */
	public int clearSharedContext(String contextId) throws Exception {
		HttpStatusAndResponse hsr = getContext("remove", contextId, null);
		logger.info(EELFLoggerDelegate.debugLogger, "clearSharedContext: resp is " + hsr);
		if (hsr == null) {
			logger.error(EELFLoggerDelegate.applicationLogger, "clearSharedContext: unexpected null response");
			return -1;
		}
		SharedContext jsonObj = null;
		try {
			jsonObj = mapper.readValue(hsr.getResponse(), SharedContext.class);
		} catch (JsonMappingException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "clearSharedContext: failed to map response onto object" + ex.getMessage());
		} catch (JsonParseException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "clearSharedContext: failed to parse response" + ex.getMessage());
		}
		if (jsonObj == null)
			return -1;
		String response = jsonObj.getResponse();
		if (response == null)
			return -1;
		return Integer.parseInt(response);
	}

	/**
	 * Creates a shared-context entry.
	 * 
	 * @param contextId
	 *            An Ecomp Portal session ID
	 * @param key
	 *            Key for the shared-context entry; e.g., "lastName"
	 * @param value
	 *            Value for the entry
	 * @throws Exception
	 * @return True if the object previously existed, false otherwise; null if
	 *         any problem happened.
	 */
	public Boolean setSharedContext(String contextId, String key, String value) throws Exception {
		String body = buildContext(contextId, key, value);
		HttpStatusAndResponse hsr = postContext("set", body);
		logger.info(EELFLoggerDelegate.debugLogger, "setSharedContext: resp is " + hsr);
		if (hsr == null) {
			logger.error(EELFLoggerDelegate.applicationLogger, "setSharedContext: unexpected null response");
			return null;
		}
		SharedContext jsonObj = null;
		try {
			jsonObj = mapper.readValue(hsr.getResponse(), SharedContext.class);
		} catch (JsonMappingException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "setSharedContext: failed to map response onto object" + ex.getMessage());
		} catch (JsonParseException ex) {
			logger.error(EELFLoggerDelegate.applicationLogger, "setSharedContext: failed to parse response" + ex.getMessage());
		}
		if (jsonObj == null)
			return null;
		String response = jsonObj.getResponse();
		return ("replaced".equals(response));
	}

	/**
	 * Builds the full URL with the specified parameters, then calls the method
	 * that adds credentials and GETs.
	 * 
	 * @param requestPath
	 * @param contextId
	 * @param contextKey
	 * @return HttpStatusAndResponse object; may be null.
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private HttpStatusAndResponse getContext(String requestPath, String contextId, String contextKey) throws Exception{
		String restUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL);
		String portalDomain = restUrl.substring(0, restUrl.lastIndexOf('/'));
		String contextRestUrl = portalDomain + "/context";						 
		if (contextRestUrl == null)
			throw new Exception("getContext: failed to get property " + SystemProperties.ECOMP_SHARED_CONTEXT_REST_URL);
		URIBuilder uriBuilder = new URIBuilder(contextRestUrl + "/" + requestPath);
		uriBuilder.addParameter("context_id", contextId);
		if (contextKey != null)
			uriBuilder.addParameter("ckey", contextKey);
		final URI uri = uriBuilder.build();
		return getRestWithCredentials(uri);
	}

	/**
	 * Builds the full URL, then calls the method that adds credentials and
	 * POSTs.
	 * 
	 * @param requestPath
	 * @param contextId
	 * @param contextKey
	 * @return HttpStatusAndResponse object; may be null.
	 * @throws Exception
	 */
	private HttpStatusAndResponse postContext(String requestPath, String json) throws Exception {
		String contextRestUrl = SystemProperties.getProperty(SystemProperties.ECOMP_SHARED_CONTEXT_REST_URL);
		if (contextRestUrl == null)
			throw new Exception("postContext: failed to get property " + SystemProperties.ECOMP_SHARED_CONTEXT_REST_URL);
		URIBuilder uriBuilder = new URIBuilder(contextRestUrl + "/" + requestPath);
		URI uri = uriBuilder.build();
		return postRestWithCredentials(uri, json);
	}

	/**
	 * Builds a JSON block with a single shared-context entry.
	 * 
	 * @param cxid
	 *            Context ID
	 * @param ckey
	 *            Context Key
	 * @param cvalue
	 *            Context value
	 * @return JSON block
	 */
	private String buildContext(String cxid, String ckey, String cvalue) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		HashMap<String, String> stringMap = new HashMap<String, String>();
		stringMap.put("context_id", cxid);
		stringMap.put("ckey", ckey);
		stringMap.put("cvalue", cvalue);
		String json = mapper.writeValueAsString(stringMap);
		return json;
	}

	// Simple test scaffold
	public static void main(String[] args) throws Exception {
		
		SharedContextRestClient client = new SharedContextRestClient();
		SharedContext get = client.getContextValue("abc", "123");
		System.out.println("Get yields " + get.toString());
	}

}