Adds distinction between pz2 and sp errors
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / data / CommandError.java
1 package com.indexdata.mkjsf.pazpar2.data;\r
2 \r
3 import static com.indexdata.mkjsf.utils.Utils.nl;\r
4 \r
5 import java.util.ArrayList;\r
6 import java.util.List;\r
7 import java.util.regex.Pattern;\r
8 \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
13 \r
14 /**\r
15  * Holds an error encountered during the execution of a command.\r
16  * \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
21  * \r
22  * The command response XML is subsequently parsed by ResponseParser, \r
23  * which will then create the CommandError object.\r
24  * \r
25  * @author Niels Erik\r
26  *\r
27  */\r
28 public class CommandError extends ResponseDataObject implements ErrorInterface {\r
29 \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
34   \r
35   \r
36   public CommandError () {    \r
37   }\r
38   \r
39   public String getLabel() {\r
40     return getOneElementValue("commandname");\r
41   }\r
42       \r
43   public String getMessage() {\r
44     if (isServiceError()) {      \r
45       return getServiceError().getMsg();\r
46     } else {      \r
47       return getOneElementValue("errormessage");\r
48     }\r
49   }\r
50     \r
51   public String getException () {\r
52     return getOneElementValue("exception");\r
53   }\r
54     \r
55   public List<String> getSuggestions() { \r
56     if (errorHelper!=null) {\r
57       return errorHelper.getSuggestions(this);\r
58     } else {\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
61       return nohelper;\r
62     }\r
63   }\r
64   \r
65   /**\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 exception\r
70    * @param errorMessage\r
71    * @return\r
72    */\r
73   public static String createErrorXml (String commandName, String statusCode, String exception, String errorMessage, String response) {\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("  <status>FAIL</status>"+nl);\r
79     errorXml.append("  <statuscode>" + statusCode + "</statuscode>"+nl);\r
80     errorXml.append("  <exception>" + (exception != null ? XmlUtils.escape(exception) : "") + "</exception>"+nl);    \r
81     errorXml.append("  <errormessage>" + (errorMessage != null  ? XmlUtils.escape(errorMessage) : "") + "</errormessage>"+nl);\r
82     errorXml.append("  <response>" + response + "</response>" + nl);\r
83     errorXml.append(" </applicationerror>"+nl);\r
84     errorXml.append("</" + commandName + ">"+nl);\r
85     return errorXml.toString(); \r
86   }\r
87   \r
88   /**\r
89    * Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of\r
90    * a command response document (like 'search' or 'show').\r
91    * This is the XML that ResponseParser will turn into a CommandError object.\r
92    * \r
93    * \r
94    * @param commandName The name of the command during which's execution the error was encountered\r
95    * @param exception The (possibly loosely defined) name of the exception that was thrown\r
96    * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or \r
97    *                        by the Pazpar2 client itself. \r
98    * @return\r
99    */\r
100   public static String insertErrorXml (String commandName, String statusCode, String exception, String pazpar2ErrorXml) {\r
101     StringBuilder errorXml = new StringBuilder("");\r
102     errorXml.append("<" + commandName + ">"+nl);\r
103     errorXml.append(" <applicationerror>"+nl);\r
104     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
105     errorXml.append("  <statuscode>" + statusCode + "</statuscode>"+nl);\r
106     errorXml.append("  <exception>" + XmlUtils.escape(exception) + "</exception>"+nl);    \r
107     errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl);    \r
108     errorXml.append(" </applicationerror>"+nl);\r
109     errorXml.append("</" + commandName + ">"+nl);\r
110     return errorXml.toString(); \r
111     \r
112   }\r
113    \r
114   /**\r
115    * Sets the object that should be used to analyze the error\r
116    *  \r
117    */\r
118   public void setErrorHelper (ErrorHelper errorHelper) {\r
119     this.errorHelper = errorHelper; \r
120   }\r
121 \r
122   @Override\r
123   public void setApplicationErrorCode(ErrorCode code) {\r
124     this.applicationErrorCode = code;    \r
125   }\r
126 \r
127   @Override\r
128   public ErrorCode getApplicationErrorCode() {\r
129     return applicationErrorCode;    \r
130   }\r
131   \r
132   public boolean isServiceError () {\r
133     ServiceError pz2err = (ServiceError) getOneElement("error");\r
134     return (pz2err != null);\r
135   }\r
136   \r
137   public ServiceError getServiceError() {\r
138     return (ServiceError) getOneElement("error");\r
139   }\r
140   \r
141   public boolean isServiceProxyError () {\r
142     return (isServiceError() && getServiceError().isServiceProxyError());\r
143   }\r
144 \r
145   public boolean isPazpar2Error () {\r
146     return (isServiceError() && getServiceError().isPazpar2Error());\r
147   }\r
148 \r
149 }\r