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.GetMapping;
26 import org.springframework.web.bind.annotation.PutMapping;
27 import org.springframework.web.bind.annotation.PostMapping;
28 import org.springframework.web.bind.annotation.DeleteMapping;
29 import org.springframework.web.bind.annotation.RequestMethod;
30 import org.springframework.web.bind.annotation.RequestParam;
31 import org.springframework.web.bind.annotation.ResponseBody;
32 import org.springframework.web.client.RestTemplate;
33 import org.springframework.web.multipart.MultipartFile;
35 import com.fasterxml.jackson.databind.ObjectMapper;
38 @org.springframework.context.annotation.Configuration
39 @EnableAspectJAutoProxy
40 public class WidgetsCatalogController {
42 @Value("${server.port}")
44 @Value("${server.servlet.context-path}")
47 @Value("${spring.security.user.name}")
49 @Value("${spring.security.user.password}")
53 WidgetCatalogService widgetCatalogService;
56 StorageService storageService;
59 RestTemplate restTemplate;
61 AuthorizationUtil util = new AuthorizationUtil();
63 private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
66 @GetMapping(value = { "/microservices/widgetCatalog" }, produces = "application/json")
67 public List<WidgetCatalog> getWidgetCatalog(HttpServletRequest request, HttpServletResponse response
68 ,@RequestHeader(value="Authorization") String auth) throws IOException{
70 List<WidgetCatalog> widgetCatalog = null;
71 if(!util.authorization(auth, security_user, security_pass)){
72 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
73 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetCatalog in widget microserivce. Please check your username and password.");
77 widgetCatalog = widgetCatalogService.getWidgetCatalog();
78 logger.debug("WidgetsCatalogController.getWidgetCatalog: getting widget list {}", widgetCatalog);
79 } catch (Exception e) {
80 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetCatalog in widget microservices. Details:", e);
86 @GetMapping(value = { "/microservices/widgetCatalog/{loginName}" }, produces = "application/json")
87 public List<WidgetCatalog> getUserWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
88 @PathVariable("loginName") String loginName, @RequestHeader(value="Authorization") String auth) throws IOException {
89 List<WidgetCatalog> widgetCatalog = null;
90 if(!util.authorization(auth, security_user, security_pass)){
91 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
92 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getUserWidgetCatalog in widget microserivce. Please check your username and password.");
96 widgetCatalog = widgetCatalogService.getUserWidgetCatalog(loginName);
97 logger.debug("WidgetsCatalogController.getUserWidgetCatalog: getting widget list {}", widgetCatalog);
98 } catch (Exception e) {
99 logger.error("Exception occurred while performing WidgetsCatalogController.getUserWidgetCatalog in widget microservices. Details:", e);
101 return widgetCatalog;
105 @PutMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
106 public void updateWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
107 @RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId,
108 @RequestHeader(value="Authorization") String auth) throws IOException {
110 if(!util.authorization(auth, security_user, security_pass)){
111 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
112 logger.error("Basic Authentication Error while performing WidgetsCatalogController.updateWidgetCatalog in widget microserivce. Please check your username and password.");
116 widgetCatalogService.updateWidgetCatalog(widgetId, newWidgetCatalog);
117 logger.debug("WidgetsCatalogController.updateWidgetCatalog: updating widget {}", newWidgetCatalog);
118 } catch (Exception e) {
119 logger.error("Exception occurred while performing WidgetsCatalogController.updateWidgetCatalog in widget microservices. Details:", e);
124 @PostMapping(value = { "/microservices/widgetCatalog" }, produces = "application/json")
125 public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
126 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {
128 ValidationRespond respond = null;
129 if(!util.authorization(auth, security_user, security_pass)){
130 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
131 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
132 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
135 //check the zip file structure first
136 respond = storageService.checkZipFile(file);
138 if(respond.isValid()){
139 //save the widget catalog
140 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
142 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
143 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
144 //save the widget zip file ;
145 storageService.save(file, newWidget, widgetId);
148 } catch (Exception e) {
149 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
155 @PostMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
156 public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
157 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {
158 logger.debug("microserivces updating with files {}", widgetId);
159 ValidationRespond respond = null;
160 if(!util.authorization(auth, security_user, security_pass)){
161 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
162 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
163 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
166 //check the zip file structure first
167 respond = storageService.checkZipFile(file);
168 if(respond.isValid()){
169 //update the widget catalog
170 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
171 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
172 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
173 //update the widget zip file
174 storageService.update(file, newWidget, widgetId);
176 } catch (Exception e) {
177 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
183 @DeleteMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
184 public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
185 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException{
186 if(!util.authorization(auth, security_user, security_pass)){
187 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
188 logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
192 logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
193 //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
194 widgetCatalogService.deleteWidgetCatalog(widgetId);
195 storageService.deleteWidgetFile(widgetId);
196 } catch (Exception e) {
197 logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:", e);
202 @GetMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, produces = "application/json")
203 public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response,
204 @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
206 Long serviceId = null;
207 if(!util.authorization(auth, security_user, security_pass)){
208 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
209 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
213 logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
214 serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
216 logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
223 @GetMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, produces = "application/json")
224 public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response,
225 @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
226 List<WidgetCatalog> list = new ArrayList<>();
227 if(!util.authorization(auth, security_user, security_pass)){
228 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
229 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
233 logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
234 list = widgetCatalogService.getWidgetsByServiceId(serviceId);
236 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
243 @GetMapping(value = { "/microservices/download/{widgetId}" }, produces = "application/json")
244 public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response,
245 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
246 byte[] byteFile = null;
247 if(!util.authorization(auth, security_user, security_pass)){
248 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
249 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
253 byteFile = storageService.getWidgetCatalogContent(widgetId);
254 logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
255 } catch (Exception e) {
256 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:", e);