db7039feb7da21caa04501337a11520ad267bb73
[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 @EnableAspectJAutoProxy
73 public class WidgetsController {
74
75        private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
76
77        private final FnUserService fnUserService;
78        private final WidgetService widgetService;
79        private final PersUserWidgetService persUserWidgetService;
80
81        @Autowired
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;
87        }
88
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");
96               } else {
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));
100                      } else {
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);
104                      }
105               }
106
107               EcompPortalUtils
108                       .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
109               return onboardingWidgets;
110        }
111
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;
118
119               assert onboardingWidget != null;
120               onboardingWidget.setId(widgetId);
121               onboardingWidget.normalize();
122               try {
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;
130               }
131
132               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
133                       response.getStatus());
134
135               return fieldsValidator;
136        }
137
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;
144
145               onboardingWidget.setId(null);
146               onboardingWidget.normalize();
147
148               try {
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;
155               }
156               response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
157
158               EcompPortalUtils
159                       .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
160               return fieldsValidator;
161        }
162
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;
169
170               fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId);
171               response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
172
173               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
174                       response.getStatus());
175               return fieldsValidator;
176        }
177
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());
183
184               try {
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());
194               }
195               result.setHttpStatusCode((long) HttpServletResponse.SC_OK);
196               return result;
197        }
198 }