From 4d750d21cffd991b4bc5b735455fc9fceb516937 Mon Sep 17 00:00:00 2001 From: wr148d Date: Thu, 11 Feb 2021 09:14:58 -0500 Subject: Update the Sparky backend to support proxying calls to resources and traversal Issue-ID: AAI-3250 Change-Id: I1c4d587ca30ea379ae28697b95ba2ca4602cd4e8 Signed-off-by: wr148d --- .../config/application.properties | 6 +- .../config/spring-beans/sparky-aai-proxy.xml | 15 ++ sparkybe-onap-application/pom.xml | 2 +- .../main/java/org/onap/aai/sparky/ProxyHelper.java | 194 +++++++++++++++++++++ 4 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 sparkybe-onap-application/config/spring-beans/sparky-aai-proxy.xml create mode 100644 sparkybe-onap-application/src/main/java/org/onap/aai/sparky/ProxyHelper.java (limited to 'sparkybe-onap-application') diff --git a/sparkybe-onap-application/config/application.properties b/sparkybe-onap-application/config/application.properties index c0616b5..cdebe11 100644 --- a/sparkybe-onap-application/config/application.properties +++ b/sparkybe-onap-application/config/application.properties @@ -3,18 +3,18 @@ # spring.mvc.favicon.enabled=false -#possible values: camel,http,ssl,portal,fe-dev,fe-prod,oxm-default,oxm-override,[resources|gizmo],sync,oxm-schema-dev,oxm-schema-prod +#possible values: camel,http,ssl,portal,fe-dev,fe-prod,oxm-default,oxm-override,[resources|gizmo],sync,oxm-schema-dev,oxm-schema-prod,aai-proxy #For oxm loading there needs to be a combo of [oxm-default OR oxm-override] AND [oxm-schema-dev OR oxm-schema-prod] # # Gizmo profile # -spring.profiles.active=camel,ssl,fe-dev,oxm-schema-dev,gizmo,oxm-default +spring.profiles.active=camel,http,oxm-default,oxm-schema-dev,fe-dev,resources,aai-proxy # # Resources profile # -#spring.profiles.active=camel,http,fe-dev,oxm-schema-dev,resources,sync,oxm-override +#spring.profiles.active=camel,http,fe-dev,oxm-schema-dev,resources,sync,oxm-override,aai-proxy portal.cadiFileLocation=${CONFIG_HOME}/portal/cadi.properties searchservice.hostname=127.0.0.1 diff --git a/sparkybe-onap-application/config/spring-beans/sparky-aai-proxy.xml b/sparkybe-onap-application/config/spring-beans/sparky-aai-proxy.xml new file mode 100644 index 0000000..bb0b98d --- /dev/null +++ b/sparkybe-onap-application/config/spring-beans/sparky-aai-proxy.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/sparkybe-onap-application/pom.xml b/sparkybe-onap-application/pom.xml index 445464a..5b1571b 100644 --- a/sparkybe-onap-application/pom.xml +++ b/sparkybe-onap-application/pom.xml @@ -253,7 +253,7 @@ org.onap.aai rest-client - 1.5.0 + 1.3.0 diff --git a/sparkybe-onap-application/src/main/java/org/onap/aai/sparky/ProxyHelper.java b/sparkybe-onap-application/src/main/java/org/onap/aai/sparky/ProxyHelper.java new file mode 100644 index 0000000..ed9557f --- /dev/null +++ b/sparkybe-onap-application/src/main/java/org/onap/aai/sparky/ProxyHelper.java @@ -0,0 +1,194 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.sparky; + +import org.onap.aai.cl.api.Logger; +import org.onap.aai.cl.eelf.LoggerFactory; +import org.onap.aai.restclient.client.OperationResult; +import org.onap.aai.sparky.security.portal.PortalRestAPICentralServiceImpl; +import org.onap.aai.sparky.util.ProxyClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MultivaluedMap; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static org.onap.aai.sparky.logging.AaiUiMsgs.INFO_GENERIC; + +@Profile("aai-proxy") +@RestController +public class ProxyHelper { + @Autowired + Environment env; + private static final Logger LOG = LoggerFactory.getInstance().getLogger(ProxyHelper.class); + public static final String SCHEMA_VERSION = "schema-version"; + private ProxyClient proxyClient; + + /** + * Proxy Helper Class + * + * @param pc the proxyclient + */ + public ProxyHelper(ProxyClient pc){ + proxyClient = pc; + } + + public Boolean isPortalEnabled(){ + List list = Arrays.asList(this.env.getActiveProfiles()); + return list.contains("portal"); + } + + /** + * Proxy Post Calls from Sparky Frontend + * + * @param request + * @return the response + */ + + @RequestMapping(value = "/proxy/**", method = {RequestMethod.POST}) + public String postProxy(HttpServletRequest request, HttpServletResponse response){ + OperationResult or = null; + String results = ""; + try { + or = proxyClient.post(request); + updateHeaders(response, or); + results = or.getResult(); + }catch(Exception e) { + results = e.getMessage(); + } + return results; + } + + /** + * Proxy Put Calls from Sparky Frontend + * + * @param request + * @return the response + * @throws Exception + */ + + @RequestMapping(value = "/proxy/**", method = {RequestMethod.PUT}) + public String putProxy(HttpServletRequest request, HttpServletResponse response){ + OperationResult or = null; + String results = ""; + try { + or = proxyClient.put(request); + updateHeaders(response, or); + results = or.getResult(); + }catch(Exception e) { + results = e.getMessage(); + } + return results; + } + + /** + * Proxy Get Calls from Sparky Frontend + * + * @param request + * @return the response + */ + + @RequestMapping(value = "/proxy/**", method = {RequestMethod.GET}) + public String getProxy(HttpServletRequest request, HttpServletResponse response){ + OperationResult or = null; + String results = ""; + try { + or = proxyClient.get(request); + updateHeaders(response, or); + results = or.getResult(); + }catch(Exception e) { + results = e.getMessage(); + } + return results; + } + + /** + * Bulk Single Transactions from Sparky Frontend + * + * @param request + * @return the response + */ + + @RequestMapping(value = "/aai/v*/bulk/single-transaction", method = {RequestMethod.POST}) + public String bulkSingleTransaction(HttpServletRequest request, HttpServletResponse response){ + String uid = "testuid"; + if(this.isPortalEnabled()) { + PortalRestAPICentralServiceImpl pr = new PortalRestAPICentralServiceImpl(); + LOG.info(INFO_GENERIC, "Getting UID from portal api"); + try { + uid = pr.getUserId(request); + }catch(Exception e){ + LOG.info(INFO_GENERIC, "error getting user id: " + e); + } + LOG.info(INFO_GENERIC, "getUserID: uid: " + uid); + } + OperationResult or = null; + String results = ""; + try { + or = proxyClient.bulkSingleTransaction(request, uid); + //updateHeaders(response, or); + results = or.getResult(); + if(results == null){ + results = or.getFailureCause(); + } + }catch(Exception e) { + results = e.getMessage(); + } + return results; + } + + + /** + * Update the Headers + * + * @param or the operation result object + * @return the response + */ + public void updateHeaders(HttpServletResponse response, OperationResult or){ + response.setHeader("Access-Control-Allow-Origin", "*"); + MultivaluedMap headers = or.getHeaders(); + response.setStatus(or.getResultCode()); + Iterator it; + String headerTags = ""; + if(headers != null) { + it = headers.keySet().iterator(); + while (it.hasNext()) { + String theKey = (String) it.next(); + headerTags = (headerTags.equals("")) ? theKey : headerTags+","+theKey; + response.setHeader(theKey, headers.getFirst(theKey)); + } + } + response.setHeader("Access-Control-Expose-Headers", headerTags); + /*if(or.getResultCode() != 200) { + throw new GenericServiceException(String.valueOf(or.getFailureCause()+"resultCode:"+or.getResultCode())); + }*/ + } + +} \ No newline at end of file -- cgit 1.2.3-korg