001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)Wsdl11TextMessageAdaptor.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms.handler;
030:
031: import com.sun.jbi.StringTranslator;
032: import com.sun.jbi.wsdl11wrapper.Wsdl11WrapperHelper;
033:
034: import java.io.Reader;
035: import java.io.StringReader;
036: import java.io.StringWriter;
037: import java.io.Writer;
038:
039: import java.util.logging.Logger;
040:
041: import javax.jbi.messaging.Fault;
042: import javax.jbi.messaging.MessageExchange;
043: import javax.jbi.messaging.NormalizedMessage;
044:
045: import javax.jbi.messaging.MessageExchange.Role;
046:
047: import javax.jms.JMSException;
048: import javax.jms.Message;
049: import javax.jms.TextMessage;
050:
051: import javax.xml.transform.Result;
052: import javax.xml.transform.Source;
053:
054: import javax.xml.transform.dom.DOMSource;
055: import javax.xml.transform.Transformer;
056: import javax.xml.transform.TransformerFactory;
057:
058: import javax.xml.transform.stream.StreamResult;
059: import javax.xml.transform.stream.StreamSource;
060:
061: import org.w3c.dom.Document;
062:
063: /**
064: * Text message adaptor.
065: *
066: * @author Sun Microsystems Inc.
067: */
068: public class Wsdl11TextMessageAdaptor extends MessageAdaptorImpl {
069: /**
070: * Creates a new Wsdl11TextMessageAdaptor object.
071: */
072: public Wsdl11TextMessageAdaptor() {
073: mLogger.info(mStringTranslator
074: .getString(JMS_TEXT_ADAPTOR_INVOKED));
075: }
076:
077: /**
078: * Gets name.
079: *
080: * @return string.
081: */
082: public String getName() {
083: return MessageProperties.TEXT_MESSAGE;
084: }
085:
086: /**
087: * Convert JMS to NMR msg.
088: *
089: * @param msg JMS msg.
090: * @param exch NMR exchange.
091: */
092: public void convertJMStoNMSMessage(Message msg, MessageExchange exch) {
093: super .convertJMStoNMSMessage(msg, exch);
094:
095: javax.jms.TextMessage textmsg = null;
096: Object obj = getEpilogueProcessor();
097: Wsdl11WrapperHelper helper = null;
098: if (obj != null) {
099: helper = (Wsdl11WrapperHelper) (obj);
100: }
101: try {
102: Source stream = getSource((TextMessage) msg);
103: String status = getJMSStatus(msg);
104: NormalizedMessage nm = null;
105: Fault flt = null;
106: Exception exp = null;
107: Document wrappeddoc = null;
108: if (status.trim().equals(MessageProperties.SUCCESS)) {
109: nm = exch.createMessage();
110:
111: if (helper != null) {
112: if (exch.getRole().equals(
113: MessageExchange.Role.PROVIDER)) {
114: wrappeddoc = helper.wrapMessage(stream, exch
115: .getEndpoint().getServiceName(), exch
116: .getEndpoint().getEndpointName(), exch
117: .getOperation().getLocalPart(), false);
118: } else {
119: wrappeddoc = helper.wrapMessage(stream, exch
120: .getEndpoint().getServiceName(), exch
121: .getEndpoint().getEndpointName(), exch
122: .getOperation().getLocalPart(), true);
123: }
124: }
125: nm.setContent(new DOMSource(wrappeddoc));
126: } else if (status.trim().equals(MessageProperties.FAULT)) {
127: flt = exch.createFault();
128: if (helper != null) {
129: wrappeddoc = helper.wrapFault(stream, exch
130: .getEndpoint().getServiceName(), exch
131: .getEndpoint().getEndpointName(), exch
132: .getOperation().getLocalPart(), null);
133:
134: }
135: flt.setContent(new DOMSource(wrappeddoc));
136: } else if (status.trim().equals(MessageProperties.ERROR)) {
137: String expstring = getSourceAsString(stream);
138:
139: if (expstring == null) {
140: expstring = new String(mStringTranslator
141: .getString(JMS_NO_ERROR_RETURNED));
142: }
143:
144: exp = new Exception(expstring);
145: }
146:
147: if (exch.getRole().equals(MessageExchange.Role.PROVIDER)) {
148: MessageExchangeHelper.updateOutMessage(exch, nm, exp,
149: flt);
150: } else {
151: MessageExchangeHelper.updateInMessage(exch, nm);
152: }
153: } catch (Throwable e) {
154: e.printStackTrace();
155: setError(mStringTranslator.getString(JMS_ERROR_JMS_NMR));
156: setError(e.getMessage());
157: exch = null;
158: }
159: }
160:
161: /**
162: * Convert NMR to JMS msg.
163: *
164: * @param exch NMR msg.
165: * @param msg JMS msg.
166: */
167: public void convertNMStoJMSMessage(MessageExchange exch, Message msg) {
168: super .convertNMStoJMSMessage(exch, msg);
169:
170: javax.jms.TextMessage txtmsg = (javax.jms.TextMessage) msg;
171:
172: NormalizedMessage nm = null;
173: boolean input;
174:
175: if (exch.getRole().equals(MessageExchange.Role.PROVIDER)) {
176: nm = MessageExchangeHelper.getInMessage(exch);
177: input = true;
178: } else {
179: nm = MessageExchangeHelper.getOutMessage(exch);
180: input = false;
181: }
182:
183: Fault flt = MessageExchangeHelper.getFault(exch);
184: Exception exp = MessageExchangeHelper.getError(exch);
185:
186: mLogger.fine("Pattern " + exch.getPattern());
187: mLogger.fine("Service " + exch.getEndpoint().getServiceName());
188:
189: String txt = null;
190: String status = MessageProperties.UNKNOWN;
191: Wsdl11WrapperHelper helper = (Wsdl11WrapperHelper) getEpilogueProcessor();
192:
193: if (nm != null) {
194: /* could be fault or error
195: */
196:
197: Source src = nm.getContent();
198: Document unwrappeddoc = null;
199: if (helper != null) {
200: try {
201:
202: unwrappeddoc = helper.unwrapMessage(src, exch
203: .getEndpoint().getServiceName(), exch
204: .getEndpoint().getEndpointName(), exch
205: .getOperation().getLocalPart(), input);
206: } catch (Exception e) {
207: setException(e);
208: return;
209: }
210: }
211: if (unwrappeddoc != null) {
212: txt = getOutputString(new DOMSource(unwrappeddoc));
213: status = MessageProperties.SUCCESS;
214: }
215: } else if (flt != null) {
216: Source src = flt.getContent();
217: Document unwrappeddoc = null;
218: if (helper != null) {
219: try {
220: unwrappeddoc = helper.unwrapFault(src, exch
221: .getEndpoint().getServiceName(), exch
222: .getEndpoint().getEndpointName(), exch
223: .getOperation().getLocalPart());
224: } catch (Exception e) {
225: setException(e);
226: return;
227: }
228: }
229:
230: if (unwrappeddoc != null) {
231: txt = getOutputString(new DOMSource(unwrappeddoc));
232: status = MessageProperties.FAULT;
233: }
234: } else if (exp != null) {
235: txt = getErrorString(exp);
236: status = MessageProperties.ERROR;
237: } else {
238: txt = new String(mStringTranslator
239: .getString(JMS_NO_ERROR_RETURNED));
240: }
241:
242: setStatus(txtmsg, status);
243:
244: try {
245: txtmsg.setText(txt);
246: } catch (JMSException je) {
247: je.printStackTrace();
248: setException(je);
249: }
250: }
251:
252: /**
253: * Update JMS error msg.
254: *
255: * @param msg JMS msg.
256: * @param obj Object.
257: */
258: public void updateJMSErrorMessage(Message msg, Object obj) {
259: if (obj instanceof java.lang.String) {
260: String s = (String) obj;
261: } else {
262: ;
263: }
264: }
265:
266: /**
267: * Get error message.
268: *
269: * @param e exception.
270: *
271: * @return string.
272: */
273: private String getErrorString(Exception e) {
274: StringBuffer sb = new StringBuffer();
275:
276: StackTraceElement[] stckTrElem = e.getStackTrace();
277:
278: if (stckTrElem != null) {
279: for (int i = 0; i < stckTrElem.length; i++) {
280: String stckTrace = stckTrElem[i].toString();
281: sb.append(stckTrace);
282: sb.append("\n");
283: }
284: }
285:
286: return sb.toString();
287: }
288:
289: /**
290: * Get fault string.
291: *
292: * @param flt fault string.
293: *
294: * @return string.
295: */
296: private String getFaultString(Fault flt) {
297: Source src = flt.getContent();
298:
299: return getSourceAsString(src);
300: }
301:
302: /**
303: * Gets the output string.
304: *
305: * @param nm NM message.
306: *
307: * @return string.
308: */
309: private String getOutputString(Source src) {
310: // Source src = nm.getContent();
311: return getSourceAsString(src);
312: }
313:
314: /**
315: * Returns the source.
316: *
317: * @param textmsg JMS text message.
318: *
319: * @return source.
320: */
321: private Source getSource(TextMessage textmsg) {
322: StreamSource stream = null;
323:
324: try {
325: mLogger.fine("JMS Message *" + textmsg.getText() + "*");
326:
327: String s = textmsg.getText();
328: StringReader reader = new StringReader(s.trim());
329: stream = new StreamSource(reader);
330: } catch (Exception e) {
331: ;
332: }
333:
334: return stream;
335: }
336:
337: /**
338: * Get source as string.
339: *
340: * @param doc soource doc.
341: *
342: * @return string.
343: */
344: private String getSourceAsString(Source doc) {
345: String s = null;
346: StringWriter out = null;
347:
348: try {
349: out = new StringWriter();
350:
351: TransformerFactory tFactory = TransformerFactory
352: .newInstance();
353: Transformer trans = tFactory.newTransformer();
354: StreamResult result = new StreamResult(out);
355: trans.transform(doc, result);
356: s = result.getWriter().toString();
357: out.close();
358: } catch (Throwable t) {
359: t.printStackTrace();
360: s = new String(mStringTranslator.getString(
361: JMS_OUTPUT_MSG_ERROR, t.getMessage()));
362: setError(t.getMessage());
363: }
364:
365: return s;
366: }
367: }
|