From 2816078831605dbc1c18147760e1212dd30909a8 Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Sat, 27 Apr 2013 19:56:09 -0400 Subject: [PATCH] Adds support for offset and binary parameters Thus support for handling unknow document types. Still lacks option to show binary response. --- .../java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java | 31 +++++++++++++++----- .../mkjsf/pazpar2/commands/CommandParameter.java | 4 +++ .../mkjsf/pazpar2/commands/Pazpar2Command.java | 12 +++++--- .../mkjsf/pazpar2/commands/RecordCommand.java | 4 +++ .../mkjsf/pazpar2/commands/SearchCommand.java | 2 +- .../mkjsf/pazpar2/data/Pazpar2ResponseParser.java | 13 ++++++-- .../mkjsf/pazpar2/data/Pazpar2Responses.java | 8 +++++ .../mkjsf/pazpar2/data/RecordOffsetResponse.java | 8 +++++ .../mkjsf/pazpar2/data/RecordResponse.java | 4 +-- 9 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordOffsetResponse.java diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java index 98f4c71..f5a0675 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java @@ -110,9 +110,10 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { * @return Number of activeclients at the time of the 'show' command */ public String update (String commands) { + try { if (! errors.hasConfigurationErrors()) { if (commandsAreValid(commands)) { - if (hasQuery() || (commands.equals("record") && pzreq.getCommand("record").hasParameterSet("recordquery"))) { + if (hasQuery() || (commands.equals("record") && pzreq.getCommand("record").hasParameterValue("recordquery"))) { handleQueryStateChanges(commands); logger.debug("Processing request for " + commands); List threadList = new ArrayList(); @@ -135,10 +136,19 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { String response = thread.getResponse(); responseLogger.debug("Response was: " + response); Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response); - pzresp.put(commandName, responseObject); + if (Pazpar2ResponseParser.docTypes.contains(responseObject.getType())) { + pzresp.put(commandName, responseObject); + } else { + if (commandName.equals("record") && pzreq.getRecord().hasParameterValue("offset")) { + RecordResponse recordResponse = new RecordResponse(); + recordResponse.setType("record"); + recordResponse.setXml(responseObject.getXml()); + recordResponse.setAttribute("activeclients", "0"); + pzresp.put(commandName, recordResponse); + } + } } - if (commands.equals("record")) { - logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients()); + if (commands.equals("record")) { return pzresp.getRecord().getActiveClients(); } else { return pzresp.getActiveClients(); @@ -156,12 +166,19 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { logger.error("Did not attempt to execute query since there are configuration errors."); return "0"; } + } catch (ClassCastException cce) { + cce.printStackTrace(); + return ""; + } catch (NullPointerException npe) { + npe.printStackTrace(); + return ""; + } } public boolean commandsAreValid(String commands) { if (commands.equals("record")) { - if (!pzreq.getCommand("record").hasParameterSet("id")) { + if (!pzreq.getCommand("record").hasParameterValue("id")) { logger.error("Attempt to send record command without the id parameter"); return false; } @@ -194,7 +211,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { } protected boolean hasQuery() { - return pzreq.getCommand("search").hasParameterSet("query"); + return pzreq.getCommand("search").hasParameterValue("query"); } @@ -220,7 +237,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) { logger.debug("Found pending record ID change. Doing record before updating " + commands); stateMgr.hasPendingStateChange("record",false); - if (pzreq.getCommand("record").hasParameterSet("id")) { + if (pzreq.getCommand("record").hasParameterValue("id")) { update("record"); } else { pzresp.put("record", new RecordResponse()); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java index f97497f..2f0aa03 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java @@ -75,6 +75,10 @@ public class CommandParameter implements Serializable { return operator != null; } + public boolean hasValue() { + return value != null && value.length()>0; + } + public String getEncodedQueryString () { try { return name + operator + URLEncoder.encode(getValueWithExpressions(),"UTF-8"); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java index 871eb30..8176512 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java @@ -83,14 +83,16 @@ public abstract class Pazpar2Command implements Serializable { return (parameters.keySet().size()>0); } - public boolean hasParameterSet(String parameterName) { - return (parameters.get(parameterName) != null); + public boolean hasParameterValue(String parameterName) { + return (parameters.get(parameterName) != null && parameters.get(parameterName).hasValue()); } public String getEncodedQueryString () { StringBuilder queryString = new StringBuilder("command="+name); for (CommandParameter parameter : parameters.values()) { - queryString.append("&"+parameter.getEncodedQueryString()); + if (parameter.hasValue()) { + queryString.append("&"+parameter.getEncodedQueryString()); + } } return queryString.toString(); } @@ -98,7 +100,9 @@ public abstract class Pazpar2Command implements Serializable { public String getValueWithExpressions() { StringBuilder value = new StringBuilder(""); for (CommandParameter parameter : parameters.values()) { - value.append("&" + parameter.getName() + parameter.operator + parameter.getValueWithExpressions()); + if (parameter.hasValue()) { + value.append("&" + parameter.getName() + parameter.operator + parameter.getValueWithExpressions()); + } } return value.toString(); } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/RecordCommand.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/RecordCommand.java index 028b0d5..93f7782 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/RecordCommand.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/RecordCommand.java @@ -63,6 +63,10 @@ public class RecordCommand extends Pazpar2Command implements ServiceProxyCommand public void setBinary (String binary) { setParameter(new CommandParameter("binary","=",binary)); } + + public String getBinary () { + return getParameterValue("binary"); + } @Override public RecordCommand copy () { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/SearchCommand.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/SearchCommand.java index cae510e..4dcb108 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/SearchCommand.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/SearchCommand.java @@ -36,7 +36,7 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand public void addFilter(String filterExpression) { // TODO: implement - if (hasParameterSet("filter")) { + if (hasParameterValue("filter")) { setFilter(filterExpression); } else { getParameter("filter"); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2ResponseParser.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2ResponseParser.java index 7cf41df..f9f607b 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2ResponseParser.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2ResponseParser.java @@ -11,6 +11,7 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import org.apache.log4j.Logger; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -37,8 +38,9 @@ public class Pazpar2ResponseParser extends DefaultHandler { private Stack dataElements = new Stack(); private Pazpar2ResponseData result = null; private String xml = null; + private static Logger logger = Logger.getLogger(Pazpar2ResponseParser.class); - private static final List docTypes = + public static final List docTypes = Arrays.asList("bytarget","termlist","show","stat","record","search"); public Pazpar2ResponseParser() { @@ -134,9 +136,14 @@ public class Pazpar2ResponseParser extends DefaultHandler { currentElement.setType(localName); for (int i=0; i< atts.getLength(); i++) { currentElement.setAttribute(atts.getLocalName(i), atts.getValue(i)); - } + } if (!docTypes.contains(localName)) { - dataElements.peek().addElement(localName, currentElement); + if (dataElements.size() == 0) { + logger.info("Encountered unknown top level element [" + localName + "]"); + currentElement.setType(localName); + } else { + dataElements.peek().addElement(localName, currentElement); + } } if (this.xml != null) { // Store XML for doc level elements currentElement.setXml(xml); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2Responses.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2Responses.java index b3748f8..09e96d5 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2Responses.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2Responses.java @@ -88,6 +88,10 @@ public class Pazpar2Responses implements Serializable { } public RecordResponse getRecord() { + logger.info("Request to get record response."); + logger.info("Has record response: [" + ((dataObjects.get("record")!=null) ? "true" : "false") + "]"); + logger.info("Its of type record: [" + ((dataObjects.get("record") instanceof RecordResponse) ? "true" : "false") +"]"); + logger.info("It has xml: [" + ((dataObjects.get("record").getXml()!=null) ? "true" : "false") + "]"); return ((RecordResponse) dataObjects.get("record")); } @@ -111,6 +115,10 @@ public class Pazpar2Responses implements Serializable { return ((ByTarget) dataObjects.get("bytarget")); } + public Pazpar2ResponseData getResponseObject (String name) { + return dataObjects.get(name); + } + public boolean hasRecords () { return getStat().getRecords() > 0 && getShow().getHits() != null diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordOffsetResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordOffsetResponse.java new file mode 100644 index 0000000..8e4ab25 --- /dev/null +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordOffsetResponse.java @@ -0,0 +1,8 @@ +package com.indexdata.mkjsf.pazpar2.data; + +public class RecordOffsetResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = -601620728296476450L; + + +} 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 0ba7988..fa5c9c8 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java @@ -56,8 +56,8 @@ public class RecordResponse extends Pazpar2ResponseData { } public String getActiveClients () { + logger.info("Request to get activeclients"); return getOneElementValue("activeclients"); } - - + } -- 1.7.10.4