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