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.contextPath}")
43 @Value("${security.user.name}")
45 @Value("${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.getMessage());
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.getMessage());
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.getMessage());
121 @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.POST, produces = "application/json")
122 public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
123 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {
125 ValidationRespond respond = null;
126 if(!util.authorization(auth, security_user, security_pass)){
127 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
128 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
129 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
132 //check the zip file structure first
133 respond = storageService.checkZipFile(file);
135 if(respond.isValid()){
136 //save the widget catalog
137 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
139 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
140 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
141 //save the widget zip file ;
142 storageService.save(file, newWidget, widgetId);
145 } catch (Exception e) {
146 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
152 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST, produces = "application/json")
153 public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
154 @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {
155 System.out.println("microserivces updating with files" + widgetId);
156 ValidationRespond respond = null;
157 if(!util.authorization(auth, security_user, security_pass)){
158 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
159 logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
160 return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
163 //check the zip file structure first
164 respond = storageService.checkZipFile(file);
165 if(respond.isValid()){
166 //update the widget catalog
167 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
168 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
169 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
170 //update the widget zip file
171 storageService.update(file, newWidget, widgetId);
173 } catch (Exception e) {
174 logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
181 @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = {
182 RequestMethod.DELETE }, 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.getMessage());
201 @RequestMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, method = RequestMethod.GET, 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.getMessage());
222 @RequestMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, method = RequestMethod.GET, 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<WidgetCatalog>();
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.getMessage());
242 @RequestMapping(value = { "/microservices/download/{widgetId}" }, method = RequestMethod.GET, 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.getMessage());