Cleans up SP auth, parse resps, store for display, etc
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Fri, 10 May 2013 22:48:54 +0000 (18:48 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Fri, 10 May 2013 22:48:54 +0000 (18:48 -0400)
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Commands.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/AuthCommand.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java
src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java
src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java

index 79d6153..9493e49 100644 (file)
@@ -38,7 +38,6 @@ import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
 import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
 import com.indexdata.mkjsf.pazpar2.data.CommandError;\r
-import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
 public class ServiceProxyClient implements SearchClient {\r
@@ -81,63 +80,11 @@ public class ServiceProxyClient implements SearchClient {
       c.printStackTrace();\r
     }    \r
   }\r
-  \r
-  \r
-  public boolean authenticate (ServiceProxyUser user) {\r
-    logger.info("Authenticating [" + user.getProperty("name") + "]");            \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
-    ClientCommandResponse commandResponse = send(auth);\r
-    String responseStr = commandResponse.getResponseString();\r
-    logger.info(responseStr);      \r
-    if (responseStr.contains("FAIL")) {\r
-      user.credentialsAuthenticationSucceeded(false);\r
-      return false;\r
-    } else {\r
-      user.credentialsAuthenticationSucceeded(true);\r
-      return true;\r
-    }      \r
-  }\r
-  \r
-  public boolean checkAuthentication (ServiceProxyUser user) {    \r
-    ClientCommandResponse commandResponse = send(checkAuth);      \r
-    String responseStr = commandResponse.getResponseString();    \r
-    logger.info(responseStr);\r
-    if (responseStr.contains("FAIL")) {  \r
-      user.authenticationCheckFailed();\r
-      return false;\r
-    } else {                \r
-      return true;\r
-    }      \r
-  }\r
-  \r
-  public boolean ipAuthenticate (ServiceProxyUser user) {\r
-    ClientCommandResponse commandResponse = send(ipAuth);      \r
-    String responseStr = commandResponse.getResponseString();\r
-    logger.info(responseStr);\r
-    if (responseStr.contains("FAIL")) {\r
-      user.ipAuthenticationSucceeded(false);        \r
-      return false;\r
-    } else {\r
-      user.ipAuthenticationSucceeded(true);\r
-      return true;\r
-    }          \r
-  }\r
-  \r
+    \r
   public boolean isAuthenticatingClient () {\r
     return true;\r
   }\r
-  \r
-  public boolean isAuthenticated (ServiceProxyUser user) {\r
-    if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
-      return checkAuthentication(user);\r
-    } else {\r
-      return false;\r
-    }\r
-  }\r
-  \r
+    \r
   /**\r
    * Makes the request\r
    * @param request\r
@@ -145,7 +92,7 @@ public class ServiceProxyClient implements SearchClient {
    * @throws ClientProtocolException\r
    * @throws IOException\r
    */\r
