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
48 @RequestMapping(value = "/portals", produces = { MediaType.APPLICATION_JSON_VALUE })
49 public class PortalController {
51 private final Logger log = LoggerFactory.getLogger(this.getClass());
54 private PortalRepository portalRepository;
57 private PortalService portalService;
61 @ApiOperation("update portal")
62 public PostReturnBody<PortalConfig> updatePortal(@RequestBody PortalConfig portalConfig, BindingResult result, HttpServletResponse response) throws IOException {
64 if (result.hasErrors()) {
65 sendError(response, 400, "Error binding PortalConfig: "+result.toString());
71 portal = portalRepository.findById(portalConfig.getName()).get();
72 log.info("Update portal "+portalConfig);
73 portalService.fillPortalConfiguration(portalConfig, portal);
74 portalRepository.save(portal);
75 return mkPostReturnBody(200, portal);
76 } catch (Exception e) {
77 log.debug("Update or delete portal failed, Portal: "+portalConfig, e.getMessage());
78 sendError(response, 400, "Error update or delete portal: "+portal);
86 @ApiOperation(value = "List all portals")
87 public List<PortalConfig> getPortals() {
89 List<Portal> portalList = null;
90 List<PortalConfig> portalConfigList = new ArrayList<>();
91 portalList = (List<Portal>)portalRepository.findAll();
92 if (portalList != null && portalList.size() > 0) {
93 log.info("PortalList is not null");
94 for(Portal portal : portalList) {
95 portalConfigList.add(portal.getPortalConfig());
98 return portalConfigList;
102 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
104 response.sendError(sc, msg);
108 private PostReturnBody<PortalConfig> mkPostReturnBody(int statusCode, Portal portal) {
109 PostReturnBody<PortalConfig> retBody = new PostReturnBody<>();
110 retBody.setStatusCode(statusCode);
111 retBody.setReturnBody(portal.getPortalConfig());