2bea2352844712c0f433d9e845d372e5d5bbaa28
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / onap / portalapp / widget / controller / WidgetsCatalogController.java
1 package org.onap.portalapp.widget.controller;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.servlet.http.HttpServletRequest; 
8 import javax.servlet.http.HttpServletResponse;
9
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;
34
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 @Controller
38 @org.springframework.context.annotation.Configuration
39 @EnableAspectJAutoProxy
40 public class WidgetsCatalogController {
41
42         @Value("${server.port}")
43         String port;
44         @Value("${server.servlet.context-path}")
45         String context;
46         
47         @Value("${spring.security.user.name}")
48         String security_user;
49         @Value("${spring.security.user.password}")
50         String security_pass;
51         
52         @Autowired
53         WidgetCatalogService widgetCatalogService;
54
55         @Autowired
56         StorageService storageService;
57         
58         @Autowired
59         RestTemplate restTemplate;
60
61         AuthorizationUtil util = new AuthorizationUtil();
62         
63         private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
64         
65         @ResponseBody
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{
69                 
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.");
74                         return widgetCatalog;
75                 }
76                 try {
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);
81                 }
82                 return widgetCatalog;
83         }
84         
85         @ResponseBody
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.");
93                         return widgetCatalog;
94                 }
95                 try {
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);
100                 }
101                 return widgetCatalog;
102         }
103
104         @ResponseBody
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 {
109
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.");
113                         return;
114                 }
115                 try {
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);
120                 }
121         }
122         
123         @ResponseBody
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 {   
127         
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.");
133                 }       
134                 try {
135                         //check the zip file structure first
136                         respond = storageService.checkZipFile(file);
137                         
138                         if(respond.isValid()){ 
139                                 //save the widget catalog
140                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
141                                 
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);
146                         }
147                         
148                 } catch (Exception e) {
149                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
150                 }
151                 return respond;
152         }
153
154         @ResponseBody
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.");
164                 }       
165                 try {
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);
175                         }
176                 } catch (Exception e) {
177                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
178                 }
179                 return respond;
180         }
181         
182         @ResponseBody
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.");
189                         return;
190                 }
191                 try {
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);
198                 }
199         }
200         
201         @ResponseBody
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 {
205                 
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.");
210                         return serviceId;
211                 }
212                 try{
213                         logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
214                         serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
215                 }catch(Exception e){
216                         logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
217                 }
218                 return serviceId;
219         }
220
221         
222         @ResponseBody
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.");
230                         return null;
231                 }       
232                 try{
233                         logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
234                         list = widgetCatalogService.getWidgetsByServiceId(serviceId);
235                 }catch(Exception e){
236                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
237                 }
238                 return list;
239         }
240         
241         
242         @ResponseBody
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.");
250                         return byteFile;
251                 }
252                 try {
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);
257                 }
258                 return byteFile;
259         }
260
261         
262 }