5620204f2bb382a1a69a585730bb0fc3e66ddee8
[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.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;
69
70 @EPAuditLog
71 @RestController
72 public class WidgetsController {
73
74        private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
75
76        private final FnUserService fnUserService;
77        private final WidgetService widgetService;
78        private final PersUserWidgetService persUserWidgetService;
79
80        @Autowired
81        public WidgetsController(final FnUserService fnUserService, final WidgetService widgetService,
82                final PersUserWidgetService persUserWidgetService) {
83               this.fnUserService = fnUserService;
84               this.widgetService = widgetService;
85               this.persUserWidgetService = persUserWidgetService;
86        }
87
88        @GetMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
89        public List<OnboardingWidget> getOnboardingWidgets(Principal principal, HttpServletRequest request,
90                HttpServletResponse response) {
91               FnUser user = fnUserService.loadUserByUsername(principal.getName());
92               List<OnboardingWidget> onboardingWidgets = null;
93               if (user.getGuest()) {
94                      EcompPortalUtils.setBadPermissions(user, response, "getOnboardingWidgets");
95               } else {
96                      String getType = request.getHeader("X-Widgets-Type");
97                      if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) {
98                             onboardingWidgets = widgetService.getOnboardingWidgets(user.getOrgUserId(), user.getId(), "managed".equals(getType));
99                      } else {
100                             logger.debug(EELFLoggerDelegate.debugLogger,
101                                     "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'");
102                             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
103                      }
104               }
105
106               EcompPortalUtils
107                       .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
108               return onboardingWidgets;
109        }
110
111        @PutMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
112        @PreAuthorize("hasRole('System_Administrator')")
113        public FieldsValidator putOnboardingWidget(Principal principal, @PathVariable("widgetId") Long widgetId,
114                @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
115               FnUser user = fnUserService.loadUserByUsername(principal.getName());
116               FieldsValidator fieldsValidator = null;
117
118               assert onboardingWidget != null;
119               onboardingWidget.setId(widgetId);
120               onboardingWidget.normalize();
121               try {
122                      fieldsValidator = widgetService.setOnboardingWidget(user.getId(), onboardingWidget);
123                      response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
124               } catch (IllegalArgumentException e) {
125                      fieldsValidator = new FieldsValidator();
126                      fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
127                      fieldsValidator.addProblematicFieldName(e.getMessage());
128                      return fieldsValidator;
129               }
130
131               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
132                       response.getStatus());
133
134               return fieldsValidator;
135        }
136
137        @PostMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
138        @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
139        public FieldsValidator postOnboardingWidget(Principal principal, HttpServletResponse response,
140                @RequestBody OnboardingWidget onboardingWidget) {
141               FnUser user = fnUserService.loadUserByUsername(principal.getName());
142               FieldsValidator fieldsValidator;
143
144               onboardingWidget.setId(null);
145               onboardingWidget.normalize();
146
147               try {
148                      fieldsValidator = widgetService.setOnboardingWidget(user.getId(), onboardingWidget);
149               } catch (IllegalArgumentException e) {
150                      fieldsValidator = new FieldsValidator();
151                      fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
152                      fieldsValidator.addProblematicFieldName(e.getMessage());
153                      return fieldsValidator;
154               }
155               response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
156
157               EcompPortalUtils
158                       .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
159               return fieldsValidator;
160        }
161
162        @DeleteMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
163        @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
164        public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletResponse response,
165                @PathVariable("widgetId") Long widgetId) {
166               FnUser user = fnUserService.loadUserByUsername(principal.getName());
167               FieldsValidator fieldsValidator;
168
169               fieldsValidator = widgetService.deleteOnboardingWidget(user.getOrgUserId(), user.getId(), widgetId);
170               response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
171
172               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
173                       response.getStatus());
174               return fieldsValidator;
175        }
176
177        @PutMapping(value = {"portalApi/widgetCatalogSelection"}, produces = MediaType.APPLICATION_JSON_VALUE)
178        public FieldsValidator putWidgetCatalogSelection(Principal principal,
179                @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
180               FieldsValidator result = new FieldsValidator();
181               FnUser user = fnUserService.loadUserByUsername(principal.getName());
182
183               try {
184                      assert persRequest != null;
185                      persUserWidgetService
186                              .setPersUserAppValue(user, persRequest);
187               } catch (IllegalArgumentException iae) {
188                      logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", iae);
189                      response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, iae.getMessage());
190               } catch (Exception e) {
191                      logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
192                      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
193               }
194               result.setHttpStatusCode((long) HttpServletResponse.SC_OK);
195               return result;
196        }
197 }