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.PersUserWidgetService;
54 import org.onap.portal.service.widget.WidgetService;
55 import org.onap.portal.service.user.FnUserService;
56 import org.onap.portal.utils.EcompPortalUtils;
57 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.context.annotation.EnableAspectJAutoProxy;
60 import org.springframework.http.MediaType;
61 import org.springframework.security.access.prepost.PreAuthorize;
62 import org.springframework.web.bind.annotation.DeleteMapping;
63 import org.springframework.web.bind.annotation.GetMapping;
64 import org.springframework.web.bind.annotation.PathVariable;
65 import org.springframework.web.bind.annotation.PostMapping;
66 import org.springframework.web.bind.annotation.PutMapping;
67 import org.springframework.web.bind.annotation.RequestBody;
68 import org.springframework.web.bind.annotation.RestController;
72 @EnableAspectJAutoProxy
73 public class WidgetsController {
75 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
77 private final FnUserService fnUserService;
78 private final WidgetService widgetService;
79 private final PersUserWidgetService persUserWidgetService;
82 public WidgetsController(final FnUserService fnUserService, final WidgetService widgetService,
83 final PersUserWidgetService persUserWidgetService) {
84 this.fnUserService = fnUserService;
85 this.widgetService = widgetService;
86 this.persUserWidgetService = persUserWidgetService;
89 @GetMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
90 public List<OnboardingWidget> getOnboardingWidgets(Principal principal, HttpServletRequest request,
91 HttpServletResponse response) {
92 FnUser user = fnUserService.loadUserByUsername(principal.getName());
93 List<OnboardingWidget> onboardingWidgets = null;
94 if (user.getGuest()) {
95 EcompPortalUtils.setBadPermissions(user, response, "getOnboardingWidgets");
97 String getType = request.getHeader("X-Widgets-Type");
98 if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) {
99 onboardingWidgets = widgetService.getOnboardingWidgets(user, "managed".equals(getType));
101 logger.debug(EELFLoggerDelegate.debugLogger,
102 "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'");
103 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
108 .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
109 return onboardingWidgets;
112 @PutMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
113 @PreAuthorize("hasRole('System_Administrator')")
114 public FieldsValidator putOnboardingWidget(Principal principal, @PathVariable("widgetId") Long widgetId,
115 @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
116 FnUser user = fnUserService.loadUserByUsername(principal.getName());
117 FieldsValidator fieldsValidator = null;
119 assert onboardingWidget != null;
120 onboardingWidget.setId(widgetId);
121 onboardingWidget.normalize();
123 fieldsValidator = widgetService.setOnboardingWidget(user.getId(), onboardingWidget);
124 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
125 } catch (IllegalArgumentException e) {
126 fieldsValidator = new FieldsValidator();
127 fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
128 fieldsValidator.addProblematicFieldName(e.getMessage());
129 return fieldsValidator;
132 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
133 response.getStatus());
135 return fieldsValidator;
138 @PostMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
139 @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
140 public FieldsValidator postOnboardingWidget(Principal principal, HttpServletResponse response,
141 @RequestBody OnboardingWidget onboardingWidget) {
142 FnUser user = fnUserService.loadUserByUsername(principal.getName());
143 FieldsValidator fieldsValidator;
145 onboardingWidget.setId(null);
146 onboardingWidget.normalize();
149 fieldsValidator = widgetService.setOnboardingWidget(user.getId(), onboardingWidget);
150 } catch (IllegalArgumentException e) {
151 fieldsValidator = new FieldsValidator();
152 fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
153 fieldsValidator.addProblematicFieldName(e.getMessage());
154 return fieldsValidator;
156 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
159 .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
160 return fieldsValidator;
163 @DeleteMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
164 @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
165 public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletResponse response,
166 @PathVariable("widgetId") Long widgetId) {
167 FnUser user = fnUserService.loadUserByUsername(principal.getName());
168 FieldsValidator fieldsValidator;
170 fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId);
171 response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
173 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
174 response.getStatus());
175 return fieldsValidator;
178 @PutMapping(value = {"portalApi/widgetCatalogSelection"}, produces = MediaType.APPLICATION_JSON_VALUE)
179 public FieldsValidator putWidgetCatalogSelection(Principal principal,
180 @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
181 FieldsValidator result = new FieldsValidator();
182 FnUser user = fnUserService.loadUserByUsername(principal.getName());
185 assert persRequest != null;
186 persUserWidgetService
187 .setPersUserAppValue(user, persRequest);
188 } catch (IllegalArgumentException iae) {
189 logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", iae);
190 response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, iae.getMessage());
191 } catch (Exception e) {
192 logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
193 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
195 result.setHttpStatusCode((long) HttpServletResponse.SC_OK);