Merge "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                         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);
174                         }
175                 } catch (Exception e) {
176                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
177                 }
178                 return respond;
179         }
180         
181         @ResponseBody
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.");
188                         return;
189                 }
190                 try {
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);
197                 }
198         }
199         
200         @ResponseBody
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 {
204                 
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.");
209                         return serviceId;
210                 }
211                 try{
212                         logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
213                         serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
214                 }catch(Exception e){
215                         logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
216                 }
217                 return serviceId;
218         }
219
220         
221         @ResponseBody
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.");
229                         return null;
230                 }       
231                 try{
232                         logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
233                         list = widgetCatalogService.getWidgetsByServiceId(serviceId);
234                 }catch(Exception e){
235                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
236                 }
237                 return list;
238         }
239         
240         
241         @ResponseBody
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.");
249                         return byteFile;
250                 }
251                 try {
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);
256                 }
257                 return byteFile;
258         }
259
260         
261 }