WidgetsCatalogMarkupController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / controller / WidgetsController.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.controller;
42
43 import java.io.IOException;
44 import java.security.Principal;
45 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import org.onap.portal.domain.db.fn.FnUser;
49 import org.onap.portal.domain.dto.transport.FieldsValidator;
50 import org.onap.portal.domain.dto.transport.OnboardingWidget;
51 import org.onap.portal.domain.dto.transport.WidgetCatalogPersonalization;
52 import org.onap.portal.logging.aop.EPAuditLog;
53 import org.onap.portal.service.AdminRolesService;
54 import org.onap.portal.service.PersUserWidgetService;
55 import org.onap.portal.service.WidgetService;
56 import org.onap.portal.service.fn.FnUserService;
57 import org.onap.portal.utils.EcompPortalUtils;
58 import org.onap.portal.validation.DataValidator;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.context.annotation.Configuration;
62 import org.springframework.context.annotation.EnableAspectJAutoProxy;
63 import org.springframework.http.MediaType;
64 import org.springframework.web.bind.annotation.DeleteMapping;
65 import org.springframework.web.bind.annotation.GetMapping;
66 import org.springframework.web.bind.annotation.PathVariable;
67 import org.springframework.web.bind.annotation.PostMapping;
68 import org.springframework.web.bind.annotation.PutMapping;
69 import org.springframework.web.bind.annotation.RequestBody;
70 import org.springframework.web.bind.annotation.RestController;
71
72 @EPAuditLog
73 @RestController
74 @EnableAspectJAutoProxy
75 public class WidgetsController {
76
77        private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
78
79        private final FnUserService fnUserService;
80        private final WidgetService widgetService;
81        private final AdminRolesService adminRolesService;
82        private final DataValidator dataValidator;
83        private final PersUserWidgetService persUserWidgetService;
84
85        @Autowired
86        public WidgetsController(final FnUserService fnUserService, final WidgetService widgetService,
87                final AdminRolesService adminRolesService, final DataValidator dataValidator,
88                final PersUserWidgetService persUserWidgetService) {
89               this.fnUserService = fnUserService;
90               this.widgetService = widgetService;
91               this.adminRolesService = adminRolesService;
92               this.dataValidator = dataValidator;
93               this.persUserWidgetService = persUserWidgetService;
94        }
95
96        @GetMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
97        public List<OnboardingWidget> getOnboardingWidgets(Principal principal, HttpServletRequest request,
98                HttpServletResponse response) {
99               FnUser user = fnUserService.loadUserByUsername(principal.getName());
100               List<OnboardingWidget> onboardingWidgets = null;
101
102               if (user == null || user.isGuest()) {
103                      EcompPortalUtils.setBadPermissions(user, response, "getOnboardingWidgets");
104               } else {
105                      String getType = request.getHeader("X-Widgets-Type");
106                      if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) {
107                             onboardingWidgets = widgetService.getOnboardingWidgets(user, "managed".equals(getType));
108                      } else {
109                             logger.debug(EELFLoggerDelegate.debugLogger,
110                                     "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'");
111                             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
112                      }
113               }
114
115               EcompPortalUtils
116                       .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
117               return onboardingWidgets;
118        }
119
120        @PutMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
121        public FieldsValidator putOnboardingWidget(Principal principal, HttpServletRequest request,
122                @PathVariable("widgetId") Long widgetId,
123                @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
124               FnUser user = fnUserService.loadUserByUsername(principal.getName());
125               FieldsValidator fieldsValidator = null;
126               if (onboardingWidget != null) {
127                      if (!dataValidator.isValid(onboardingWidget)) {
128                             fieldsValidator = new FieldsValidator();
129                             fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
130                             return fieldsValidator;
131                      }
132               }
133
134               if (userHasPermissions(user, response, "putOnboardingWidget")) {
135                      assert onboardingWidget != null;
136                      onboardingWidget.setId(widgetId);
137                      onboardingWidget.normalize();
138                      fieldsValidator = widgetService.setOnboardingWidget(user, onboardingWidget);
139                      response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
140               }
141               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
142                       response.getStatus());
143
144               return fieldsValidator;
145        }
146
147        private boolean userHasPermissions(FnUser user, HttpServletResponse response, String invocator) {
148               if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)) {
149                      EcompPortalUtils.setBadPermissions(user, response, invocator);
150                      return false;
151               }
152               return true;
153        }
154
155        @PostMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
156        public FieldsValidator postOnboardingWidget(Principal principal, HttpServletRequest request,
157                @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
158               FnUser user = fnUserService.loadUserByUsername(principal.getName());
159               FieldsValidator fieldsValidator = null;
160
161               if (onboardingWidget != null) {
162                      if (!dataValidator.isValid(onboardingWidget)) {
163                             fieldsValidator = new FieldsValidator();
164                             fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
165                             return fieldsValidator;
166                      }
167               }
168
169               if (userHasPermissions(user, response, "postOnboardingWidget")) {
170                      onboardingWidget.setId(null);
171                      onboardingWidget.normalize();
172                      fieldsValidator = widgetService.setOnboardingWidget(user, onboardingWidget);
173                      response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
174               }
175
176               EcompPortalUtils
177                       .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
178               return fieldsValidator;
179        }
180
181        @DeleteMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
182        public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletRequest request,
183                @PathVariable("widgetId") Long widgetId, HttpServletResponse response) {
184               FnUser user = fnUserService.loadUserByUsername(principal.getName());
185               FieldsValidator fieldsValidator = null;
186
187               if (userHasPermissions(user, response, "deleteOnboardingWidget")) {
188                      fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId);
189                      response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
190               }
191
192               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
193                       response.getStatus());
194               return fieldsValidator;
195        }
196
197        @PutMapping(value = {"portalApi/widgetCatalogSelection"}, produces = MediaType.APPLICATION_JSON_VALUE)
198        public FieldsValidator putWidgetCatalogSelection(Principal principal, HttpServletRequest request,
199                @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
200               FieldsValidator result = new FieldsValidator();
201               FnUser user = fnUserService.loadUserByUsername(principal.getName());
202
203               if (persRequest != null) {
204                      if (!dataValidator.isValid(persRequest)) {
205                             result.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
206                             return result;
207                      }
208               }
209               try {
210                      if (persRequest.getWidgetId() == null || user == null) {
211                             EcompPortalUtils.setBadPermissions(user, response, "putWidgetCatalogSelection");
212                      } else {
213                             persUserWidgetService
214                                     .setPersUserAppValue(user, persRequest.getWidgetId(), persRequest.getSelect());
215                      }
216               } catch (Exception e) {
217                      logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
218                      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
219               }
220               result.setHttpStatusCode((long) HttpServletResponse.SC_OK);
221               return result;
222        }
223 }