35390b19250095f5ea1b900cc62fb8e4c6de1516
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / AppCatalogController.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.portal.controller;
39
40 import java.io.IOException;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
47 import org.openecomp.portalapp.portal.domain.EPApp;
48 import org.openecomp.portalapp.portal.domain.EPUser;
49 import org.openecomp.portalapp.portal.ecomp.model.AppCatalogItem;
50 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
51 import org.openecomp.portalapp.portal.service.AdminRolesService;
52 import org.openecomp.portalapp.portal.service.EPAppService;
53 import org.openecomp.portalapp.portal.service.PersUserAppService;
54 import org.openecomp.portalapp.portal.transport.AppCatalogPersonalization;
55 import org.openecomp.portalapp.portal.transport.FieldsValidator;
56 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
57 import org.openecomp.portalapp.util.EPUserUtils;
58 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.web.bind.annotation.RequestBody;
62 import org.springframework.web.bind.annotation.RequestMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.bind.annotation.RestController;
65
66 @RestController
67 @org.springframework.context.annotation.Configuration
68 @EnableAspectJAutoProxy
69 @EPAuditLog
70 public class AppCatalogController extends EPRestrictedBaseController {
71
72         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppCatalogController.class);
73
74         @Autowired
75         private AdminRolesService adminRolesService;
76         @Autowired
77         private EPAppService appService;
78         @Autowired
79         private PersUserAppService persUserAppService;
80
81         /**
82          * RESTful service method to fetch all enabled applications, with details
83          * about which are accessible to the current user, selected by the current
84          * user.
85          * 
86          * @param request
87          *            HttpServletRequest
88          * @param response
89          *            HttpServletResponse
90          * @throws IOException If sendError fails
91          * @return List of items suitable for display
92          */
93         @RequestMapping(value = { "/portalApi/appCatalog" }, method = RequestMethod.GET, produces = "application/json")
94         public List<AppCatalogItem> getAppCatalog(HttpServletRequest request, HttpServletResponse response)
95                         throws IOException {
96                 EPUser user = EPUserUtils.getUserSession(request);
97                 List<AppCatalogItem> appCatalog = null;
98                 try {
99                         if (user == null) {
100                                 EcompPortalUtils.setBadPermissions(user, response, "getAppCatalog");
101                         } else {
102                                 if (adminRolesService.isSuperAdmin(user))
103                                         appCatalog = appService.getAdminAppCatalog(user);
104                                 else
105                                         appCatalog = appService.getUserAppCatalog(user);
106                                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/getAppCatalog", "GET result =", appCatalog);
107                         }
108                 } catch (Exception e) {
109                         logger.error(EELFLoggerDelegate.errorLogger, "getAppCatalog failed", e);
110                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
111                 }
112                 return appCatalog;
113         }
114
115         /**
116          * RESTful service to accept a user's action made on the application
117          * catalog.
118          * 
119          * @param request
120          *            HttpServletRequest
121          * @param persRequest
122          *            JSON with data including application ID
123          * @param response
124          *            HttpServletResponse
125          * @return FieldsValidator
126          * @throws IOException If sendError fails
127          */
128         @RequestMapping(value = { "/portalApi/appCatalog" }, method = RequestMethod.PUT, produces = "application/json")
129         public FieldsValidator putAppCatalogSelection(HttpServletRequest request,
130                         @RequestBody AppCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
131                 FieldsValidator result = new FieldsValidator();
132                 EPApp app = appService.getApp(persRequest.getAppId());
133                 EPUser user = EPUserUtils.getUserSession(request);
134                 try {
135                         if (app == null || user == null) {
136                                 EcompPortalUtils.setBadPermissions(user, response, "putAppCatalogSelection");
137                         } else {
138                                 persUserAppService.setPersUserAppValue(user, app, persRequest.getSelect(), persRequest.getPending());
139                         }
140                 } catch (Exception e) {
141                         logger.error(EELFLoggerDelegate.errorLogger, "putAppCatalogSelection failed", e);
142                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
143                 }
144                 result.httpStatusCode = new Long(HttpServletResponse.SC_OK);
145                 return result;
146         }
147
148 }