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.GetMapping;
25 import org.springframework.web.bind.annotation.PutMapping;
26 import org.springframework.web.bind.annotation.PostMapping;
27 import org.springframework.web.bind.annotation.DeleteMapping;
28 import org.springframework.web.bind.annotation.RequestParam;
29 import org.springframework.web.bind.annotation.ResponseBody;
30 import org.springframework.web.client.RestTemplate;
31 import org.springframework.web.multipart.MultipartFile;
33 import com.fasterxml.jackson.databind.ObjectMapper;
36 @org.springframework.context.annotation.Configuration
37 @EnableAspectJAutoProxy
38 public class WidgetsCatalogController {
40 @Value("${server.port}")
42 @Value("${server.servlet.context-path}")
45 @Value("${spring.security.user.name}")
47 @Value("${spring.security.user.password}")
51 WidgetCatalogService widgetCatalogService;
54 StorageService storageService;
57 RestTemplate restTemplate;
59 AuthorizationUtil util = new AuthorizationUtil();
61 private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
64 @GetMapping(value = { "/microservices/widgetCatalog" }, produces = "application/json")
65 public List<WidgetCatalog> getWidgetCatalog(HttpServletRequest request, HttpServletResponse response
66 ,@RequestHeader(value="Authorization") String auth) throws IOException{
68 List<WidgetCatalog> widgetCatalog = null;
69 if(!util.authorization(auth, security_user, security_pass)){
70 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
71 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetCatalog in widget microserivce. Please check your username and password.");
75 widgetCatalog = widgetCatalogService.getWidgetCatalog();
76 logger.debug("WidgetsCatalogController.getWidgetCatalog: getting widget list {}", widgetCatalog);
77 } catch (Exception e) {
78 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetCatalog in widget microservices. Details:", e);
84 @GetMapping(value = { "/microservices/widgetCatalog/{loginName}" }, produces = "application/json")
85 public List<WidgetCatalog> getUserWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
86 @PathVariable("loginName") String loginName, @RequestHeader(value="Authorization") String auth) throws IOException {
87 List<WidgetCatalog> widgetCatalog = null;
88 if(!util.authorization(auth, security_user, security_pass)){
89 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
90 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getUserWidgetCatalog in widget microserivce. Please check your username and password.");
94 widgetCatalog = widgetCatalogService.getUserWidgetCatalog(loginName);
95 logger.debug("WidgetsCatalogController.getUserWidgetCatalog: getting widget list {}", widgetCatalog);
96 } catch (Exception e) {
97 logger.error("Exception occurred while performing WidgetsCatalogController.getUserWidgetCatalog in widget microservices. Details:", e);
103 @PutMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
104 public void updateWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
105 @RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId,
106 @RequestHeader(value="Authorization") String auth) throws IOException {
108 if(!util.authorization(auth, security_user, security_pass)){
109 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
110 logger.error("Basic Authentication Error while performing WidgetsCatalogController.updateWidgetCatalog in widget microserivce. Please check your username and password.");
114 widgetCatalogService.updateWidgetCatalog(widgetId, newWidgetCatalog);
115 logger.debug("WidgetsCatalogController.updateWidgetCatalog: updating widget {}", newWidgetCatalog);
116 } catch (Exception e) {
117 logger.error("Exception occurred while performing WidgetsCatalogController.updateWidgetCatalog in widget microservices. Details:", e);
122 @PostMapping(value = { "/microservices/widgetCatalog" }, produces = "application/json")
123 public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
124 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {
126 ValidationRespond respond = null;
127 if(!util.authorization(auth, security_user, security_pass)){
128 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
129 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
130 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
133 //check the zip file structure first
134 respond = storageService.checkZipFile(file);
136 if(respond.isValid()){
137 //save the widget catalog
138 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
140 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
141 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
142 //save the widget zip file ;
143 storageService.save(file, newWidget, widgetId);
146 } catch (Exception e) {
147 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
153 @PostMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
154 public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
155 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {
156 logger.debug("microserivces updating with files {}", widgetId);
157 ValidationRespond respond = null;
158 if(!util.authorization(auth, security_user, security_pass)){
159 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
160 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
161 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
164 //check the zip file structure first
165 respond = storageService.checkZipFile(file);
166 logger.debug("Check file validity"+respond.isValid()+respond.getError());
167 if(respond.isValid()){
168 //update the widget catalog
169 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
170 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
171 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
172 //update the widget zip file
173 storageService.updateJsFile(file, newWidget, widgetId);
175 } catch (Exception e) {
176 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
182 @DeleteMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
183 public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
184 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException{
185 if(!util.authorization(auth, security_user, security_pass)){
186 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
187 logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
191 logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
192 //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
193 widgetCatalogService.deleteWidgetCatalog(widgetId);
194 storageService.deleteWidgetFile(widgetId);
195 } catch (Exception e) {
196 logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:", e);
201 @GetMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, produces = "application/json")
202 public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response,
203 @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
205 Long serviceId = null;
206 if(!util.authorization(auth, security_user, security_pass)){
207 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
208 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
212 logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
213 serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
215 logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
222 @GetMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, produces = "application/json")
223 public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response,
224 @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
225 List<WidgetCatalog> list = new ArrayList<>();
226 if(!util.authorization(auth, security_user, security_pass)){
227 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
228 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
232 logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
233 list = widgetCatalogService.getWidgetsByServiceId(serviceId);
235 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
242 @GetMapping(value = { "/microservices/download/{widgetId}" }, produces = "application/json")
243 public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response,
244 @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
245 byte[] byteFile = null;
246 if(!util.authorization(auth, security_user, security_pass)){
247 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
248 logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
252 byteFile = storageService.getWidgetCatalogContent(widgetId);
253 logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
254 } catch (Exception e) {
255 logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:", e);