KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > util > SoapUtil


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 17, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.core.util;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25
26 // import org.apache.soap.Envelope;
27
// import org.apache.soap.Fault;
28
import org.pentaho.core.repository.IContentItem;
29 import org.pentaho.core.runtime.IRuntimeContext;
30 import org.pentaho.core.solution.IOutputHandler;
31 import org.pentaho.core.solution.SimpleOutputHandler;
32 import org.pentaho.messages.Messages;
33 import org.pentaho.messages.util.LocaleHelper;
34
35 public class SoapUtil {
36
37     public static void generateSoapResponse(IRuntimeContext context, OutputStream JavaDoc outputStream, SimpleOutputHandler outputHandler, OutputStream JavaDoc contentStream, List JavaDoc messages) {
38
39         StringBuffer JavaDoc messageBuffer = new StringBuffer JavaDoc();
40         generateSoapResponse(context, outputHandler, contentStream, messageBuffer, messages);
41         try {
42             outputStream.write(messageBuffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
43         } catch (IOException JavaDoc e) {
44             // TODO log thid
45
}
46     }
47
48     public static String JavaDoc getSoapHeader() {
49         return "<SOAP-ENV:Envelope " + //$NON-NLS-1$
50
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " + //$NON-NLS-1$
51
"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n " + //$NON-NLS-1$
52
"<SOAP-ENV:Body>\n"; //$NON-NLS-1$
53

54     }
55
56     public static String JavaDoc getSoapFooter() {
57         return "</SOAP-ENV:Body>\n</SOAP-ENV:Envelope>"; //$NON-NLS-1$
58

59     }
60
61     public static void generateSoapError(StringBuffer JavaDoc messageBuffer, List JavaDoc messages) {
62
63         String JavaDoc message = UIUtil.getFirstError(messages);
64
65         messageBuffer.append("<SOAP-ENV:Fault>\n"); //$NON-NLS-1$
66

67         if (message == null) {
68             message = Messages.getErrorString("SoapUtil.ERROR_0001_UNKNOWN_ERROR"); //$NON-NLS-1$
69
}
70
71         // Envelope envelope = new Envelope();
72
// Fault fault = new Fault( );
73
// TODO: Generate the following message using the envelope and fault objects
74

75         
76         // TODO determine if this is a reciever or a sender problem by examining
77
// the error code
78
boolean senderFault = (message.indexOf("SolutionEngine.ERROR_0002") != -1) || //$NON-NLS-1$ // solution not specifed
79
(message.indexOf("SolutionEngine.ERROR_0003") != -1) || //$NON-NLS-1$ // Path not specifeid
80
(message.indexOf("SolutionEngine.ERROR_0004") != -1) || //$NON-NLS-1$ // Action not specified
81
(message.indexOf("SolutionEngine.ERROR_0005") != -1); //$NON-NLS-1$ // Action not found
82
// send the error code
83
// TODO parse out the error code
84
messageBuffer.append("<SOAP-ENV:faultcode>\n <SOAP-ENV:Subcode>\n<SOAP-ENV:Value><![CDATA[" + message + "]]></SOAP-ENV:Value>\n </SOAP-ENV:Subcode>\n </SOAP-ENV:faultcode>"); //$NON-NLS-1$ //$NON-NLS-2$
85

86         if (senderFault) {
87             messageBuffer.append("<SOAP-ENV:faultactor>SOAP-ENV:Client</SOAP-ENV:faultactor>\n"); //$NON-NLS-1$
88
} else {
89             messageBuffer.append("<SOAP-ENV:faultactor>SOAP-ENV:Server</SOAP-ENV:faultactor>\n"); //$NON-NLS-1$
90
}
91
92         // send the error reason
93
messageBuffer.append("<SOAP-ENV:faultstring><SOAP-ENV:Text xml:lang=\"" + LocaleHelper.getDefaultLocale().toString() + "\"><![CDATA[" + message + "]]></SOAP-ENV:Text>\n </SOAP-ENV:faultstring>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
94

95         // send the details
96
messageBuffer.append("<SOAP-ENV:Detail>"); //$NON-NLS-1$
97

98         Iterator JavaDoc messageIterator = messages.iterator();
99         while (messageIterator.hasNext()) {
100             messageBuffer.append("<message name=\"trace\"><![CDATA[" + (String JavaDoc) messageIterator.next() + "]]></message>\n"); //$NON-NLS-1$ //$NON-NLS-2$
101
}
102         messageBuffer.append("</SOAP-ENV:Detail>"); //$NON-NLS-1$
103

104         messageBuffer.append("</SOAP-ENV:Fault>\n"); //$NON-NLS-1$
105

106     }
107
108     public static String JavaDoc openSoapResponse() {
109         return "<ExecuteActivityResponse xmlns:m=\"http://pentaho.org\">\n"; //$NON-NLS-1$
110
}
111
112     public static String JavaDoc closeSoapResponse() {
113         return "</ExecuteActivityResponse>\n"; //$NON-NLS-1$
114
}
115
116     public static void generateSoapResponse(IRuntimeContext context, SimpleOutputHandler outputHandler, OutputStream JavaDoc contentStream, StringBuffer JavaDoc messageBuffer, List JavaDoc messages) {
117         // we need to generate a soap package for this
118
// the outputs of the action need to be put into the package
119

120         /*
121          * This is the format of the response message package
122          *
123          *
124          * <SOAP-ENV:Envelope
125          * xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
126          * SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
127          * <SOAP-ENV:Body> <m:ExecuteActivityResponse
128          * xmlns:m="http://pentaho.org"> <result>...</result> <result>...</result>
129          * <result>...</result> </m:ExecuteActivityResponse> </SOAP-ENV:Body>
130          * </SOAP-ENV:Envelope>
131          *
132          */

133
134         if (context == null || context.getStatus() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
135             generateSoapError(messageBuffer, messages);
136         } else {
137
138             messageBuffer.append(openSoapResponse());
139
140             IContentItem contentItem = outputHandler.getFeedbackContentItem();
141
142             // hmm do we need this to be ordered?
143
Set JavaDoc outputNames = context.getOutputNames();
144
145             Iterator JavaDoc outputNameIterator = outputNames.iterator();
146             while (outputNameIterator.hasNext()) {
147                 String JavaDoc outputName = (String JavaDoc) outputNameIterator.next();
148                 contentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT);
149                 if (outputNames.size() == 1 && contentItem != null) {
150                     String JavaDoc mimeType = contentItem.getMimeType();
151                     if (mimeType != null && mimeType.startsWith("text/")) { //$NON-NLS-1$
152
if (mimeType.equals("text/xml")) { //$NON-NLS-1$
153
// this should be ok to embed directly, any CDATA
154
// sections in the XML will be ok
155
messageBuffer.append("<").append(outputName).append(">") //$NON-NLS-1$//$NON-NLS-2$
156
.append(contentStream.toString()).append("</").append(outputName).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
157
} else if (mimeType.startsWith("text/")) { //$NON-NLS-1$
158
// put this is a CDATA section and hope it does not
159
// contain the string ']]>'
160
messageBuffer.append("<").append(outputName).append("><![CDATA[") //$NON-NLS-1$ //$NON-NLS-2$
161
.append(contentStream.toString()).append("]]></").append(outputName).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
162
}
163                     } else {
164                         Object JavaDoc value = context.getOutputParameter(outputName).getValue();
165                         if (value == null) {
166                             value = ""; //$NON-NLS-1$
167
}
168                         messageBuffer.append(SoapHelper.toSOAP(outputName, value));
169                     }
170                 } else {
171                     Object JavaDoc value = context.getOutputParameter(outputName).getValue();
172                     if (value == null) {
173                         value = ""; //$NON-NLS-1$
174
}
175                     messageBuffer.append(SoapHelper.toSOAP(outputName, value));
176                 }
177             }
178             messageBuffer.append(closeSoapResponse());
179         }
180     }
181
182 }
183
Popular Tags