2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.openecomp.portalapp.widget.controller;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.openecomp.portalapp.widget.domain.ValidationRespond;
48 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
49 import org.openecomp.portalapp.widget.service.StorageService;
50 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
51 import org.openecomp.portalapp.widget.utils.AuthorizationUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.beans.factory.annotation.Value;
56 import org.springframework.context.annotation.EnableAspectJAutoProxy;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.PathVariable;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestHeader;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RequestMethod;
63 import org.springframework.web.bind.annotation.RequestParam;
64 import org.springframework.web.bind.annotation.ResponseBody;
65 import org.springframework.web.client.RestTemplate;
66 import org.springframework.web.multipart.MultipartFile;
68 import com.fasterxml.jackson.databind.ObjectMapper;
71 @org.springframework.context.annotation.Configuration
72 @EnableAspectJAutoProxy
73 public class WidgetsCatalogController {
75 @Value("${server.port}")
77 @Value("${server.contextPath}")
80 @Value("${security.user.name}")
82 @Value("${security.user.password}")
86 WidgetCatalogService widgetCatalogService;
89 StorageService storageService;
92 RestTemplate restTemplate;
94 AuthorizationUtil util = new AuthorizationUtil();
96 private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
99 @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.GET, produces = "application/json")
100 public List<WidgetCatalog> getWidgetCatalog(HttpServletRequest request, HttpServletResponse response
101 ,@RequestHeader(value="Authorization") String auth) throws IOException{
103 List<WidgetCatalog> widgetCatalog = null;
104 if(!util.authorization(auth, security_user, security_pass)){
105 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
106 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetCatalog in widget microserivce. Please check your username and password.");
107 return widgetCatalog;
110 widgetCatalog = widgetCatalogService.getWidgetCatalog();
111 logger.debug("WidgetsCatalogController.getWidgetCatalog: getting widget list {}", widgetCatalog);
112 } catch (Exception e) {
113 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetCatalog in widget microservices. Details:" + e.getMessage());
115 return widgetCatalog;
119 @RequestMapping(value = { "/microservices/widgetCatalog/{loginName}" }, method = RequestMethod.GET, produces = "application/json")
120 public List<WidgetCatalog> getUserWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
121 @PathVariable("loginName") String loginName, @RequestHeader(value="Authorization") String auth) throws IOException {
122 List<WidgetCatalog> widgetCatalog = null;
123 if(!util.authorization(auth, security_user, security_pass)){
124 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
125 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getUserWidgetCatalog in widget microserivce. Please check your username and password.");
126 return widgetCatalog;
129 widgetCatalog = widgetCatalogService.getUserWidgetCatalog(loginName);
130 logger.debug("WidgetsCatalogController.getUserWidgetCatalog: getting widget list {}", widgetCatalog);
131 } catch (Exception e) {
132 logger.error("Exception occurred while performing WidgetsCatalogController.getUserWidgetCatalog in widget microservices. Details:" + e.getMessage());
134 return widgetCatalog;
138 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.PUT, produces = "application/json")
139 public void updateWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
140 @RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId,
141 @RequestHeader(value="Authorization") String auth) throws IOException {
143 if(!util.authorization(auth, security_user, security_pass)){
144 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
145 logger.error("Basic Authentication Error while performing WidgetsCatalogController.updateWidgetCatalog in widget microserivce. Please check your username and password.");
149 widgetCatalogService.updateWidgetCatalog(widgetId, newWidgetCatalog);
150 logger.debug("WidgetsCatalogController.updateWidgetCatalog: updating widget {}", newWidgetCatalog);
151 } catch (Exception e) {
152 logger.error("Exception occurred while performing WidgetsCatalogController.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
158 @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.POST, produces = "application/json")
159 public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
160 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {
162 ValidationRespond respond = null;
163 if(!util.authorization(auth, security_user, security_pass)){
164 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
165 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
166 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
169 //check the zip file structure first
170 respond = storageService.checkZipFile(file);
172 if(respond.isValid()){
173 //save the widget catalog
174 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
176 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
177 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
178 //save the widget zip file ;
179 storageService.save(file, newWidget, widgetId);
182 } catch (Exception e) {
183 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
189 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST, produces = "application/json")
190 public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
191 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {
192 System.out.println("microserivces updating with files" + widgetId);
193 ValidationRespond respond = null;
194 if(!util.authorization(auth, security_user, security_pass)){
195 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
196 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
197 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
200 //check the zip file structure first
201 respond = storageService.checkZipFile(file);
202 if(respond.isValid()){
203 //update the widget catalog
204 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
205 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
206 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
207 //update the widget zip file
208 storageService.update(file, newWidget, widgetId);
210 } catch (Exception e) {
211 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
218 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = {
219 RequestMethod.DELETE }, produces = "application/json")
220 public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
221 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException{
222 if(!util.authorization(auth, security_user, security_pass)){
223 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
224 logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
228 logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
229 //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
230 widgetCatalogService.deleteWidgetCatalog(widgetId);
231 storageService.deleteWidgetFile(widgetId);
232 } catch (Exception e) {
233 logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:" + e.getMessage());
238 @RequestMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
239 public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response,
240 @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
242 Long serviceId = null;
243 if(!util.authorization(auth, security_user, security_pass)){
244 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
245 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
249 logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
250 serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
252 logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:" + e.getMessage());
259 @RequestMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, method = RequestMethod.GET, produces = "application/json")
260 public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response,
261 @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
262 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
263 if(!util.authorization(auth, security_user, security_pass)){
264 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
265 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
269 logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
270 list = widgetCatalogService.getWidgetsByServiceId(serviceId);
272 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:" + e.getMessage());
279 @RequestMapping(value = { "/microservices/download/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
280 public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response,
281 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
282 byte[] byteFile = null;
283 if(!util.authorization(auth, security_user, security_pass)){
284 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
285 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
289 byteFile = storageService.getWidgetCatalogContent(widgetId);
290 logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
291 } catch (Exception e) {
292 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:" + e.getMessage());