Adds support for downloading binary record response
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Mon, 20 May 2013 18:42:42 +0000 (14:42 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Mon, 20 May 2013 18:42:42 +0000 (14:42 -0400)
src/main/java/com/indexdata/mkjsf/pazpar2/ClientCommandResponse.java
src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java
src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java

index e33ce1e..01444ef 100644 (file)
@@ -45,6 +45,8 @@ public class ClientCommandResponse implements HttpResponseWrapper {
   public String getResponseString() {\r
     if (content == null) {\r
       return contentString;\r
+    } else if (isBinary()) {\r
+      return "[binary response]";\r
     } else {\r
       try {\r
         return new String(content,"UTF-8");\r
index 30ac455..4c6d8a0 100644 (file)
@@ -141,17 +141,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       return "";\r
     } else {\r
       logger.debug("Executing record command");\r
-      ResponseDataObject responseObject = doCommand("record");\r
-      if ((pzreq.getRecord().hasParameterValue("offset") ||\r
-            pzreq.getRecord().hasParameterValue("checksum")) &&\r
-            !responseObject.getType().equals("record")) {\r
-        logger.debug("Storing record offset response as 'record'");\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
+      doCommand("record");\r
       return pzresp.getRecord().getActiveClients();\r
     }\r
   }\r
@@ -336,12 +326,31 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     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
-      logger.debug("Storing " + responseObject.getType() + " in pzresp. ");\r
-      pzresp.put(commandName, responseObject);\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.info("Unrecognized response object type not cached in pzresp: " + responseObject.getType());\r
+      logger.error("Response was not found to be XML or binary. The response was not handled.");\r
     }\r
     return responseObject;\r
   }\r
index 2e6ca31..388253d 100644 (file)
@@ -9,6 +9,9 @@ import java.util.HashMap;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import javax.faces.context.ExternalContext;\r
+import javax.faces.context.FacesContext;\r
+\r
 import org.apache.log4j.Logger;\r
 \r
 import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
@@ -99,6 +102,9 @@ public class Pz2Client implements SearchClient {
       pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
       if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("xml")) {\r
         commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
+      } else if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("octet-stream")) {\r
+        commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
+        logger.info("Content type: " + commandResponse.getContentType() + ". isBinary?: " + commandResponse.isBinary());\r
       } else if (pz2HttpResponse.getStatusCode()==417) {\r
         logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8"));\r
         commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()) ,"Pazpar2: Expectation failed (417)", baos.toString("UTF-8")),"text/xml");                       \r
index 5af17a0..2db4558 100644 (file)
@@ -100,13 +100,14 @@ public class ServiceProxyClient implements SearchClient {
     byte[] response = null;\r
     try {\r
       response = client.execute(httpget, handler);\r
-      if (handler.getStatusCode()==200 && handler.getContentType().contains("xml")) {\r
+      if (handler.getStatusCode()==200 && (handler.getContentType().contains("xml") || handler.getContentType().contains("octet-stream"))) {\r
+        logger.debug("Creating command response holding content of type " + handler.getContentType());\r
         commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());\r
       } else {\r
         logger.error("Service Proxy status code: " + handler.getStatusCode());\r
         String errorXml = "";\r
         if (handler.getContentType().contains("xml")) {\r
-          errorXml = CommandError.insertErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8"));          \r
+          errorXml = CommandError.insertErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8"));        \r
         } else {\r
           if (handler.getContentType().contains("html")) {\r
             String htmlStrippedOfTags = (new String(response,"UTF-8")).replaceAll("\\<[^>]*>","");\r
index 048b75d..55aca4e 100644 (file)
@@ -62,9 +62,17 @@ public class RecordResponse extends ResponseDataObject {
   }\r
   \r
   public String getActiveClients () {\r
-    String activeclients = getOneElementValue("activeclients");\r
-    logger.info("Request to get activeclients on record [" + getRecId() + "]. Is [" + activeclients + "]");    \r
-    return activeclients;\r
+    if (getIsBinary()) {\r
+      return "0";\r
+    } else {\r
+      String activeclients = getOneElementValue("activeclients");\r
+      if (activeclients == null || activeclients.length()==0) {\r
+        // Look for override\r
+        activeclients = getAttribute("activeclients");\r
+      }\r
+      logger.info("Request to get activeclients on record [" + getRecId() + "]. Is [" + activeclients + "]");    \r
+      return activeclients;\r
+    }\r
   }\r
    \r
 }\r
index 57c17e1..3fe9803 100644 (file)
@@ -19,6 +19,8 @@ public class ResponseDataObject implements Serializable {
   String textContent = "";\r
   CommandError error = null;\r
   String xml = null;\r
+  boolean isBinary = false;\r
+  byte[] binary = null;\r
         \r
   public void setType (String type) {\r
     this.type = type;\r
@@ -110,6 +112,8 @@ public class ResponseDataObject implements Serializable {
     return hasApplicationError() && getApplicationError().hasPazpar2Error();\r
   }\r
   \r
+  \r
+  \r
   public void setXml(String xml) {\r
     this.xml = xml; \r
   }\r
@@ -122,7 +126,22 @@ public class ResponseDataObject implements Serializable {
   }\r
   \r
   public boolean getHasResults () {\r
-    return (xml != null && xml.length()>0);\r
+    return (xml != null && xml.length()>0) || (getIsBinary() && binary.length>0);\r
+  }\r
+  \r
+  public boolean getIsBinary () {\r
+    return isBinary;\r
+  }\r
+    \r
+  public void setBinary(byte[] bytes) {\r
+    isBinary = true;\r
+    binary = bytes;\r
+  }\r
+  \r
+  public byte[] getBinary () {\r
+    return binary;\r
   }\r
+  \r
+  \r
         \r
 }\r
index 5276eef..920ef6b 100644 (file)
@@ -164,12 +164,21 @@ public class Responses implements Serializable {
   public void download(String commandName) throws UnsupportedEncodingException, IOException {\r
     logger.info(Utils.objectId(this) + " got a download request for "\r
         + commandName);\r
+    ResponseDataObject object = dataObjects.get(commandName);    \r
     FacesContext facesContext = FacesContext.getCurrentInstance();\r
     ExternalContext externalContext = facesContext.getExternalContext();\r
-    externalContext.setResponseHeader("Content-Type","application/xml; charset=\"utf-8\"");\r
-    externalContext.setResponseHeader("Content-Length",String.valueOf(dataObjects.get(commandName).getXml().getBytes("UTF-8").length));\r
-    externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + commandName + ".xml\"");\r
-    externalContext.getResponseOutputStream().write(dataObjects.get(commandName).getXml().getBytes("UTF-8"));\r
+    if (object.getIsBinary()) {\r
+      externalContext.setResponseHeader("Content-Type","application/octet-stream");\r
+      externalContext.setResponseHeader("Content-Length",String.valueOf(object.getBinary().length));\r
+      externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + commandName + ".data\"");\r
+      externalContext.getResponseOutputStream().write(object.getBinary());      \r
+    } else {\r
+      externalContext.setResponseHeader("Content-Type","application/xml; charset=\"utf-8\"");\r
+      externalContext.setResponseHeader("Content-Length",String.valueOf(dataObjects.get(commandName).getXml().getBytes("UTF-8").length));\r
+      externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + commandName + ".xml\"");\r
+      externalContext.getResponseOutputStream().write(dataObjects.get(commandName).getXml().getBytes("UTF-8"));\r
+    }\r
     facesContext.responseComplete();\r
   }\r
+  \r
 }
\ No newline at end of file