60d927724ef80138ffc617ed7101fda334c01daf
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / openecomp / portalapp / widget / controller / WidgetsCatalogController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.widget.controller;
39
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47 import org.openecomp.portalapp.widget.domain.ValidationRespond;
48 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
49 import org.openecomp.portalapp.widget.service.StorageService;
50 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
51 import org.openecomp.portalapp.widget.utils.AuthorizationUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.beans.factory.annotation.Value;
56 import org.springframework.context.annotation.EnableAspectJAutoProxy;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.PathVariable;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestHeader;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RequestMethod;
63 import org.springframework.web.bind.annotation.RequestParam;
64 import org.springframework.web.bind.annotation.ResponseBody;
65 import org.springframework.web.client.RestTemplate;
66 import org.springframework.web.multipart.MultipartFile;
67
68 import com.fasterxml.jackson.databind.ObjectMapper;
69
70 @Controller
71 @org.springframework.context.annotation.Configuration
72 @EnableAspectJAutoProxy
73 public class WidgetsCatalogController {
74
75         @Value("${server.port}")
76         String port;
77         @Value("${server.contextPath}")
78         String context;
79         
80         @Value("${security.user.name}")
81         String security_user;
82         @Value("${security.user.password}")
83         String security_pass;
84         
85         @Autowired
86         WidgetCatalogService widgetCatalogService;
87
88         @Autowired
89         StorageService storageService;
90         
91         @Autowired
92         RestTemplate restTemplate;
93
94         AuthorizationUtil util = new AuthorizationUtil();
95         
96         private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
97         
98         @ResponseBody
99         @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.GET, produces = "application/json")
100         public List<WidgetCatalog> getWidgetCatalog(HttpServletRequest request, HttpServletResponse response
101                         ,@RequestHeader(value="Authorization") String auth) throws IOException{
102                 
103                 List<WidgetCatalog> widgetCatalog = null;
104                 if(!util.authorization(auth, security_user, security_pass)){ 
105                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
106                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetCatalog in widget microserivce. Please check your username and password.");
107                         return widgetCatalog;
108                 }
109                 try {
110                         widgetCatalog = widgetCatalogService.getWidgetCatalog();
111                         logger.debug("WidgetsCatalogController.getWidgetCatalog: getting widget list {}", widgetCatalog);
112                 } catch (Exception e) {
113                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetCatalog in widget microservices. Details:" + e.getMessage());
114                 }
115                 return widgetCatalog;
116         }
117         
118         @ResponseBody
119         @RequestMapping(value = { "/microservices/widgetCatalog/{loginName}" }, method = RequestMethod.GET, produces = "application/json")
120         public List<WidgetCatalog> getUserWidgetCatalog(HttpServletRequest request, HttpServletResponse response, 
121                         @PathVariable("loginName") String loginName, @RequestHeader(value="Authorization") String auth) throws IOException {
122                 List<WidgetCatalog> widgetCatalog = null;
123                 if(!util.authorization(auth, security_user, security_pass)){
124                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
125                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getUserWidgetCatalog in widget microserivce. Please check your username and password.");
126                         return widgetCatalog;
127                 }
128                 try {
129                         widgetCatalog = widgetCatalogService.getUserWidgetCatalog(loginName);
130                         logger.debug("WidgetsCatalogController.getUserWidgetCatalog: getting widget list {}", widgetCatalog);
131                 } catch (Exception e) {
132                         logger.error("Exception occurred while performing WidgetsCatalogController.getUserWidgetCatalog in widget microservices. Details:" + e.getMessage());
133                 }
134                 return widgetCatalog;
135         }
136
137         @ResponseBody
138         @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.PUT, produces = "application/json")
139         public void updateWidgetCatalog(HttpServletRequest request, HttpServletResponse response,
140                         @RequestBody WidgetCatalog newWidgetCatalog, @PathVariable("widgetId") long widgetId,
141                         @RequestHeader(value="Authorization") String auth) throws IOException {
142
143                 if(!util.authorization(auth, security_user, security_pass)){
144                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
145                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.updateWidgetCatalog in widget microserivce. Please check your username and password.");
146                         return;
147                 }
148                 try {
149                         widgetCatalogService.updateWidgetCatalog(widgetId, newWidgetCatalog);
150                         logger.debug("WidgetsCatalogController.updateWidgetCatalog: updating widget {}", newWidgetCatalog);
151                 } catch (Exception e) {
152                         logger.error("Exception occurred while performing WidgetsCatalogController.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
153                         e.printStackTrace();
154                 }
155         }
156         
157         @ResponseBody
158         @RequestMapping(value = { "/microservices/widgetCatalog" }, method = RequestMethod.POST, produces = "application/json")
159         public ValidationRespond saveWidgetCatalog(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
160                         @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget) throws IOException {   
161         
162                 ValidationRespond respond = null;
163                 if(!util.authorization(auth, security_user, security_pass)){
164                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
165                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
166                         return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
167                 }       
168                 try {
169                         //check the zip file structure first
170                         respond = storageService.checkZipFile(file);
171                         
172                         if(respond.isValid()){ 
173                                 //save the widget catalog
174                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
175                                 
176                                 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
177                                 logger.debug("WidgetsCatalogController.saveWidgetCatalog: saving widget={}", newWidget);
178                                 //save the widget zip file ;
179                                 storageService.save(file, newWidget, widgetId);
180                         }
181                         
182                 } catch (Exception e) {
183                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
184                 }
185                 return respond;
186         }
187
188         @ResponseBody
189         @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = RequestMethod.POST, produces = "application/json")
190         public ValidationRespond updateWidgetCatalogwithFiles(HttpServletRequest request, HttpServletResponse response, @RequestHeader(value="Authorization") String auth,
191                         @RequestParam("file") MultipartFile file, @RequestParam("widget") String widget, @PathVariable("widgetId") long widgetId) throws IOException {  
192                 System.out.println("microserivces updating with files" + widgetId);
193                 ValidationRespond respond = null;
194                 if(!util.authorization(auth, security_user, security_pass)){
195                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
196                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.saveWidgetCatalog in widget microserivce. Please check your username and password.");
197                         return new ValidationRespond(false, "Basic Authentication Error, please check your username and password.");
198                 }       
199                 try {
200                         //check the zip file structure first
201                         respond = storageService.checkZipFile(file);
202                         if(respond.isValid()){
203                                 //update the widget catalog
204                                 WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
205                                 widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
206                                 logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
207                                 //update the widget zip file
208                                 storageService.update(file, newWidget, widgetId);
209                         }
210                 } catch (Exception e) {
211                         logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
212                         e.printStackTrace();
213                 }
214                 return respond;
215         }
216         
217         @ResponseBody
218         @RequestMapping(value = { "/microservices/widgetCatalog/{widgetId}" }, method = {
219                         RequestMethod.DELETE }, produces = "application/json")
220         public void deleteOnboardingWidget(HttpServletRequest request, HttpServletResponse response,
221                         @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth)  throws IOException{                
222                 if(!util.authorization(auth, security_user, security_pass)){
223                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
224                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.deleteOnboardingWidget in widget microserivce. Please check your username and password.");
225                         return;
226                 }
227                 try {
228                         logger.debug("WidgetsCatalogController.deleteOnboardingWidget: deleting widget {}", widgetId);
229                         //WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
230                         widgetCatalogService.deleteWidgetCatalog(widgetId);
231                         storageService.deleteWidgetFile(widgetId);
232                 } catch (Exception e) {
233                         logger.error("Exception occurred while performing WidgetsCatalogController.deleteOnboardingWidget in widget microservices. Details:" + e.getMessage());
234                 }
235         }
236         
237         @ResponseBody
238         @RequestMapping(value = { "/microservices/widgetCatalog/parameters/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
239         public Long getServiceIdByWidget(HttpServletRequest request, HttpServletResponse response, 
240                         @PathVariable("widgetId") Long widgetId, @RequestHeader(value="Authorization") String auth) throws IOException {
241                 
242                 Long serviceId = null;
243                 if(!util.authorization(auth, security_user, security_pass)){
244                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
245                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getServiceIdByWidget in widget microserivce. Please check your username and password.");
246                         return serviceId;
247                 }
248                 try{
249                         logger.debug("WidgetsCatalogController.getServiceIdByWidget: getting service Id for widget {}", widgetId);
250                         serviceId = widgetCatalogService.getServiceIdByWidget(widgetId);
251                 }catch(Exception e){
252                         logger.error("Exception occurred while performing WidgetsCatalogController.getServiceIdByWidget in widget microservices. Details:" + e.getMessage());
253                 }
254                 return serviceId;
255         }
256
257         
258         @ResponseBody
259         @RequestMapping(value = { "/microservices/widgetCatalog/service/{serviceId}" }, method = RequestMethod.GET, produces = "application/json")
260         public List<WidgetCatalog> getWidgetByServiceId(HttpServletRequest request, HttpServletResponse response, 
261                         @PathVariable("serviceId") Long serviceId, @RequestHeader(value="Authorization") String auth) throws IOException {
262                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
263                 if(!util.authorization(auth, security_user, security_pass)){
264                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
265                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetByServiceId in widget microserivce. Please check your username and password.");
266                         return null;
267                 }       
268                 try{
269                         logger.debug("WidgetsCatalogController.getWidgetByServiceId: getting service Id for widget {}", serviceId);
270                         list = widgetCatalogService.getWidgetsByServiceId(serviceId);
271                 }catch(Exception e){
272                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetByServiceId in widget microservices. Details:" + e.getMessage());
273                 }
274                 return list;
275         }
276         
277         
278         @ResponseBody
279         @RequestMapping(value = { "/microservices/download/{widgetId}" }, method = RequestMethod.GET, produces = "application/json")
280         public byte[] getWidgetZipFile(HttpServletRequest request, HttpServletResponse response, 
281                         @PathVariable("widgetId") long widgetId, @RequestHeader(value="Authorization") String auth) throws Exception {
282                 byte[] byteFile = null;
283                 if(!util.authorization(auth, security_user, security_pass)){
284                         response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
285                         logger.error("Basic Authentication Error while performing WidgetsCatalogController.getWidgetZipFile in widget microserivce. Please check your username and password.");
286                         return byteFile;
287                 }
288                 try {
289                         byteFile = storageService.getWidgetCatalogContent(widgetId);
290                         logger.debug("WidgetsCatalogController.getWidgetZipFile: getting widget zip file for widget with id {}", widgetId);
291                 } catch (Exception e) {
292                         logger.error("Exception occurred while performing WidgetsCatalogController.getWidgetZipFile in widget microservices. Details:" + e.getMessage());
293                 }
294                 return byteFile;
295         }
296
297         
298 }