1 package com.indexdata.mkjsf.pazpar2.data;
\r
3 import static com.indexdata.mkjsf.utils.Utils.nl;
\r
5 import java.util.ArrayList;
\r
6 import java.util.List;
\r
7 import java.util.regex.Pattern;
\r
9 import com.indexdata.mkjsf.errors.ErrorHelper;
\r
10 import com.indexdata.mkjsf.errors.ErrorInterface;
\r
11 import com.indexdata.mkjsf.errors.ErrorHelper.ErrorCode;
\r
12 import com.indexdata.utils.XmlUtils;
\r
15 * Holds an error encountered during the execution of a command.
\r
17 * An error can be received by a command thread as an exception message
\r
18 * or as an error XML. In both cases the error (string or xml) will be embedded
\r
19 * in a new 'applicationerror' element which in turn will be embedded in a
\r
20 * command XML (i.e. a 'search' or a 'show' response XML)
\r
22 * The command response XML is subsequently parsed by ResponseParser,
\r
23 * which will then create the CommandError object.
\r
25 * @author Niels Erik
\r
28 public class CommandError extends ResponseDataObject implements ErrorInterface {
\r
30 private static final long serialVersionUID = 8878776025779714122L;
\r
31 private static Pattern xmlDeclaration = Pattern.compile("<\\?xml.*\\?>");
\r
32 private ErrorCode applicationErrorCode;
\r
33 private ErrorHelper errorHelper = null;
\r
36 public CommandError () {
\r
39 public String getLabel() {
\r
40 return getOneElementValue("commandname");
\r
43 public String getMessage() {
\r
44 if (hasPazpar2Error()) {
\r
45 return getPazpar2Error().getMsg();
\r
47 return getOneElementValue("errormessage");
\r
51 public String getException () {
\r
52 return getOneElementValue("exception");
\r
55 public List<String> getSuggestions() {
\r
56 if (errorHelper!=null) {
\r
57 return errorHelper.getSuggestions(this);
\r
59 List<String> nohelper = new ArrayList<String>();
\r
60 nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");
\r
66 * Creates an XML string error message, embedded in an XML string document named by the command
\r
67 * This is the XML that ResponseParser will turn into a CommandError object.
\r
68 * @param commandName
\r
69 * @param exceptionName
\r
70 * @param errorMessage
\r
73 public static String createErrorXml (String commandName, String statusCode, String exceptionName, String errorMessage) {
\r
74 StringBuilder errorXml = new StringBuilder("");
\r
75 errorXml.append("<" + commandName + ">"+nl);
\r
76 errorXml.append(" <applicationerror>"+nl);
\r
77 errorXml.append(" <commandname>" + commandName + "</commandname>"+nl);
\r
78 errorXml.append(" <statuscode>" + statusCode + "</statuscode>"+nl);
\r
79 errorXml.append(" <exception>" + (exceptionName != null ? XmlUtils.escape(exceptionName) : "") + "</exception>"+nl);
\r
80 errorXml.append(" <errormessage>" + (errorMessage != null ? XmlUtils.escape(errorMessage) : "") + "</errormessage>"+nl);
\r
81 errorXml.append(" </applicationerror>"+nl);
\r
82 errorXml.append("</" + commandName + ">"+nl);
\r
83 return errorXml.toString();
\r
87 * Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of
\r
88 * a command response document (like 'search' or 'show').
\r
89 * This is the XML that ResponseParser will turn into a CommandError object.
\r
92 * @param commandName The name of the command during which's execution the error was encountered
\r
93 * @param exceptionName The (possibly loosely defined) name of the exception that was thrown
\r
94 * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or
\r
95 * by the Pazpar2 client itself.
\r
98 public static String insertErrorXml (String commandName, String statusCode, String exceptionName, String pazpar2ErrorXml) {
\r
99 StringBuilder errorXml = new StringBuilder("");
\r
100 errorXml.append("<" + commandName + ">"+nl);
\r
101 errorXml.append(" <applicationerror>"+nl);
\r
102 errorXml.append(" <commandname>" + commandName + "</commandname>"+nl);
\r
103 errorXml.append(" <statuscode>" + statusCode + "</statuscode>"+nl);
\r
104 errorXml.append(" <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);
\r
105 errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl);
\r
106 errorXml.append(" </applicationerror>"+nl);
\r
107 errorXml.append("</" + commandName + ">"+nl);
\r
108 return errorXml.toString();
\r
113 * Sets the object that should be used to analyze the error
\r
116 public void setErrorHelper (ErrorHelper errorHelper) {
\r
117 this.errorHelper = errorHelper;
\r
121 public void setApplicationErrorCode(ErrorCode code) {
\r
122 this.applicationErrorCode = code;
\r
126 public ErrorCode getApplicationErrorCode() {
\r
127 return applicationErrorCode;
\r
130 public boolean hasPazpar2Error () {
\r
131 return ( getOneElement("error") != null);
\r
134 public Pazpar2Error getPazpar2Error() {
\r
135 return (Pazpar2Error) getOneElement("error");
\r