Fix sonar issues in portal
[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.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);
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);
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);
116                 }
117         }
118         
119         @ResponseBody
120         @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.POST, produces = "application/json")
121         public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
122                         @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {   
123         
124                 ValidationRespond respond = null;
125                 if(!util.authorization(auth, security_user, security_pass)){
126                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
127                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
128                         return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
129                 }       
130                 try {
131                         //check the zip file structure first
132                         respond = storageService.checkZipFile(file);
133                         
134                         if(respond.isValid()){ 
135                                 //save the widget catalog
136                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
137                                 
138                                 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
139                                 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
140                                 //save the widget zip file ;
141                                 storageService.save(file, newWidget, widgetId);
142                         }
143                         
144                 } catch (Exception e) {
145                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
146                 }
147                 return respond;
148         }
149
150         @ResponseBody
151         @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST, produces = "application/json")
152         public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
153                         @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {  
154                 logger.debug("microserivces updating with files {}", widgetId);
155                 ValidationRespond respond = null;
156                 if(!util.authorization(auth, security_user, security_pass)){
157                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
158                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
159                         return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
160                 }       
161                 try {
162                         //check the zip file structure first
163                         respond = storageService.checkZipFile(file);
164                         if(respond.isValid()){
165                                 //update the widget catalog
166                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
167                                 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
168                                 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
169                                 //update the widget zip file
170                                 storageService.update(file, newWidget, widgetId);
171                         }
172                 } catch (Exception e) {
173                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
174                 }
175                 return respond;
176         }
177         
178         @ResponseBody
179         @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = {
180                         RequestMethod.DELETE }, produces = "application/json")
181         public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
182                         @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth)  throws IOException{                
183                 if(!util.authorization(auth, security_user, security_pass)){
184                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
185                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
186                         return;
187                 }
188                 try {
189                         logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
190                         //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
191                         widgetCatalogService.deleteWidgetCatalog(widgetId);
192                         storageService.deleteWidgetFile(widgetId);
193                 } catch (Exception e) {
194                         logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:", e);
195                 }
196         }
197         
198         @ResponseBody
199         @RequestMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
200         public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response, 
201                         @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
202                 
203                 Long serviceId = null;
204                 if(!util.authorization(auth, security_user, security_pass)){
205                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
206                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
207                         return serviceId;
208                 }
209                 try{
210                         logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
211                         serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
212                 }catch(Exception e){
213                         logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:", e);
214                 }
215                 return serviceId;
216         }
217
218         
219         @ResponseBody
220         @RequestMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, method = RequestMethod.GET, produces = "application/json")
221         public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response, 
222                         @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
223                 List<WidgetCatalog> list = new ArrayList<>();
224                 if(!util.authorization(auth, security_user, security_pass)){
225                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
226                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
227                         return null;
228                 }       
229                 try{
230                         logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
231                         list = widgetCatalogService.getWidgetsByServiceId(serviceId);
232                 }catch(Exception e){
233                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:", e);
234                 }
235                 return list;
236         }
237         
238         
239         @ResponseBody
240         @RequestMapping(value = { "/microservices/download/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
241         public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response, 
242                         @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
243                 byte[] byteFile = null;
244                 if(!util.authorization(auth, security_user, security_pass)){
245                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
246                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
247                         return byteFile;
248                 }
249                 try {
250                         byteFile = storageService.getWidgetCatalogContent(widgetId);
251                         logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
252                 } catch (Exception e) {
253                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:", e);
254                 }
255                 return byteFile;
256         }
257
258         
259 }