Merge "Reorder modifiers"
[so.git] / common / src / main / java / org / openecomp / mso / properties / MsoJavaProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.mso.properties;
22
23
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import org.apache.commons.codec.binary.Base64;
32 import org.openecomp.mso.utils.CryptoUtils;
33
34 public class MsoJavaProperties extends AbstractMsoProperties {
35
36         
37         private Properties msoProperties = new Properties();
38
39
40         public MsoJavaProperties() {
41                 
42         }
43
44         public synchronized void setProperty(String key,String value) {
45                 msoProperties.setProperty(key, value);
46         }
47         
48         public synchronized String getProperty(String key, String defaultValue) {
49                 if (msoProperties.containsKey(key)) {
50                         return msoProperties.getProperty(key);
51                 } else {
52                         return defaultValue;
53                 }
54         }
55
56         public synchronized int getIntProperty(String key, int defaultValue) {
57
58                 int value = defaultValue;
59                 if (msoProperties.containsKey(key)) {
60                         try {
61                                 value = Integer.parseInt(msoProperties.getProperty(key));
62                         } catch (NumberFormatException e) {
63                                 LOGGER.debug("Exception while parsing integer: " + msoProperties.getProperty(key), e);
64                         }
65                 }
66                 return value;
67
68         }
69
70         public synchronized boolean getBooleanProperty(String key, boolean defaultValue) {
71
72                 if (msoProperties.containsKey(key)) {
73                         return Boolean.parseBoolean(msoProperties.getProperty(key));
74                 } else {
75                         return defaultValue;
76                 }
77
78         }
79
80         public synchronized String getEncryptedProperty(String key, String defaultValue, String encryptionKey) {
81
82                 if (msoProperties.containsKey(key)) {
83                         try {
84                                 return CryptoUtils.decrypt(msoProperties.getProperty(key), encryptionKey);
85                         } catch (GeneralSecurityException e) {
86                                 LOGGER.debug("Exception while decrypting property: " + msoProperties.getProperty(key), e);
87                         }
88                 }
89                 return defaultValue;
90
91         }
92
93         /**
94          * @param encryptedAuth: encrypted credentials from properties
95          * @param msoKey: key to use to decrypt from properties
96          * @return base 64 encoded basic auth credentials
97          */
98         public synchronized String getBasicAuth(String encryptedAuth, String msoKey){
99                 String encodedString = null;
100                 if ((encryptedAuth == null || encryptedAuth.isEmpty()) || (msoKey == null || msoKey.isEmpty()))
101                         return null;
102                 try {
103                         String auth = decrypt(encryptedAuth, msoKey);
104                         byte[] encoded = Base64.encodeBase64(auth.getBytes());
105                         encodedString = new String(encoded);
106                         encodedString = "Basic " + encodedString;
107                         
108                 } catch (Exception ex) {
109                         LOGGER.debug("Exception while getBasicAuth " + encryptedAuth, ex);
110                 }
111                 return encodedString;
112         }               
113         
114         public synchronized int size() {
115                 return this.msoProperties.size();
116         }
117         
118         public synchronized String decrypt(String toDecrypt, String msokey){
119                 String result = null;
120                 try {
121                         result = CryptoUtils.decrypt(toDecrypt, msokey);
122                         
123                 }
124                 catch (Exception e) {
125                         LOGGER.debug("Failed to decrypt credentials: " + toDecrypt, e);
126                 }
127                 return result;
128         }       
129         
130         @Override
131         protected synchronized void reloadPropertiesFile() throws IOException {
132                 this.loadPropertiesFile(this.propertiesFileName);
133         }
134
135         /**
136          * This method load a properties file from a source path.
137          *
138          * @param propertiesPath The path to the file
139          * @throws IOException In case of issues during the opening
140          */
141         @Override
142         protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
143
144                 FileReader reader = null;
145         
146                 propertiesFileName = propertiesPath;
147                 try {
148                         msoProperties.clear();
149                         reader = new FileReader(propertiesPath);
150                         msoProperties.load(reader);
151
152                 } finally {
153                         this.automaticRefreshInMinutes = this.getIntProperty(RELOAD_TIME_PROPERTY, DEFAULT_RELOAD_TIME_MIN);
154                         try {
155                                 if (reader != null) {
156                                         reader.close();
157                                 }
158                         } catch (IOException e) {
159                                 LOGGER.debug("Exception while closing reader for file:" + propertiesPath, e);
160                         }
161                 }
162         }
163
164         @Override
165         public synchronized MsoJavaProperties clone() {
166                 MsoJavaProperties msoCopy = new MsoJavaProperties();
167                 msoCopy.msoProperties.putAll(msoProperties);
168                 msoCopy.propertiesFileName = this.propertiesFileName;
169                 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
170                 return msoCopy;
171         }
172
173         @Override
174         public int hashCode() {
175                 final int prime = 31;
176                 int result = 1;
177                 result = prime * result + ((msoProperties == null) ? 0 : msoProperties.hashCode());
178                 return result;
179         }
180
181         @Override
182         public boolean equals(Object obj) {
183                 if (this == obj) {
184                         return true;
185                 }
186                 if (obj == null) {
187                         return false;
188                 }
189                 if (getClass() != obj.getClass()) {
190                         return false;
191                 }
192                 MsoJavaProperties other = (MsoJavaProperties) obj;
193
194                 return msoProperties.equals(other.msoProperties);
195         }
196
197         @Override
198         public String toString() {
199
200                 StringBuilder response = new StringBuilder();
201                 response.append("Config file ")
202                                 .append(propertiesFileName)
203                                 .append("(Timer:")
204                                 .append(automaticRefreshInMinutes)
205                                 .append("mins):")
206                                 .append(System.lineSeparator());
207
208                 for (Object key : this.msoProperties.keySet()) {
209                         String propertyName = (String) key;
210                         response.append(propertyName);
211                         response.append("=");
212                         response.append(this.msoProperties.getProperty(propertyName));
213                         response.append(System.lineSeparator());
214                 }
215                 response.append(System.lineSeparator());
216                 response.append(System.lineSeparator());
217
218                 return response.toString();
219         }
220          
221     public Map<String, String> asMap() {
222         final Map<String, String> result = new HashMap<>();
223         msoProperties.forEach((key, value) -> result.put(key.toString(), value.toString()));
224         
225         return result;
226     }
227
228 }