Cleans up auth logic
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / Pz2Bean.java
index 4c6d8a0..1c825a8 100644 (file)
@@ -30,7 +30,6 @@ import com.indexdata.mkjsf.pazpar2.data.RecordResponse;
 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
 import com.indexdata.mkjsf.pazpar2.data.Responses;\r
-import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.pazpar2.state.StateListener;\r
 import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
 import com.indexdata.mkjsf.utils.Utils;\r
@@ -60,8 +59,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   @Inject StateManager stateMgr;\r
   @Inject Pazpar2Commands pzreq;\r
   @Inject Responses pzresp;\r
-  @Inject ErrorCentral errors;\r
-  @Inject ServiceProxyUser user;\r
+  @Inject ErrorCentral errors;  \r
   \r
   protected ResultsPager pager = null; \r
   \r
@@ -97,7 +95,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       errors.addConfigurationError(new ConfigurationError("Search Client","Configuration",e.getMessage()));                \r
     } \r
     logger.info(configReader.document());\r
-    pzresp.resetAllSessionData();    \r
+    pzresp.getSp().resetAuthAndBeyond(true);    \r
   }\r
   \r
   public void resetSearchAndRecordCommands () {\r
@@ -118,9 +116,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       errors.addConfigurationError(new ConfigurationError("No client defined","Client is null","No search client defined. A client must be pre-configured or selected runtime, prior to searching."));\r
     } else {\r
       stateMgr.hasPendingStateChange("search",false);\r
-      pzresp.resetSearchResponses();\r
-      // resets some record and show command parameters without \r
-      // changing state or creating state change feedback\r
+      pzresp.resetSearchAndBeyond();\r
       pzreq.getRecord().removeParametersInState();        \r
       pzreq.getShow().setParameterInState(new CommandParameter("start","=",0));    \r
       logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("search").getUrlEncodedParameterValue("query"));\r
