KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jbi > serviceengine > util > soap > MessageDenormalizerImpl


1 // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
2

3 /*
4  * MessageDenormalizerImpl.java
5  *
6  * SUN PROPRIETARY/CONFIDENTIAL.
7  * This software is the proprietary information of Sun Microsystems, Inc.
8  * Use is subject to license terms.
9  *
10  */

11 package com.sun.enterprise.jbi.serviceengine.util.soap;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.io.Writer JavaDoc;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import javax.jbi.messaging.Fault;
25 import javax.jbi.messaging.NormalizedMessage;
26
27 import javax.xml.soap.MessageFactory JavaDoc;
28 import javax.xml.soap.SOAPException JavaDoc;
29 import javax.xml.soap.SOAPMessage JavaDoc;
30 import javax.xml.soap.SOAPHeader JavaDoc;
31 import javax.xml.soap.AttachmentPart JavaDoc;
32 import javax.xml.transform.Result JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import javax.xml.transform.Transformer JavaDoc;
35 import javax.xml.transform.TransformerFactory JavaDoc;
36 import javax.xml.transform.dom.DOMSource JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
39
40 import javax.activation.DataHandler JavaDoc;
41
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * This Basic Profile 1.0 aware implementation is used to denormalize a JBI Normalized
46  * Message and convert it into a SOAP message format.
47  *
48  * @author Sun Microsystems, Inc.
49  */

