Generalizes and distinguishes - pz2 and sp. Adds auth.
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / ProxyPz2Client.java
index db1fe91..e6e96bc 100644 (file)
@@ -10,7 +10,6 @@ import java.util.List;
 import java.util.Map;\r
 \r
 import javax.enterprise.context.SessionScoped;\r
-import javax.enterprise.inject.Alternative;\r
 import javax.inject.Named;\r
 \r
 import org.apache.http.HttpEntity;\r
@@ -34,11 +33,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.sp.auth.AuthenticationEntity;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.pz2utils4jsf.utils.Utils;\r
 \r
-@Named @SessionScoped @Alternative\r
+@Named @SessionScoped \r
 public class ProxyPz2Client implements SearchClient {\r
-\r
+    \r
   private static final long serialVersionUID = -4031644009579840277L;\r
   private static Logger logger = Logger.getLogger(ProxyPz2Client.class);\r
   public static final String MODULENAME = "proxyclient";\r
@@ -46,12 +47,13 @@ public class ProxyPz2Client implements SearchClient {
   \r
   ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
   private HttpClient client;\r
+  private ServiceProxyUser user;\r
 \r
   public ProxyPz2Client () {\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
@@ -59,33 +61,72 @@ public class ProxyPz2Client implements SearchClient {
     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
-      authenticate();\r
+      serviceUrl = config.getMandatory("SERVICE_PROXY_URL");      \r
     } catch (ConfigurationException c) {\r
-      // TODO Auto-generated catch block\r
       c.printStackTrace();\r
     } catch (MissingMandatoryParameterException mmp) {\r
       mmp.printStackTrace();\r
-    }\r
+    }    \r
   }\r
   \r
-  public void authenticate () {\r
-    try {\r
+  public boolean authenticate (AuthenticationEntity user) {\r
+    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","=","demo"));\r
-      auth.setParameter(new CommandParameter("password","=","demo"));\r
-      send(auth);\r
+      auth.setParameter(new CommandParameter("username","=",user.getProperty("name")));\r
+      auth.setParameter(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
+        return false;\r
+      } else {\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
-    }\r
+      return false;\r
+    }        \r
+  }\r
+  \r
+  public boolean checkAuthentication () {\r
+    try {\r
+      Pazpar2Command check = new Pazpar2Command("auth");\r
+      check.setParameter(new CommandParameter("action","=","check"));\r
+      byte[] response = send(check);\r
+      logger.info(new String(response,"UTF-8"));\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
+    return true;\r
     \r
   }\r
   \r
+  public boolean isAuthenticatingClient () {\r
+    return true;\r
+  }\r
+  \r
+  public boolean isAuthenticated () {\r
+    if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
+      return checkAuthentication();\r
+    } else {\r
+      return false;\r
+    }\r
+  }\r
+  \r
   /**\r
    * Makes the request\r
    * @param request\r
@@ -93,12 +134,12 @@ public class ProxyPz2Client implements SearchClient {
    * @throws ClientProtocolException\r
    * @throws IOException\r
    */\r
-  private String send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
+  private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
     String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
     logger.info("Sending request "+url);    \r
     HttpGet httpget = new HttpGet(url);     \r
     byte[] response = client.execute(httpget, handler);    \r
-    return new String(response);\r
+    return response;\r
   }\r
 \r
   \r
@@ -138,8 +179,9 @@ public class ProxyPz2Client implements SearchClient {
   @Override\r
   public CommandResponse executeCommand(Pazpar2Command command,\r
       ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {\r
-    String response = send(command);\r
-    return new ProxyPz2ClientCommandResponse(getStatusCode(), response);    \r
+    byte[] response = send(command);\r
+    baos.write(response);\r
+    return new ProxyPz2ClientCommandResponse(getStatusCode(), new String(response,"UTF-8"));    \r
   }\r
 \r
   public ProxyPz2Client cloneMe() {\r