[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-widget-ms / src / main / java / org / openecomp / portalapp / widget / controller / WidgetsCatalogController.java
1 package org.openecomp.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.openecomp.portalapp.widget.domain.ValidationRespond;
11 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
12 import org.openecomp.portalapp.widget.service.StorageService;
13 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
14 import org.openecomp.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;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 @Controller
34 @org.springframework.context.annotation.Configuration
35 @EnableAspectJAutoProxy
36 public class WidgetsCatalogController {
37
38         @Value("${server.port}")
39         String port;
40         @Value("${server.contextPath}")
41         String context;
42         
43         @Value("${security.user.name}")
44         String security_user;
45         @Value("${security.user.password}")
46         String security_pass;
47         
48         @Autowired
49         WidgetCatalogService widgetCatalogService;
50
51         @Autowired
52         StorageService storageService;
53         
54         @Autowired
55         RestTemplate restTemplate;
56
57         AuthorizationUtil util = new AuthorizationUtil();
58         
59         private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
60         
61         @ResponseBody
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{
65                 
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.");
70                         return widgetCatalog;
71                 }
72                 try {
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());
77                 }
78                 return widgetCatalog;
79         }
80         
81         @ResponseBody
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.");
89                         return widgetCatalog;
90                 }
91                 try {
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());
96                 }
97                 return widgetCatalog;
98         }
99
100         @ResponseBody
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 {
105
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.");
109                         return;
110                 }
111                 try {
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());
116                         e.printStackTrace();
117                 }
118         }
119         
120         @ResponseBody
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 {   
124         
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.");
130                 }       
131                 try {
132                         //check the zip file structure first
133                         respond = storageService.checkZipFile(file);
134                         
135                         if(respond.isValid()){ 
136                                 //save the widget catalog
137                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
138                                 
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);
143                         }
144                         
145                 } catch (Exception e) {
146                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
147                 }
148                 return respond;
149         }
150
151         @ResponseBody
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.");
161                 }       
162                 try {
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);
172                         }
173                 } catch (Exception e) {
174                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
175                         e.printStackTrace();
176                 }
177                 return respond;
178         }
179         
180         @ResponseBody
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.");
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.getMessage());
197                 }
198         }
199         
200         @ResponseBody
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 {
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.getMessage());
216                 }
217                 return serviceId;
218         }
219
220         
221         @ResponseBody
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.");
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.getMessage());
236                 }
237                 return list;
238         }
239         
240         
241         @ResponseBody
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.");
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.getMessage());
256                 }
257                 return byteFile;
258         }
259
260         
261 }