Adds methods for displaying authentication status
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / sp / ServiceProxyClient.java
index 520ca3e..92e46a7 100644 (file)
@@ -11,9 +11,7 @@ import java.util.ArrayList;
 import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Map;\r
-\r
-import javax.enterprise.context.SessionScoped;\r
-import javax.inject.Inject;\r
+import java.util.StringTokenizer;\r
 \r
 import org.apache.http.HttpEntity;\r
 import org.apache.http.HttpResponse;\r
@@ -34,7 +32,6 @@ import org.apache.http.impl.conn.PoolingClientConnectionManager;
 import org.apache.http.util.EntityUtils;\r
 import org.apache.log4j.Logger;\r
 \r
-import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
 import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
 import com.indexdata.mkjsf.config.Configuration;\r
 import com.indexdata.mkjsf.config.ConfigurationReader;\r
@@ -43,6 +40,7 @@ import com.indexdata.mkjsf.pazpar2.CommandResponse;
 import com.indexdata.mkjsf.pazpar2.SearchClient;\r
 import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
 import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
@@ -53,8 +51,9 @@ public class ServiceProxyClient implements SearchClient {
   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 String selectedServiceUrl = "";\r
+  private List<String> serviceUrls = new ArrayList<String>();\r
+  private List<String> initDocPaths = null;\r
   private Configuration config = null;\r
   \r
   ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
@@ -74,31 +73,35 @@ public class ServiceProxyClient implements SearchClient {
     logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
     try {\r
       config = configReader.getConfiguration(this);      \r
-      serviceUrl = config.getMandatory(SERVICE_PROXY_URL);  \r
+      serviceUrls = getMultiProperty(config.get(SERVICE_PROXY_URL));\r
+      if (serviceUrls.size()==1) {\r
+        selectedServiceUrl = serviceUrls.get(0);\r
+      }\r
       this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));\r
-      checkAuth = new Pazpar2Command("auth",null);\r
+      checkAuth = new AuthCommand(null);\r
       checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
-      ipAuth = new Pazpar2Command("auth",null);\r
+      ipAuth = new AuthCommand(null);\r
       ipAuth.setParameterInState(new CommandParameter("action","=","ipauth"));\r
     } catch (ConfigurationException c) {\r
       c.printStackTrace();\r
-    } catch (MissingMandatoryParameterException mmp) {\r
-      mmp.printStackTrace();\r
     }    \r
   }\r
   \r
