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 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.EnableAspectJAutoProxy;
62 import org.springframework.http.MediaType;
63 import org.springframework.security.access.prepost.PreAuthorize;
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;
74 @EnableAspectJAutoProxy
75 public class WidgetsController {
77 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
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;
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;
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 if (user.getGuest()) {
102 EcompPortalUtils.setBadPermissions(user, response, "getOnboardingWidgets");
104 String getType = request.getHeader("X-Widgets-Type");
105 if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) {
106 onboardingWidgets = widgetService.getOnboardingWidgets(user, "managed".equals(getType));
108 logger.debug(EELFLoggerDelegate.debugLogger,
109 "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'");
110 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
115 .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
116 return onboardingWidgets;
119 @PutMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
120 @PreAuthorize("hasRole('System_Administrator')")
121 public FieldsValidator putOnboardingWidget(Principal principal, @PathVariable("widgetId") Long widgetId,
122 @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
123 FnUser user = fnUserService.loadUserByUsername(principal.getName());
124 FieldsValidator fieldsValidator = null;
126 assert onboardingWidget != null;
127 onboardingWidget.setId(widgetId);
128 onboardingWidget.normalize();
130 fieldsValidator = widgetService.setOnboardingWidget(user.getUserId(), onboardingWidget);
131 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
132 } catch (IllegalArgumentException e) {
133 fieldsValidator = new FieldsValidator();
134 fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
135 fieldsValidator.addProblematicFieldName(e.getMessage());
136 return fieldsValidator;
139 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
140 response.getStatus());
142 return fieldsValidator;
145 @PostMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
146 @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
147 public FieldsValidator postOnboardingWidget(Principal principal, HttpServletResponse response,
148 @RequestBody OnboardingWidget onboardingWidget) {
149 FnUser user = fnUserService.loadUserByUsername(principal.getName());
150 FieldsValidator fieldsValidator;
152 onboardingWidget.setId(null);
153 onboardingWidget.normalize();
156 fieldsValidator = widgetService.setOnboardingWidget(user.getUserId(), onboardingWidget);
157 } catch (IllegalArgumentException e) {
158 fieldsValidator = new FieldsValidator();
159 fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
160 fieldsValidator.addProblematicFieldName(e.getMessage());
161 return fieldsValidator;
163 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
166 .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
167 return fieldsValidator;
170 @DeleteMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
171 @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
172 public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletResponse response,
173 @PathVariable("widgetId") Long widgetId) {
174 FnUser user = fnUserService.loadUserByUsername(principal.getName());
175 FieldsValidator fieldsValidator;
177 fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId);
178 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
180 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
181 response.getStatus());
182 return fieldsValidator;
185 @PutMapping(value = {"portalApi/widgetCatalogSelection"}, produces = MediaType.APPLICATION_JSON_VALUE)
186 public FieldsValidator putWidgetCatalogSelection(Principal principal,
187 @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
188 FieldsValidator result = new FieldsValidator();
189 FnUser user = fnUserService.loadUserByUsername(principal.getName());
192 assert persRequest != null;
193 persUserWidgetService
194 .setPersUserAppValue(user, persRequest);
195 } catch (IllegalArgumentException iae) {
196 logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", iae);
197 response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, iae.getMessage());
198 } catch (Exception e) {
199 logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
200 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
202 result.setHttpStatusCode((long) HttpServletResponse.SC_OK);