--- /dev/null
+package com.indexdata.mkjsf.pazpar2.data;\r
+\r
+import static com.indexdata.mkjsf.utils.Utils.nl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.regex.Pattern;\r
+\r
+import com.indexdata.mkjsf.errors.ErrorHelper;\r
+import com.indexdata.mkjsf.errors.ErrorHelper.ErrorCode;\r
+import com.indexdata.mkjsf.errors.ErrorInterface;\r
+import com.indexdata.utils.XmlUtils;\r
+\r
+/**\r
+ * Holds an error encountered during the execution of a command.\r
+ * \r
+ * An error can be received by a command thread as an exception message \r
+ * or as an error XML. In both cases the error (string or xml) will be embedded\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 ResponseParser, \r
+ * which will then create the CommandError object.\r
+ * \r
+ * @author Niels Erik\r
+ *\r
+ */\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
+ private ErrorCode applicationErrorCode;\r
+ private ErrorHelper errorHelper = null;\r
+ \r
+ \r
+ public CommandError () { \r
+ }\r
+ \r
+ public String getLabel() {\r
+ return getOneValue("commandname");\r
+ }\r
+ \r
+ public String getMessage() {\r
+ if (isServiceError()) { \r
+ return getServiceError().getMsg();\r
+ } else { \r
+ return getOneValue("errormessage");\r
+ }\r
+ }\r
+ \r
+ public String getException () {\r
+ return getOneValue("exception");\r
+ }\r
+ \r
+ public List<String> getSuggestions() { \r
+ if (errorHelper!=null) { \r
+ return errorHelper.getSuggestions(this);\r
+ } else {\r
+ List<String> nohelper = new ArrayList<String>();\r
+ nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");\r
+ return nohelper;\r
+ }\r
+ }\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 ResponseParser will turn into a CommandError object. \r
+ * @param commandName\r
+ * @param exception\r
+ * @param errorMessage\r
+ * @return\r
+ */\r
+ public static String createErrorXml (String commandName, String statusCode, String exception, String errorMessage, String response) {\r
+ StringBuilder errorXml = new StringBuilder("");\r
+ errorXml.append("<" + commandName + ">"+nl);\r
+ errorXml.append(" <applicationerror>"+nl);\r
+ errorXml.append(" <commandname>" + commandName + "</commandname>"+nl);\r
+ errorXml.append(" <status>FAIL</status>"+nl);\r
+ errorXml.append(" <statuscode>" + statusCode + "</statuscode>"+nl);\r
+ errorXml.append(" <exception>" + (exception != null ? XmlUtils.escape(exception) : "") + "</exception>"+nl); \r
+ errorXml.append(" <errormessage>" + (errorMessage != null ? XmlUtils.escape(errorMessage) : "") + "</errormessage>"+nl);\r
+ errorXml.append(" <response>" + response + "</response>" + nl);\r
+ errorXml.append(" </applicationerror>"+nl);\r
+ errorXml.append("</" + commandName + ">"+nl);\r
+ return errorXml.toString(); \r
+ }\r
+ \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 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
+ * @param exception The (possibly loosely defined) name of the exception that was thrown\r
+ * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or \r
+ * by the Pazpar2 client itself. \r
+ * @return\r
+ */\r
+ public static String insertErrorXml (String commandName, String statusCode, String exception, String pazpar2ErrorXml) {\r
+ StringBuilder errorXml = new StringBuilder("");\r
+ errorXml.append("<" + commandName + ">"+nl);\r
+ errorXml.append(" <applicationerror>"+nl);\r
+ errorXml.append(" <commandname>" + commandName + "</commandname>"+nl);\r
+ errorXml.append(" <statuscode>" + statusCode + "</statuscode>"+nl);\r
+ errorXml.append(" <exception>" + XmlUtils.escape(exception) + "</exception>"+nl); \r
+ errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl); \r
+ errorXml.append(" </applicationerror>"+nl);\r
+ errorXml.append("</" + commandName + ">"+nl);\r
+ return errorXml.toString(); \r
+ \r
+ }\r
+ \r
+ /**\r
+ * Sets the object that should be used to analyze the error\r
+ * \r
+ */\r
+ public void setErrorHelper (ErrorHelper errorHelper) {\r
+ this.errorHelper = errorHelper; \r
+ }\r
+\r
+ @Override\r
+ public void setApplicationErrorCode(ErrorCode code) {\r
+ this.applicationErrorCode = code; \r
+ }\r
+\r
+ @Override\r
+ public ErrorCode getApplicationErrorCode() {\r
+ return applicationErrorCode; \r
+ }\r
+ \r
+ public boolean isServiceError () {\r
+ ServiceError pz2err = (ServiceError) getOneElement("error");\r
+ return (pz2err != null);\r
+ }\r
+ \r
+ public ServiceError getServiceError() {\r
+ return (ServiceError) getOneElement("error");\r
+ }\r
+ \r
+ public boolean isServiceProxyError () {\r
+ return (isServiceError() && getServiceError().isServiceProxyError());\r
+ }\r
+\r
+ public boolean isPazpar2Error () {\r
+ return (isServiceError() && getServiceError().isPazpar2Error());\r
+ }\r
+\r
+}\r