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