2  * ============LICENSE_START==========================================
 
   4  * ===================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ===================================================================
 
   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
 
  13  *             http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  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
 
  26  *             https://creativecommons.org/licenses/by/4.0/
 
  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.
 
  34  * ============LICENSE_END============================================
 
  36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  38 package org.onap.portalapp.portal.controller;
 
  40 import java.io.IOException;
 
  42 import javax.servlet.http.HttpServletRequest;
 
  43 import javax.servlet.http.HttpServletResponse;
 
  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;
 
  59 import com.fasterxml.jackson.databind.ObjectMapper;
 
  62 @org.springframework.context.annotation.Configuration
 
  63 @EnableAspectJAutoProxy
 
  65 public class MicroserviceProxyController extends EPUnRestrictedBaseController {
 
  67         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceProxyController.class);
 
  70         private MicroserviceProxyService microserviceProxyService;
 
  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);
 
  79                         answer = microserviceProxyService.proxyToDestination(serviceId, user, request);
 
  80                 } catch (HttpClientErrorException e) {
 
  81                         answer = e.getResponseBodyAsString();
 
  83                 return isValidJSON(answer) ? answer : "{\"error\":\"" + answer.replace(System.getProperty("line.separator"), "") + "\"}";
 
  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);
 
  93                         answer = microserviceProxyService.proxyToDestinationByWidgetId(widgetId, user, request);
 
  94                 } catch (HttpClientErrorException e) {
 
  95                         answer = e.getResponseBodyAsString();
 
  97                 return isValidJSON(answer) ? answer : "{\"error\":\"" + answer.replace(System.getProperty("line.separator"), "") + "\"}";
 
 101          * Check whether the response is a valid JSON
 
 103          * @return true if the response is valid JSON, otherwise, false
 
 105         private boolean isValidJSON(String response) {
 
 107                         final ObjectMapper mapper = new ObjectMapper();
 
 108                         mapper.readTree(response);
 
 110                 } catch (IOException e) {
 
 111                         logger.debug(EELFLoggerDelegate.debugLogger, "isValidJSON failed", e);