2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 * https://creativecommons.org/licenses/by/4.0/
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.
36 * ============LICENSE_END============================================
41 package org.onap.portal.controller;
43 import java.io.IOException;
44 import java.security.Principal;
45 import java.util.List;
46 import java.util.Optional;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49 import org.onap.portal.domain.db.fn.FnUser;
50 import org.onap.portal.domain.dto.transport.FieldsValidator;
51 import org.onap.portal.domain.dto.transport.OnboardingWidget;
52 import org.onap.portal.domain.dto.transport.WidgetCatalogPersonalization;
53 import org.onap.portal.logging.aop.EPAuditLog;
54 import org.onap.portal.service.AdminRolesService;
55 import org.onap.portal.service.PersUserWidgetService;
56 import org.onap.portal.service.WidgetService;
57 import org.onap.portal.service.fn.FnUserService;
58 import org.onap.portal.utils.EcompPortalUtils;
59 import org.onap.portal.validation.DataValidator;
60 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.context.annotation.Configuration;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.http.MediaType;
65 import org.springframework.web.bind.annotation.DeleteMapping;
66 import org.springframework.web.bind.annotation.GetMapping;
67 import org.springframework.web.bind.annotation.PathVariable;
68 import org.springframework.web.bind.annotation.PostMapping;
69 import org.springframework.web.bind.annotation.PutMapping;
70 import org.springframework.web.bind.annotation.RequestBody;
71 import org.springframework.web.bind.annotation.RestController;
75 @EnableAspectJAutoProxy
76 public class WidgetsController {
78 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
80 private final FnUserService fnUserService;
81 private final WidgetService widgetService;
82 private final AdminRolesService adminRolesService;
83 private final DataValidator dataValidator;
84 private final PersUserWidgetService persUserWidgetService;
87 public WidgetsController(final FnUserService fnUserService, final WidgetService widgetService,
88 final AdminRolesService adminRolesService, final DataValidator dataValidator,
89 final PersUserWidgetService persUserWidgetService) {
90 this.fnUserService = fnUserService;
91 this.widgetService = widgetService;
92 this.adminRolesService = adminRolesService;
93 this.dataValidator = dataValidator;
94 this.persUserWidgetService = persUserWidgetService;
97 @GetMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
98 public List<OnboardingWidget> getOnboardingWidgets(Principal principal, HttpServletRequest request,
99 HttpServletResponse response) {
100 FnUser user = fnUserService.loadUserByUsername(principal.getName());
101 List<OnboardingWidget> onboardingWidgets = null;
103 if (user.getGuest()) {
104 EcompPortalUtils.setBadPermissions(user, response, "getOnboardingWidgets");
106 String getType = request.getHeader("X-Widgets-Type");
107 if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) {
108 onboardingWidgets = widgetService.getOnboardingWidgets(user, "managed".equals(getType));
110 logger.debug(EELFLoggerDelegate.debugLogger,
111 "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'");
112 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
117 .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
118 return onboardingWidgets;
121 @PutMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
122 public FieldsValidator putOnboardingWidget(Principal principal, HttpServletRequest request,
123 @PathVariable("widgetId") Long widgetId,
124 @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
125 FnUser user = fnUserService.loadUserByUsername(principal.getName());
126 FieldsValidator fieldsValidator = null;
127 if (onboardingWidget != null) {
128 if (!dataValidator.isValid(onboardingWidget)) {
129 fieldsValidator = new FieldsValidator();
130 fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
131 return fieldsValidator;
135 if (userHasPermissions(user, response, "putOnboardingWidget")) {
136 assert onboardingWidget != null;
137 onboardingWidget.setId(widgetId);
138 onboardingWidget.normalize();
139 fieldsValidator = widgetService.setOnboardingWidget(user, onboardingWidget);
140 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
142 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
143 response.getStatus());
145 return fieldsValidator;
148 private boolean userHasPermissions(FnUser user, HttpServletResponse response, String invocator) {
149 if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)) {
150 EcompPortalUtils.setBadPermissions(user, response, invocator);
156 @PostMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
157 public FieldsValidator postOnboardingWidget(Principal principal, HttpServletRequest request,
158 @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
159 FnUser user = fnUserService.loadUserByUsername(principal.getName());
160 FieldsValidator fieldsValidator = null;
162 if (onboardingWidget != null) {
163 if (!dataValidator.isValid(onboardingWidget)) {
164 fieldsValidator = new FieldsValidator();
165 fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
166 return fieldsValidator;
170 if (userHasPermissions(user, response, "postOnboardingWidget")) {
171 onboardingWidget.setId(null);
172 onboardingWidget.normalize();
173 fieldsValidator = widgetService.setOnboardingWidget(user, onboardingWidget);
174 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
178 .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
179 return fieldsValidator;
182 @DeleteMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
183 public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletRequest request,
184 @PathVariable("widgetId") Long widgetId, HttpServletResponse response) {
185 FnUser user = fnUserService.loadUserByUsername(principal.getName());
186 FieldsValidator fieldsValidator = null;
188 if (userHasPermissions(user, response, "deleteOnboardingWidget")) {
189 fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId);
190 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
193 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
194 response.getStatus());
195 return fieldsValidator;
198 @PutMapping(value = {"portalApi/widgetCatalogSelection"}, produces = MediaType.APPLICATION_JSON_VALUE)
199 public FieldsValidator putWidgetCatalogSelection(Principal principal, HttpServletRequest request,
200 @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
201 FieldsValidator result = new FieldsValidator();
202 FnUser user = fnUserService.loadUserByUsername(principal.getName());
204 if (persRequest != null) {
205 if (!dataValidator.isValid(persRequest)) {
206 result.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
211 if (persRequest.getWidgetId() == null || user == null) {
212 EcompPortalUtils.setBadPermissions(user, response, "putWidgetCatalogSelection");
214 persUserWidgetService
215 .setPersUserAppValue(user, persRequest.getWidgetId(), persRequest.getSelect());
217 } catch (Exception e) {
218 logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
219 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
221 result.setHttpStatusCode((long) HttpServletResponse.SC_OK);