Fixes bug in return of non-record XML response
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / RecordCommand.java
index 8713ece..e9c4b54 100644 (file)
@@ -1,13 +1,59 @@
 package com.indexdata.mkjsf.pazpar2.commands;\r
 \r
-import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
+import org.apache.log4j.Logger;\r
 \r
-public class RecordCommand extends Pazpar2Command {\r
+import com.indexdata.mkjsf.pazpar2.ClientCommandResponse;\r
+import com.indexdata.mkjsf.pazpar2.HttpResponseWrapper;\r
+import com.indexdata.mkjsf.pazpar2.Pz2Service;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.RecordCommandSp;\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
+\r
+public class RecordCommand extends Pazpar2Command implements ServiceProxyCommand {\r
 \r
   private static final long serialVersionUID = 2817539422114569506L;\r
+  private static Logger logger = Logger.getLogger(RecordCommand.class);\r
 \r
-  public RecordCommand(StateManager stateMgr) {\r
-    super("record",stateMgr);\r
+  public RecordCommand() {\r
+    super("record");\r
+  }\r
+  \r
+  @Override\r
+  public ResponseDataObject run() {\r
+    ResponseDataObject responseObject = null;\r
+    if (hasParameterValue("id")) {\r
+      HttpResponseWrapper commandResponse = Pz2Service.get().getSearchClient().executeCommand(this);\r
+      \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
+        } else {\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
+          responseObject = recordResponse;\r
+        }\r
+      } else if (commandResponse.isBinary()) {\r
+        responseObject = new RecordResponse();\r
+        responseObject.setType(getCommandName());\r
+        logger.info("Binary response");\r
+        responseObject.setAttribute("activeclients", "0");\r
+        responseObject.setXml("<record>binary response</record>");\r
+        responseObject.setBinary(commandResponse.getBytes());\r
+      } else {\r
+        logger.error("Response was not found to be XML or binary. The response was not handled.");\r
+      }\r
+      Pz2Service.get().getPzresp().put(getCommandName(), responseObject);\r
+    } else {\r
+      logger.debug("No record id parameter on this command. Ignoring request but clearing any previous record result.");\r
+      Pz2Service.get().getPzresp().put(getCommandName(), new RecordResponse());\r
+    }\r
+    return responseObject;\r
   }\r
   \r
   public void setId(String recId) {\r
@@ -61,13 +107,31 @@ public class RecordCommand extends Pazpar2Command {
   public void setBinary (String binary) {\r
     setParameter(new CommandParameter("binary","=",binary));\r
   }\r
+  \r
+  public String getBinary () {\r
+    return getParameterValue("binary");\r
+  }\r
 \r
   @Override\r
   public RecordCommand copy () {\r
-    RecordCommand newCommand = new RecordCommand(stateMgr);\r
+    RecordCommand newCommand = new RecordCommand();\r
     for (String parameterName : parameters.keySet()) {\r
       newCommand.setParameterInState(parameters.get(parameterName).copy());      \r
     }    \r
     return newCommand;\r
   }\r
+  \r
+  \r
+  /**\r
+   * Returns a record command object with Service Proxy extension parameters \r
+   * \r
+   */\r
+  public RecordCommandSp getSp () {\r
+    return new RecordCommandSp(this);\r
+  }\r
+\r
+  @Override\r
+  public boolean spOnly() {    \r
+    return false;\r
+  }\r
 }\r