2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.portal.controller;
22 import java.io.IOException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
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;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import com.fasterxml.jackson.databind.ObjectMapper;
44 @SuppressWarnings("unchecked")
46 @org.springframework.context.annotation.Configuration
47 @EnableAspectJAutoProxy
49 public class MicroserviceProxyController extends EPUnRestrictedBaseController {
52 private MicroserviceProxyService microserviceProxyService;
54 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceProxyController.class);
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);
63 answer = microserviceProxyService.proxyToDestination(serviceId, user, request);
64 } catch (HttpClientErrorException e) {
65 answer = e.getResponseBodyAsString();
67 return isValidJSON(answer) ? answer : "{\"error\":\"" + answer.replace(System.getProperty("line.separator"), "") + "\"}";
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);
77 answer = microserviceProxyService.proxyToDestinationByWidgetId(widgetId, user, request);
78 } catch (HttpClientErrorException e) {
79 answer = e.getResponseBodyAsString();
81 return isValidJSON(answer) ? answer : "{\"error\":\"" + answer.replace(System.getProperty("line.separator"), "") + "\"}";
85 * Check whether the response is a valid JSON
87 * @return true if the response is valid JSON, otherwise, false
89 private boolean isValidJSON(String response) {
91 final ObjectMapper mapper = new ObjectMapper();
92 mapper.readTree(response);
94 } catch (IOException e) {