81ada7ce52a4f174d038208ff0316e0c4009e8a7
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / apigateway / impl / src / main / java / com / highstreet / technologies / apigateway / MyProperties.java
1 package com.highstreet.technologies.apigateway;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.Properties;
10
11 import org.json.JSONArray;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class MyProperties {
16
17         private static Logger LOG = LoggerFactory.getLogger(MyProperties.class);
18         public static final String PROPFILE = "etc/apigateway.properties";
19         private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization: Basic QUFJOkFBSQ==\"]";
20
21         @Override
22         public String toString() {
23                 return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
24                                 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
25         }
26
27         private static MyProperties mObj;
28
29         private String aaiBase;
30         private Map<String, String> aaiHeaders;
31         private String esBase;
32
33         private boolean trustInsecure;
34
35         private boolean corsEnabled;
36
37         public boolean isAAIOff() {
38                 return this.aaiBase == null ? true : this.aaiBase.equals("off");
39         }
40
41         public boolean isEsOff() {
42                 return this.esBase == null ? true : this.esBase.equals("off");
43         }
44
45         public String getAAIBaseUrl() {
46                 return this.aaiBase;
47         }
48
49         public String getEsBaseUrl() {
50                 return this.esBase;
51         }
52
53         public Map<String, String> getAAIHeaders() {
54                 return this.aaiHeaders;
55         }
56
57         public boolean trustInsecure() {
58                 return this.trustInsecure;
59         }
60
61         public boolean corsEnabled() {
62                 return this.corsEnabled;
63         }
64
65         public static MyProperties Instantiate(String filename) throws IOException, NumberFormatException {
66                 mObj = new MyProperties(filename);
67                 if (mObj != null)
68                         LOG.debug("instantiated:" + mObj.toString());
69                 return mObj;
70         }
71
72         private MyProperties(String filename) throws IOException, NumberFormatException {
73                 this.aaiBase = "off";
74                 this.trustInsecure = false;
75                 File f = new File(filename);
76                 if (!f.exists()) {
77                         this.writeDefaults(f);
78                 }
79                 Properties defaultProps = new Properties();
80                 FileInputStream in = new FileInputStream(filename);
81                 defaultProps.load(in);
82                 in.close();
83
84                 this.aaiBase = defaultProps.getProperty("aai", "off");
85                 this.aaiHeaders = _parseHeadersMap(defaultProps.getProperty("aaiHeaders", DEFAULT_AAI_HEADERS));
86                 this.esBase = defaultProps.getProperty("database", "off");
87                 this.trustInsecure = Integer.parseInt(defaultProps.getProperty("insecure", "0")) == 1;
88                 this.corsEnabled = Integer.parseInt(defaultProps.getProperty("cors", "0")) == 1;
89         }
90
91         private static Map<String, String> _parseHeadersMap(String s) {
92                 Map<String, String> r = new HashMap<String, String>();
93                 try {
94                         JSONArray a = new JSONArray(s);
95                         if (a != null && a.length() > 0) {
96                                 for (int i = 0; i < a.length(); i++) {
97                                         String item = a.getString(i);
98                                         String[] hlp = item.split(":");
99                                         if (hlp.length > 1) {
100                                                 r.put(hlp[0], hlp[1]);
101                                         }
102                                 }
103                         }
104                 } catch (Exception e) {
105                         LOG.warn("problem loading headers map:" + e.getMessage());
106                 }
107                 return r;
108         }
109
110         private void writeDefaults(File f) throws IOException {
111                 FileWriter fw = new FileWriter(f);
112                 final String LR = "\n";
113                 fw.write("aai=off" + LR);
114                 fw.write("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR);
115                 fw.write("database=http://localhost:9200" + LR);
116                 fw.write("insecure=1" + LR);
117                 fw.write("cors=1");
118                 fw.close();
119
120         }
121
122         public static MyProperties getInstance() {
123                 return mObj;
124         }
125
126 }