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
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
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
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
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
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
}\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
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
return hasApplicationError() && getApplicationError().hasPazpar2Error();\r
}\r
\r
+ \r
+ \r
public void setXml(String xml) {\r
this.xml = xml; \r
}\r
}\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
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