diff options
Diffstat (limited to 'sdnr/wt/apigateway/provider/src')
9 files changed, 247 insertions, 162 deletions
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 index c0b6c99a2..aee7a3b09 100644 --- 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 @@ -38,14 +38,25 @@ public class MsServlet extends BaseServlet { private static Logger LOG = LoggerFactory.getLogger(MsServlet.class); private static final long serialVersionUID = -5361461082028405171L; private static final String OFFLINE_RESPONSE_MESSAGE = "MediatorServer interface is offline"; - private static final String DATABASE_REQUEST_URI_REGEX = "/mwtn/mediator-server"; + private static final String DATABASE_REQUEST_URI_REGEX = "/mediator-server/mediator-server"; private final DatabaseEntryProvider entryProvider; + private boolean isSecure; + public void setIsSecure(boolean secure) { + if(this.isSecure!=secure) { + this.isSecure=secure; + this.entryProvider.setBaseUrl(this.getDBBaseUrl()); + } + } public MsServlet() { super(); - this.entryProvider = new DatabaseEntryProvider("http://localhost:9200/",60); + this.entryProvider = new DatabaseEntryProvider(this.getDBBaseUrl(),60); EsServlet.registerRequestCallback(DATABASE_REQUEST_URI_REGEX, this.dbRequestCallback); } + private String getDBBaseUrl() { + return MyProperties.getInstance().getEsBaseUrl(); + } + private final IRequestCallback dbRequestCallback = new IRequestCallback() { @Override @@ -64,7 +75,11 @@ public class MsServlet extends BaseServlet { protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(200); } - + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + this.isSecure=req.getScheme().equals("https"); + } @Override protected String getOfflineResponse() { return OFFLINE_RESPONSE_MESSAGE; 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 index 3d66f526e..36fce2081 100644 --- 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 @@ -40,7 +40,7 @@ public class MyProperties { 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_ESDATABASE = "http://sdnrdb:9200"; private static final String DEFAULT_AAI = "off"; @Override diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseEntryProvider.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseEntryProvider.java index 086c3c977..b467e27a0 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseEntryProvider.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseEntryProvider.java @@ -102,5 +102,8 @@ public class DatabaseEntryProvider implements AutoCloseable { } return s; } + public void setBaseUrl(String baseUrl) { + this.httpClient.setBaseUrl(baseUrl); + } } diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseHttpClient.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseHttpClient.java index af46509e9..d0f575362 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseHttpClient.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/DatabaseHttpClient.java @@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory; public class DatabaseHttpClient extends BaseHTTPClient { private static Logger LOG = LoggerFactory.getLogger(DatabaseHttpClient.class); - private static final String URI = "/mwtn/mediator-server/_search"; + private static final String URI = "/mediator-server/mediator-server/_search"; private final Map<String, String> headers; public DatabaseHttpClient(String base, boolean trustAllCerts) { @@ -44,7 +44,7 @@ public class DatabaseHttpClient extends BaseHTTPClient { private Map<String, String> getHeaders() { Map<String, String> h = new HashMap<String, String>(); - + h.put("Content-Type", "application/json"); return h; } @@ -80,4 +80,6 @@ public class DatabaseHttpClient extends BaseHTTPClient { } return entries; } + + } diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/http/BaseHTTPClient.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/http/BaseHTTPClient.java index f8f95b386..9c89eeff3 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/http/BaseHTTPClient.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/database/http/BaseHTTPClient.java @@ -40,154 +40,161 @@ import org.slf4j.LoggerFactory; public class BaseHTTPClient { - private static Logger LOG = LoggerFactory.getLogger(BaseHTTPClient.class); - private static final int BUFSIZE = 1024; - private static final Charset CHARSET = StandardCharsets.UTF_8; - private static final String SSLCONTEXT = "TLSv1.2"; - private static final int DEFAULT_HTTP_TIMEOUT_MS = 30000; // in ms - - private final boolean trustAll; - private final String baseUrl; - - private int timeout = DEFAULT_HTTP_TIMEOUT_MS; - private SSLContext sc = null; - - public BaseHTTPClient(String base) { - this(base, false); - } - - - public BaseHTTPClient(String base, boolean trustAllCerts) { - this.baseUrl = base; - this.trustAll = trustAllCerts; - try { - sc = setupSsl(trustAll); - } catch (KeyManagementException | NoSuchAlgorithmException e) { - LOG.warn("problem ssl setup: " + e.getMessage()); - } - } - - protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, String body, Map<String, String> headers) - throws IOException { - return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers); - } - - protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, byte[] body, Map<String, String> headers) - throws IOException { - if (uri == null) { - uri = ""; - } - String surl = this.baseUrl; - if (!surl.endsWith("/") && uri.length() > 0) { - surl += "/"; - } - if (uri.startsWith("/")) { - uri = uri.substring(1); - } - surl += uri; - LOG.debug("try to send request with url=" + this.baseUrl + uri + " as method=" + method); - LOG.trace("body:" + (body == null ? "null" : new String(body, CHARSET))); - URL url = new URL(surl); - URLConnection http = url.openConnection(); - http.setConnectTimeout(this.timeout); - if (surl.toString().startsWith("https")) { - if (sc != null) { - ((HttpsURLConnection) http).setSSLSocketFactory(sc.getSocketFactory()); - if (trustAll) { - LOG.debug("trusting all certs"); - HostnameVerifier allHostsValid = (hostname, session) -> true; - ((HttpsURLConnection) http).setHostnameVerifier(allHostsValid); - } - } else // Should never happen - { - LOG.warn("No SSL context available"); - return new BaseHTTPResponse(-1, ""); - } - } - ((HttpURLConnection) http).setRequestMethod(method); - http.setDoOutput(true); - if (headers != null && headers.size() > 0) { - for (String key : headers.keySet()) { - http.setRequestProperty(key, headers.get(key)); - LOG.trace("set http header " + key + ": " + headers.get(key)); - } - } - byte[] buffer = new byte[BUFSIZE]; - int len = 0, lensum = 0; - // send request - // Send the message to destination - if (!method.equals("GET") && body != null && body.length > 0) { - try (OutputStream output = http.getOutputStream()) { - output.write(body); - } - } - // Receive answer - int responseCode = ((HttpURLConnection) http).getResponseCode(); - String sresponse = ""; - InputStream response = null; - try { - if (responseCode >= 200 && responseCode < 300) { - response = http.getInputStream(); - } else { - response = ((HttpURLConnection) http).getErrorStream(); - if (response == null) { - response = http.getInputStream(); - } - } - if (response != null) { - while (true) { - len = response.read(buffer, 0, BUFSIZE); - if (len <= 0) { - break; - } - lensum += len; - sresponse += new String(buffer, 0, len, CHARSET); - } - } else { - LOG.debug("response is null"); - } - } catch (Exception e) { - LOG.debug("No response. ", e); - } finally { - if (response != null) { - response.close(); - } - } - LOG.debug("ResponseCode: " + responseCode); - LOG.trace("Response (len:{}): {}", String.valueOf(lensum), sresponse); - return new BaseHTTPResponse(responseCode, sresponse); - } - - - public static SSLContext setupSsl(boolean trustall) throws KeyManagementException, NoSuchAlgorithmException{ - - SSLContext sc = SSLContext.getInstance(SSLCONTEXT); - TrustManager[] trustCerts = null; - if (trustall) { - trustCerts = new TrustManager[] {new javax.net.ssl.X509TrustManager() { - @Override - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return null; - } - - @Override - public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {} - - @Override - public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {} - }}; - - } - KeyManager[] kms = null; - // Init the SSLContext with a TrustManager[] and SecureRandom() - sc.init(kms, trustCerts, new java.security.SecureRandom()); - return sc; - } - - public static String getAuthorizationHeaderValue(String username, String password) { - return "Basic " + new String(Base64.getEncoder().encode((username + ":" + password).getBytes())); - } - - - + private static Logger LOG = LoggerFactory.getLogger(BaseHTTPClient.class); + private static final int BUFSIZE = 1024; + private static final Charset CHARSET = StandardCharsets.UTF_8; + private static final String SSLCONTEXT = "TLSv1.2"; + private static final int DEFAULT_HTTP_TIMEOUT_MS = 30000; // in ms + + private final boolean trustAll; + private String baseUrl; + + private int timeout = DEFAULT_HTTP_TIMEOUT_MS; + private SSLContext sc = null; + + public BaseHTTPClient(String base) { + this(base, false); + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + try { + sc = setupSsl(trustAll); + } catch (KeyManagementException | NoSuchAlgorithmException e) { + LOG.warn("problem ssl setup: " + e.getMessage()); + } + } + + public BaseHTTPClient(String base, boolean trustAllCerts) { + this.baseUrl = base; + this.trustAll = trustAllCerts; + try { + sc = setupSsl(trustAll); + } catch (KeyManagementException | NoSuchAlgorithmException e) { + LOG.warn("problem ssl setup: " + e.getMessage()); + } + } + + protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, String body, Map<String, String> headers) + throws IOException { + return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers); + } + + protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, byte[] body, Map<String, String> headers) + throws IOException { + if (uri == null) { + uri = ""; + } + String surl = this.baseUrl; + if (!surl.endsWith("/") && uri.length() > 0) { + surl += "/"; + } + if (uri.startsWith("/")) { + uri = uri.substring(1); + } + surl += uri; + LOG.debug("try to send request with url=" + this.baseUrl + uri + " as method=" + method); + LOG.trace("body:" + (body == null ? "null" : new String(body, CHARSET))); + URL url = new URL(surl); + URLConnection http = url.openConnection(); + http.setConnectTimeout(this.timeout); + if (surl.toString().startsWith("https")) { + if (sc != null) { + ((HttpsURLConnection) http).setSSLSocketFactory(sc.getSocketFactory()); + if (trustAll) { + LOG.debug("trusting all certs"); + HostnameVerifier allHostsValid = (hostname, session) -> true; + ((HttpsURLConnection) http).setHostnameVerifier(allHostsValid); + } + } else // Should never happen + { + LOG.warn("No SSL context available"); + return new BaseHTTPResponse(-1, ""); + } + } + ((HttpURLConnection) http).setRequestMethod(method); + http.setDoOutput(true); + if (headers != null && headers.size() > 0) { + for (String key : headers.keySet()) { + http.setRequestProperty(key, headers.get(key)); + LOG.trace("set http header " + key + ": " + headers.get(key)); + } + } + byte[] buffer = new byte[BUFSIZE]; + int len = 0, lensum = 0; + // send request + // Send the message to destination + if (!method.equals("GET") && body != null && body.length > 0) { + try (OutputStream output = http.getOutputStream()) { + output.write(body); + } + } + // Receive answer + int responseCode = ((HttpURLConnection) http).getResponseCode(); + String sresponse = ""; + InputStream response = null; + try { + if (responseCode >= 200 && responseCode < 300) { + response = http.getInputStream(); + } else { + response = ((HttpURLConnection) http).getErrorStream(); + if (response == null) { + response = http.getInputStream(); + } + } + if (response != null) { + while (true) { + len = response.read(buffer, 0, BUFSIZE); + if (len <= 0) { + break; + } + lensum += len; + sresponse += new String(buffer, 0, len, CHARSET); + } + } else { + LOG.debug("response is null"); + } + } catch (Exception e) { + LOG.debug("No response. ", e); + } finally { + if (response != null) { + response.close(); + } + } + LOG.debug("ResponseCode: " + responseCode); + LOG.trace("Response (len:{}): {}", String.valueOf(lensum), sresponse); + return new BaseHTTPResponse(responseCode, sresponse); + } + + public static SSLContext setupSsl(boolean trustall) throws KeyManagementException, NoSuchAlgorithmException { + + SSLContext sc = SSLContext.getInstance(SSLCONTEXT); + TrustManager[] trustCerts = null; + if (trustall) { + trustCerts = new TrustManager[] { new javax.net.ssl.X509TrustManager() { + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { + } + + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { + } + } }; + + } + KeyManager[] kms = null; + // Init the SSLContext with a TrustManager[] and SecureRandom() + sc.init(kms, trustCerts, new java.security.SecureRandom()); + return sc; + } + + public static String getAuthorizationHeaderValue(String username, String password) { + return "Basic " + new String(Base64.getEncoder().encode((username + ":" + password).getBytes())); + } + } diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java index 795228834..791e9bad0 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java @@ -1,3 +1,20 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * 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.test; import static org.junit.Assert.*; diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java index 6e21bc133..18e84853a 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java @@ -1,3 +1,20 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * 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.test; import static org.junit.Assert.fail; diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java index 1a63240d8..6efd06feb 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java @@ -27,6 +27,8 @@ import java.io.File; import java.util.HashMap; import java.util.Map; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; @@ -38,7 +40,7 @@ public class TestProperties { private static final boolean DEFAULT_TRUSTINSECURE = false; private static final String DEFAULT_AAIBASEURL = "off"; private static Map<String, String> DEFAULT_AAIHEADERS = new HashMap<String, String>(); - private static final String DEFAULT_ESBASEURL = "http://localhost:9200"; + private static final String DEFAULT_ESBASEURL = "http://sdnrdb:9200"; private static final boolean CUSTOM_CORSENABLED = true; private static final boolean CUSTOM_AAIOFF = false; @@ -49,10 +51,18 @@ public class TestProperties { private static final String CUSTOM_ESBASEURL = "http://localhost:9200"; private static final String LR = "\n"; - + final String tmpFilename = "tmp2.cfg"; + + @Before + @After + public void init() { + File f = new File(tmpFilename); + if(f.exists()) { + f.delete(); + } + } @Test public void test() { - final String tmpFilename = "tmp2.cfg"; DEFAULT_AAIHEADERS.put("X-FromAppId", "SDNR"); DEFAULT_AAIHEADERS.put("Authorization", "Basic QUFJOkFBSQ=="); CUSTOM_AAIHEADERS.put("X-FromAppId", "SDNC"); @@ -62,9 +72,6 @@ public class TestProperties { + "\"]" + LR + "database=" + CUSTOM_ESBASEURL + LR + "insecure=" + (CUSTOM_TRUSTINSECURE ? "1" : "0") + LR + "cors=" + (CUSTOM_CORSENABLED ? "1" : "0"); File ftest = new File(tmpFilename); - // delete autogenerated testfile - if (ftest.exists()) - ftest.delete(); MyProperties prop = null; ftest = new File(tmpFilename); try { diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java index 78341dfad..3340ec66c 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java @@ -1,3 +1,20 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * 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.test; import static org.junit.Assert.*; |