More error handling/reporting. Normalizing auth resp
[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 (hasPazpar2Error()) {      \r
45       return getPazpar2Error().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 exceptionName\r
70    * @param errorMessage\r
71    * @return\r
72    */\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
84   }\r
85   \r
86   /**\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
90    * \r
91    * \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
96    * @return\r
97    */\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
109     \r
110   }\r
111    \r
112   /**\r
113    * Sets the object that should be used to analyze the error\r
114    *  \r
115    */\r
116   public void setErrorHelper (ErrorHelper errorHelper) {\r
117     this.errorHelper = errorHelper; \r
118   }\r
119 \r
120   @Override\r
121   public void setApplicationErrorCode(ErrorCode code) {\r
122     this.applicationErrorCode = code;    \r
123   }\r
124 \r
125   @Override\r
126   public ErrorCode getApplicationErrorCode() {\r
127     return applicationErrorCode;    \r
128   }\r
129   \r
130   public boolean hasPazpar2Error () {\r
131     return ( getOneElement("error") != null);            \r
132   }\r
133   \r
134   public Pazpar2Error getPazpar2Error() {\r
135     return (Pazpar2Error) getOneElement("error");\r
136   }\r
137 \r
138 \r
139 }\r