Moves Auth response class to SP data package
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / Pz2Bean.java
index 9c43ddd..29d9280 100644 (file)
@@ -26,6 +26,7 @@ import com.indexdata.mkjsf.errors.ErrorHelper;
 import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommand;\r
 import com.indexdata.mkjsf.pazpar2.data.RecordResponse;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
@@ -113,6 +114,9 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   public void doSearch() {\r
     if (errors.hasConfigurationErrors()) {\r
       logger.error("Ignoring search request due to configuration errors.");\r
+    } else if (searchClient == null){\r
+      logger.error("No search client defined. A client must either be pre-configured or selected before searching.");\r
+      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
@@ -137,15 +141,8 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       logger.debug("Ignoring record request due search error.");\r
       return "";\r
     } else {\r
-      ResponseDataObject responseObject = doCommand("record");\r
-      if (pzreq.getRecord().hasParameterValue("offset") ||\r
-            pzreq.getRecord().hasParameterValue("checksum")) {\r
-        RecordResponse 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.debug("Executing record command");\r
+      doCommand("record");\r
       return pzresp.getRecord().getActiveClients();\r
     }\r
   }\r
@@ -164,7 +161,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
@@ -175,28 +172,32 @@ 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
       } else {\r
         handleQueryStateChanges(commands);\r
         if (pzresp.getSearch().hasApplicationError()) {\r
-          logger.error("The command(s) " + commands + " are cancelled because the latest search command had an error.");\r
+          logger.error("The command(s) " + commands + " cancelled because the latest search command had an error.");\r
+          return "0";\r
+        } else if (errors.hasConfigurationErrors()) {\r
+          logger.error("The command(s) " + commands + " cancelled due to configuration errors.");\r
           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
@@ -282,7 +283,19 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     pager =  new ResultsPager(pzresp,pageRange,pzreq);\r
     return pager;\r
   }\r
-    \r
+   \r
+  /**\r
+   * This methods main purpose is to support browser history.\r
+   *  \r
+   * When the browsers back or forward buttons are pressed, a  \r
+   * re-search /might/ be required - namely if the query changes.\r
+   * So, as the UI requests updates of the page (show,facets,\r
+   * etc) this method checks if a search must be executed\r
+   * before those updates are performed.\r
+   *  \r
+   * @see {@link com.indexdata.mkjsf.pazpar2.state.StateManager#setCurrentStateKey} \r
+   * @param commands\r
+   */\r
   protected void handleQueryStateChanges (String commands) {\r
     if (stateMgr.hasPendingStateChange("search") && hasQuery()) { \r
       logger.info("Triggered search: Found pending search change [" + pzreq.getCommand("search").toString() + "], doing search before updating " + commands);      \r
@@ -300,26 +313,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
-    responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
-    if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
-      pzresp.put(commandName, responseObject);\r
-    }          \r
-    return 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
+        logger.error("Response was not found to be XML or binary. The response was not handled.");\r
+      }\r
+      return responseObject;\r
+    }\r
   }\r
     \r
   @Override\r
@@ -375,6 +415,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.resetSearchResponses();\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
@@ -452,6 +503,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       serviceProxyUrls = config.getMultiProperty(SERVICE_PROXY_URL_LIST,",");\r
       pazpar2Urls = config.getMultiProperty(PAZPAR2_URL_LIST, ",");\r
     }\r
+    logger.info(reader.document());\r
     logger.info("Service Type is configured to " + serviceType);\r
     \r
   }\r