Sets all auth params at once to avoid state proliferation
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / sp / ServiceProxyClient.java
index d9f89fc..2f96b13 100644 (file)
@@ -2,8 +2,10 @@ package com.indexdata.pz2utils4jsf.pazpar2.sp;
 \r
 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
 \r
+import java.io.BufferedReader;\r
 import java.io.ByteArrayOutputStream;\r
 import java.io.File;\r
+import java.io.FileReader;\r
 import java.io.IOException;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
@@ -25,6 +27,7 @@ import org.apache.http.conn.ClientConnectionManager;
 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
@@ -36,12 +39,13 @@ import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;
 import com.indexdata.pz2utils4jsf.config.Configuration;\r
 import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
 import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter;\r
 import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;\r
-import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;\r
 import com.indexdata.pz2utils4jsf.pazpar2.SearchClient;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
 import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;\r
 import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
+import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager;\r
 import com.indexdata.pz2utils4jsf.utils.Utils;\r
 \r
 @Named @SessionScoped \r
@@ -54,6 +58,7 @@ public class ServiceProxyClient implements SearchClient {
   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
@@ -70,7 +75,7 @@ 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
+      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
@@ -92,10 +97,10 @@ public class ServiceProxyClient implements SearchClient {
     try {      \r
       logger.info("Authenticating [" + user.getProperty("name") + "]");\r
       this.user = (ServiceProxyUser) user;\r
-      Pazpar2Command auth = new Pazpar2Command("auth");\r
-      auth.setParameter(new CommandParameter("action","=","login"));\r
-      auth.setParameter(new CommandParameter("username","=",user.getProperty("name")));\r
-      auth.setParameter(new CommandParameter("password","=",user.getProperty("password")));\r
+      Pazpar2Command auth = new Pazpar2Command("auth",new StateManager());\r
+      auth.setParameters(new CommandParameter("action","=","login"), \r
+                         new CommandParameter("username","=",user.getProperty("name")), \r
+                         new CommandParameter("password","=",user.getProperty("password")));\r
       byte[] response = send(auth);\r
       String responseStr = new String(response,"UTF-8");\r
       logger.info(responseStr);      \r
@@ -117,7 +122,7 @@ public class ServiceProxyClient implements SearchClient {
   \r
   public boolean checkAuthentication () {\r
     try {\r
-      Pazpar2Command check = new Pazpar2Command("auth");\r
+      Pazpar2Command check = new Pazpar2Command("auth",new StateManager());\r
       check.setParameter(new CommandParameter("action","=","check"));\r
       byte[] response = send(check);\r
       logger.info(new String(response,"UTF-8"));\r
@@ -160,7 +165,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
@@ -233,18 +237,35 @@ public class ServiceProxyClient implements SearchClient {
     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
+    if (logger.isDebugEnabled()) {\r
+      BufferedReader reader = new BufferedReader(new FileReader(initDoc));\r
+      String line;\r
+      while ( (line = reader.readLine()) != null) {\r
+        System.out.println(line);\r
+      }\r
+      reader.close();\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
+    logger.debug("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
+    logger.debug("Get init doc paths ");\r
+    logger.debug("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.debug("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
@@ -252,5 +273,9 @@ public class ServiceProxyClient implements SearchClient {
   public String getServiceProxyUrl () {\r
     return serviceUrl;\r
   }\r
-\r
+  \r
+  public Configuration getConfiguration () {\r
+    return config;\r
+  }\r
+  \r
 }\r