1 package com.highstreet.technologies.apigateway;
4 import java.io.FileInputStream;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.HashMap;
9 import java.util.Properties;
11 import org.json.JSONArray;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 public class MyProperties {
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==\"]";
22 public String toString() {
23 return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
24 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
27 private static MyProperties mObj;
29 private String aaiBase;
30 private Map<String, String> aaiHeaders;
31 private String esBase;
33 private boolean trustInsecure;
35 private boolean corsEnabled;
37 public boolean isAAIOff() {
38 return this.aaiBase == null ? true : this.aaiBase.equals("off");
41 public boolean isEsOff() {
42 return this.esBase == null ? true : this.esBase.equals("off");
45 public String getAAIBaseUrl() {
49 public String getEsBaseUrl() {
53 public Map<String, String> getAAIHeaders() {
54 return this.aaiHeaders;
57 public boolean trustInsecure() {
58 return this.trustInsecure;
61 public boolean corsEnabled() {
62 return this.corsEnabled;
65 public static MyProperties Instantiate(String filename) throws IOException, NumberFormatException {
66 mObj = new MyProperties(filename);
68 LOG.debug("instantiated:" + mObj.toString());
72 private MyProperties(String filename) throws IOException, NumberFormatException {
74 this.trustInsecure = false;
75 File f = new File(filename);
77 this.writeDefaults(f);
79 Properties defaultProps = new Properties();
80 FileInputStream in = new FileInputStream(filename);
81 defaultProps.load(in);
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;
91 private static Map<String, String> _parseHeadersMap(String s) {
92 Map<String, String> r = new HashMap<String, String>();
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(":");
100 r.put(hlp[0], hlp[1]);
104 } catch (Exception e) {
105 LOG.warn("problem loading headers map:" + e.getMessage());
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);
122 public static MyProperties getInstance() {