Adds IP authentication to SP client
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / sp / ServiceProxyClient.java
index 3724362..520ca3e 100644 (file)
@@ -12,6 +12,9 @@ import java.util.HashMap;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import javax.enterprise.context.SessionScoped;\r
+import javax.inject.Inject;\r
+\r
 import org.apache.http.HttpEntity;\r
 import org.apache.http.HttpResponse;\r
 import org.apache.http.StatusLine;\r
@@ -40,11 +43,9 @@ 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.sp.auth.AuthenticationEntity;\r
 import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
-\r
 public class ServiceProxyClient implements SearchClient {\r
     \r
   private static final long serialVersionUID = -4031644009579840277L;\r
@@ -57,14 +58,15 @@ public class ServiceProxyClient implements SearchClient {
   private Configuration config = null;\r
   \r
   ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
-  private HttpClient client;\r
-  private ServiceProxyUser user;\r
+  private transient HttpClient client;  \r
+  private Pazpar2Command checkAuth = null;\r
+  private Pazpar2Command ipAuth = null;\r
 \r
   public ServiceProxyClient () {\r
     SchemeRegistry schemeRegistry = new SchemeRegistry();\r
     schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));\r
     ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);\r
-    client = new DefaultHttpClient(cm);    \r
+    client = new DefaultHttpClient(cm);\r
   }\r
     \r
   @Override\r
@@ -73,7 +75,11 @@ public class ServiceProxyClient implements SearchClient {
     try {\r
       config = configReader.getConfiguration(this);      \r
       serviceUrl = config.getMandatory(SERVICE_PROXY_URL);  \r
-      this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));            \r
+      this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));\r
+      checkAuth = new Pazpar2Command("auth",null);\r
+      checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
+      ipAuth = new Pazpar2Command("auth",null);\r
+      ipAuth.setParameterInState(new CommandParameter("action","=","ipauth"));\r
     } catch (ConfigurationException c) {\r
       c.printStackTrace();\r
     } catch (MissingMandatoryParameterException mmp) {\r
@@ -89,20 +95,21 @@ public class ServiceProxyClient implements SearchClient {
     }\r
   }\r
   \r
-  public boolean authenticate (AuthenticationEntity user) {\r
+  public boolean authenticate (ServiceProxyUser user) {\r
     try {      \r
-      logger.info("Authenticating [" + user.getProperty("name") + "]");\r
-      this.user = (ServiceProxyUser) user;\r
+      logger.info("Authenticating [" + user.getProperty("name") + "]");            \r
       Pazpar2Command auth = new Pazpar2Command("auth",null);\r
       auth.setParametersInState(new CommandParameter("action","=","login"), \r
                                 new CommandParameter("username","=",user.getProperty("name")), \r
-                                new CommandParameter("password","=",user.getProperty("password")));\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
       if (responseStr.contains("FAIL")) {\r
+        user.isAuthenticated(false);\r
         return false;\r
       } else {\r
+        user.isAuthenticated(true);\r
         return true;\r
       }      \r
     } catch (ClientProtocolException e) {\r
@@ -116,12 +123,18 @@ public class ServiceProxyClient implements SearchClient {
     }        \r
   }\r
   \r
-  public boolean checkAuthentication () {\r
+  public boolean checkAuthentication (ServiceProxyUser user) {    \r
     try {\r
-      Pazpar2Command check = new Pazpar2Command("auth",null);\r
-      check.setParameter(new CommandParameter("action","=","check"));\r
-      byte[] response = send(check);\r
+      byte[] response = send(checkAuth);\r
       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
+        return false;\r
+      } else {        \r
+        user.isAuthenticated(true);\r
+        return true;\r
+      }      \r
     } catch (ClientProtocolException e) {\r
       // TODO Auto-generated catch block\r
       e.printStackTrace();\r
@@ -130,8 +143,30 @@ public class ServiceProxyClient implements SearchClient {
       // TODO Auto-generated catch block\r
       e.printStackTrace();\r
       return false;\r
-    }    \r
-    return true;\r
+    }        \r
+  }\r
+  \r
+  public boolean ipAuthenticate (ServiceProxyUser user) {\r
+    try {\r
+      byte[] response = send(ipAuth);\r
+      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
+        return false;\r
+      } else {\r
+        user.isAuthenticated(true);\r
+        return true;\r
+      }      \r
+    } catch (ClientProtocolException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+      return false;\r
+    } catch (IOException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+      return false;\r
+    }        \r
     \r
   }\r
   \r
@@ -139,9 +174,9 @@ public class ServiceProxyClient implements SearchClient {
     return true;\r
   }\r
   \r
-  public boolean isAuthenticated () {\r
+  public boolean isAuthenticated (ServiceProxyUser user) {\r
     if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
-      return checkAuthentication();\r
+      return checkAuthentication(user);\r
     } else {\r
       return false;\r
     }\r