@@ -160,7 +156,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       logger.error("Ignoring show,stat,termlist,bytarget commands due to problem with most recent search.");\r
       return "";\r
     } else if (!hasQuery()) {\r
-      logger.error("Ignoring show,stat,termlist,bytarget commands because there is not yet a query.");\r
+      logger.debug("Ignoring show,stat,termlist,bytarget commands because there is not yet a query.");\r
       return "";\r
     } else {\r
       return update("show,stat,termlist,bytarget");\r
@@ -171,17 +167,19 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
    * Refreshes the data objects listed in 'commands' from pazpar2\r
    * \r
    * @param commands\r
-   * @return Number of activeclients at the time of the 'show' command\r
+   * @return Number of activeclients at the time of the 'show' command,\r
+   *         or 'new' if search was just initiated.\r
    */\r
   public String update (String commands) {\r
-    logger.info("Request to update: " + commands);\r
+    logger.debug("Request to update: " + commands);\r
     try {\r
       if (commands.equals("search")) {\r
         doSearch();\r
-        return "";\r
+        return "new";\r
       } else if (commands.equals("record")) {\r
         return doRecord();\r
       } else if (pzresp.getSearch().isNew()) {\r
+        // For returning notification of 'search started' quickly to UI\r
         logger.info("New search. Marking it old, then returning 'new' to trigger another round-trip.");\r
         pzresp.getSearch().setIsNew(false);\r
         return "new";\r
@@ -195,7 +193,6 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
           return "0";\r
         } else {\r
           logger.debug("Processing request for " + commands); \r
-          \r
           List<CommandThread> threadList = new ArrayList<CommandThread>();\r
           StringTokenizer tokens = new StringTokenizer(commands,",");\r
           while (tokens.hasMoreElements()) {          \r
@@ -311,48 +308,53 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   }\r
   \r
   /**\r
-   * Validates the request then executes the command and parses the response.\r
+   * Executes the command and parses the response to create data objects.\r
    * If the parsed response is of a known type it will be cached in 'pzresp'\r
    * \r
    * @param commandName The command to be executed\r
    * @return An XML response parsed to form a response data object\r
    */\r
   protected ResponseDataObject doCommand(String commandName) {\r
-    ResponseDataObject responseObject = null;     \r
-    logger.info("Request "+commandName + ": "+ pzreq.getCommand("search").toString());\r
-    Pazpar2Command command = pzreq.getCommand(commandName);\r
-    long start = System.currentTimeMillis();\r
-    HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
-    long end = System.currentTimeMillis();\r
-    logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
-    responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
-    if (commandResponse.getContentType().contains("xml")) {\r
-      responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
-      if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
-        logger.debug("Storing " + responseObject.getType() + " in pzresp. ");\r
-        pzresp.put(commandName, responseObject);\r
+    if (pzreq.getCommand(commandName).spOnly() && isPazpar2Service()) {\r
+      logger.warn("Skipping " + commandName + " - SP-only command, un-supported by Pazpar2");\r
+      return new ResponseDataObject();\r
+    } else {\r
+      ResponseDataObject responseObject = null;     \r
+      logger.info("Request "+commandName + ": "+ pzreq.getCommand("search").toString());\r
+      Pazpar2Command command = pzreq.getCommand(commandName);\r
+      long start = System.currentTimeMillis();\r
+      HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
+      long end = System.currentTimeMillis();\r
+      logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+      responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
+      if (commandResponse.getContentType().contains("xml")) {\r
+        responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
+        if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+          logger.debug("Storing " + responseObject.getType() + " in pzresp. ");\r
+          pzresp.put(commandName, responseObject);\r
+        } else {\r
+          if (commandName.equals("record")) {\r
+            logger.debug("Command was 'record' but response not '<record>' - assuming raw record response.");\r
+            ResponseDataObject recordResponse = new RecordResponse(); \r
+            recordResponse.setType("record");\r
+            recordResponse.setXml(responseObject.getXml());          \r
+            recordResponse.setAttribute("activeclients", "0");\r
+            pzresp.put("record", recordResponse); \r
+          }        \r
+        }\r
+      } else if (commandResponse.isBinary()) {\r
+        responseObject = new RecordResponse(); \r
+        responseObject.setType(commandName);\r
+        logger.info("Binary response");\r
+        responseObject.setAttribute("activeclients", "0");\r
+        responseObject.setXml("<record>binary response</record>");\r
+        responseObject.setBinary(commandResponse.getBytes());\r
+        pzresp.put("record", responseObject);\r
       } else {\r
-        if (commandName.equals("record")) {\r
-          logger.debug("Command was 'record' but response not '<record>' - assuming raw record response.");\r
-          ResponseDataObject recordResponse = new RecordResponse(); \r
-          recordResponse.setType("record");\r
-          recordResponse.setXml(responseObject.getXml());          \r
-          recordResponse.setAttribute("activeclients", "0");\r
-          pzresp.put("record", recordResponse); \r
-        }        \r
+        logger.error("Response was not found to be XML or binary. The response was not handled.");\r
       }\r
-    } else if (commandResponse.isBinary()) {\r
-      responseObject = new RecordResponse(); \r
-      responseObject.setType(commandName);\r
-      logger.info("Binary response");\r
-      responseObject.setAttribute("activeclients", "0");\r
-      responseObject.setXml("<record>binary response</record>");\r
-      responseObject.setBinary(commandResponse.getBytes());\r
-      pzresp.put("record", responseObject);\r
-    } else {\r
-      logger.error("Response was not found to be XML or binary. The response was not handled.");\r
+      return responseObject;\r
     }\r
-    return responseObject;\r
   }\r
     \r
   @Override\r
@@ -398,8 +400,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     if (url!=null && searchClient != null && !url.equals(searchClient.getServiceUrl())) {\r
       pzreq.getRecord().removeParametersInState();\r
       pzreq.getSearch().removeParametersInState();\r
-      pzresp.resetAllSessionData();\r
-      user.clear();\r
+      pzresp.getSp().resetAuthAndBeyond(true);      \r
       searchClient.setServiceUrl(url);\r
     }    \r
   }\r
@@ -408,6 +409,17 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     return (searchClient!=null ? searchClient.getServiceUrl() : "");\r
   }\r
   \r
+  public void setServiceId () {\r
+    pzreq.getRecord().removeParametersInState();\r
+    pzreq.getSearch().removeParametersInState();\r
+    pzresp.resetSearchAndBeyond();\r
+    pz2Client.setServiceId(pzreq.getInit().getService());\r
+  }\r
+  \r
+  public String getServiceId () {\r
+    return pzreq.getInit().getService();\r
+  }\r
+  \r
   public boolean getServiceUrlIsDefined() {\r
     return (searchClient != null && searchClient.hasServiceUrl());\r
   }\r
@@ -524,7 +536,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     if (!serviceType.equals(type)  &&\r
         !serviceType.equals(SERVICE_TYPE_TBD)) {\r
       resetSearchAndRecordCommands();\r
-      pzresp.resetAllSessionData();\r
+      pzresp.getSp().resetAuthAndBeyond(true);\r
     }\r
     serviceType = type;\r
     if (serviceType.equals(SERVICE_TYPE_PZ2)) {\r