Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / PropertyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright 2019 Nokia
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.controller;
23
24 import static org.onap.vid.utils.Logging.getMethodName;
25 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
26 import static org.springframework.http.HttpStatus.OK;
27
28 import javax.servlet.http.HttpServletRequest;
29 import org.onap.portalsdk.core.controller.RestrictedBaseController;
30 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.onap.vid.category.CategoryParametersResponse;
32 import org.onap.vid.model.CategoryParameter.Family;
33 import org.onap.vid.services.CategoryParameterService;
34 import org.onap.vid.services.CategoryParameterServiceWithRoles;
35 import org.onap.vid.utils.SystemPropertiesWrapper;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.bind.annotation.RequestParam;
42 import org.springframework.web.bind.annotation.RestController;
43 import org.springframework.web.servlet.ModelAndView;
44
45 @RestController
46 public class PropertyController extends RestrictedBaseController {
47
48     private static final String ERROR_MESSAGE = "Internal error occurred: ";
49     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(PropertyController.class);
50     private final CategoryParameterService categoryParameterService;
51     private final SystemPropertiesWrapper systemPropertiesWrapper;
52
53     @Autowired
54     public PropertyController(CategoryParameterServiceWithRoles service, SystemPropertiesWrapper systemPropertiesWrapper) {
55         categoryParameterService = service;
56         this.systemPropertiesWrapper = systemPropertiesWrapper;
57     }
58
59     @RequestMapping(value = {"/propertyhome"}, method = RequestMethod.GET)
60     public ModelAndView welcome(HttpServletRequest request) {
61         LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== PropertyController welcome start");
62         return new ModelAndView(getViewName());
63     }
64
65     @RequestMapping(value = "/get_property/{name}/{defaultvalue}", method = RequestMethod.GET)
66     public ResponseEntity<String> getProperty(@PathVariable("name") String name,
67         @PathVariable("defaultvalue") String defaultvalue,
68         HttpServletRequest request) {
69
70         String methodName = "getProperty";
71         LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== {} {}", methodName, " start");
72         try {
73             String propertyName = name.replace('_', '.');
74             String pvalue = systemPropertiesWrapper.getOrDefault(propertyName, defaultvalue);
75             LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== {} {} {}", methodName, "returning", pvalue);
76             return ResponseEntity.status(OK).body(pvalue);
77         } catch (Exception e) {
78             LOGGER.info(EELFLoggerDelegate.errorLogger, "<== {} {}", methodName, e.toString());
79             LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== {} {}", methodName, e.toString());
80             return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(ERROR_MESSAGE + e.getMessage());
81         }
82     }
83
84     @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
85     public ResponseEntity getCategoryParameter(HttpServletRequest request,
86         @RequestParam(value = "familyName", required = true) Family familyName) {
87         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
88         try {
89             CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
90             LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response);
91             return ResponseEntity.status(OK).body(response);
92         } catch (Exception e) {
93             LOGGER.error("failed to retrieve category parameter list from DB.", e);
94             return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(ERROR_MESSAGE + e.getMessage());
95         }
96     }
97 }