001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 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: SourceHelper.java 10988 2007-12-14 15:31:05Z mpreston $
023: */
024:
025: package com.bostechcorp.cbesb.runtime.ccsl.nmhandler;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.IOException;
029: import java.io.PrintWriter;
030: import java.io.StringWriter;
031: import java.io.UnsupportedEncodingException;
032:
033: import javax.xml.parsers.DocumentBuilder;
034: import javax.xml.parsers.DocumentBuilderFactory;
035: import javax.xml.transform.Source;
036: import javax.xml.transform.TransformerException;
037: import javax.xml.transform.dom.DOMSource;
038: import javax.xml.transform.stream.StreamSource;
039:
040: import org.w3c.dom.Document;
041:
042: import com.bostechcorp.cbesb.common.runtime.CbesbException;
043: import com.bostechcorp.cbesb.common.util.Dom;
044: import com.bostechcorp.cbesb.common.util.ErrorUtil;
045:
046: public final class SourceHelper {
047:
048: private static final String START_TAG = "<content>";
049:
050: private static final String END_TAG = "</content>";
051:
052: public static Source createContentSource(String msg) {
053: return createSource(START_TAG + msg + END_TAG);
054: }
055:
056: private SourceHelper() {
057: }
058:
059: // public static StringBuffer createSoapFaultHelper(Throwable e, String code, StringBuffer s) {
060: //
061: // /*
062: // * s.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); s.append("<s:Envelope
063: // * xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"");
064: // * s.append("xmlns:wsmd=\"http://www.openuri.org/2003/02/soap/messagedata/\">");
065: // * s.append("<s:Header>");
066: // *
067: // * s.append("<wsmd:MessageData><wsmd:RefToMessageId>"); if(id != null)
068: // * s.append(id); s.append("</wsmd:RefToMessageId></wsmd:MessageData>");
069: // *
070: // * s.append("</s:Header>");
071: // *
072: // * s.append("<s:Body>");
073: // */
074: //
075: //
076: // s.append("<s:code>");
077: // if (code != null) {
078: // s.append(code);
079: // }
080: // s.append("</s:code>");
081: //
082: // s.append("<s:message>");
083: // s.append(e.getMessage());
084: // s.append("</s:message>");
085: //
086: // if (e instanceof CbesbException && ((CbesbException)e).getRemedy() != null) {
087: // s.append("<s:remedy>");
088: // s.append(((CbesbException)e).getRemedy());
089: // s.append("</s:remedy>");
090: // }
091: //
092: // s.append("<s:detail>");
093: //
094: // StringWriter sw = new StringWriter();
095: // PrintWriter pw = new PrintWriter(sw);
096: // e.printStackTrace(pw);
097: // s.append(sw);
098: //
099: // s.append("</s:detail></s:Fault>");
100: // return s;
101: // }
102: //
103: //
104: // /**
105: // * Creates an XML fault with the stacktrace, message and the specified code
106: // *
107: // * @param e
108: // * exception, used to provide faultstring and detail
109: // * @param code
110: // * code of the fault
111: // * @return
112: // */
113: // public static String createSoapFault(Throwable e, String code) {
114: // StringBuffer s = new StringBuffer();
115: // /*
116: // * s.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); s.append("<s:Envelope
117: // * xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"");
118: // * s.append("xmlns:wsmd=\"http://www.openuri.org/2003/02/soap/messagedata/\">");
119: // * s.append("<s:Header>");
120: // *
121: // * s.append("<wsmd:MessageData><wsmd:RefToMessageId>"); if(id != null)
122: // * s.append(id); s.append("</wsmd:RefToMessageId></wsmd:MessageData>");
123: // *
124: // * s.append("</s:Header>");
125: // *
126: // * s.append("<s:Body>");
127: // */
128: // s.append("<s:Fault xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
129: // s = createSoapFaultHelper(e, code, s);
130: //
131: //// s.append("<s:code>");
132: //// if (code != null) {
133: //// s.append(code);
134: //// }
135: //// s.append("</s:code>");
136: ////
137: //// s.append("<s:message>");
138: //// s.append(e.getMessage());
139: //// s.append("</s:message>");
140: ////
141: //// if (e instanceof CbesbException && ((CbesbException)e).getRemedy() != null) {
142: //// s.append("<s:remedy>");
143: //// s.append(((CbesbException)e).getRemedy());
144: //// s.append("</s:remedy>");
145: //// }
146: ////
147: //// s.append("<s:detail>");
148: ////
149: //// StringWriter sw = new StringWriter();
150: //// PrintWriter pw = new PrintWriter(sw);
151: //// e.printStackTrace(pw);
152: //// s.append(sw);
153: ////
154: //// s.append("</s:detail></s:Fault>");
155: //// /*
156: //// s.append("</s:Body></s:Envelope>");
157: //// */
158: //
159: // return s.toString();
160: // }
161: //
162: // /**
163: // * Creates an Recoverable XML fault with the stacktrace, message and the specified code
164: // *
165: // * @param e
166: // * exception, used to provide faultstring and detail
167: // * @param code
168: // * code of the fault
169: // * @return
170: // */
171: // public static String createRecoverableFault(Throwable e, String code) {
172: // StringBuffer s = new StringBuffer();
173: // /*
174: // * s.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); s.append("<s:Envelope
175: // * xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"");
176: // * s.append("xmlns:wsmd=\"http://www.openuri.org/2003/02/soap/messagedata/\">");
177: // * s.append("<s:Header>");
178: // *
179: // * s.append("<wsmd:MessageData><wsmd:RefToMessageId>"); if(id != null)
180: // * s.append(id); s.append("</wsmd:RefToMessageId></wsmd:MessageData>");
181: // *
182: // * s.append("</s:Header>");
183: // *
184: // * s.append("<s:Body>");
185: // */
186: // s.append("<s:Fault xmlns:s=\"http://cbesb.bostechcorp.com/fault/recoverable/1.0/\">");
187: // s = createSoapFaultHelper(e, code, s);
188: // return s.toString();
189: //
190: //// s.append("<s:faultcode>");
191: //// if (code != null) {
192: //// s.append(code);
193: //// }
194: //// s.append("</s:faultcode><s:faultstring>");
195: //// s.append(e.getMessage());
196: //// s.append("</s:faultstring><s:detail>");
197: ////
198: //// StringWriter sw = new StringWriter();
199: //// PrintWriter pw = new PrintWriter(sw);
200: //// e.printStackTrace(pw);
201: //// s.append(sw);
202: ////
203: //// s.append("</s:detail></s:Fault>");
204: //// /*
205: //// s.append("</s:Body></s:Envelope>");
206: //// */
207: //// return s.toString();
208: // }
209:
210: public static Source createSource(String msg) {
211: byte[] msgByte = null;
212: DOMSource source = null;
213: try {
214: msgByte = msg.getBytes("utf-8");
215: ByteArrayInputStream in = new ByteArrayInputStream(msgByte);
216:
217: DocumentBuilderFactory fac = DocumentBuilderFactory
218: .newInstance();
219: fac.setNamespaceAware(true);
220: DocumentBuilder parser = fac.newDocumentBuilder();
221: Document doc = parser.parse(in);
222: source = new DOMSource(doc);
223: } catch (Exception e) {
224: ErrorUtil.printError("getBytes() error: ", e);
225: }
226:
227: return source;
228: }
229:
230: public static String createString(Source s) throws CbesbException {
231: if (s == null) {
232: throw new CbesbException("source is null");
233: } else if (s instanceof DOMSource) {
234: try {
235: return Dom.createStringFromDOMDocument(
236: (Document) (((DOMSource) s).getNode()), true);
237: } catch (TransformerException e) {
238: throw new CbesbException(
239: "Fail to create String from a DOMSource.", e);
240: }
241: } else if (s instanceof StringSource) {
242: return ((StringSource) s).getText();
243: } else if (s instanceof ByteArraySource) {
244: return ((ByteArraySource) s).toString();
245: } else if (s instanceof StreamSource) {
246: ByteArrayInputStream bio = (ByteArrayInputStream) ((StreamSource) s)
247: .getInputStream();
248: byte[] buff = new byte[bio.available()];
249: String str;
250: try {
251: bio.read(buff);
252: str = new String(buff, "utf-8");
253: } catch (IOException e) {
254: throw new CbesbException(
255: "Fail to create String from a StreamSource.", e);
256: }
257: return str;
258: } else {
259: throw new CbesbException("data source (" + s
260: + ") is not a supported type");
261: }
262: }
263:
264: public static String createStringContent(Source s) throws Exception {
265: String content = createString(s);
266: int index = content.indexOf(START_TAG);
267: String result = content.substring(index + START_TAG.length());
268: return result.substring(0, result.indexOf(END_TAG));
269: }
270:
271: }
|