diff options
author | herbert <herbert.eiselt@highstreet-technologies.com> | 2019-12-14 00:22:47 +0100 |
---|---|---|
committer | herbert <herbert.eiselt@highstreet-technologies.com> | 2019-12-16 10:35:58 +0100 |
commit | 7ce54d65de834d2901285e152c42044e8136c473 (patch) | |
tree | 652e29e86489a9a5390db06a34c79833e0fda9b3 /sdnr/wt/apigateway/provider/src/main/java | |
parent | da4fd6169717cfa04d644d0af0d23dd089a6e373 (diff) |
update old version
of apigateway common data-provider helpserver websocketmanager2
Issue-ID: SDNC-1005
Signed-off-by: herbert <herbert.eiselt@highstreet-technologies.com>
Change-Id: I13990aa4329810bb7a7dd815e6149e9890617d07
Signed-off-by: herbert <herbert.eiselt@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/apigateway/provider/src/main/java')
5 files changed, 183 insertions, 156 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())); + } + } |