fed3b98e7610621548c51b8f2308880806e5e56a
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / MicroserviceProxyController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.io.IOException;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
46 import org.onap.portalapp.portal.domain.EPUser;
47 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
48 import org.onap.portalapp.portal.service.MicroserviceProxyService;
49 import org.onap.portalapp.util.EPUserUtils;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
53 import org.springframework.web.bind.annotation.PathVariable;
54 import org.springframework.web.bind.annotation.RequestMapping;
55 import org.springframework.web.bind.annotation.RequestMethod;
56 import org.springframework.web.bind.annotation.RestController;
57 import org.springframework.web.client.HttpClientErrorException;
58
59 import com.fasterxml.jackson.databind.ObjectMapper;
60
61 @RestController
62 @org.springframework.context.annotation.Configuration
63 @EnableAspectJAutoProxy
64 @EPAuditLog
65 public class MicroserviceProxyController extends EPUnRestrictedBaseController {
66
67         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceProxyController.class);
68
69         @Autowired
70         private MicroserviceProxyService microserviceProxyService;
71
72         @RequestMapping(value = { "/portalApi/microservice/proxy/{serviceId}" }, method = {
73                         RequestMethod.GET }, produces = "application/json")
74         public String getMicroserviceProxy(HttpServletRequest request, HttpServletResponse response,
75                         @PathVariable("serviceId") long serviceId) throws Exception {
76                 EPUser user = EPUserUtils.getUserSession(request);
77                 String answer = "";
78                 try {
79                         answer = microserviceProxyService.proxyToDestination(serviceId, user, request);
80                 } catch (HttpClientErrorException e) {
81                         answer = e.getResponseBodyAsString();
82                 }
83                 return isValidJSON(answer) ? answer : "{\"error\":\"" + answer.replace(System.getProperty("line.separator"), "") + "\"}";
84         }
85
86         @RequestMapping(value = { "/portalApi/microservice/proxy/parameter/{widgetId}" }, method = {
87                         RequestMethod.GET }, produces = "application/json")
88         public String getMicroserviceProxyByWidgetId(HttpServletRequest request, HttpServletResponse response,
89                         @PathVariable("widgetId") long widgetId) throws Exception {
90                 EPUser user = EPUserUtils.getUserSession(request);
91                 String answer = "";
92                 try {
93                         answer = microserviceProxyService.proxyToDestinationByWidgetId(widgetId, user, request);
94                 } catch (HttpClientErrorException e) {
95                         answer = e.getResponseBodyAsString();
96                 }
97                 return isValidJSON(answer) ? answer : "{\"error\":\"" + answer.replace(System.getProperty("line.separator"), "") + "\"}";
98         }
99
100         /**
101          * Check whether the response is a valid JSON
102          * @param response
103          * @return true if the response is valid JSON, otherwise, false
104          */
105         private boolean isValidJSON(String response) {
106                 try {
107                         final ObjectMapper mapper = new ObjectMapper();
108                         mapper.readTree(response);
109                         return true;
110                 } catch (IOException e) {
111                         logger.debug(EELFLoggerDelegate.debugLogger, "isValidJSON failed", e);
112                         return false;
113                 }
114         }
115 }