Update the Sparky backend to support proxying calls to resources and traversal
[aai/sparky-be.git] / sparkybe-onap-application / src / main / java / org / onap / aai / sparky / ProxyHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky;
22
23 import org.onap.aai.cl.api.Logger;
24 import org.onap.aai.cl.eelf.LoggerFactory;
25 import org.onap.aai.restclient.client.OperationResult;
26 import org.onap.aai.sparky.security.portal.PortalRestAPICentralServiceImpl;
27 import org.onap.aai.sparky.util.ProxyClient;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.Profile;
30 import org.springframework.core.env.Environment;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RestController;
34
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import javax.ws.rs.core.MultivaluedMap;
38 import java.util.Arrays;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42
43 import static org.onap.aai.sparky.logging.AaiUiMsgs.INFO_GENERIC;
44
45 @Profile("aai-proxy")
46 @RestController
47 public class ProxyHelper {
48     @Autowired
49     Environment env;
50     private static final Logger LOG = LoggerFactory.getInstance().getLogger(ProxyHelper.class);
51     public static final String SCHEMA_VERSION = "schema-version";
52     private ProxyClient proxyClient;
53
54     /**
55      * Proxy Helper Class
56      *
57      * @param pc the proxyclient
58      */
59     public ProxyHelper(ProxyClient pc){
60         proxyClient = pc;
61     }
62
63     public Boolean isPortalEnabled(){
64         List<String> list = Arrays.asList(this.env.getActiveProfiles());
65         return list.contains("portal");
66     }
67
68     /**
69      * Proxy Post Calls from Sparky Frontend
70      *
71      * @param request
72      * @return the response
73      */
74
75     @RequestMapping(value = "/proxy/**", method = {RequestMethod.POST})
76     public String postProxy(HttpServletRequest request,  HttpServletResponse response){
77         OperationResult or = null;
78         String results = "";
79         try {
80             or = proxyClient.post(request);
81             updateHeaders(response, or);
82             results = or.getResult();
83         }catch(Exception e) {
84             results = e.getMessage();
85         }
86         return results;
87     }
88
89     /**
90      * Proxy Put Calls from Sparky Frontend
91      *
92      * @param request
93      * @return the response
94      * @throws Exception
95      */
96
97     @RequestMapping(value = "/proxy/**", method = {RequestMethod.PUT})
98     public String putProxy(HttpServletRequest request,  HttpServletResponse response){
99         OperationResult or = null;
100         String results = "";
101         try {
102             or = proxyClient.put(request);
103             updateHeaders(response, or);
104             results = or.getResult();
105         }catch(Exception e) {
106             results = e.getMessage();
107         }
108         return results;
109     }
110
111     /**
112      * Proxy Get Calls from Sparky Frontend
113      *
114      * @param request
115      * @return the response
116      */
117
118     @RequestMapping(value = "/proxy/**", method = {RequestMethod.GET})
119     public String getProxy(HttpServletRequest request,  HttpServletResponse response){
120         OperationResult or = null;
121         String results = "";
122         try {
123             or = proxyClient.get(request);
124             updateHeaders(response, or);
125             results = or.getResult();
126         }catch(Exception e) {
127             results = e.getMessage();
128         }
129         return results;
130     }
131
132     /**
133      * Bulk Single Transactions from Sparky Frontend
134      *
135      * @param request
136      * @return the response
137      */
138
139     @RequestMapping(value = "/aai/v*/bulk/single-transaction", method = {RequestMethod.POST})
140     public String bulkSingleTransaction(HttpServletRequest request,  HttpServletResponse response){
141         String uid = "testuid";
142         if(this.isPortalEnabled()) {
143             PortalRestAPICentralServiceImpl pr = new PortalRestAPICentralServiceImpl();
144             LOG.info(INFO_GENERIC, "Getting UID from portal api");
145             try {
146                 uid = pr.getUserId(request);
147             }catch(Exception e){
148                 LOG.info(INFO_GENERIC, "error getting user id: " + e);
149             }
150             LOG.info(INFO_GENERIC, "getUserID: uid: " + uid);
151         }
152         OperationResult or = null;
153         String results = "";
154         try {
155             or = proxyClient.bulkSingleTransaction(request, uid);
156             //updateHeaders(response, or);
157             results = or.getResult();
158             if(results == null){
159                 results = or.getFailureCause();
160             }
161         }catch(Exception e) {
162             results = e.getMessage();
163         }
164         return results;
165     }
166
167
168     /**
169      * Update the Headers
170      *
171      * @param or the operation result object
172      * @return the response
173      */
174     public void updateHeaders(HttpServletResponse response, OperationResult or){
175         response.setHeader("Access-Control-Allow-Origin", "*");
176         MultivaluedMap<String, String> headers = or.getHeaders();
177         response.setStatus(or.getResultCode());
178         Iterator<String> it;
179         String headerTags = "";
180         if(headers != null) {
181             it = headers.keySet().iterator();
182             while (it.hasNext()) {
183                 String theKey = (String) it.next();
184                 headerTags = (headerTags.equals("")) ? theKey : headerTags+","+theKey;
185                 response.setHeader(theKey, headers.getFirst(theKey));
186             }
187         }
188         response.setHeader("Access-Control-Expose-Headers", headerTags);
189         /*if(or.getResultCode() != 200) {
190             throw new GenericServiceException(String.valueOf(or.getFailureCause()+"resultCode:"+or.getResultCode()));
191         }*/
192     }
193
194 }