summaryrefslogtreecommitdiffstats
path: root/sdnr/wt/apigateway/provider/src/main/java
diff options
context:
space:
mode:
authorHerbert Eiselt <herbert.eiselt@highstreet-technologies.com>2019-01-25 14:50:52 +0100
committerHerbert Eiselt <herbert.eiselt@highstreet-technologies.com>2019-01-25 14:55:02 +0100
commitac33bcd5a389473b98f856825009c17f327c53fd (patch)
treed22067df230c12669ec2f1b2c9f74c9b0dda0b17 /sdnr/wt/apigateway/provider/src/main/java
parenta2f171d1fa87c9a54356f4992c1a23611caa8ae8 (diff)
Add sdnr wt apigateway
Add complete sdnr wireless transport app apigateway Change-Id: I133ece9cd78c8e5d1a4d74cca1b45feaaf4da9a5 Issue-ID: SDNC-571 Signed-off-by: Herbert Eiselt <herbert.eiselt@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/apigateway/provider/src/main/java')
-rw-r--r--sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java74
-rw-r--r--sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java350
-rw-r--r--sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java67
-rw-r--r--sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MsServlet.java74
-rw-r--r--sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java171
5 files changed, 736 insertions, 0 deletions
diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java
new file mode 100644
index 000000000..e73d5c668
--- /dev/null
+++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java
@@ -0,0 +1,74 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : CCSDK.apps.sdnr.wt.apigateway
+ * ================================================================================
+ * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.ccsdk.features.sdnr.wt.apigateway;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class AaiServlet extends BaseServlet {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 5946205120796162644L;
+ private static final String OFFLINE_RESPONSE_MESSAGE = "AAI interface is offline";
+
+ public AaiServlet() {
+ super();
+ }
+
+ @Override
+ protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ resp.setStatus(200);
+ }
+
+ @Override
+ protected String getOfflineResponse() {
+ return OFFLINE_RESPONSE_MESSAGE;
+ }
+
+ @Override
+ protected boolean isOff() {
+ return MyProperties.getInstance().isAAIOff();
+ }
+
+ @Override
+ protected String getRemoteUrl(String uri) {
+
+ if (uri.startsWith("/")) {
+ uri = uri.substring(1);
+ }
+ if (uri.startsWith("aai")) {
+ uri = uri.substring("aai".length());
+ }
+ if (uri.startsWith("/")) {
+ uri = uri.substring(1);
+ }
+ String base = MyProperties.getInstance().getAAIBaseUrl();
+ if (!base.endsWith("/")) {
+ base += "/";
+ }
+
+ return base + uri;
+ }
+}
diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java
new file mode 100644
index 000000000..b2bc30c0e
--- /dev/null
+++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java
@@ -0,0 +1,350 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : CCSDK.apps.sdnr.wt.apigateway
+ * ================================================================================
+ * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.ccsdk.features.sdnr.wt.apigateway;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class BaseServlet extends HttpServlet {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 7403047480257892794L;
+ private static Logger LOG = LoggerFactory.getLogger(BaseServlet.class);
+ private static SSLContext sc;
+ private boolean trustAll = false;
+ private static TrustManager[] trustCerts = null;
+ private static final int BUFSIZE = 2048;
+
+ protected abstract String getOfflineResponse();
+
+ protected abstract boolean isOff();
+
+ protected abstract String getRemoteUrl(String uri);
+
+ /**
+ *
+ * @throws NoSuchAlgorithmException
+ * @throws KeyManagementException
+ */
+ private static void setupSslTrustAll(boolean trustall) throws NoSuchAlgorithmException, KeyManagementException {
+
+ sc = SSLContext.getInstance("TLSv1.2");
+ if (trustall) {
+ if (trustCerts == null) {
+ trustCerts = new TrustManager[] { new javax.net.ssl.X509TrustManager() {
+ @Override
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+ return new java.security.cert.X509Certificate[] {};
+ }
+
+ @Override
+ public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
+ // do not check anything when trust all
+ }
+
+ @Override
+ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
+ // do not check anything when trust all
+ }
+ } };
+ }
+ } else {
+ if (trustCerts != null)
+ trustCerts = null;
+ }
+ // Init the SSLContext with a TrustManager[] and SecureRandom()
+ sc.init(null, trustCerts, new java.security.SecureRandom());
+ }
+
+ public BaseServlet() {
+ try {
+ MyProperties.Instantiate();
+ } catch (Exception e) {
+ LOG.error(e.getMessage());
+ }
+ this.trysslSetup(true);
+ }
+
+ private void trysslSetup() {
+ this.trysslSetup(false);
+ }
+
+ /**
+ * init or deinit ssl insecure mode regarding to property
+ *
+ * @param force init independent from property
+ */
+ private void trysslSetup(boolean force) {
+ // if trustall config has changed
+ if (force || trustAll != MyProperties.getInstance().trustInsecure()) {
+ // resetup ssl config
+ trustAll = MyProperties.getInstance().trustInsecure();
+ try {
+ setupSslTrustAll(trustAll);
+ } catch (Exception e) {
+ LOG.error("problem setting up SSL: {}", e.getMessage());
+ }
+ }
+ }
+
+ protected void sendOffResponse(HttpServletResponse response) {
+ response.setStatus(200);// HTML/OK
+ response.setHeader("Content-Type", "text/html; charset=utf-8");
+ try {
+ response.getOutputStream().write(this.getOfflineResponse().getBytes(StandardCharsets.UTF_8));
+ } catch (IOException e) {
+ LOG.debug("problem writing offline response");
+ }
+
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "GET");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "GET");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ }
+ }
+
+ @Override
+ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "PUT");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "PUT");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "POST");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "POST");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ }
+ }
+
+ @Override
+ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "DELETE");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "DELETE");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ }
+ }
+
+ private URLConnection getConnection(HttpServletRequest req, final String method) throws IOException {
+
+ LOG.debug("{} Request", method);
+ String surl = this.getRemoteUrl(req.getRequestURI());
+ LOG.debug("RemoteURL: {}", surl);
+ URL url = new URL(surl);
+ URLConnection http = url.openConnection();
+ ((HttpURLConnection) http).setRequestMethod(method);
+ if (url.toString().startsWith("https")) {
+ ((HttpsURLConnection) http).setSSLSocketFactory(sc.getSocketFactory());
+ if (trustAll) {
+ HostnameVerifier allHostsValid = new HostnameVerifier() {
+
+ @Override
+ public boolean verify(String hostname, SSLSession session) {
+ // do not verify host if trust all
+ return true;
+ }
+ };
+ ((HttpsURLConnection) http).setHostnameVerifier(allHostsValid);
+ }
+ }
+ http.setDoOutput(true);
+ // copy request headers
+ String s = "";
+ Enumeration<String> headers = req.getHeaderNames();
+ while (headers.hasMoreElements()) {
+ String h = headers.nextElement();
+ String v = req.getHeader(h);
+ if (h != null && h.equals("Host")) {
+ v = url.getAuthority();
+ }
+ s += String.format("%s:%s;", h, v);
+ http.setRequestProperty(h, v);
+ }
+ LOG.debug("Request Headers: {}", s);
+ return http;
+ }
+
+ private void handleRequest(HttpURLConnection http, HttpServletRequest req, HttpServletResponse resp, String method)
+ throws IOException {
+ byte[] buffer = new byte[BUFSIZE];
+ int len = 0, lensum = 0;
+ // send request
+ // Send the message to destination
+ OutputStream output = null;
+ if (!method.equals("GET")) {
+ try {
+ output = http.getOutputStream();
+ } catch (Exception e) {
+ LOG.debug("problem reading output stream: {}", e.getMessage());
+ }
+ }
+ if (output != null) {
+ while (true) {
+ len = req.getInputStream().read(buffer, 0, BUFSIZE);
+ if (len <= 0) {
+ break;
+ }
+ lensum += len;
+ output.write(buffer, 0, len);
+ }
+ }
+ LOG.debug("written {} data out", lensum);
+ int responseCode = http.getResponseCode();
+ // Receive answer
+ InputStream response;
+ if (responseCode >= 200 && responseCode < 300) {
+ response = http.getInputStream();
+ } else {
+ response = http.getErrorStream();
+ if (response == null) {
+ http.getInputStream();
+ }
+ }
+
+ LOG.debug("ResponseCode: {}", responseCode);
+ resp.setStatus(responseCode);
+ Map<String, List<String>> set = http.getHeaderFields();
+ String s = "";
+ if (set != null) {
+ for (Map.Entry<String, List<String>> entry : set.entrySet()) {
+ if (entry.getKey() == null) {
+ continue;
+ }
+ for (String v : entry.getValue()) {
+ resp.setHeader(entry.getKey(), v);
+ s += String.format("%s:%s;", entry.getKey(), v);
+ }
+ if (MyProperties.getInstance().corsEnabled()) {
+ resp.setHeader("Access-Control-Allow-Origin", "*");
+ // resp.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
+ resp.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
+ }
+
+ }
+ }
+ LOG.debug("Received Headers: {}", s);
+ lensum = 0;
+ if (response != null) {
+ while (true) {
+ len = response.read(buffer, 0, BUFSIZE);
+ if (len <= 0) {
+ break;
+ }
+ lensum += len;
+ resp.getOutputStream().write(buffer, 0, len);
+ }
+ } else {
+ LOG.debug("response is null");
+ }
+ LOG.debug("Received {} bytes", lensum);
+ }
+
+}
diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java
new file mode 100644
index 000000000..9d276ffc8
--- /dev/null
+++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java
@@ -0,0 +1,67 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : CCSDK.apps.sdnr.wt.apigateway
+ * ================================================================================
+ * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.ccsdk.features.sdnr.wt.apigateway;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class EsServlet extends BaseServlet {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -3996363343749995011L;
+ private static final String OFFLINE_RESPONSE_MESSAGE = "Database interface is offline";
+
+ public EsServlet() {
+ super();
+ }
+
+ @Override
+ protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (MyProperties.getInstance().corsEnabled()) {
+ resp.addHeader("Access-Control-Allow-Origin", "*");
+ resp.addHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
+ resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
+ }
+ resp.setStatus(200);
+ }
+
+ @Override
+ protected String getOfflineResponse() {
+ return OFFLINE_RESPONSE_MESSAGE;
+ }
+
+ @Override
+ protected boolean isOff() {
+ return MyProperties.getInstance().isEsOff();
+ }
+
+ @Override
+ protected String getRemoteUrl(String uri) {
+ if (uri != null && uri.length() > 0) {
+ uri = uri.substring("/database".length());
+ }
+ return MyProperties.getInstance().getEsBaseUrl() + uri;
+ }
+}
diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MsServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MsServlet.java
new file mode 100644
index 000000000..27697930f
--- /dev/null
+++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MsServlet.java
@@ -0,0 +1,74 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : CCSDK.apps.sdnr.wt.apigateway
+ * ================================================================================
+ * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.ccsdk.features.sdnr.wt.apigateway;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MsServlet extends BaseServlet {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -5361461082028405171L;
+ private static final String OFFLINE_RESPONSE_MESSAGE = "MediatorServer interface is offline";
+
+ public MsServlet() {
+
+ super();
+ }
+
+ @Override
+ protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ resp.setStatus(200);
+ }
+
+ @Override
+ protected String getOfflineResponse() {
+ return OFFLINE_RESPONSE_MESSAGE;
+ }
+
+ @Override
+ protected boolean isOff() {
+ return true;
+ }
+
+ @Override
+ protected String getRemoteUrl(String uri) {
+ String dbServerId = "0";
+ if (uri == null)
+ uri = "";
+ if (uri.length() > 0) {
+ uri = uri.substring("/ms".length());
+ dbServerId = uri.substring(0, uri.indexOf("/"));
+ }
+ return this.getBaseUrl(dbServerId) + uri;
+ }
+
+ protected String getBaseUrl(String dbServerId) {
+ return "";
+ }
+}
diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java
new file mode 100644
index 000000000..3d66f526e
--- /dev/null
+++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java
@@ -0,0 +1,171 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : CCSDK.apps.sdnr.wt.apigateway
+ * ================================================================================
+ * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.ccsdk.features.sdnr.wt.apigateway;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.json.JSONArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MyProperties {
+
+ private static Logger LOG = LoggerFactory.getLogger(MyProperties.class);
+ public static final String PROPFILE = "etc/apigateway.properties";
+ private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization:Basic QUFJOkFBSQ==\"]";
+ private static final String DEFAULT_CORSENABLED = "0";
+ private static final String DEFAULT_TRUSTINSECURE = "0";
+ private static final String DEFAULT_ESDATABASE = "http://localhost:9200";
+ private static final String DEFAULT_AAI = "off";
+
+ @Override
+ public String toString() {
+ return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
+ + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
+ }
+
+ private static MyProperties mObj;
+
+ private String aaiBase;
+ private Map<String, String> aaiHeaders;
+ private String esBase;
+
+ private boolean trustInsecure;
+
+ private boolean corsEnabled;
+
+ public boolean isAAIOff() {
+ return this.aaiBase == null ? true : this.aaiBase.equals("off");
+ }
+
+ public boolean isEsOff() {
+ return this.esBase == null ? true : this.esBase.equals("off");
+ }
+
+ public String getAAIBaseUrl() {
+ return this.aaiBase;
+ }
+
+ public String getEsBaseUrl() {
+ return this.esBase;
+ }
+
+ public Map<String, String> getAAIHeaders() {
+ return this.aaiHeaders;
+ }
+
+ public boolean trustInsecure() {
+ return this.trustInsecure;
+ }
+
+ public boolean corsEnabled() {
+ return this.corsEnabled;
+ }
+
+ public static MyProperties Instantiate() throws IOException, NumberFormatException {
+ return Instantiate(new File(PROPFILE));
+ }
+
+ public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
+
+ return Instantiate(file, false);
+ }
+
+ public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException {
+ if (mObj == null || force) {
+ mObj = new MyProperties(file);
+ LOG.debug("instantiated: {}", mObj.toString());
+ }
+ return mObj;
+ }
+
+ private MyProperties(File file) throws IOException, NumberFormatException {
+ this.aaiBase = "off";
+ this.trustInsecure = false;
+ if (!file.exists()) {
+ this.writeDefaults(file);
+ }
+ this.load(new FileInputStream(file));
+ }
+
+ public void load(InputStream in) throws IOException, NumberFormatException {
+
+ Properties defaultProps = new Properties();
+ defaultProps.load(in);
+ in.close();
+
+ this.aaiBase = defaultProps.getProperty("aai", DEFAULT_AAI);
+ this.aaiHeaders = _parseHeadersMap(defaultProps.getProperty("aaiHeaders", DEFAULT_AAI_HEADERS));
+ this.esBase = defaultProps.getProperty("database", DEFAULT_ESDATABASE);
+ this.trustInsecure = Integer.parseInt(defaultProps.getProperty("insecure", DEFAULT_TRUSTINSECURE)) == 1;
+ this.corsEnabled = Integer.parseInt(defaultProps.getProperty("cors", DEFAULT_CORSENABLED)) == 1;
+ }
+
+ private static Map<String, String> _parseHeadersMap(String s) {
+ Map<String, String> r = new HashMap<>();
+ try {
+ JSONArray a = new JSONArray(s);
+ if ( a.length() > 0) {
+ for (int i = 0; i < a.length(); i++) {
+ String item = a.getString(i);
+ String[] hlp = item.split(":");
+ if (hlp.length > 1) {
+ r.put(hlp[0], hlp[1]);
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.warn("problem loading headers map: {}",e.getMessage());
+ }
+ return r;
+ }
+
+ private String writeDefaults(File f) throws IOException {
+ StringBuilder sb = new StringBuilder();
+ final String LR = "\n";
+ FileWriter fw = new FileWriter(f);
+ sb.append("aai=" + DEFAULT_AAI + LR);
+ sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR);
+ sb.append("database=" + DEFAULT_ESDATABASE + LR);
+ sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR);
+ sb.append("cors=" + DEFAULT_CORSENABLED);
+ try {
+ fw.write(sb.toString());
+ } catch (Exception e) {
+ LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath() , e.getMessage());
+ } finally {
+ fw.close();
+ }
+ return sb.toString();
+ }
+
+ public static MyProperties getInstance() {
+ return mObj;
+ }
+
+}