import java.util.List;\r
\r
import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2Responses;\r
+import com.indexdata.mkjsf.pazpar2.data.Responses;\r
\r
public class ResultsPager implements Serializable {\r
\r
private static final long serialVersionUID = 8854795222615583071L;\r
- private Pazpar2Responses data = null;\r
+ private Responses data = null;\r
private int pageRangeLength = 13;\r
private Pazpar2Commands req;\r
\r
- public ResultsPager(Pazpar2Responses data) {\r
+ public ResultsPager(Responses data) {\r
this.data = data; \r
}\r
\r
- public ResultsPager(Pazpar2Responses data, int pageRange, Pazpar2Commands req) {\r
+ public ResultsPager(Responses data, int pageRange, Pazpar2Commands req) {\r
this.data = data;\r
this.pageRangeLength = pageRange;\r
this.req = req;\r
import org.apache.log4j.Logger;\r
\r
import com.indexdata.mkjsf.config.ConfigurationReader;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2Responses;\r
+import com.indexdata.mkjsf.pazpar2.data.Responses;\r
\r
@Named("errors") @SessionScoped\r
public class ErrorCentral implements Serializable {\r
private static Logger logger = Logger.getLogger(ErrorCentral.class); \r
private ErrorHelper errorHelper = null;\r
\r
- @Inject Pazpar2Responses pzresp;\r
+ @Inject Responses pzresp;\r
@Inject ConfigurationReader configurator;\r
\r
private List<ErrorInterface> configurationErrors = new ArrayList<ErrorInterface>();\r
import com.indexdata.mkjsf.errors.ErrorHelper;\r
import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseParser;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2Responses;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
+import com.indexdata.mkjsf.pazpar2.data.Responses;\r
import com.indexdata.mkjsf.pazpar2.data.RecordResponse;\r
import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
import com.indexdata.mkjsf.pazpar2.state.StateListener;\r
@Inject ConfigurationReader configurator;\r
@Inject StateManager stateMgr;\r
@Inject Pazpar2Commands pzreq;\r
- @Inject Pazpar2Responses pzresp;\r
+ @Inject Responses pzresp;\r
@Inject ErrorCentral errors;\r
@Inject ServiceProxyUser user;\r
\r
String commandName = thread.getCommand().getCommandName();\r
HttpResponseWrapper response = thread.getCommandResponse();\r
responseLogger.debug("Response was: " + response.getResponseString());\r
- Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response.getResponseString());\r
- if (Pazpar2ResponseParser.docTypes.contains(responseObject.getType())) {\r
+ ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response.getResponseString());\r
+ if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
pzresp.put(commandName, responseObject);\r
} else {\r
if (commandName.equals("record") && \r
import com.indexdata.mkjsf.config.ConfigurationReader;\r
import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;\r
import com.indexdata.mkjsf.pazpar2.commands.sp.InitDocUpload;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2Responses;\r
+import com.indexdata.mkjsf.pazpar2.data.Responses;\r
import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
\r
@Named("pz2x") @SessionScoped\r
@Inject ServiceProxyUser user; \r
@Inject Pz2Bean pz2;\r
@Inject Pazpar2Commands pzreq;\r
- @Inject Pazpar2Responses pzresp;\r
+ @Inject Responses pzresp;\r
\r
\r
public ServiceProxyExtensions() {\r
import java.util.ArrayList;\r
import java.util.List;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
import com.indexdata.mkjsf.pazpar2.data.Target;\r
\r
-public class ByTarget extends Pazpar2ResponseData {\r
+public class ByTarget extends ResponseDataObject {\r
\r
private static final long serialVersionUID = 3960644950805644518L;\r
\r
public List<Target> getTargets() {\r
List<Target> targets = new ArrayList<Target>();\r
if (getElements("target") != null) {\r
- for (Pazpar2ResponseData element : getElements("target")) {\r
+ for (ResponseDataObject element : getElements("target")) {\r
targets.add((Target)element);\r
}\r
}\r
* in a new 'applicationerror' element which in turn will be embedded in a\r
* command XML (i.e. a 'search' or a 'show' response XML) \r
* \r
- * The command response XML is subsequently parsed by Pazpar2ResponseParser, \r
+ * The command response XML is subsequently parsed by ResponseParser, \r
* which will then create the CommandError object.\r
* \r
* @author Niels Erik\r
*\r
*/\r
-public class CommandError extends Pazpar2ResponseData implements ErrorInterface {\r
+public class CommandError extends ResponseDataObject implements ErrorInterface {\r
\r
private static final long serialVersionUID = 8878776025779714122L;\r
private static Pattern xmlDeclaration = Pattern.compile("<\\?xml.*\\?>");\r
\r
/**\r
* Creates an XML string error message, embedded in an XML string document named by the command\r
- * This is the XML that Pazpar2ResponseParser will turn into a CommandError object. \r
+ * This is the XML that ResponseParser will turn into a CommandError object. \r
* @param commandName\r
* @param exceptionName\r
* @param errorMessage\r
/**\r
* Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of\r
* a command response document (like 'search' or 'show').\r
- * This is the XML that Pazpar2ResponseParser will turn into a CommandError object.\r
+ * This is the XML that ResponseParser will turn into a CommandError object.\r
* \r
* \r
* @param commandName The name of the command during which's execution the error was encountered\r
import java.util.List;\r
\r
import com.indexdata.mkjsf.pazpar2.data.Location;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
-public class Hit extends Pazpar2ResponseData {\r
+public class Hit extends ResponseDataObject {\r
\r
\r
private static final long serialVersionUID = 9039281987691623220L;\r
\r
public List<Location> getLocations() {\r
List<Location> locations = new ArrayList<Location>();\r
- for (Pazpar2ResponseData element : getElements("location")) {\r
+ for (ResponseDataObject element : getElements("location")) {\r
locations.add((Location)element);\r
}\r
return locations;\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-public class InitResponse extends Pazpar2ResponseData {\r
+public class InitResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -1479775157276901600L;\r
\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
\r
-public class Location extends Pazpar2ResponseData {\r
+public class Location extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -1386527442857478225L;\r
private int seqno = -1;\r
\r
public String getSubjects() {\r
StringBuilder builder = new StringBuilder("");\r
- for (Pazpar2ResponseData data : getElements("md-subject")) {\r
+ for (ResponseDataObject data : getElements("md-subject")) {\r
if (builder.length()==0) {\r
builder.append(data.getValue());\r
} else {\r
public String getAuthors() {\r
StringBuilder builder = new StringBuilder("");\r
if (getElements("md-author") != null) {\r
- for (Pazpar2ResponseData data : getElements("md-author")) {\r
+ for (ResponseDataObject data : getElements("md-author")) {\r
if (builder.length()==0) {\r
builder.append(data.getValue());\r
} else {\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-public class Pazpar2Error extends Pazpar2ResponseData {\r
+public class Pazpar2Error extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -7060267782024414318L;\r
\r
+++ /dev/null
-package com.indexdata.mkjsf.pazpar2.data;\r
-\r
-import java.io.Serializable;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
-\r
-public class Pazpar2ResponseData implements Serializable {\r
-\r
- Logger logger = Logger.getLogger(Pazpar2ResponseData.class);\r
- private static final long serialVersionUID = -3909755656714679959L;\r
- String type = null;\r
- HashMap<String,String> attributes = new HashMap<String,String>();\r
- HashMap<String,List<Pazpar2ResponseData>> elements = new HashMap<String,List<Pazpar2ResponseData>>();\r
- String textContent = "";\r
- CommandError error = null;\r
- String xml = null;\r
- \r
- public void setType (String type) {\r
- this.type = type;\r
- }\r
- \r
- public String getType () {\r
- return type;\r
- }\r
- \r
- public void setAttribute (String name, String value) {\r
- attributes.put(name, value);\r
- }\r
- \r
- public String getAttribute (String name) {\r
- return attributes.get(name);\r
- }\r
- \r
- public void addElement (String name, Pazpar2ResponseData value) { \r
- if (elements.containsKey(name)) {\r
- elements.get(name).add(value);\r
- } else {\r
- List<Pazpar2ResponseData> list = new ArrayList<Pazpar2ResponseData>();\r
- list.add(value);\r
- elements.put(name,list);\r
- }\r
- }\r
- \r
- public List<Pazpar2ResponseData> getElements (String name) {\r
- return elements.get(name);\r
- }\r
- \r
- public Pazpar2ResponseData getOneElement (String name) {\r
- if (elements.get(name) != null) {\r
- return elements.get(name).get(0);\r
- } else {\r
- return null;\r
- }\r
- }\r
- \r
- /**\r
- * Returns the text content of the first element found with the given\r
- * name\r
- * @param name of the element \r
- * @return text value, empty string if none found\r
- */\r
- public String getOneElementValue (String name) {\r
- if (getOneElement(name)!=null && getOneElement(name).getValue().length()>0) {\r
- return getOneElement(name).getValue();\r
- } else {\r
- return "";\r
- }\r
- }\r
- \r
- public void appendContent (String content) {\r
- textContent = textContent + content;\r
- }\r
- \r
- public String getValue () {\r
- return textContent;\r
- }\r
- \r
- public String getProperty(String name) {\r
- List<Pazpar2ResponseData> els = elements.get(name);\r
- if (els != null) {\r
- return els.get(0).getValue();\r
- } else { \r
- return null;\r
- }\r
- }\r
- \r
- public int getIntValue(String name) {\r
- String val = getOneElementValue(name);\r
- if (val.length()==0) {\r
- return 0;\r
- } else {\r
- return Integer.parseInt(val);\r
- }\r
- }\r
- \r
- public boolean hasApplicationError () {\r
- return (getOneElement("applicationerror") != null); \r
- }\r
- \r
- public CommandError getApplicationError() {\r
- return (CommandError) getOneElement("applicationerror");\r
- }\r
- \r
- public boolean hasPazpar2Error() {\r
- return hasApplicationError() && getApplicationError().hasPazpar2Error();\r
- }\r
- \r
- public void setXml(String xml) {\r
- this.xml = xml; \r
- }\r
- \r
- public String getXml() {\r
- if (type != null && type.equals("record")) {\r
- logger.debug("Getting XML for "+type + ": "+xml);\r
- } \r
- return xml == null ? "" : xml;\r
- }\r
- \r
-}\r
+++ /dev/null
-package com.indexdata.mkjsf.pazpar2.data;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.IOException;\r
-import java.io.UnsupportedEncodingException;\r
-import java.util.Arrays;\r
-import java.util.List;\r
-import java.util.Stack;\r
-\r
-import javax.xml.parsers.ParserConfigurationException;\r
-import javax.xml.parsers.SAXParser;\r
-import javax.xml.parsers.SAXParserFactory;\r
-\r
-import org.apache.log4j.Logger;\r
-import org.xml.sax.Attributes;\r
-import org.xml.sax.InputSource;\r
-import org.xml.sax.SAXException;\r
-import org.xml.sax.XMLReader;\r
-import org.xml.sax.helpers.DefaultHandler;\r
-\r
-import com.indexdata.mkjsf.pazpar2.data.ByTarget;\r
-import com.indexdata.mkjsf.pazpar2.data.Hit;\r
-import com.indexdata.mkjsf.pazpar2.data.Location;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
-import com.indexdata.mkjsf.pazpar2.data.RecordResponse;\r
-import com.indexdata.mkjsf.pazpar2.data.ShowResponse;\r
-import com.indexdata.mkjsf.pazpar2.data.StatResponse;\r
-import com.indexdata.mkjsf.pazpar2.data.Target;\r
-import com.indexdata.mkjsf.pazpar2.data.TermListResponse;\r
-import com.indexdata.mkjsf.pazpar2.data.TermListsResponse;\r
-import com.indexdata.mkjsf.pazpar2.data.TermResponse;\r
-import com.indexdata.mkjsf.pazpar2.data.TermXTargetResponse;\r
-\r
-public class Pazpar2ResponseParser extends DefaultHandler {\r
-\r
- private XMLReader xmlReader = null;\r
- private Pazpar2ResponseData currentElement = null;\r
- private Stack<Pazpar2ResponseData> dataElements = new Stack<Pazpar2ResponseData>();\r
- private Pazpar2ResponseData result = null;\r
- private String xml = null;\r
- private static Logger logger = Logger.getLogger(Pazpar2ResponseParser.class);\r
-\r
- public static final List<String> docTypes = \r
- Arrays.asList("bytarget","termlist","show","stat","record","search");\r
- \r
- public Pazpar2ResponseParser() { \r
- try {\r
- initSax();\r
- } catch (ParserConfigurationException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- } catch (SAXException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- public static Pazpar2ResponseParser getParser() {\r
- return new Pazpar2ResponseParser();\r
- }\r
- \r
- private void initSax() throws ParserConfigurationException, SAXException {\r
- SAXParserFactory spf = SAXParserFactory.newInstance();\r
- spf.setNamespaceAware(true);\r
- SAXParser saxParser = spf.newSAXParser();\r
- xmlReader = saxParser.getXMLReader();\r
- xmlReader.setContentHandler(this); \r
- }\r
- \r
- /**\r
- * Parses a Pazpar2 XML response -- or an error response as XML -- and produces a \r
- * Pazpar2ResponseData object, i.e. a 'show' object\r
- * \r
- * @param response XML response string from Pazpar2\r
- * @return Response data object\r
- */\r
- public Pazpar2ResponseData getDataObject (String response) {\r
- this.xml = response;\r
- try { \r
- xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))));\r
- } catch (UnsupportedEncodingException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace(); \r
- } catch (IOException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- } catch (SAXException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace(); \r
- }\r
- return result;\r
- }\r
-\r
- /** \r
- * Receive notification at the start of element \r
- * \r
- */\r
- @Override\r
- public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {\r
- if (localName.equals("show")) {\r
- currentElement = new ShowResponse(); \r
- } else if (localName.equals("hit")) {\r
- currentElement = new Hit();\r
- } else if (localName.equals("location")) {\r
- currentElement = new Location();\r
- } else if (localName.equals("bytarget")) {\r
- currentElement = new ByTarget(); \r
- } else if (localName.equals("target")) {\r
- currentElement = new Target();\r
- } else if (localName.equals("stat")) {\r
- currentElement = new StatResponse(); \r
- } else if (localName.equals("termlist")) {\r
- currentElement = new TermListsResponse(); \r
- } else if (localName.equals("list")) {\r
- currentElement = new TermListResponse();\r
- ((TermListResponse)currentElement).setName(atts.getValue("name"));\r
- ((TermListsResponse)dataElements.peek()).addTermList((TermListResponse)currentElement);\r
- } else if (localName.equals("term")) {\r
- if (dataElements.peek().getAttribute("name").equals("xtargets")) {\r
- currentElement = new TermXTargetResponse(); \r
- } else {\r
- currentElement = new TermResponse();\r
- }\r
- ((TermListResponse)dataElements.peek()).addTerm((TermResponse)currentElement);\r
- } else if (localName.equals("record")) {\r
- currentElement = new RecordResponse(); \r
- } else if (localName.equals("search")) {\r
- currentElement = new SearchResponse();\r
- } else if (localName.equals("applicationerror")) {\r
- currentElement = new CommandError();\r
- } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) {\r
- currentElement = new Pazpar2Error(); \r
- } else {\r
- currentElement = new Pazpar2ResponseData();\r
- }\r
- currentElement.setType(localName);\r
- for (int i=0; i< atts.getLength(); i++) {\r
- currentElement.setAttribute(atts.getLocalName(i), atts.getValue(i));\r
- } \r
- if (!docTypes.contains(localName)) {\r
- if (dataElements.size() == 0) {\r
- logger.info("Encountered unknown top level element [" + localName + "]. Creating generic data object.");\r
- currentElement.setType(localName);\r
- } else {\r
- dataElements.peek().addElement(localName, currentElement);\r
- }\r
- }\r
- if (this.xml != null) { // Store XML for doc level elements\r
- currentElement.setXml(xml);\r
- xml = null;\r
- }\r
- dataElements.push(currentElement); \r
- }\r
- \r
- @Override\r
- public void characters(char[] ch, int start, int length) throws SAXException {\r
- String data = new String(ch, start, length); \r
- dataElements.peek().appendContent(data); \r
- }\r
- \r
- @Override\r
- public void endElement(String namespaceURI, String localName, String qName) throws SAXException {\r
- if (dataElements.size()==1) {\r
- result = dataElements.pop();\r
- } else {\r
- dataElements.pop();\r
- }\r
- }\r
-}\r
+++ /dev/null
-package com.indexdata.mkjsf.pazpar2.data;\r
-\r
-import java.io.Serializable;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.concurrent.ConcurrentHashMap;\r
-\r
-import javax.enterprise.context.SessionScoped;\r
-import javax.inject.Named;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import com.indexdata.mkjsf.errors.ErrorHelper;\r
-import com.indexdata.mkjsf.errors.ErrorInterface;\r
-import com.indexdata.mkjsf.pazpar2.data.sp.SpResponses;\r
-\r
-@Named("pzresp") @SessionScoped\r
-public class Pazpar2Responses implements Serializable {\r
- \r
- private static final long serialVersionUID = -7543231258346154642L;\r
- protected Map<String,Pazpar2ResponseData> dataObjects = new ConcurrentHashMap<String,Pazpar2ResponseData>();\r
- private static Logger logger = Logger.getLogger(Pazpar2Responses.class);\r
- private ErrorHelper errorHelper = null;\r
-\r
- public Pazpar2Responses() { \r
- }\r
- \r
- public void put(String name, Pazpar2ResponseData responseData) {\r
- dataObjects.put(name, responseData);\r
- }\r
- \r
- public void setErrorHelper(ErrorHelper helper) { \r
- this.errorHelper = helper;\r
- }\r
- \r
- public boolean hasApplicationError () {\r
- if (getSearch().hasApplicationError()) {\r
- logger.info("Error detected in search");\r
- return true;\r
- }\r
- for (String name : dataObjects.keySet()) {\r
- if (dataObjects.get(name).hasApplicationError()) {\r
- logger.info("Error detected in " + name);\r
- return true;\r
- }\r
- } \r
- return false;\r
- }\r
- \r
- /**\r
- * Returns a search command error, if any, otherwise the first\r
- * error found for an arbitrary command, if any, otherwise\r
- * an empty dummy error. \r
- */ \r
- public ErrorInterface getCommandError() {\r
- CommandError error = new CommandError();\r
- if (dataObjects.get("search").hasApplicationError()) {\r
- error = dataObjects.get("search").getApplicationError();\r
- error.setErrorHelper(errorHelper);\r
- } else {\r
- for (String name : dataObjects.keySet()) { \r
- if (dataObjects.get(name).hasApplicationError()) { \r
- error = dataObjects.get(name).getApplicationError();\r
- error.setErrorHelper(errorHelper);\r
- break;\r
- } \r
- }\r
- }\r
- return error; \r
- }\r
- \r
- public void reset() {\r
- logger.debug("Resetting show,stat,termlist,bytarget,search response objects.");\r
- dataObjects = new ConcurrentHashMap<String,Pazpar2ResponseData>();\r
- dataObjects.put("show", new ShowResponse());\r
- dataObjects.put("stat", new StatResponse());\r
- dataObjects.put("termlist", new TermListsResponse());\r
- dataObjects.put("bytarget", new ByTarget());\r
- dataObjects.put("record", new RecordResponse());\r
- dataObjects.put("search", new SearchResponse());\r
- }\r
-\r
- public ShowResponse getShow () {\r
- return ((ShowResponse) dataObjects.get("show"));\r
- }\r
- \r
- public StatResponse getStat () {\r
- return ((StatResponse) dataObjects.get("stat"));\r
- }\r
- \r
- public RecordResponse getRecord() {\r
- return ((RecordResponse) dataObjects.get("record"));\r
- }\r
- \r
- public SearchResponse getSearch() {\r
- return ((SearchResponse) dataObjects.get("search"));\r
- }\r
- \r
- public TermListsResponse getTermLists () {\r
- return ((TermListsResponse) dataObjects.get("termlist"));\r
- }\r
- \r
- public List<TermResponse> getFacetTerms (String facet, int count) {\r
- return (getTermLists().getTermList(facet).getTerms(count));\r
- }\r
- \r
- public List<TermResponse> getFacetTerms (String facet) {\r
- return (getTermLists().getTermList(facet).getTerms());\r
- }\r
- \r
- public ByTarget getByTarget() {\r
- return ((ByTarget) dataObjects.get("bytarget"));\r
- }\r
-\r
- public Pazpar2ResponseData getResponseObject (String name) {\r
- return dataObjects.get(name);\r
- }\r
- \r
- public boolean hasRecords () {\r
- return getStat().getRecords() > 0 \r
- && getShow().getHits() != null \r
- && getShow().getHits().size()>0;\r
- }\r
- \r
- public String getActiveClients() { \r
- if (getShow()!=null) {\r
- logger.debug("Active clients: "+getShow().getActiveClients());\r
- return getShow().getActiveClients();\r
- } else {\r
- return "";\r
- }\r
- }\r
-\r
- public SpResponses getSp() {\r
- // TODO:\r
- return null;\r
- }\r
-\r
-}\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-public class RecordOffsetResponse extends Pazpar2ResponseData {\r
+public class RecordOffsetResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -601620728296476450L;\r
\r
import java.util.List;\r
\r
import com.indexdata.mkjsf.pazpar2.data.Location;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
-public class RecordResponse extends Pazpar2ResponseData {\r
+public class RecordResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = 6682722004285796002L;\r
\r
List<Location> locations = new ArrayList<Location>();\r
if (getElements("location")!=null) { \r
int i = 0;\r
- for (Pazpar2ResponseData element : getElements("location")) {\r
+ for (ResponseDataObject element : getElements("location")) {\r
((Location)element).setSequenceNumber(i++);\r
locations.add((Location)element);\r
}\r
\r
public String getSubjects() {\r
StringBuilder builder = new StringBuilder("");\r
- for (Pazpar2ResponseData data : getElements("md-subject")) {\r
+ for (ResponseDataObject data : getElements("md-subject")) {\r
if (builder.length()==0) {\r
builder.append(data.getValue());\r
} else {\r
--- /dev/null
+package com.indexdata.mkjsf.pazpar2.data;\r
+\r
+import java.io.Serializable;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
+\r
+public class ResponseDataObject implements Serializable {\r
+\r
+ Logger logger = Logger.getLogger(ResponseDataObject.class);\r
+ private static final long serialVersionUID = -3909755656714679959L;\r
+ String type = null;\r
+ HashMap<String,String> attributes = new HashMap<String,String>();\r
+ HashMap<String,List<ResponseDataObject>> elements = new HashMap<String,List<ResponseDataObject>>();\r
+ String textContent = "";\r
+ CommandError error = null;\r
+ String xml = null;\r
+ \r
+ public void setType (String type) {\r
+ this.type = type;\r
+ }\r
+ \r
+ public String getType () {\r
+ return type;\r
+ }\r
+ \r
+ public void setAttribute (String name, String value) {\r
+ attributes.put(name, value);\r
+ }\r
+ \r
+ public String getAttribute (String name) {\r
+ return attributes.get(name);\r
+ }\r
+ \r
+ public void addElement (String name, ResponseDataObject value) { \r
+ if (elements.containsKey(name)) {\r
+ elements.get(name).add(value);\r
+ } else {\r
+ List<ResponseDataObject> list = new ArrayList<ResponseDataObject>();\r
+ list.add(value);\r
+ elements.put(name,list);\r
+ }\r
+ }\r
+ \r
+ public List<ResponseDataObject> getElements (String name) {\r
+ return elements.get(name);\r
+ }\r
+ \r
+ public ResponseDataObject getOneElement (String name) {\r
+ if (elements.get(name) != null) {\r
+ return elements.get(name).get(0);\r
+ } else {\r
+ return null;\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Returns the text content of the first element found with the given\r
+ * name\r
+ * @param name of the element \r
+ * @return text value, empty string if none found\r
+ */\r
+ public String getOneElementValue (String name) {\r
+ if (getOneElement(name)!=null && getOneElement(name).getValue().length()>0) {\r
+ return getOneElement(name).getValue();\r
+ } else {\r
+ return "";\r
+ }\r
+ }\r
+ \r
+ public void appendContent (String content) {\r
+ textContent = textContent + content;\r
+ }\r
+ \r
+ public String getValue () {\r
+ return textContent;\r
+ }\r
+ \r
+ public String getProperty(String name) {\r
+ List<ResponseDataObject> els = elements.get(name);\r
+ if (els != null) {\r
+ return els.get(0).getValue();\r
+ } else { \r
+ return null;\r
+ }\r
+ }\r
+ \r
+ public int getIntValue(String name) {\r
+ String val = getOneElementValue(name);\r
+ if (val.length()==0) {\r
+ return 0;\r
+ } else {\r
+ return Integer.parseInt(val);\r
+ }\r
+ }\r
+ \r
+ public boolean hasApplicationError () {\r
+ return (getOneElement("applicationerror") != null); \r
+ }\r
+ \r
+ public CommandError getApplicationError() {\r
+ return (CommandError) getOneElement("applicationerror");\r
+ }\r
+ \r
+ public boolean hasPazpar2Error() {\r
+ return hasApplicationError() && getApplicationError().hasPazpar2Error();\r
+ }\r
+ \r
+ public void setXml(String xml) {\r
+ this.xml = xml; \r
+ }\r
+ \r
+ public String getXml() {\r
+ if (type != null && type.equals("record")) {\r
+ logger.debug("Getting XML for "+type + ": "+xml);\r
+ } \r
+ return xml == null ? "" : xml;\r
+ }\r
+ \r
+}\r
--- /dev/null
+package com.indexdata.mkjsf.pazpar2.data;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.IOException;\r
+import java.io.UnsupportedEncodingException;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+import java.util.Stack;\r
+\r
+import javax.xml.parsers.ParserConfigurationException;\r
+import javax.xml.parsers.SAXParser;\r
+import javax.xml.parsers.SAXParserFactory;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.xml.sax.Attributes;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.XMLReader;\r
+import org.xml.sax.helpers.DefaultHandler;\r
+\r
+import com.indexdata.mkjsf.pazpar2.data.ByTarget;\r
+import com.indexdata.mkjsf.pazpar2.data.Hit;\r
+import com.indexdata.mkjsf.pazpar2.data.Location;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
+import com.indexdata.mkjsf.pazpar2.data.RecordResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.ShowResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.StatResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.Target;\r
+import com.indexdata.mkjsf.pazpar2.data.TermListResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.TermListsResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.TermResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.TermXTargetResponse;\r
+\r
+public class ResponseParser extends DefaultHandler {\r
+\r
+ private XMLReader xmlReader = null;\r
+ private ResponseDataObject currentElement = null;\r
+ private Stack<ResponseDataObject> dataElements = new Stack<ResponseDataObject>();\r
+ private ResponseDataObject result = null;\r
+ private String xml = null;\r
+ private static Logger logger = Logger.getLogger(ResponseParser.class);\r
+\r
+ public static final List<String> docTypes = \r
+ Arrays.asList("bytarget","termlist","show","stat","record","search");\r
+ \r
+ public ResponseParser() { \r
+ try {\r
+ initSax();\r
+ } catch (ParserConfigurationException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (SAXException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ public static ResponseParser getParser() {\r
+ return new ResponseParser();\r
+ }\r
+ \r
+ private void initSax() throws ParserConfigurationException, SAXException {\r
+ SAXParserFactory spf = SAXParserFactory.newInstance();\r
+ spf.setNamespaceAware(true);\r
+ SAXParser saxParser = spf.newSAXParser();\r
+ xmlReader = saxParser.getXMLReader();\r
+ xmlReader.setContentHandler(this); \r
+ }\r
+ \r
+ /**\r
+ * Parses a Pazpar2 XML response -- or an error response as XML -- and produces a \r
+ * ResponseDataObject object, i.e. a 'show' object\r
+ * \r
+ * @param response XML response string from Pazpar2\r
+ * @return Response data object\r
+ */\r
+ public ResponseDataObject getDataObject (String response) {\r
+ this.xml = response;\r
+ try { \r
+ xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))));\r
+ } catch (UnsupportedEncodingException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace(); \r
+ } catch (IOException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (SAXException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace(); \r
+ }\r
+ return result;\r
+ }\r
+\r
+ /** \r
+ * Receive notification at the start of element \r
+ * \r
+ */\r
+ @Override\r
+ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {\r
+ if (localName.equals("show")) {\r
+ currentElement = new ShowResponse(); \r
+ } else if (localName.equals("hit")) {\r
+ currentElement = new Hit();\r
+ } else if (localName.equals("location")) {\r
+ currentElement = new Location();\r
+ } else if (localName.equals("bytarget")) {\r
+ currentElement = new ByTarget(); \r
+ } else if (localName.equals("target")) {\r
+ currentElement = new Target();\r
+ } else if (localName.equals("stat")) {\r
+ currentElement = new StatResponse(); \r
+ } else if (localName.equals("termlist")) {\r
+ currentElement = new TermListsResponse(); \r
+ } else if (localName.equals("list")) {\r
+ currentElement = new TermListResponse();\r
+ ((TermListResponse)currentElement).setName(atts.getValue("name"));\r
+ ((TermListsResponse)dataElements.peek()).addTermList((TermListResponse)currentElement);\r
+ } else if (localName.equals("term")) {\r
+ if (dataElements.peek().getAttribute("name").equals("xtargets")) {\r
+ currentElement = new TermXTargetResponse(); \r
+ } else {\r
+ currentElement = new TermResponse();\r
+ }\r
+ ((TermListResponse)dataElements.peek()).addTerm((TermResponse)currentElement);\r
+ } else if (localName.equals("record")) {\r
+ currentElement = new RecordResponse(); \r
+ } else if (localName.equals("search")) {\r
+ currentElement = new SearchResponse();\r
+ } else if (localName.equals("applicationerror")) {\r
+ currentElement = new CommandError();\r
+ } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) {\r
+ currentElement = new Pazpar2Error(); \r
+ } else {\r
+ currentElement = new ResponseDataObject();\r
+ }\r
+ currentElement.setType(localName);\r
+ for (int i=0; i< atts.getLength(); i++) {\r
+ currentElement.setAttribute(atts.getLocalName(i), atts.getValue(i));\r
+ } \r
+ if (!docTypes.contains(localName)) {\r
+ if (dataElements.size() == 0) {\r
+ logger.info("Encountered unknown top level element [" + localName + "]. Creating generic data object.");\r
+ currentElement.setType(localName);\r
+ } else {\r
+ dataElements.peek().addElement(localName, currentElement);\r
+ }\r
+ }\r
+ if (this.xml != null) { // Store XML for doc level elements\r
+ currentElement.setXml(xml);\r
+ xml = null;\r
+ }\r
+ dataElements.push(currentElement); \r
+ }\r
+ \r
+ @Override\r
+ public void characters(char[] ch, int start, int length) throws SAXException {\r
+ String data = new String(ch, start, length); \r
+ dataElements.peek().appendContent(data); \r
+ }\r
+ \r
+ @Override\r
+ public void endElement(String namespaceURI, String localName, String qName) throws SAXException {\r
+ if (dataElements.size()==1) {\r
+ result = dataElements.pop();\r
+ } else {\r
+ dataElements.pop();\r
+ }\r
+ }\r
+}\r
--- /dev/null
+package com.indexdata.mkjsf.pazpar2.data;\r
+\r
+import java.io.Serializable;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.inject.Named;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.errors.ErrorHelper;\r
+import com.indexdata.mkjsf.errors.ErrorInterface;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.SpResponses;\r
+\r
+@Named("pzresp") @SessionScoped\r
+public class Responses implements Serializable {\r
+ \r
+ private static final long serialVersionUID = -7543231258346154642L;\r
+ protected Map<String,ResponseDataObject> dataObjects = new ConcurrentHashMap<String,ResponseDataObject>();\r
+ private static Logger logger = Logger.getLogger(Responses.class);\r
+ private ErrorHelper errorHelper = null;\r
+\r
+ public Responses() { \r
+ }\r
+ \r
+ public void put(String name, ResponseDataObject responseData) {\r
+ dataObjects.put(name, responseData);\r
+ }\r
+ \r
+ public void setErrorHelper(ErrorHelper helper) { \r
+ this.errorHelper = helper;\r
+ }\r
+ \r
+ public boolean hasApplicationError () {\r
+ if (getSearch().hasApplicationError()) {\r
+ logger.info("Error detected in search");\r
+ return true;\r
+ }\r
+ for (String name : dataObjects.keySet()) {\r
+ if (dataObjects.get(name).hasApplicationError()) {\r
+ logger.info("Error detected in " + name);\r
+ return true;\r
+ }\r
+ } \r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Returns a search command error, if any, otherwise the first\r
+ * error found for an arbitrary command, if any, otherwise\r
+ * an empty dummy error. \r
+ */ \r
+ public ErrorInterface getCommandError() {\r
+ CommandError error = new CommandError();\r
+ if (dataObjects.get("search").hasApplicationError()) {\r
+ error = dataObjects.get("search").getApplicationError();\r
+ error.setErrorHelper(errorHelper);\r
+ } else {\r
+ for (String name : dataObjects.keySet()) { \r
+ if (dataObjects.get(name).hasApplicationError()) { \r
+ error = dataObjects.get(name).getApplicationError();\r
+ error.setErrorHelper(errorHelper);\r
+ break;\r
+ } \r
+ }\r
+ }\r
+ return error; \r
+ }\r
+ \r
+ public void reset() {\r
+ logger.debug("Resetting show,stat,termlist,bytarget,search response objects.");\r
+ dataObjects = new ConcurrentHashMap<String,ResponseDataObject>();\r
+ dataObjects.put("show", new ShowResponse());\r
+ dataObjects.put("stat", new StatResponse());\r
+ dataObjects.put("termlist", new TermListsResponse());\r
+ dataObjects.put("bytarget", new ByTarget());\r
+ dataObjects.put("record", new RecordResponse());\r
+ dataObjects.put("search", new SearchResponse());\r
+ }\r
+\r
+ public ShowResponse getShow () {\r
+ return ((ShowResponse) dataObjects.get("show"));\r
+ }\r
+ \r
+ public StatResponse getStat () {\r
+ return ((StatResponse) dataObjects.get("stat"));\r
+ }\r
+ \r
+ public RecordResponse getRecord() {\r
+ return ((RecordResponse) dataObjects.get("record"));\r
+ }\r
+ \r
+ public SearchResponse getSearch() {\r
+ return ((SearchResponse) dataObjects.get("search"));\r
+ }\r
+ \r
+ public TermListsResponse getTermLists () {\r
+ return ((TermListsResponse) dataObjects.get("termlist"));\r
+ }\r
+ \r
+ public List<TermResponse> getFacetTerms (String facet, int count) {\r
+ return (getTermLists().getTermList(facet).getTerms(count));\r
+ }\r
+ \r
+ public List<TermResponse> getFacetTerms (String facet) {\r
+ return (getTermLists().getTermList(facet).getTerms());\r
+ }\r
+ \r
+ public ByTarget getByTarget() {\r
+ return ((ByTarget) dataObjects.get("bytarget"));\r
+ }\r
+\r
+ public ResponseDataObject getResponseObject (String name) {\r
+ return dataObjects.get(name);\r
+ }\r
+ \r
+ public boolean hasRecords () {\r
+ return getStat().getRecords() > 0 \r
+ && getShow().getHits() != null \r
+ && getShow().getHits().size()>0;\r
+ }\r
+ \r
+ public String getActiveClients() { \r
+ if (getShow()!=null) {\r
+ logger.debug("Active clients: "+getShow().getActiveClients());\r
+ return getShow().getActiveClients();\r
+ } else {\r
+ return "";\r
+ }\r
+ }\r
+\r
+ public SpResponses getSp() {\r
+ // TODO:\r
+ return null;\r
+ }\r
+\r
+}\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-public class SearchResponse extends Pazpar2ResponseData {\r
+public class SearchResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -3320013021497018972L;\r
\r
import java.util.List;\r
\r
import com.indexdata.mkjsf.pazpar2.data.Hit;\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
-public class ShowResponse extends Pazpar2ResponseData {\r
+public class ShowResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = 7103554232106330370L;\r
\r
public List<Hit> getHits() {\r
List<Hit> hits = new ArrayList<Hit>();\r
if (getElements("hit") != null) {\r
- for (Pazpar2ResponseData element : getElements("hit")) {\r
+ for (ResponseDataObject element : getElements("hit")) {\r
hits.add((Hit)element);\r
}\r
} \r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
-public class StatResponse extends Pazpar2ResponseData {\r
+public class StatResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -6578979787689458761L;\r
\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
-public class Target extends Pazpar2ResponseData {\r
+public class Target extends ResponseDataObject {\r
\r
private static final long serialVersionUID = 3343881183545520108L;\r
\r
\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
import com.indexdata.mkjsf.pazpar2.data.TermListResponse;\r
import com.indexdata.mkjsf.pazpar2.data.TermResponse;\r
\r
-public class TermListResponse extends Pazpar2ResponseData {\r
+public class TermListResponse extends ResponseDataObject {\r
\r
private static Logger logger = Logger.getLogger(TermListResponse.class);\r
private static final long serialVersionUID = 3838585739723097393L;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
import com.indexdata.mkjsf.pazpar2.data.TermListResponse;\r
\r
-public class TermListsResponse extends Pazpar2ResponseData {\r
+public class TermListsResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -1370643625715834978L;\r
private int activeClients = -1;\r
package com.indexdata.mkjsf.pazpar2.data;\r
\r
-import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
\r
-public class TermResponse extends Pazpar2ResponseData {\r
+public class TermResponse extends ResponseDataObject {\r
\r
private static final long serialVersionUID = -8323959763575180678L;\r
\r