-  private String[] getMultiProperty(String prop) {    \r
-    if (prop != null) {\r
-      return prop.split(",");\r
-    } else {\r
-      return null;\r
+  private List<String> getMultiProperty(String prop) {\r
+    List<String> props = new ArrayList<String>();\r
+    if (prop != null) {      \r
+      StringTokenizer tokenizer = new StringTokenizer(prop,",");\r
+      while (tokenizer.hasMoreElements()) {\r
+        props.add(tokenizer.nextToken());\r
+      }     \r
     }\r
+    return props;\r
   }\r
   \r
   public boolean authenticate (ServiceProxyUser user) {\r
     try {      \r
       logger.info("Authenticating [" + user.getProperty("name") + "]");            \r
-      Pazpar2Command auth = new Pazpar2Command("auth",null);\r
+      Pazpar2Command auth = new AuthCommand(null);\r
       auth.setParametersInState(new CommandParameter("action","=","login"), \r
                                 new CommandParameter("username","=",user.getProperty("name")), \r
                                 new CommandParameter("password","=",user.getProperty("password")));                                \r
@@ -106,10 +109,10 @@ public class ServiceProxyClient implements SearchClient {
       String responseStr = new String(response,"UTF-8");\r
       logger.info(responseStr);      \r
       if (responseStr.contains("FAIL")) {\r
-        user.isAuthenticated(false);\r
+        user.credentialsAuthenticationSucceeded(false);\r
         return false;\r
       } else {\r
-        user.isAuthenticated(true);\r
+        user.credentialsAuthenticationSucceeded(true);\r
         return true;\r
       }      \r
     } catch (ClientProtocolException e) {\r
@@ -129,10 +132,9 @@ public class ServiceProxyClient implements SearchClient {
       logger.info(new String(response,"UTF-8"));\r
       String responseStr = new String(response,"UTF-8");    \r
       if (responseStr.contains("FAIL")) {  \r
-        user.isAuthenticated(false);\r
+        user.authenticationCheckFailed();\r
         return false;\r
-      } else {        \r
-        user.isAuthenticated(true);\r
+      } else {                \r
         return true;\r
       }      \r
     } catch (ClientProtocolException e) {\r
@@ -152,10 +154,10 @@ public class ServiceProxyClient implements SearchClient {
       logger.info(new String(response,"UTF-8"));\r
       String responseStr = new String(response,"UTF-8");    \r
       if (responseStr.contains("FAIL")) {\r
-        user.isAuthenticated(false);\r
+        user.ipAuthenticationSucceeded(false);        \r
         return false;\r
       } else {\r
-        user.isAuthenticated(true);\r
+        user.ipAuthenticationSucceeded(true);\r
         return true;\r
       }      \r
     } catch (ClientProtocolException e) {\r
@@ -190,7 +192,7 @@ public class ServiceProxyClient implements SearchClient {
    * @throws IOException\r
    */\r
   private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
-    String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
+    String url = selectedServiceUrl + "?" + command.getEncodedQueryString(); \r
     logger.info("Sending request "+url);    \r
     HttpGet httpget = new HttpGet(url);     \r
     byte[] response = client.execute(httpget, handler);    \r
@@ -242,7 +244,8 @@ public class ServiceProxyClient implements SearchClient {
     logger.debug("Cloning Pz2Client");\r
     ServiceProxyClient clone = new ServiceProxyClient();\r
     clone.client = this.client;\r
-    clone.serviceUrl = this.serviceUrl;\r
+    clone.serviceUrls = this.serviceUrls;\r
+    clone.selectedServiceUrl = this.selectedServiceUrl;\r
     clone.initDocPaths = this.initDocPaths;\r
     return clone;\r
   }\r
@@ -260,13 +263,13 @@ public class ServiceProxyClient implements SearchClient {
   @Override\r
   public List<String> documentConfiguration () {\r
     List<String> doc = new ArrayList<String>();\r
-    doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl);\r
+    doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (selectedServiceUrl.length()>0 ? selectedServiceUrl : "[not defined yet]"));\r
     return null;\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
+    HttpPost post = new HttpPost(selectedServiceUrl+"?command=init&includeDebug=yes");\r
     File initDoc = new File(filePath);\r
     logger.info("Posting to SP: ");\r
     if (logger.isDebugEnabled()) {\r
@@ -283,14 +286,14 @@ public class ServiceProxyClient implements SearchClient {
     return response;\r
   }\r
   \r
-  public String[] getInitDocPaths () {\r
+  public List<String> getInitDocPaths () {\r
     logger.debug("Get init doc paths ");\r
-    logger.debug("length: " + initDocPaths.length);\r
+    logger.debug("length: " + initDocPaths.size());\r
     return initDocPaths;\r
   }\r
   \r
-  public byte[] postInitDoc(byte[] initDoc) throws IOException {\r
-    HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
+  public byte[] postInitDoc(byte[] initDoc, boolean includeDebug) throws IOException {\r
+    HttpPost post = new HttpPost(selectedServiceUrl+"?command=init" + (includeDebug? "&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
@@ -298,13 +301,17 @@ public class ServiceProxyClient implements SearchClient {
   }\r
   \r
   public void setServiceProxyUrl (String url) {\r
-    serviceUrl = url;\r
+    selectedServiceUrl = url;\r
   }\r
   \r
   public String getServiceProxyUrl () {\r
-    return serviceUrl;\r
+    return selectedServiceUrl;\r
   }\r
   \r
+  public List<String> getServiceProxyUrls () {\r
+    return serviceUrls;\r
+  }\r
+    \r
   public Configuration getConfiguration () {\r
     return config;\r
   }\r