App onboarding fixes
[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.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;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 @Controller
36 @org.springframework.context.annotation.Configuration
37 @EnableAspectJAutoProxy
38 public class WidgetsCatalogController {
39
40         @Value("${server.port}")
41         String port;
42         @Value("${server.servlet.context-path}")
43         String context;
44         
45         @Value("${spring.security.user.name}")
46         String security_user;
47         @Value("${spring.security.user.password}")
48         String security_pass;
49         
50         @Autowired
51         WidgetCatalogService widgetCatalogService;
52
53         @Autowired
54         StorageService storageService;
55         
56         @Autowired
57         RestTemplate restTemplate;
58
59         AuthorizationUtil util = new AuthorizationUtil();
60         
61         private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
62         
63         @ResponseBody
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{
67                 
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.");
72                         return widgetCatalog;
73                 }
74                 try {
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);
79                 }
80                 return widgetCatalog;
81         }
82         
83         @ResponseBody
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.");
91                         return widgetCatalog;
92                 }
93                 try {
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);
98                 }
99                 return widgetCatalog;
100         }
101
102         @ResponseBody
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 {
107
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.");
111                         return;
112                 }
113                 try {
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);
118                 }
119         }
120         
121         @ResponseBody
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 {   
125         
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.");
131                 }       
132                 try {
133                         //check the zip file structure first
134                         respond = storageService.checkZipFile(file);
135                         
136                         if(respond.isValid()){ 
137                                 //save the widget catalog
138                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
139                                 
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);
144                         }
145                         
146                 } catch (Exception e) {
147                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
148                 }
149                 return respond;
150         }
151
152         @ResponseBody
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.");
162                 }       
163                 try {
164                         //check the zip file structure first
165                         respond = storageService.checkZipFile(file);
166                         if(respond.isValid()){
167                                 //update the widget catalog
168                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
169                                 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
170                                 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
171                                 //update the widget zip file
172                                 storageService.update(file, newWidget, widgetId);
173                         }
174                 } catch (Exception e) {
175                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
176                 }
177                 return respond;
178         }
179         
180         @ResponseBody
181         @DeleteMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, produces = "application/json")
182         public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
183                         @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth)  throws IOException{                
184                 if(!util.authorization(auth, security_user, security_pass)){
185                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
186                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
187                         return;
188                 }
189                 try {
190                         logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
191                         //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
192                         widgetCatalogService.deleteWidgetCatalog(widgetId);
193                         storageService.deleteWidgetFile(widgetId);
194                 } catch (Exception e) {
195                         logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:", e);
196                 }
197         }
198         
199         @ResponseBody
200         @GetMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, produces = "application/json")
201         public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response, 
202                         @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
203                 
204                 Long serviceId = null;
205                 if(!util.authorization(auth, security_user, security_pass)){
206                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
207                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
208                         return serviceId;
209                 }
210                 try{
211                         logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
212                         serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
213                 }catch(Exception e){
214                         logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
215                 }
216                 return serviceId;
217         }
218
219         
220         @ResponseBody
221         @GetMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, produces = "application/json")
222         public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response, 
223                         @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
224                 List<WidgetCatalog> list = new ArrayList<>();
225                 if(!util.authorization(auth, security_user, security_pass)){
226                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
227                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
228                         return null;
229                 }       
230                 try{
231                         logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
232                         list = widgetCatalogService.getWidgetsByServiceId(serviceId);
233                 }catch(Exception e){
234                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
235                 }
236                 return list;
237         }
238         
239         
240         @ResponseBody
241         @GetMapping(value = { "/microservices/download/{widgetId}" }, produces = "application/json")
242         public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response, 
243                         @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
244                 byte[] byteFile = null;
245                 if(!util.authorization(auth, security_user, security_pass)){
246                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
247                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
248                         return byteFile;
249                 }
250                 try {
251                         byteFile = storageService.getWidgetCatalogContent(widgetId);
252                         logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
253                 } catch (Exception e) {
254                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:", e);
255                 }
256                 return byteFile;
257         }
258
259         
260 }