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