1 package org.onap.portalapp.widget.controller;
3 import java.io.IOException;
4 import java.util.ArrayList;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
10 import org.onap.portalapp.widget.domain.ValidationRespond;
11 import org.onap.portalapp.widget.domain.WidgetCatalog;
12 import org.onap.portalapp.widget.service.StorageService;
13 import org.onap.portalapp.widget.service.WidgetCatalogService;
14 import org.onap.portalapp.widget.utils.AuthorizationUtil;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.beans.factory.annotation.Value;
19 import org.springframework.context.annotation.EnableAspectJAutoProxy;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.web.bind.annotation.PathVariable;
22 import org.springframework.web.bind.annotation.RequestBody;
23 import org.springframework.web.bind.annotation.RequestHeader;
24 import org.springframework.web.bind.annotation.RequestMapping;
25 import org.springframework.web.bind.annotation.RequestMethod;
26 import org.springframework.web.bind.annotation.RequestParam;
27 import org.springframework.web.bind.annotation.ResponseBody;
28 import org.springframework.web.client.RestTemplate;
29 import org.springframework.web.multipart.MultipartFile;
31 import com.fasterxml.jackson.databind.ObjectMapper;
34 @org.springframework.context.annotation.Configuration
35 @EnableAspectJAutoProxy
36 public class WidgetsCatalogController {
38 @Value("${server.port}")
40 @Value("${server.servlet.context-path}")
43 @Value("${spring.security.user.name}")
45 @Value("${spring.security.user.password}")
49 WidgetCatalogService widgetCatalogService;
52 StorageService storageService;
55 RestTemplate restTemplate;
57 AuthorizationUtil util = new AuthorizationUtil();
59 private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
62 @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.GET, produces = "application/json")
63 public List<WidgetCatalog> getWidgetCatalog(HttpServletRequest request, HttpServletResponse response
64 ,@RequestHeader(value="Authorization") String auth) throws IOException{
66 List<WidgetCatalog> widgetCatalog = null;
67 if(!util.authorization(auth, security_user, security_pass)){
68 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
69 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetCatalog in widget microserivce. Please check your username and password.");
73 widgetCatalog = widgetCatalogService.getWidgetCatalog();
74 logger.debug("WidgetsCatalogController.getWidgetCatalog: getting widget list {}", widgetCatalog);
75 } catch (Exception e) {
76 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetCatalog in widget microservices. Details:", e);
82 @RequestMapping(value = { "/microservices/widgetCatalog/{loginName}" }, method = RequestMethod.GET, produces = "application/json")
83 public List<WidgetCatalog> getUserWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
84 @PathVariable("loginName") String loginName, @RequestHeader(value="Authorization") String auth) throws IOException {
85 List<WidgetCatalog> widgetCatalog = null;
86 if(!util.authorization(auth, security_user, security_pass)){
87 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
88 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getUserWidgetCatalog in widget microserivce. Please check your username and password.");
92 widgetCatalog = widgetCatalogService.getUserWidgetCatalog(loginName);
93 logger.debug("WidgetsCatalogController.getUserWidgetCatalog: getting widget list {}", widgetCatalog);
94 } catch (Exception e) {
95 logger.error("Exception occurred while performing WidgetsCatalogController.getUserWidgetCatalog in widget microservices. Details:", e);
101 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.PUT, produces = "application/json")
102 public void updateWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
103 @RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId,
104 @RequestHeader(value="Authorization") String auth) throws IOException {
106 if(!util.authorization(auth, security_user, security_pass)){
107 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
108 logger.error("Basic Authentication Error while performing WidgetsCatalogController.updateWidgetCatalog in widget microserivce. Please check your username and password.");
112 widgetCatalogService.updateWidgetCatalog(widgetId, newWidgetCatalog);
113 logger.debug("WidgetsCatalogController.updateWidgetCatalog: updating widget {}", newWidgetCatalog);
114 } catch (Exception e) {
115 logger.error("Exception occurred while performing WidgetsCatalogController.updateWidgetCatalog in widget microservices. Details:", e);
120 @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.POST, produces = "application/json")
121 public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
122 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {
124 ValidationRespond respond = null;
125 if(!util.authorization(auth, security_user, security_pass)){
126 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
127 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
128 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
131 //check the zip file structure first
132 respond = storageService.checkZipFile(file);
134 if(respond.isValid()){
135 //save the widget catalog
136 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
138 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
139 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
140 //save the widget zip file ;
141 storageService.save(file, newWidget, widgetId);
144 } catch (Exception e) {
145 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
151 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST, produces = "application/json")
152 public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
153 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {
154 logger.debug("microserivces updating with files {}", widgetId);
155 ValidationRespond respond = null;
156 if(!util.authorization(auth, security_user, security_pass)){
157 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
158 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
159 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
162 //check the zip file structure first
163 respond = storageService.checkZipFile(file);
164 if(respond.isValid()){
165 //update the widget catalog
166 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
167 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
168 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
169 //update the widget zip file
170 storageService.update(file, newWidget, widgetId);
172 } catch (Exception e) {
173 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
179 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = {
180 RequestMethod.DELETE }, produces = "application/json")
181 public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
182 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException{
183 if(!util.authorization(auth, security_user, security_pass)){
184 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
185 logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
189 logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
190 //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
191 widgetCatalogService.deleteWidgetCatalog(widgetId);
192 storageService.deleteWidgetFile(widgetId);
193 } catch (Exception e) {
194 logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:", e);
199 @RequestMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
200 public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response,
201 @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
203 Long serviceId = null;
204 if(!util.authorization(auth, security_user, security_pass)){
205 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
206 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
210 logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
211 serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
213 logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
220 @RequestMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, method = RequestMethod.GET, produces = "application/json")
221 public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response,
222 @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
223 List<WidgetCatalog> list = new ArrayList<>();
224 if(!util.authorization(auth, security_user, security_pass)){
225 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
226 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
230 logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
231 list = widgetCatalogService.getWidgetsByServiceId(serviceId);
233 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
240 @RequestMapping(value = { "/microservices/download/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
241 public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response,
242 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
243 byte[] byteFile = null;
244 if(!util.authorization(auth, security_user, security_pass)){
245 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
246 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
250 byteFile = storageService.getWidgetCatalogContent(widgetId);
251 logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
252 } catch (Exception e) {
253 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:", e);