-  private ClientCommandResponse send(Pazpar2Command command) {\r
+  public ClientCommandResponse send(Pazpar2Command command) {\r
     ClientCommandResponse commandResponse = null;\r
     String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
     logger.info("Sending request "+url);    \r
index 560edb6..0ee2825 100644 (file)
@@ -12,8 +12,11 @@ import org.apache.log4j.Logger;
 import org.apache.myfaces.custom.fileupload.UploadedFile;\r
 \r
 import com.indexdata.mkjsf.config.ConfigurationReader;\r
+import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
 import com.indexdata.mkjsf.pazpar2.commands.sp.InitDocUpload;\r
+import com.indexdata.mkjsf.pazpar2.data.AuthResponse;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
 import com.indexdata.mkjsf.pazpar2.data.Responses;\r
@@ -41,7 +44,8 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
     // TODO: \r
     //stateMgr.addStateListener(this);\r
   }\r
-    \r
+   \r
+  /*\r
   public void login(String un, String pw) {\r
     if (user.isAuthenticated() && user.getName().equals(un) && pz2.spClient.checkAuthentication(user)) {\r
       logger.info("Repeat request from UI to authenticate user. Auth verified for given user name so skipping log-in.");\r
@@ -52,16 +56,34 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
       login("dummy");\r
     }\r
   }\r
+  */\r
 \r
-  @Override\r
+  \r
+  @Override  \r
   public String login(String navigateTo) {\r
     logger.info("doing login by " + user + " using " + pz2 + " and client " + pz2.getSpClient());\r
     pz2.resetSearchAndRecordCommands();\r
     pzresp.resetAllSessionData();\r
-    pz2.getSpClient().authenticate(user);    \r
+    AuthCommand auth = pzreq.getSp().getAuth(); \r
+    auth.setParametersInState(new CommandParameter("action","=","login"),\r
+                              new CommandParameter("username","=",user.getName()),\r
+                              new CommandParameter("password","=",user.getPassword()));\r
+    ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);\r
+    AuthResponse responseObject = (AuthResponse) (ResponseParser.getParser().getDataObject(commandResponse.getResponseString()));\r
+    if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+      pzresp.put(auth.getCommandName(), responseObject);\r
+    }\r
+    String responseStr = commandResponse.getResponseString();\r
+    logger.info(responseStr);      \r
+    if (responseStr.contains("FAIL")) {\r
+      user.credentialsAuthenticationSucceeded(false);    \r
+    } else {\r
+      user.credentialsAuthenticationSucceeded(true);    \r
+    }      \r
     return navigateTo;\r
   }\r
   \r
+  \r
   public void ipAuthenticate (ServiceProxyUser user) {\r
     if (!user.isIpAuthenticated()) {\r
       if (user.isAuthenticated()) {\r
@@ -69,7 +91,20 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
       }\r
       pz2.resetSearchAndRecordCommands();\r
       pzresp.resetAllSessionData();\r
-      pz2.getSpClient().ipAuthenticate(user);\r
+      AuthCommand auth = pzreq.getSp().getAuth(); \r
+      auth.setParameterInState(new CommandParameter("action","=","ipAuth"));\r
+      ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);\r
+      AuthResponse responseObject = (AuthResponse) (ResponseParser.getParser().getDataObject(commandResponse.getResponseString()));\r
+      if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+        pzresp.put(auth.getCommandName(), responseObject);\r
+      }\r
+      String responseStr = commandResponse.getResponseString();\r
+      logger.info(responseStr);      \r
+      if (responseStr.contains("FAIL")) {\r
+        user.credentialsAuthenticationSucceeded(false);    \r
+      } else {\r
+        user.credentialsAuthenticationSucceeded(true);    \r
+      }      \r
     }\r
   }\r
     \r
index 9e04d6e..2524d47 100644 (file)
@@ -9,6 +9,7 @@ import javax.inject.Named;
 \r
 import org.apache.log4j.Logger;\r
 \r
+import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommands;\r
 import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
