From 093deaf4738d56602f94e6a4193993a77e053a51 Mon Sep 17 00:00:00 2001 From: Stela Stoykova Date: Fri, 15 Feb 2019 10:40:54 -0500 Subject: Allow custom ES mapping files to be used Change-Id: Ie2fc04b381691ebba38de18ba64581952cceacc9 Issue-ID: AAI-2156 Signed-off-by: Stela Stoykova --- .../aai/datarouter/util/DataRouterConstants.java | 2 ++ .../aai/datarouter/util/SearchServiceAgent.java | 42 +++++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/onap/aai/datarouter/util/DataRouterConstants.java b/src/main/java/org/onap/aai/datarouter/util/DataRouterConstants.java index 98fdc62..14cf6a7 100644 --- a/src/main/java/org/onap/aai/datarouter/util/DataRouterConstants.java +++ b/src/main/java/org/onap/aai/datarouter/util/DataRouterConstants.java @@ -54,4 +54,6 @@ public class DataRouterConstants { // Logging related public static final String DATA_ROUTER_SERVICE_NAME = "DataRouter"; + + public static final String DR_SEARCH_SCHEMA_HOME = DR_SPECIFIC_CONFIG + "search-schema"; } diff --git a/src/main/java/org/onap/aai/datarouter/util/SearchServiceAgent.java b/src/main/java/org/onap/aai/datarouter/util/SearchServiceAgent.java index 4baa81a..b62237d 100644 --- a/src/main/java/org/onap/aai/datarouter/util/SearchServiceAgent.java +++ b/src/main/java/org/onap/aai/datarouter/util/SearchServiceAgent.java @@ -37,13 +37,15 @@ import org.eclipse.jetty.util.security.Password; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.mdc.MdcContext; import org.onap.aai.datarouter.logging.DataRouterMsgs; -import org.onap.aai.datarouter.policy.EntityEventPolicy; import org.onap.aai.restclient.client.Headers; import org.onap.aai.restclient.client.OperationResult; import org.onap.aai.restclient.client.RestClient; import org.onap.aai.restclient.enums.RestAuthenticationMode; import org.onap.aai.restclient.rest.HttpUtil; import org.slf4j.MDC; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; import com.sun.jersey.core.util.MultivaluedMapImpl; @@ -56,6 +58,7 @@ public class SearchServiceAgent { private String searchUrl = null; private String documentEndpoint = null; + private String schemaHomeDir = null; /** @@ -109,6 +112,7 @@ public class SearchServiceAgent { this.documentEndpoint = documentEndpoint; this.logger = logger; + this.schemaHomeDir = DataRouterConstants.DR_SEARCH_SCHEMA_HOME; } @@ -338,22 +342,42 @@ public class SearchServiceAgent { * @throws Exception */ protected String loadFileData(String filename) throws Exception { - StringBuilder data = new StringBuilder(); - - try (InputStreamReader inputStreamReader = new InputStreamReader(EntityEventPolicy.class.getClassLoader() - .getResourceAsStream("/" + filename), StandardCharsets.UTF_8); BufferedReader in = new BufferedReader( - inputStreamReader) - ) { + Resource fileResource = getSchemaResource(filename); + if (fileResource == null) { + throw new Exception("Could not find file = " + filename + "."); + } + try ( + InputStreamReader inputStreamReader = new InputStreamReader(fileResource.getInputStream(), StandardCharsets.UTF_8); + BufferedReader in = new BufferedReader(inputStreamReader); + ){ String line; - while ((line = in.readLine()) != null) { + StringBuilder data = new StringBuilder(); + while((line = in.readLine()) != null) { data.append(line); } + return data.toString(); } catch (Exception e) { throw new Exception("Failed to read from file = " + filename + ".", e); } + } + + private Resource getSchemaResource(String filename) { + Resource fileResource = null; + + if(filename == null) { + return null; + } + + if ((schemaHomeDir != null) && (fileResource = new FileSystemResource(schemaHomeDir + "/" + filename)).isReadable()) { + return fileResource; + } + + if ((fileResource = new ClassPathResource(filename)).isReadable()) { + return fileResource; + } - return data.toString(); + return null; } -- cgit 1.2.3-korg