50 public class MessageDenormalizerImpl implements MessageDenormalizer
51 {
52     /**
53      * Namespace prefix for the payload.
54      */

55     private static final String JavaDoc PAYLOAD_NAMESPACE_PREFIX = "jbisb0";
56
57     /**
58      * SOAP Namespace prefix.
59      */

60     private static final String JavaDoc SOAP_NAMESPACE_PREFIX = "soap";
61
62     /**
63      * XML Schema Instance prefix.
64      */

65     private static final String JavaDoc XML_SCHEMA_INSTANCE_NAMESPACE_PREFIX = "xsi";
66
67     /**
68      * Internal handle to the message factory
69      */

70     private MessageFactory JavaDoc mMessageFactory;
71
72     /**
73      * Internal handle to the logger instance
74      */

75     private Logger JavaDoc mLogger;
76
77     /**
78      * Internal handle to String Translator instance.
79      */

80     private StringTranslator mStringTranslator;
81
82     /**
83      * Internal handle to the transformer instance
84      */

85     private Transformer JavaDoc mTransformer;
86
87     /**
88      * Creates a new instance of MessageDenormalizerImpl.
89      */

90     public MessageDenormalizerImpl()
91     {
92         try
93         {
94             mLogger = Logger.getLogger(this.getClass().getPackage().getName());
95             mStringTranslator = new StringTranslator(this.getClass().getPackage().getName(), this.getClass().getClassLoader());
96             mMessageFactory = MessageFactory.newInstance();
97
98             TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
99             mTransformer = transformerFactory.newTransformer();
100             mTransformer.setOutputProperty("method", "xml");
101             mTransformer.setOutputProperty("omit-xml-declaration", "yes");
102         }
103         catch (Exception JavaDoc exception)
104         {
105             // This should not happen. In case it does, log the exception and
106
// set the factory object to null
107
mLogger.severe( mStringTranslator.getString("SBC_MESSAGE_FACTORY_CREATION_FAILURE") );
108         mLogger.severe( mStringTranslator.getString("SBC_ERROR_DETAILS", exception.toString()) );
109             mMessageFactory = null;
110             mTransformer = null;
111         }
112     }
113
114     /**
115      * Converts a JBI normalized message to a <code> javax.jbi.soap.SOAPMessage </code>
116      * instance. The SOAP Header information is extracted from the NormalizedMessage
117      * property "SoapHeader" and the SOAP Body content is extracted from the Normalized
118      * Message content. Any attachments present in the NormalizedMessage are also
119      * denormalized and added to the created <code> javax.jbi.soap.SOAPMessage </code>
120      * instance.
121      *
122      * @param normalizedMessage message to be denormalized.
123      * @param operation operation invoked
124      * @param isResponse indicates if a response messages needs to be generated
125      *
126      * @return the SOAP Message.
127      */

128     public SOAPWrapper denormalizeMessage(
129         NormalizedMessage normalizedMessage, Operation operation, boolean isResponse)
130     {
131         SOAPWrapper wrapper = null;
132         Writer JavaDoc writer = null;
133     mLogger.fine( mStringTranslator.getString("SBC_DENORMALIZE_JBI_MESSAGE") );
134
135         try
136         {
137             // Create a SOAP Message
138
ByteArrayOutputStream JavaDoc bufferedStream = new ByteArrayOutputStream JavaDoc();
139             writer = new OutputStreamWriter JavaDoc(bufferedStream, "UTF-8");
140
141             writeEnvelopeHeader(writer);
142
143             if ( normalizedMessage != null)
144             {
145                 // Writer the header to the writer instance.
146
writeHeader(normalizedMessage, writer);
147             }
148
149             // Extract the body information from the Normalized Message
150
writeBody(normalizedMessage, operation, isResponse, writer);
151             writeEnvelopeFooter(writer);
152             writer.flush();
153
154             // Create a soap message
155
SOAPMessage JavaDoc soapMessage = createSOAPMessage(bufferedStream);
156
157             // Denormalize Attachments
158
denormalizeAttachments ( soapMessage, normalizedMessage);
159             // Create a soap response wrapper
160
wrapper = new SOAPWrapper(soapMessage);
161
162             if (normalizedMessage instanceof Fault)
163             {
164                 wrapper.setStatus(SOAPConstants.JBI_FAULT);
165             }
166             else
167             {
168                 wrapper.setStatus(SOAPConstants.JBI_SUCCESS);
169             }
170         }
171         catch (RuntimeException JavaDoc runtimeException)
172         {
173         mLogger.severe( mStringTranslator.getString("SBC_DENORMALIZE_JBI_MESSAGE_FAILURE_RT_EXP") );
174             // Create a soap fault wrapper
175
wrapper = denormalizeMessage(runtimeException);
176         }
177         catch (Exception JavaDoc exception)
178         {
179         mLogger.warning( mStringTranslator.getString("SBC_DENORMALIZE_JBI_MESSAGE_FAILURE_EXP") );
180         mLogger.warning( mStringTranslator.getString("SBC_ERROR_DETAILS", exception.toString()) );
181         mLogger.warning( mStringTranslator.getString("SBC_CREATE_SOAP_FAULT") );
182
183             // Create a soap fault wrapper
184
wrapper = denormalizeMessage(exception);
185         }
186         finally
187         {
188             closeWriter(writer);
189         }
190
191     mLogger.fine( mStringTranslator.getString("SBC_SUCCESS_DENORMALIZE_JBI_MESSAGE") );
192
193         return wrapper;
194     }
195
196     /**
197      * Converts an exception to a SOAP Message. It uses the default Server fault code
198      * for denormalization.
199      *
200      * @param exception exception instance
201      *
202      * @return denormalized exception object
203      */

204     public SOAPWrapper denormalizeMessage(Exception JavaDoc exception)
205     {
206         return denormalizeMessage(exception, SOAPConstants.SERVER_FAULT_CODE);
207     }
208
209     /**
210      * Converts an exception to a SOAP Message using the provided faultCode. The code
211      * expects the faultcode passed to be part of the soap namespace.
212      *
213      * @param exception exception instance
214      * @param faultCode fault code
215      *
216      * @return denormalized exception object
217      */

218     public SOAPWrapper denormalizeMessage(Exception JavaDoc exception, String JavaDoc faultCode)
219     {
220         SOAPWrapper wrapper = null;
221         Writer JavaDoc writer = null;
222
223     mLogger.fine( mStringTranslator.getString("SBC_DENORMALIZE_EXCEPTION") );
224
225         try
226         {
227             // Create the ws-i compliant fault message from the exception
228
ByteArrayOutputStream JavaDoc bufferedStream = new ByteArrayOutputStream JavaDoc();
229             writer = new OutputStreamWriter JavaDoc(bufferedStream, "UTF-8");
230
231             if (exception == null)
232             {
233         mLogger.warning( mStringTranslator.getString("SBC_NULL_OBJECT_DENORMALIZATION") );
234             }
235
236             writeEnvelopeHeader(writer);
237             writer.write("<" + SOAP_NAMESPACE_PREFIX + ":Body>");
238             writeFault(exception, faultCode, writer);
239             writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Body>");
240             writeEnvelopeFooter(writer);
241             writer.flush();
242
243             // Create a soap message
244
SOAPMessage JavaDoc soapMessage = createSOAPMessage(bufferedStream);
245
246             // Create a SOAP wrapper with service url as null
247
wrapper = new SOAPWrapper(soapMessage);
248             wrapper.setStatus(SOAPConstants.JBI_ERROR);
249         }
250         catch (RuntimeException JavaDoc runtimeException)
251         {
252         mLogger.severe( mStringTranslator.getString( "SBC_SOAP_FAULT_GENERATION_FAILURE_RT_EXP") );
253         }
254         catch (Exception JavaDoc denormalizationException)
255         {
256             // This should not happen. In case it does do nothing. Log message
257
mLogger.severe( mStringTranslator.getString("SBC_SOAP_FAULT_GENERATION_FAILURE") );
258         }
259         finally
260         {
261             closeWriter(writer);
262         }
263
264     mLogger.fine( mStringTranslator.getString("SBC_SUCCESS_DENORMALIZE_EXCEPTION") );
265
266         return wrapper;
267     }
268
269     /**
270      * This method extracts the payload from the Normalized Message and writes it
271      * using the writer stream. The payload content is enclosed between the SOAP:Body
272      * header and SOAP:Body footer information.
273      *
274      * @param normalizedMessage normalized message
275      * @param operation operation invoked
276      * @param isResponse indicates if a response messages needs to be generated
277      * @param writer writer object to be used
278      *
279      * @throws Exception if the body cannot be written
280      */

281     protected void writeBody(
282         NormalizedMessage normalizedMessage, Operation operation, boolean isResponse,
283         Writer JavaDoc writer) throws Exception JavaDoc
284     {
285         StringWriter JavaDoc stringWriter = null;
286
287         try
288         {
289             boolean isEmptyResponse = isResponse && ( normalizedMessage == null );
290             // Add the body information
291
writeBodyHeader(operation, writer, isEmptyResponse);
292             if ( normalizedMessage != null)
293             {
294                 stringWriter = new StringWriter JavaDoc();
295                 Result JavaDoc result = new StreamResult JavaDoc(stringWriter);
296                 mTransformer.transform(normalizedMessage.getContent(), result);
297                 writer.write(stringWriter.toString());
298             }
299             writeBodyFooter(operation, writer, isEmptyResponse);
300             writer.flush();
301         }
302         finally
303         {
304             closeWriter(stringWriter);
305         }
306     }
307
308     /**
309      * The method extracts the header information from the Normalized Message property
310      * "SoapHeader" and writes it using the writer instance. The header information
311      * is expected to be propagated as a <code> javax.xml.soap.SOAPHeader </code>
312      * implementation instance.
313      *
314      * @param normalizedMessage normalizedMessage
315      * @param writer writer object to be used
316      *
317      * @throws Exception if header cannot be used to write to the writer instance
318      */

319     protected void writeHeader(NormalizedMessage normalizedMessage, Writer JavaDoc writer)
320         throws Exception JavaDoc
321     {
322         // Extract header information from the Normalized Message
323
SOAPHeader JavaDoc soapHeader =
324             (SOAPHeader JavaDoc) normalizedMessage.getProperty(
325                 SOAPConstants.HEADER_PROPERTY_NAME );
326         StringWriter JavaDoc stringWriter = null;
327
328         if ( soapHeader != null)
329         {
330             try
331             {
332                 stringWriter = new StringWriter JavaDoc();
333
334                 Source JavaDoc source = new DOMSource JavaDoc( soapHeader );
335                 Result JavaDoc result = new StreamResult JavaDoc(stringWriter);
336                 mTransformer.transform(source, result);
337
338                 // Add the header information
339
writer.write("<" + SOAP_NAMESPACE_PREFIX + ":Header>");
340                 writer.write(stringWriter.toString());
341                 writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Header>");
342                 writer.flush();
343             }
344             finally
345             {
346                 closeWriter(stringWriter);
347             }
348         }
349         else
350         {
351         mLogger.fine( mStringTranslator.getString("SBC_NO_HEADER") );
352         }
353     }
354
355     /**
356      * Uses the writer object to write the SOAP:Body header information. This method
357      * is invoked before the body payload is written.
358      *
359      * @param operation operation invoked
360      * @param writer writer object to be used
361      * @param isEmptyResponse indicates if an empty response message needs to be generated
362      *
363      * @throws Exception if body header cannot be written.
364      */

365     protected void writeBodyHeader(
366         Operation operation, Writer JavaDoc writer, boolean isEmptyResponse)
367         throws Exception JavaDoc
368     {
369         writer.write("<" + SOAP_NAMESPACE_PREFIX + ":Body>");
370
371         if ( isEmptyResponse)
372         {
373             writer.write("<" + PAYLOAD_NAMESPACE_PREFIX + ":");
374             writer.write(operation.getName() + "Response");
375             writer.write(" xmlns:" + PAYLOAD_NAMESPACE_PREFIX + "=\"");
376             writer.write(operation.getOutputNamespace() + "\"");
377             writer.write(">");
378         }
379
380         writer.flush();
381     }
382
383     /**
384      * Uses writer object to write the SOAP:Body footer information. This method is
385      * invoked after the body payload has been written.
386      *
387      * @param operation operation invoked
388      * @param writer writer object.
389      * @param isEmptyResponse indicates if a response messages needs to be generated
390      *
391      * @throws Exception if body footer cannot be written
392      */

393     protected void writeBodyFooter(
394         Operation operation, Writer JavaDoc writer, boolean isEmptyResponse)
395         throws Exception JavaDoc
396     {
397
398         if ( isEmptyResponse )
399         {
400             writer.write("</" + PAYLOAD_NAMESPACE_PREFIX + ":");
401             writer.write(operation.getName() + "Response>");
402         }
403         writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Body>");
404         writer.flush();
405     }
406
407     /**
408      * Uses the provided input data to create a <code> javax.xml.soap.SOAPMessage </code>
409      * instance.
410      *
411      * @param byteStream Stream which contains the soap messages information as bytes.
412      *
413      * @return SOAP Message object
414      *
415      * @throws SOAPException if soap message object cannot be created.
416      * @throws IOException if soap message object cannot be created.
417      */

418     protected SOAPMessage JavaDoc createSOAPMessage(ByteArrayOutputStream JavaDoc byteStream)
419         throws SOAPException JavaDoc, IOException JavaDoc
420     {
421         if (mLogger.isLoggable(Level.FINEST))
422         {
423         mLogger.finest( mStringTranslator.getString("SBC_DEONRMALIZED_MESSAGE_DETAILS", byteStream.toString()) );
424         }
425
426         // Create a soap message
427
SOAPMessage JavaDoc soapMessage = mMessageFactory.createMessage();
428
429         // Populate the fault message in the soap Message
430
byte[] data = byteStream.toByteArray();
431         ByteArrayInputStream JavaDoc soapInputStream = new ByteArrayInputStream JavaDoc(data);
432         StreamSource JavaDoc streamSource = new StreamSource JavaDoc(soapInputStream);
433         soapMessage.getSOAPPart().setContent(streamSource);
434         soapInputStream.close();
435
436         return soapMessage;
437     }
438
439     /**
440      * Closes the writer instance. This method handles any exceptions thrown
441      * while handling this request.
442      *
443      * @param writer writer instance.
444      */

445     protected void closeWriter(Writer JavaDoc writer)
446     {
447         if (writer != null)
448         {
449             try
450             {
451                 writer.close();
452             }
453             catch (Exception JavaDoc ioException)
454             {
455                 // This should not happen. In case it does do nothing
456
mLogger.warning( mStringTranslator.getString("SBC_CLOSE_OUTPUT_STREAM") );
457         mLogger.warning( mStringTranslator.getString("SBC_ERROR_DETAILS", ioException.toString()) );
458             }
459         }
460     }
461
462     /**
463      * Uses writer object to write the SOAP:Envelope header information. This method
464      * is invoked before writing the envelope content ( header and body content).
465      *
466      * @param writer writer object.
467      *
468      * @throws IOException if envelope header information cannot be written.
469      */

470     protected void writeEnvelopeHeader(Writer JavaDoc writer) throws IOException JavaDoc
471     {
472         // Write the soap envelope
473
writer.write(
474             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<" + SOAP_NAMESPACE_PREFIX +
475             ":Envelope xmlns:" + SOAP_NAMESPACE_PREFIX +
476             "=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:" +
477             XML_SCHEMA_INSTANCE_NAMESPACE_PREFIX +
478             "=\"http://www.w3.org/2001/XMLSchema-instance\">");
479         writer.flush();
480     }
481
482     /**
483      * Uses writer object to write the SOAP:Envelope footer information. This method
484      * is invoked after writing the envelope content ( header and body content).
485      *
486      * @param writer writer object
487      *
488      * @throws IOException if envelope footer information cannot be written.
489      */

490     protected void writeEnvelopeFooter(Writer JavaDoc writer) throws IOException JavaDoc
491     {
492         writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Envelope>");
493         writer.flush();
494     }
495
496     /**
497      * Create the SOAP:Fault message based on the provided exception details and writes
498      * it using the writer instance.
499      *
500      * @param exception exception thrown
501      * @param faultCode fault code
502      * @param writer writer object
503      *
504      * @throws IOException if fault message cannot be generated.
505      */

506     protected void writeFault(Exception JavaDoc exception, String JavaDoc faultCode, Writer JavaDoc writer)
507         throws IOException JavaDoc
508     {
509         writer.write(
510             "<" + SOAP_NAMESPACE_PREFIX + ":Fault><faultcode>" + SOAP_NAMESPACE_PREFIX +
511             ":" + faultCode + "</faultcode>");
512
513         if (exception != null)
514         {
515             writer.write("<faultstring>" +
516                          sanitizeMessage(exception.getMessage()) + "</faultstring>");
517         }
518
519         writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Fault>");
520         writer.flush();
521     }
522
523     /**
524      * Converts a JBI Fault mesage to a standard <code> javax.xml.soap.SOAPMessage </code>
525      * message instance. It uses the default Server fault code for denormalization.
526      *
527      * @param faultMessage JBI fault message.
528      *
529      * @return a new SOAPWrapper instance which contains the SOAP fault Message.
530      */

531     public SOAPWrapper denormalizeFaultMessage(Fault faultMessage)
532     {
533         return denormalizeFaultMessage(faultMessage, SOAPConstants.SERVER_FAULT_CODE);
534     }
535
536     /**
537      * Converts a JBI Fault mesage to a SOAP Message using the specified fault code.
538      *
539      * @param faultMessage JBI fault message.
540      * @param faultCode fault code to be used in the fault message
541      *
542      * @return a new SOAPWrapper instance which contains the SOAP fault Message.
543      */

544     public SOAPWrapper denormalizeFaultMessage(Fault faultMessage, String JavaDoc faultCode)
545     {
546         SOAPWrapper wrapper = null;
547         Writer JavaDoc writer = null;
548
549     mLogger.fine( mStringTranslator.getString("SBC_DENORMALIZE_FAULT_MESSAGE") );
550
551         try
552         {
553             // Create the ws-i compliant fault message from the exception
554
ByteArrayOutputStream JavaDoc bufferedStream = new ByteArrayOutputStream JavaDoc();
555             String JavaDoc messageFaultCode = (String JavaDoc) faultMessage.getProperty(
556                                         SOAPConstants.FAULT_CODE_PROPERTY_NAME);
557             String JavaDoc faultString = (String JavaDoc) faultMessage.getProperty(
558                                         SOAPConstants.FAULT_STRING_PROPERTY_NAME);
559
560             if ( messageFaultCode != null )
561             {
562                 // Override the fault code with the message fault code.
563
faultCode = messageFaultCode;
564             }
565
566             if ( faultString == null )
567             {
568                 faultString = mStringTranslator.getString("SBC_DEFAULT_FAULT_STRING");
569             }
570             writer = new OutputStreamWriter JavaDoc(bufferedStream, "UTF-8");
571             writeEnvelopeHeader(writer);
572             writer.write("<" + SOAP_NAMESPACE_PREFIX + ":Body>");
573             writer.write(
574                 "<" + SOAP_NAMESPACE_PREFIX + ":Fault " +
575                 XML_SCHEMA_INSTANCE_NAMESPACE_PREFIX + ":type=\"" +
576                 SOAP_NAMESPACE_PREFIX + ":Fault\"" + "><faultcode>" +
577                 SOAP_NAMESPACE_PREFIX + ":" + faultCode + "</faultcode>");
578             writer.write("<faultstring>" + faultString + "</faultstring>");
579             writeFaultDetail(faultMessage, writer);
580             writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Fault>");
581             writer.write("</" + SOAP_NAMESPACE_PREFIX + ":Body>");
582             writeEnvelopeFooter(writer);
583             writer.flush();
584
585             // Create a soap message
586
SOAPMessage JavaDoc soapMessage = createSOAPMessage(bufferedStream);
587
588             // Create a SOAP wrapper with service url as null
589
wrapper = new SOAPWrapper(soapMessage);
590             wrapper.setStatus(SOAPConstants.JBI_FAULT);
591         }
592         catch (RuntimeException JavaDoc runtimeException)
593         {
594         mLogger.severe( mStringTranslator.getString( "SBC_SOAP_FAULT_GENERATION_FAILURE_RT_EXP") );
595         }
596         catch (Exception JavaDoc exception)
597         {
598             // This should not happen. In case it does do nothing. Log message
599
mLogger.severe( mStringTranslator.getString("SBC_SOAP_FAULT_GENERATION_FAILURE") );
600         }
601         finally
602         {
603             closeWriter(writer);
604         }
605
606     mLogger.fine( mStringTranslator.getString("SBC_SUCCESS_DENORMALIZE_FAULT") );
607
608         return wrapper;
609     }
610
611     /**
612      * Writes the detailed fault message using the provided writer instance.
613      *
614      * @param faultMessage JBI Fault object which contains the fault details.
615      * @param writer writer object to be used.
616      *
617      * @throws Exception if the fault detail vould not be written.
618      */

619     private void writeFaultDetail(Fault faultMessage, Writer JavaDoc writer)
620         throws Exception JavaDoc
621     {
622         StringWriter JavaDoc stringWriter = null;
623
624         try
625         {
626             stringWriter = new StringWriter JavaDoc();
627
628             Result JavaDoc result = new StreamResult JavaDoc(stringWriter);
629             mTransformer.transform(faultMessage.getContent(), result);
630
631             // Add the fault detail
632
String JavaDoc detailString = stringWriter.toString().trim();
633
634             if (!detailString.equals(""))
635             {
636                 writer.write("<detail>");
637                 writer.write(detailString);
638                 writer.write("</detail>");
639                 writer.flush();
640             }
641         }
642         finally
643         {
644             closeWriter(stringWriter);
645         }
646     }
647
648     /**
649      * Sanitizes the messages so that it can be properly read by an XML parser.
650      *
651      * @param errorMessage error message to be sanitized.
652      *
653      * @return sanitized error message.
654      */

655     protected String JavaDoc sanitizeMessage(String JavaDoc errorMessage)
656     {
657         StringBuffer JavaDoc sanitizedBuffer = new StringBuffer JavaDoc();
658
659         for (int i = 0; (errorMessage != null) && (i < errorMessage.length()); i++)
660         {
661             char currentChar = errorMessage.charAt(i);
662
663             switch (currentChar)
664             {
665             case '"':
666                 sanitizedBuffer.append("&quot;");
667
668                 break;
669
670             case '&':
671                 sanitizedBuffer.append("&amp;");
672
673                 break;
674
675             case '<':
676                 sanitizedBuffer.append("&lt;");
677
678                 break;
679
680             case '>':
681                 sanitizedBuffer.append("&gt;");
682
683                 break;
684
685             default:
686                 sanitizedBuffer.append(currentChar);
687             }
688         }
689
690         if (errorMessage == null)
691         {
692             return "INTERNAL SERVER ERROR";
693         }
694         else
695         {
696             return sanitizedBuffer.toString();
697         }
698     }
699
700     /**
701      * Denormalizes the attachments present in the JBI Normalized Message and adds
702      * them to the <code> javax.xml.soap.SoapMessage </code> instance.
703      *
704      * @param soapMessage soap message.
705      * @param normalizedMessage normalized message instance.
706      */

707     private void denormalizeAttachments ( SOAPMessage JavaDoc soapMessage,
708                                           NormalizedMessage normalizedMessage)
709     {
710         if ( normalizedMessage != null )
711         {
712             Iterator JavaDoc attachmentIter = normalizedMessage.getAttachmentNames().iterator();
713             for (; attachmentIter.hasNext();)
714             {
715                 String JavaDoc attachmentIdentifier = (String JavaDoc) attachmentIter.next();
716                 DataHandler JavaDoc dataHandler =
717                             normalizedMessage.getAttachment( attachmentIdentifier);
718                 AttachmentPart JavaDoc attachment =
719                     soapMessage.createAttachmentPart( dataHandler);
720                 attachment.setContentId ( attachmentIdentifier);
721                 attachment.setContentType ( dataHandler.getContentType());
722                 soapMessage.addAttachmentPart ( attachment );
723             }
724         }
725     }
726 }
727
Popular Tags