@@ -17,7 +18,7 @@ public class Pazpar2Commands implements Serializable {
 \r
   private static final long serialVersionUID = -5172466320351302413L;\r
   private static Logger logger = Logger.getLogger(Pazpar2Commands.class);\r
-\r
+  \r
   public static final String INIT =     "init";\r
   public static final String PING =     "ping";\r
   public static final String SETTINGS = "settings";\r
@@ -27,8 +28,11 @@ public class Pazpar2Commands implements Serializable {
   public static final String RECORD =   "record";\r
   public static final String TERMLIST = "termlist";\r
   public static final String BYTARGET = "bytarget";\r
+  \r
+  private ServiceProxyCommands sp = null;\r
     \r
-  @Inject StateManager stateMgr; \r
+  @Inject\r
+  protected StateManager stateMgr; \r
   \r
   public Pazpar2Commands() {\r
     logger.info("Initializing Pazpar2Commands [" + Utils.objectId(this) + "]");\r
@@ -78,5 +82,12 @@ public class Pazpar2Commands implements Serializable {
   public Pazpar2Command getCommand(String name) {\r
     return stateMgr.getCommand(name);\r
   }\r
+  \r
+  public ServiceProxyCommands getSp() {\r
+    if (sp == null) {\r
+      sp = new ServiceProxyCommands(stateMgr);\r
+    }\r
+    return sp;\r
+  }\r
       \r
 }\r
index 698f265..4d72d61 100644 (file)
@@ -9,7 +9,6 @@ public class AuthCommand extends Pazpar2Command implements ServiceProxyCommand {
 \r
   public AuthCommand(StateManager stateMgr) {\r
     super("auth", stateMgr);\r
-    // TODO Auto-generated constructor stub\r
   }\r
   \r
   public AuthCommand copy () {\r
@@ -24,6 +23,4 @@ public class AuthCommand extends Pazpar2Command implements ServiceProxyCommand {
   public ServiceProxyCommand getSp() {\r
     return this;\r
   }\r
-\r
-\r
 }\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java
new file mode 100644 (file)
index 0000000..cac8100
--- /dev/null
@@ -0,0 +1,28 @@
+package com.indexdata.mkjsf.pazpar2.commands.sp;\r
+\r
+import java.io.Serializable;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
+import com.indexdata.mkjsf.utils.Utils;\r
+\r
+public class ServiceProxyCommands implements Serializable {\r
+\r
+  public static final String AUTH = "auth";\r
+  private static final long serialVersionUID = 6223527018096841188L;\r
+  private static Logger logger = Logger.getLogger(ServiceProxyCommands.class);\r
+  private StateManager stateMgr = null; \r
+  \r
+  public ServiceProxyCommands(StateManager stateMgr) {\r
+    logger.info("Initializing ServiceProxyCommands [" + Utils.objectId(this) + "]");\r
+    this.stateMgr = stateMgr;\r
+  }\r
+\r
+  \r
+  public AuthCommand getAuth() {\r
+    return (AuthCommand) (stateMgr.getCommand(AUTH));\r
+  }\r
+\r
+\r
+}\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java
new file mode 100644 (file)
index 0000000..a0a015f
--- /dev/null
@@ -0,0 +1,25 @@
+package com.indexdata.mkjsf.pazpar2.data;\r
+\r
+public class AuthResponse extends ResponseDataObject {\r
+\r
+  private static final long serialVersionUID = 8006774126022849936L;\r
+\r
+  public String getDisplayName () {\r
+    return getOneElementValue("displayName");\r
+  }\r
+  \r
+  public String getRealm () {\r
+    return getOneElementValue("realm");\r
+  }\r
+  \r
+  public String getStatus () {\r
+    return getOneElementValue("status");\r
+  }\r
+  \r
+  public String getAuthenticationType () {\r
+    return getOneElementValue("type");\r
+  }\r
+  \r
+\r
+  \r
+}\r
index 4af0ec2..29a5fb7 100644 (file)
@@ -27,10 +27,10 @@ public class ResponseParser extends DefaultHandler {
   private String xml = null;\r
   private static Logger logger = Logger.getLogger(ResponseParser.class);\r
 \r
-  public static final List<String> docTypes = \r
-      Arrays.asList("bytarget","termlist","show","stat","record","search","init");\r
+  public static List<String> docTypes = Arrays.asList(  "bytarget","termlist","show","stat","record","search","init",\r
+                                        /* SP extras */ "response" );                                        \r
   \r
-  public ResponseParser() {    \r
+  public ResponseParser() {\r
     try {\r
       initSax();\r
     } catch (ParserConfigurationException e) {\r
@@ -66,13 +66,10 @@ public class ResponseParser extends DefaultHandler {
     try {      \r
       xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))));\r
     } catch (UnsupportedEncodingException e) {\r
-      // TODO Auto-generated catch block\r
-      e.printStackTrace();      \r
+      e.printStackTrace(); \r
     } catch (IOException e) {\r
-      // TODO Auto-generated catch block\r
       e.printStackTrace();\r
     } catch (SAXException e) {\r
-      // TODO Auto-generated catch block\r
       e.printStackTrace();      \r
     }\r
     return result;\r
@@ -118,7 +115,9 @@ public class ResponseParser extends DefaultHandler {
     } else if (localName.equals("applicationerror")) {\r
       currentElement = new CommandError();\r
     } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) {\r
-      currentElement = new Pazpar2Error();     \r
+      currentElement = new Pazpar2Error(); \r
+    } else if (localName.equals("response")) {  // Note, document element not named 'auth'\r
+      currentElement = new AuthResponse();\r
     } else {\r
       currentElement = new ResponseDataObject();\r
     }\r
index a442805..8e4078d 100644 (file)
@@ -16,7 +16,6 @@ import org.apache.log4j.Logger;
 \r
 import com.indexdata.mkjsf.errors.ErrorHelper;\r
 import com.indexdata.mkjsf.errors.ErrorInterface;\r
-import com.indexdata.mkjsf.pazpar2.data.sp.SpResponses;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
 @Named("pzresp") @SessionScoped\r
@@ -89,12 +88,16 @@ public class Responses implements Serializable {
     dataObjects = new ConcurrentHashMap<String,ResponseDataObject>();    \r
     resetSearchResponses();\r
     dataObjects.put("init", new InitResponse());\r
+    dataObjects.put("auth", new AuthResponse());\r
   }\r
   \r
-  public InitResponse getInit () {\r
-    logger.info("Request to show init response from " + Utils.objectId(this));\r
+  public InitResponse getInit () {    \r
     return ((InitResponse) dataObjects.get("init"));\r
   }\r
+  \r
+  public AuthResponse getAuth () {\r
+    return ((AuthResponse) dataObjects.get("auth"));\r
+  }\r
 \r
   public ShowResponse getShow () {\r
     return ((ShowResponse) dataObjects.get("show"));\r
@@ -147,11 +150,6 @@ public class Responses implements Serializable {
     }\r
   }\r
 \r
-  public SpResponses getSp() {\r
-    // TODO:\r
-    return null;\r
-  }\r
-\r
   public void download(String commandName) throws UnsupportedEncodingException, IOException {\r
     logger.info(Utils.objectId(this) + " got a download request for "\r
         + commandName);\r
index dc2d2e1..2551c3b 100644 (file)
@@ -14,6 +14,8 @@ import com.indexdata.mkjsf.pazpar2.commands.SettingsCommand;
 import com.indexdata.mkjsf.pazpar2.commands.ShowCommand;\r
 import com.indexdata.mkjsf.pazpar2.commands.StatCommand;\r
 import com.indexdata.mkjsf.pazpar2.commands.TermlistCommand;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommands;\r
 \r
 /**\r
  * Holds a 'pazpar2 state', understood as a full set of pazpar2 commands and \r
@@ -36,7 +38,9 @@ public class Pazpar2State {
     commands.put(Pazpar2Commands.SHOW,     new ShowCommand(mgr));\r
     commands.put(Pazpar2Commands.RECORD,   new RecordCommand(mgr));\r
     commands.put(Pazpar2Commands.TERMLIST, new TermlistCommand(mgr));\r
-    commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr));    \r
+    commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr));  \r
+\r
+    commands.put(ServiceProxyCommands.AUTH, new AuthCommand(mgr));\r
     key = "#1";\r
   }\r
     \r
index c7661e0..7106f6d 100644 (file)
@@ -21,7 +21,7 @@ public class StateManager implements Serializable {
 \r
   Map<String, Pazpar2State> states = new HashMap<String, Pazpar2State>();\r
   String currentKey = "";\r
-  private static List<String> allCommands = new ArrayList<String>(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget"));\r
+  private static List<String> allCommands = new ArrayList<String>(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget","auth"));\r
   Map<String,Boolean> pendingStateChanges = new HashMap<String,Boolean>();\r
   private static Logger logger = Logger.getLogger(StateManager.class);\r
   private List<StateListener> listeners = new ArrayList<StateListener>();\r