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