2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky;
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;
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;
43 import static org.onap.aai.sparky.logging.AaiUiMsgs.INFO_GENERIC;
47 public class ProxyHelper {
50 private static final Logger LOG = LoggerFactory.getInstance().getLogger(ProxyHelper.class);
51 public static final String SCHEMA_VERSION = "schema-version";
52 private ProxyClient proxyClient;
57 * @param pc the proxyclient
59 public ProxyHelper(ProxyClient pc){
63 public Boolean isPortalEnabled(){
64 List<String> list = Arrays.asList(this.env.getActiveProfiles());
65 return list.contains("portal");
69 * Proxy Post Calls from Sparky Frontend
72 * @return the response
75 @RequestMapping(value = "/proxy/**", method = {RequestMethod.POST})
76 public String postProxy(HttpServletRequest request, HttpServletResponse response){
77 OperationResult or = null;
80 or = proxyClient.post(request);
81 updateHeaders(response, or);
82 results = or.getResult();
84 results = e.getMessage();
90 * Proxy Put Calls from Sparky Frontend
93 * @return the response
97 @RequestMapping(value = "/proxy/**", method = {RequestMethod.PUT})
98 public String putProxy(HttpServletRequest request, HttpServletResponse response){
99 OperationResult or = null;
102 or = proxyClient.put(request);
103 updateHeaders(response, or);
104 results = or.getResult();
105 }catch(Exception e) {
106 results = e.getMessage();
112 * Proxy Get Calls from Sparky Frontend
115 * @return the response
118 @RequestMapping(value = "/proxy/**", method = {RequestMethod.GET})
119 public String getProxy(HttpServletRequest request, HttpServletResponse response){
120 OperationResult or = null;
123 or = proxyClient.get(request);
124 updateHeaders(response, or);
125 results = or.getResult();
126 }catch(Exception e) {
127 results = e.getMessage();
133 * Bulk Single Transactions from Sparky Frontend
136 * @return the response
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");
146 uid = pr.getUserId(request);
148 LOG.info(INFO_GENERIC, "error getting user id: " + e);
150 LOG.info(INFO_GENERIC, "getUserID: uid: " + uid);
152 OperationResult or = null;
155 or = proxyClient.bulkSingleTransaction(request, uid);
156 //updateHeaders(response, or);
157 results = or.getResult();
159 results = or.getFailureCause();
161 }catch(Exception e) {
162 results = e.getMessage();
171 * @param or the operation result object
172 * @return the response
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());
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));
188 response.setHeader("Access-Control-Expose-Headers", headerTags);
189 /*if(or.getResultCode() != 200) {
190 throw new GenericServiceException(String.valueOf(or.getFailureCause()+"resultCode:"+or.getResultCode()));