Downgrades to Java 1.6 to run demo on Debian Wheezy
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / sp / ServiceProxyClient.java
index 0602d4f..e881506 100644 (file)
@@ -3,7 +3,10 @@ package com.indexdata.pz2utils4jsf.pazpar2.sp;
 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
 \r
 import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
 import java.io.IOException;\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 import java.util.List;\r
@@ -19,10 +22,13 @@ import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.HttpClient;\r
 import org.apache.http.client.ResponseHandler;\r
 import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.client.methods.HttpPost;\r
 import org.apache.http.conn.ClientConnectionManager;\r
 import org.apache.http.conn.scheme.PlainSocketFactory;\r
 import org.apache.http.conn.scheme.Scheme;\r
 import org.apache.http.conn.scheme.SchemeRegistry;\r
+import org.apache.http.entity.ByteArrayEntity;\r
+import org.apache.http.entity.FileEntity;\r
 import org.apache.http.impl.client.DefaultHttpClient;\r
 import org.apache.http.impl.conn.PoolingClientConnectionManager;\r
 import org.apache.http.util.EntityUtils;\r
@@ -47,7 +53,11 @@ public class ServiceProxyClient implements SearchClient {
   private static final long serialVersionUID = -4031644009579840277L;\r
   private static Logger logger = Logger.getLogger(ServiceProxyClient.class);\r
   public static final String MODULENAME = "proxyclient";\r
+  public static final String SERVICE_PROXY_URL = "SERVICE_PROXY_URL";\r
+  public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";\r
   private String serviceUrl = "undefined";\r
+  private String[] initDocPaths = null;\r
+  private Configuration config = null;\r
   \r
   ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
   private HttpClient client;\r
@@ -64,8 +74,9 @@ public class ServiceProxyClient implements SearchClient {
   public void configure (ConfigurationReader configReader) {\r
     logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
     try {\r
-      Configuration config = configReader.getConfiguration(this);      \r
-      serviceUrl = config.getMandatory("SERVICE_PROXY_URL");      \r
+      config = configReader.getConfiguration(this);      \r
+      serviceUrl = config.getMandatory(SERVICE_PROXY_URL);  \r
+      this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));            \r
     } catch (ConfigurationException c) {\r
       c.printStackTrace();\r
     } catch (MissingMandatoryParameterException mmp) {\r
@@ -73,6 +84,14 @@ public class ServiceProxyClient implements SearchClient {
     }    \r
   }\r
   \r
+  private String[] getMultiProperty(String prop) {    \r
+    if (prop != null) {\r
+      return prop.split(",");\r
+    } else {\r
+      return null;\r
+    }\r
+  }\r
+  \r
   public boolean authenticate (AuthenticationEntity user) {\r
     try {      \r
       logger.info("Authenticating [" + user.getProperty("name") + "]");\r
@@ -145,7 +164,6 @@ public class ServiceProxyClient implements SearchClient {
     byte[] response = client.execute(httpget, handler);    \r
     return response;\r
   }\r
-\r
   \r
   public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
     private StatusLine statusLine = null;\r
@@ -193,6 +211,7 @@ public class ServiceProxyClient implements SearchClient {
     ServiceProxyClient clone = new ServiceProxyClient();\r
     clone.client = this.client;\r
     clone.serviceUrl = this.serviceUrl;\r
+    clone.initDocPaths = this.initDocPaths;\r
     return clone;\r
   }\r
 \r
@@ -212,5 +231,52 @@ public class ServiceProxyClient implements SearchClient {
     doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl);\r
     return null;\r
   }\r
-\r
+  \r
+  public byte[] postInitDoc (String filePath) throws IOException {\r
+    logger.info("Looking to post the file in : [" + filePath +"]");\r
+    HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
+    File initDoc = new File(filePath);\r
+    logger.info("Posting to SP: ");\r
+    Path path = Paths.get(filePath);\r
+    /*\r
+    if (logger.isDebugEnabled()) {\r
+      try (Scanner scanner =  new Scanner(path, "UTF-8")){\r
+        while (scanner.hasNextLine()){\r
+          System.out.println(scanner.nextLine());\r
+        }      \r
+      }     \r
+    }\r
+    */\r
+    post.setEntity(new FileEntity(initDoc));\r
+    byte[] response = client.execute(post, handler);\r
+    logger.info("Response on POST was: " + new String(response,"UTF-8"));    \r
+    return response;\r
+  }\r
+  \r
+  public String[] getInitDocPaths () {\r
+    logger.info("Get init doc paths ");\r
+    logger.info("length: " + initDocPaths.length);\r
+    return initDocPaths;\r
+  }\r
+  \r
+  public byte[] postInitDoc(byte[] initDoc) throws IOException {\r
+    HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
+    post.setEntity(new ByteArrayEntity(initDoc));\r
+    byte[] response = client.execute(post, handler);\r
+    logger.info("Response on POST was: " + new String(response,"UTF-8"));    \r
+    return response;\r
+  }\r
+  \r
+  public void setServiceProxyUrl (String url) {\r
+    serviceUrl = url;\r
+  }\r
+  \r
+  public String getServiceProxyUrl () {\r
+    return serviceUrl;\r
+  }\r
+  \r
+  public Configuration getConfiguration () {\r
+    return config;\r
+  }\r
+  \r
 }\r