2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 China Mobile
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.datalake.feeder.controller;
23 import io.swagger.annotations.ApiOperation;
24 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
25 import org.onap.datalake.feeder.domain.Portal;
26 import org.onap.datalake.feeder.dto.PortalConfig;
27 import org.onap.datalake.feeder.repository.PortalRepository;
28 import org.onap.datalake.feeder.service.PortalService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.MediaType;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.web.bind.annotation.*;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
42 * This controller manages Portal settings
47 @CrossOrigin(origins = "*")
49 @RequestMapping(value = "/portals", produces = { MediaType.APPLICATION_JSON_VALUE })
50 public class PortalController {
52 private final Logger log = LoggerFactory.getLogger(this.getClass());
55 private PortalRepository portalRepository;
58 private PortalService portalService;
62 @ApiOperation("update portal")
63 public PostReturnBody<PortalConfig> updatePortal(@RequestBody PortalConfig portalConfig, BindingResult result, HttpServletResponse response) throws IOException {
65 if (result.hasErrors()) {
66 sendError(response, 400, "Error binding PortalConfig: "+result.toString());
72 portal = portalRepository.findById(portalConfig.getName()).get();
73 if (portalConfig.getEnabled() == false) {
74 log.info("Disable portal "+portalConfig.getName());
77 portal.setLogin(null);
79 portal.setEnabled(false);
81 log.info("Update portal "+portalConfig);
82 portalService.fillPortalConfiguration(portalConfig, portal);
84 portalRepository.save(portal);
85 return mkPostReturnBody(200, portal);
86 } catch (Exception e) {
87 log.debug("Update or delete portal failed, Portal: "+portalConfig, e.getMessage());
88 sendError(response, 400, "Error update or delete portal: "+portal);
96 @ApiOperation(value = "List all portals")
97 public List<PortalConfig> getPortals() {
99 List<Portal> portalList = null;
100 List<PortalConfig> portalConfigList = new ArrayList<>();
101 portalList = (List<Portal>)portalRepository.findAll();
102 if (portalList != null && portalList.size() > 0) {
103 log.info("PortalList is not null");
104 for(Portal portal : portalList) {
105 portalConfigList.add(portal.getPortalConfig());
108 return portalConfigList;
112 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
114 response.sendError(sc, msg);
118 private PostReturnBody<PortalConfig> mkPostReturnBody(int statusCode, Portal portal) {
119 PostReturnBody<PortalConfig> retBody = new PostReturnBody<>();
120 retBody.setStatusCode(statusCode);
121 retBody.setReturnBody(portal.getPortalConfig());