[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / WidgetsCatalogMarkupController.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 javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.openecomp.portalapp.controller.EPUnRestrictedBaseController;
26 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
27 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
28 import org.openecomp.portalapp.portal.service.ConsulHealthService;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.openecomp.portalsdk.core.util.SystemProperties;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.annotation.Bean;
33 import org.springframework.context.annotation.EnableAspectJAutoProxy;
34 import org.springframework.web.bind.annotation.PathVariable;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestMethod;
37 import org.springframework.web.bind.annotation.RestController;
38 import org.springframework.web.client.RestClientException;
39 import org.springframework.web.client.RestTemplate;
40 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
41
42 @RestController
43 @org.springframework.context.annotation.Configuration
44 @EnableAspectJAutoProxy
45 @EPAuditLog
46 public class WidgetsCatalogMarkupController extends EPUnRestrictedBaseController {
47
48         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogMarkupController.class);
49
50         private RestTemplate template = new RestTemplate();
51
52         private final String whatService = "widgets-service";
53
54         @Autowired
55         private ConsulHealthService consulHealthService;
56
57         @Bean
58         public CommonsMultipartResolver multipartResolver() {
59                 return new CommonsMultipartResolver();
60         }
61
62         static {
63                 // for localhost testing only
64                 javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
65
66                         public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
67                                 if (hostname.equals("localhost")) {
68                                         return true;
69                                 }
70                                 return false;
71                         }
72                 });
73         }
74
75         @RequestMapping(value = "/portalApi/microservices/markup/{widgetId}", method = RequestMethod.GET)
76         public String getWidgetMarkup(HttpServletRequest request, HttpServletResponse response,
77                         @PathVariable("widgetId") long widgetId) throws RestClientException, Exception {
78                 return template
79                                 .getForObject(
80                                                 "https://"
81                                                                 + consulHealthService.getServiceLocation(whatService,
82                                                                                 SystemProperties.getProperty("microservices.widget.local.port"))
83                                                                 + "/widget/microservices/markup/" + widgetId,
84                                                 String.class, WidgetServiceHeaders.getInstance());
85         }
86 }