Allow custom ES mapping files to be used
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / util / SearchServiceAgent.java
index 4baa81a..b62237d 100644 (file)
@@ -37,13 +37,15 @@ import org.eclipse.jetty.util.security.Password;
 import org.onap.aai.cl.api.Logger;\r
 import org.onap.aai.cl.mdc.MdcContext;\r
 import org.onap.aai.datarouter.logging.DataRouterMsgs;\r
-import org.onap.aai.datarouter.policy.EntityEventPolicy;\r
 import org.onap.aai.restclient.client.Headers;\r
 import org.onap.aai.restclient.client.OperationResult;\r
 import org.onap.aai.restclient.client.RestClient;\r
 import org.onap.aai.restclient.enums.RestAuthenticationMode;\r
 import org.onap.aai.restclient.rest.HttpUtil;\r
 import org.slf4j.MDC;\r
+import org.springframework.core.io.ClassPathResource;\r
+import org.springframework.core.io.FileSystemResource;\r
+import org.springframework.core.io.Resource;\r
 \r
 import com.sun.jersey.core.util.MultivaluedMapImpl;\r
 \r
@@ -56,6 +58,7 @@ public class SearchServiceAgent {
   \r
   private String searchUrl = null;\r
   private String documentEndpoint = null;\r
+  private String schemaHomeDir = null;\r
   \r
   \r
   /**\r
@@ -109,6 +112,7 @@ public class SearchServiceAgent {
     this.documentEndpoint = documentEndpoint;\r
     \r
     this.logger           = logger;\r
+    this.schemaHomeDir = DataRouterConstants.DR_SEARCH_SCHEMA_HOME;\r
   }\r
   \r
   \r
@@ -338,22 +342,42 @@ public class SearchServiceAgent {
    * @throws Exception\r
    */\r
   protected String loadFileData(String filename) throws Exception {\r
-    StringBuilder data = new StringBuilder();\r
-\r
-    try (InputStreamReader inputStreamReader = new InputStreamReader(EntityEventPolicy.class.getClassLoader()\r
-        .getResourceAsStream("/" + filename), StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(\r
-        inputStreamReader)\r
-    ) {\r
+    Resource fileResource = getSchemaResource(filename);\r
+    if (fileResource == null) {\r
+      throw new Exception("Could not find file = " + filename + ".");\r
+    }\r
 \r
+    try (\r
+        InputStreamReader inputStreamReader = new InputStreamReader(fileResource.getInputStream(), StandardCharsets.UTF_8);\r
+        BufferedReader in = new BufferedReader(inputStreamReader);\r
+        ){\r
       String line;\r
-      while ((line = in.readLine()) != null) {\r
+      StringBuilder data = new StringBuilder();\r
+      while((line = in.readLine()) != null) {\r
         data.append(line);\r
       }\r
+      return data.toString();\r
     } catch (Exception e) {\r
       throw new Exception("Failed to read from file = " + filename + ".", e);\r
     }\r
+  }\r
+\r
+  private Resource getSchemaResource(String filename) {\r
+    Resource fileResource = null;\r
+\r
+    if(filename == null) {\r
+      return null;\r
+    }\r
+    \r
+    if ((schemaHomeDir != null) && (fileResource = new FileSystemResource(schemaHomeDir + "/" + filename)).isReadable()) {\r
+      return fileResource;\r
+    }\r
+\r
+    if ((fileResource = new ClassPathResource(filename)).isReadable()) {\r
+      return fileResource;\r
+    }\r
 \r
-    return data.toString();\r
+    return null;\r
   }\r
   \r
   \r