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