Widget MS startup fix and Onboarding changes
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / WidgetsCatalogUnRestrictedController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2020 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.File;
41 import java.io.FileInputStream;
42 import java.io.FileOutputStream;
43 import java.io.OutputStream;
44 import java.util.ArrayList;
45 import java.util.List;
46
47 import javax.servlet.ServletContext;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
53 import org.onap.portalapp.portal.domain.EPUser;
54 import org.onap.portalapp.portal.domain.MicroserviceParameter;
55 import org.onap.portalapp.portal.domain.WidgetCatalog;
56 import org.onap.portalapp.portal.domain.WidgetCatalogParameter;
57 import org.onap.portalapp.portal.domain.WidgetParameterResult;
58 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
59 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
60 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
61 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
62 import org.onap.portalapp.portal.service.WidgetMService;
63 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
64 import org.onap.portalapp.portal.utils.EcompPortalUtils;
65 import org.onap.portalapp.util.EPUserUtils;
66 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.context.annotation.Bean;
70 import org.springframework.context.annotation.EnableAspectJAutoProxy;
71 import org.springframework.core.io.FileSystemResource;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpHeaders;
74 import org.springframework.http.HttpMethod;
75 import org.springframework.http.MediaType;
76 import org.springframework.http.ResponseEntity;
77 import org.springframework.util.LinkedMultiValueMap;
78 import org.springframework.util.MultiValueMap;
79 import org.springframework.web.bind.annotation.PathVariable;
80 import org.springframework.web.bind.annotation.RequestBody;
81 import org.springframework.web.bind.annotation.GetMapping;
82 import org.springframework.web.bind.annotation.RestController;
83 import org.springframework.web.client.RestClientException;
84 import org.springframework.web.client.RestTemplate;
85 import org.springframework.web.multipart.MultipartFile;
86 import org.springframework.web.multipart.MultipartHttpServletRequest;
87 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
88
89 @SuppressWarnings("unchecked")
90 @RestController
91 @org.springframework.context.annotation.Configuration
92 @EnableAspectJAutoProxy
93 @EPAuditLog
94 public class WidgetsCatalogUnRestrictedController extends EPUnRestrictedBaseController {
95
96         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogUnRestrictedController.class);
97
98         private static final String MS_WIDGET_LOCAL_PORT = "microservices.widget.local.port";
99
100     private static final String MS_WIDGET_URL = "/widget/microservices/";
101
102         private RestTemplate template = new RestTemplate();
103
104         private String whatService = "widgets-service";
105
106         @Autowired
107         private WidgetMService widgetMService;
108
109
110         @Bean
111         public CommonsMultipartResolver multipartResolver() {
112                 return new CommonsMultipartResolver();
113         }
114
115         static {
116                 // for localhost testing only
117                 javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
118                         public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
119                                 if ("localhost".equals(hostname))
120                                         return true;
121                                 return false;
122                         }
123                 });
124         }
125         @GetMapping(value = "/portalApi/microservices/{widgetId}/controller.js")
126         public String getWidgetController(@PathVariable("widgetId") long widgetId) throws Exception {
127                 return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://"
128                                 + widgetMService.getServiceLocation(whatService,
129                                                 SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT))
130                 + MS_WIDGET_URL + widgetId + "/controller.js", String.class,
131                                 WidgetServiceHeaders.getInstance());
132         }
133 }