Allow custom ES mapping files to be used
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / util / SearchServiceAgent.java
index 42861b4..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
@@ -96,19 +99,20 @@ public class SearchServiceAgent {
                           String documentEndpoint, \r
                           Logger logger) {\r
     \r
+    String deobfuscatedCertPassword = keystorePwd.startsWith("OBF:")?Password.deobfuscate(keystorePwd):keystorePwd;\r
     // Create REST client for search service\r
     searchClient = new RestClient()\r
                     .authenticationMode(RestAuthenticationMode.SSL_CERT)\r
                     .validateServerHostname(false)\r
-                    .validateServerCertChain(true)\r
+                    .validateServerCertChain(false)\r
                     .clientCertFile(DataRouterConstants.DR_HOME_AUTH + certName)\r
-                    .clientCertPassword(Password.deobfuscate(keystorePwd))\r
-                    .trustStore(DataRouterConstants.DR_HOME_AUTH + keystore);\r
+                    .clientCertPassword(deobfuscatedCertPassword);                    \r
     \r
     this.searchUrl        = searchUrl;\r
     this.documentEndpoint = documentEndpoint;\r
     \r
     this.logger           = logger;\r
+    this.schemaHomeDir = DataRouterConstants.DR_SEARCH_SCHEMA_HOME;\r
   }\r
   \r
   \r
@@ -320,8 +324,7 @@ public class SearchServiceAgent {
    * Removes a document from the Search Service.\r
    * \r
    * @param index   - The index to create the document in.\r
-   * @param id      - The identifier for the document.\r
-   * @param payload - The document contents.\r
+   * @param documentId      - The identifier for the document.\r
    * @param headers - HTTP headers.\r
    */\r
   public void deleteDocument(String index, String documentId, Map<String, List<String>> headers) {\r
@@ -339,21 +342,42 @@ public class SearchServiceAgent {
    * @throws Exception\r
    */\r
   protected String loadFileData(String filename) throws Exception {\r
-    StringBuilder data = new StringBuilder();\r
-    try {\r
-      BufferedReader in = new BufferedReader(new InputStreamReader(\r
-          EntityEventPolicy.class.getClassLoader().getResourceAsStream("/" + filename),\r
-          StandardCharsets.UTF_8));\r
-      String line;\r
+    Resource fileResource = getSchemaResource(filename);\r
+    if (fileResource == null) {\r
+      throw new Exception("Could not find file = " + filename + ".");\r
+    }\r
 \r
-      while ((line = in.readLine()) != null) {\r
+    try (\r
+        InputStreamReader inputStreamReader = new InputStreamReader(fileResource.getInputStream(), StandardCharsets.UTF_8);\r
+        BufferedReader in = new BufferedReader(inputStreamReader);\r
+        ){\r
+      String line;\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