001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: FaultHandler.java 9472 2007-10-09 15:24:17Z elu $
023: */
024:
025: package com.bostechcorp.cbesb.runtime.ccsl.nmhandler;
026:
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.PrintWriter;
030: import java.io.StringWriter;
031: import java.io.UnsupportedEncodingException;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.jdom.DefaultJDOMFactory;
036: import org.jdom.Document;
037: import org.jdom.Element;
038: import org.jdom.JDOMException;
039: import org.jdom.JDOMFactory;
040: import org.jdom.Namespace;
041: import org.jdom.input.SAXBuilder;
042: import org.jdom.output.Format;
043: import org.jdom.output.XMLOutputter;
044:
045: import com.bostechcorp.cbesb.common.runtime.CbesbException;
046: import com.bostechcorp.cbesb.common.runtime.ResourcesConnectionException;
047: import com.bostechcorp.cbesb.common.util.ErrorUtil;
048: import com.bostechcorp.cbesb.common.util.FileUtil;
049: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbEndpoint;
050:
051: public class FaultHandler {
052:
053: protected final transient Log logger = LogFactory
054: .getLog(getClass());
055:
056: public static final String FAULT_NS = "http://schemas.xmlsoap.org/soap/envelope/";
057:
058: public static final String RECOVERABLE_FAULT_NS = "http://cbesb.bostechcorp.com/fault/recoverable/1.0/";
059:
060: public static final String FAULT_ROOT = "Fault";
061:
062: public static final String FAULT_CODE = "code";
063:
064: public static final String FAULT_MESSAGE = "message";
065:
066: public static final String FAULT_REMEDY = "remedy";
067:
068: public static final String FAULT_ENDPOINT = "endpoint";
069:
070: public static final String FAULT_DETAIL = "detail";
071:
072: private Element rootElement;
073:
074: private Exception ex;
075:
076: private CbEndpoint endpoint;
077:
078: private Namespace faultNS;
079:
080: JDOMFactory jdomFactory = new DefaultJDOMFactory();
081:
082: private String code;
083:
084: private String message;
085:
086: private String remedy;
087:
088: private String endpointString;
089:
090: private String detail;
091:
092: private boolean isRecoverable;
093:
094: /**
095: * @param ex
096: */
097: public FaultHandler(String faultString) {
098: super ();
099: fromSoap(faultString);
100: }
101:
102: protected void fromSoap(String faultString) {
103:
104: if (logger.isDebugEnabled())
105: logger.debug("fault: " + faultString);
106: SAXBuilder builder = new SAXBuilder();
107: Document doc = null;
108: try {
109: doc = builder.build(FileUtil
110: .String2InputStream(faultString));
111: } catch (Exception e) {
112: logger.error("Exception while getting JDom tree", e);
113: ErrorUtil.printWarn("fail to parse the fault string:"
114: + faultString, e);
115: return;
116: }
117:
118: Element root = doc.getRootElement();
119: if (!root.getName().equals(FAULT_ROOT)) {
120: ErrorUtil
121: .printWarn("Root Node is not " + FAULT_ROOT,
122: new JDOMException("Root Node is not "
123: + FAULT_ROOT));
124: return;
125: }
126:
127: isRecoverable = false;
128: if (root.getNamespaceURI() != null
129: && root.getNamespaceURI().equals(RECOVERABLE_FAULT_NS))
130: isRecoverable = true;
131:
132: code = root.getChildText(FAULT_CODE, root.getNamespace());
133: message = root.getChildText(FAULT_MESSAGE, root.getNamespace());
134: remedy = root.getChildText(FAULT_REMEDY, root.getNamespace());
135: endpointString = root.getChildText(FAULT_ENDPOINT, root
136: .getNamespace());
137: detail = root.getChildText(FAULT_DETAIL, root.getNamespace());
138: }
139:
140: /**
141: * @return the code
142: */
143: public String getCode() {
144: return code;
145: }
146:
147: /**
148: * @return the detail
149: */
150: public String getDetail() {
151: return detail;
152: }
153:
154: /**
155: * @return the endpointString
156: */
157: public String getEndpointString() {
158: return endpointString;
159: }
160:
161: /**
162: * @return the isRecoverable
163: */
164: public boolean isRecoverable() {
165: return isRecoverable;
166: }
167:
168: /**
169: * @return the message
170: */
171: public String getMessage() {
172: return message;
173: }
174:
175: /**
176: * @return the remedy
177: */
178: public String getRemedy() {
179: return remedy;
180: }
181:
182: /**
183: * @param ex
184: */
185: public FaultHandler(Exception ex, CbEndpoint endpoint) {
186: super ();
187: this .endpoint = endpoint;
188: this .ex = ex;
189: faultNS = Namespace.getNamespace(FAULT_NS);
190: }
191:
192: public String toSoap() {
193:
194: if (ex instanceof ResourcesConnectionException)
195: faultNS = Namespace.getNamespace(RECOVERABLE_FAULT_NS);
196: // else
197: // faultNS = Namespace.getNamespace("fault", FAULT_NS);
198:
199: rootElement = new Element(FAULT_ROOT, faultNS);
200:
201: Element partElem = new Element(FAULT_CODE, faultNS);
202: partElem.setText("");
203: rootElement.addContent(partElem);
204:
205: partElem = new Element(FAULT_MESSAGE, faultNS);
206: partElem.setText(ex.getMessage());
207: rootElement.addContent(partElem);
208:
209: if (ex instanceof CbesbException) {
210: partElem = new Element(FAULT_REMEDY, faultNS);
211: partElem.setText(((CbesbException) ex).getRemedy());
212: rootElement.addContent(partElem);
213: }
214:
215: if (endpoint.getEndpoint() != null) {
216: partElem = new Element(FAULT_ENDPOINT, faultNS);
217: partElem.setText(endpoint.getEndpoint());
218: rootElement.addContent(partElem);
219: }
220:
221: StringBuffer s = new StringBuffer();
222: StringWriter sw = new StringWriter();
223: PrintWriter pw = new PrintWriter(sw);
224: ex.printStackTrace(pw);
225: s.append(sw);
226:
227: partElem = new Element(FAULT_DETAIL, faultNS);
228: partElem.setText(s.toString());
229: rootElement.addContent(partElem);
230:
231: Document jdomDoc = new Document(rootElement);
232:
233: XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
234: ByteArrayOutputStream ostream = new ByteArrayOutputStream();
235:
236: try {
237: xmlOut.output(jdomDoc, ostream);
238: } catch (IOException e) {
239: return "";
240: }
241:
242: try {
243: return ostream.toString("utf-8");
244: } catch (UnsupportedEncodingException e) {
245: return "";
246: }
247: }
248:
249: @Override
250: public String toString() {
251: String result = "\tFault Handler:\n";
252: result += "Recoverable:" + isRecoverable() + "\n";
253: result += "Message:\t" + getMessage() + "\n";
254: result += "Code:\t" + getCode() + "\n";
255: result += "Remedy:\t" + getRemedy() + "\n";
256: result += "Endpoint:\t" + getEndpointString() + "\n";
257: result += "Details:\t" + getDetail() + "\n";
258: return result;
259: }
260:
261: // public static void main(String[] args)
262: // {
263: // try {
264: // String s =FileUtil.readStringFromFile("c:\\asd\\fault\\oneFaultMessage.xml");
265: // FaultHandler fh = new FaultHandler(s);
266: // Log loggerr = LogFactory.getLog("Logger");
267: // loggerr.error(fh);
268: // ///
269: // } catch (IOException e) {
270: // e.printStackTrace();
271: // }
272: // }
273: }
|