actually adding the files to the initial commit
[vid.git] / vid / src / main / java / org / openecomp / vid / controller / PropertyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.vid.controller;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.PathVariable;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.RequestMethod;
34 import org.springframework.web.bind.annotation.RestController;
35 import org.springframework.web.servlet.ModelAndView;
36
37 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40
41 /**
42  * The Class PropertyController.
43  */
44 @RestController
45 public class PropertyController extends RestrictedBaseController{
46         
47         /** The view name. */
48         String viewName;
49         
50         /** The logger. */
51         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertyController.class);
52         
53         /** The Constant dateFormat. */
54         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
55         
56         /**
57          * Welcome.
58          *
59          * @param request the request
60          * @return the model and view
61          */
62         public ModelAndView welcome(HttpServletRequest request) {
63                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== PropertyController welcome start");
64                 return new ModelAndView(getViewName());         
65         }
66         
67         /* (non-Javadoc)
68          * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#getViewName()
69          */
70         public String getViewName() {
71                 return viewName;
72         }
73         
74         /* (non-Javadoc)
75          * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#setViewName(java.lang.String)
76          */
77         public void setViewName(String viewName) {
78                 this.viewName = viewName;
79         }
80         
81         /**
82          * Gets the property.
83          *
84          * @param name the name
85          * @param defaultvalue the defaultvalue
86          * @param request the request
87          * @return the property
88          * @throws Exception the exception
89          */
90         @RequestMapping(value = "/get_property/{name}/{defaultvalue}", method = RequestMethod.GET)
91         public ResponseEntity<String> getProperty (@PathVariable("name") String name, @PathVariable("defaultvalue") String defaultvalue,
92                         HttpServletRequest request) throws Exception {
93                 
94                 String methodName = "getProperty";      
95                 ResponseEntity<String> resp = null;
96                 String pvalue = null;
97                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
98                 
99                 try {
100                         // convert "_" to "." in the property name
101                         if (name == null || name.length() == 0 ) {
102                                 return ( new ResponseEntity<String> (defaultvalue, HttpStatus.OK));
103                         }
104                         // convert "_" to "." in the property name
105                         String propertyName = name.replace('_', '.');
106                         pvalue = SystemProperties.getProperty(propertyName);
107                         if ( ( pvalue == null ) || ( pvalue.length() == 0 ) ) {
108                                 pvalue = defaultvalue;
109                         }
110                         resp = new ResponseEntity<String>(pvalue, HttpStatus.OK);
111                 }
112                 catch (Exception e) {
113                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
114                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
115                         throw e;
116                 }
117                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " returning " + pvalue);
118                 return ( resp );
119         }
120
121 }