From 7f535078ef80a7b7efa3e3325bfccb994fbd00e8 Mon Sep 17 00:00:00 2001 From: "Christopher Lott (cl778h)" Date: Thu, 31 Aug 2017 15:16:38 -0400 Subject: Rename packages to org.onap in 1.4.0-SNAPSHOT 19 - remove openecomp 72 - remediate Sonar scan issues 79 - removed unwanted left menu under Report 90 - apply approved license text Issue: PORTAL-19, PORTAL-72, PORTAL-79, PORTAL-90 Change-Id: I41a0ef5fba623d2242574bd15f2d9fb8029a496c Signed-off-by: Christopher Lott (cl778h) --- .../core/restful/client/HttpStatusAndResponse.java | 42 --- .../core/restful/client/PortalRestClientBase.java | 171 ---------- .../restful/client/SharedContextRestClient.java | 351 --------------------- 3 files changed, 564 deletions(-) delete mode 100644 ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/HttpStatusAndResponse.java delete mode 100644 ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/PortalRestClientBase.java delete mode 100644 ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/SharedContextRestClient.java (limited to 'ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client') diff --git a/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/HttpStatusAndResponse.java b/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/HttpStatusAndResponse.java deleted file mode 100644 index a8242234..00000000 --- a/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/HttpStatusAndResponse.java +++ /dev/null @@ -1,42 +0,0 @@ -/*- - * ================================================================================ - * 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; - -/** - * Holds the status code and body that result from accessing an HTTP URL. - */ -public class HttpStatusAndResponse { - - private int statusCode; - private String response; - - public HttpStatusAndResponse(int status, String resp) { - this.statusCode = status; - this.response = resp; - } - - public int getStatusCode() { - return statusCode; - } - - public String getResponse() { - return response; - } -} diff --git a/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/PortalRestClientBase.java b/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/PortalRestClientBase.java deleted file mode 100644 index b74d87c4..00000000 --- a/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/PortalRestClientBase.java +++ /dev/null @@ -1,171 +0,0 @@ -/*- - * ================================================================================ - * 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.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; - -import javax.servlet.http.HttpServletResponse; - -import org.apache.http.Consts; -import org.apache.http.HttpEntity; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; -import org.openecomp.portalsdk.core.domain.App; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.onboarding.util.CipherUtil; -import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants; -import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties; -import org.openecomp.portalsdk.core.service.AppService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * Provides a basic client to access a REST endpoint at the Portal via get or - * post. Usage caveats: - *
    - *
  1. 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. - *
  2. 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. - *
- */ -@Component -public class PortalRestClientBase { - - EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalRestClientBase.class); - - @Autowired - AppService appService; - - /** - * Constructs and sends a GET request for the URI, with REST application - * credentials in the header as the Portal expects. - * - * @param uri - * URI of the service - * @return Result of the get; null if an error happens - * @throws URISyntaxException - * @throws IOException - * @throws ClientProtocolException - */ - public HttpStatusAndResponse getRestWithCredentials(final URI uri) throws Exception { - - String uebKey = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY); - App app = appService.getDefaultApp(); - if (uebKey == null || app == null || app.getUsername() == null || app.getAppPassword() == null) - throw new Exception("Missing one or more required properties and/or database entries"); - String decryptedPassword = CipherUtil.decrypt(app.getAppPassword()); - CloseableHttpClient httpClient = HttpClients.createDefault(); - HttpGet httpGet = new HttpGet(uri); - httpGet.setHeader("uebkey", uebKey); - httpGet.setHeader("username", app.getUsername()); - httpGet.setHeader("password", decryptedPassword); - - String responseJson = null; - CloseableHttpResponse response = null; - try { - logger.info(EELFLoggerDelegate.debugLogger, "GET from " + uri); - response = httpClient.execute(httpGet); - logger.info(EELFLoggerDelegate.debugLogger, "Status is " + response.getStatusLine()); - if (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) - logger.info(EELFLoggerDelegate.debugLogger, "Status is " + response.getStatusLine().toString()); - HttpEntity entity = response.getEntity(); - if (entity == null) { - logger.info(EELFLoggerDelegate.debugLogger, "Entity is null!"); - } else { - // entity content length is never set. - // this naively tries to read everything. - responseJson = EntityUtils.toString(entity); - logger.info(EELFLoggerDelegate.debugLogger, responseJson); - EntityUtils.consume(entity); - } - } finally { - if (response != null) - response.close(); - } - if (response == null) - return null; - return new HttpStatusAndResponse(response.getStatusLine().getStatusCode(), responseJson); - } - - /** - * Constructs and sends a POST request using the specified body, with REST - * application credentials in the header as the Portal expects. - * - * @param uri - * REST endpoint - * @param json - * Content to post - * @return Result of the post; null if an error happens - * @throws Exception - */ - public HttpStatusAndResponse postRestWithCredentials(final URI uri, final String json) throws Exception { - - String uebKey = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY); - App app = appService.getDefaultApp(); - if (uebKey == null || app == null || app.getUsername() == null || app.getAppPassword() == null) - throw new Exception("Missing one or more required properties and/or database entries"); - - CloseableHttpClient httpClient = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(uri); - httpPost.setHeader("uebkey", uebKey); - httpPost.setHeader("username", app.getUsername()); - httpPost.setHeader("password", app.getAppPassword()); - - StringEntity postEntity = new StringEntity(json, ContentType.create("application/json", Consts.UTF_8)); - httpPost.setEntity(postEntity); - - String responseJson = null; - CloseableHttpResponse response = null; - try { - logger.info(EELFLoggerDelegate.debugLogger, "POST to " + uri); - response = httpClient.execute(httpPost); - logger.info(EELFLoggerDelegate.debugLogger, "Status is " + response.getStatusLine()); - if (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) - throw new Exception("Status is " + response.getStatusLine().toString()); - - HttpEntity entity = response.getEntity(); - if (entity == null) { - logger.info(EELFLoggerDelegate.debugLogger, "Entity is null!"); - } else { - // entity content length is never set. - // this naively tries to read everything. - responseJson = EntityUtils.toString(entity); - logger.info(EELFLoggerDelegate.debugLogger, responseJson); - EntityUtils.consume(entity); - } - } finally { - if (response != null) - response.close(); - } - return new HttpStatusAndResponse(response.getStatusLine().getStatusCode(), responseJson); - } - -} diff --git a/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/SharedContextRestClient.java b/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/SharedContextRestClient.java deleted file mode 100644 index 91f4652a..00000000 --- a/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/restful/client/SharedContextRestClient.java +++ /dev/null @@ -1,351 +0,0 @@ -/*- - * ================================================================================ - * 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.util.PortalApiConstants; -import org.openecomp.portalsdk.core.onboarding.util.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): - *
    - *
  1. 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. - *
  2. 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. - *
- */ -@Component -public class SharedContextRestClient extends PortalRestClientBase { - - @Autowired - SystemProperties systemProperties; - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextRestClient.class); - - /** - * Reusable JSON (de)serializer - */ - private final ObjectMapper mapper = new ObjectMapper(); - - /** - * Builds the URl for the shared context service using the portal.properties - * value for the AUXAPI endpoint. - * - * @throws Exception - * if the ECOMP_REST_URL property is not found - */ - private String getSharedContextUrl() throws Exception { - String restUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL); - if (restUrl == null || restUrl.length() == 0) - throw new Exception("getSharedContextUrl: no property " + PortalApiConstants.ECOMP_REST_URL); - String contextUrl = restUrl + (restUrl.endsWith("/") ? "" : "/") + "context/"; - return contextUrl; - } - - /** - * 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 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 jsonList = null; - try { - TypeReference> typeRef = new TypeReference>() { - }; - 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 - */ - private HttpStatusAndResponse getContext(String requestPath, String contextId, String contextKey) throws Exception { - URIBuilder uriBuilder = new URIBuilder(getSharedContextUrl() + 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 { - URIBuilder uriBuilder = new URIBuilder(getSharedContextUrl() + 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 stringMap = new HashMap(); - 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 { - // ObjectMapper mapper = new ObjectMapper(); - // SharedContext cxt = mapper.readValue("{ \"response\":\"foo\" }", - // SharedContext.class); - SharedContextRestClient client = new SharedContextRestClient(); - SharedContext get = client.getContextValue("abc", "123"); - System.out.println("Get yields " + get.toString()); - } - -} -- cgit 1.2.3-korg