KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > xmla > SoapUtil


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.xmla;
14
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.xml.soap.MessageFactory JavaDoc;
23 import javax.xml.soap.MimeHeaders JavaDoc;
24 import javax.xml.soap.Name JavaDoc;
25 import javax.xml.soap.SOAPBody JavaDoc;
26 import javax.xml.soap.SOAPElement JavaDoc;
27 import javax.xml.soap.SOAPEnvelope JavaDoc;
28 import javax.xml.soap.SOAPException JavaDoc;
29 import javax.xml.soap.SOAPFault JavaDoc;
30 import javax.xml.soap.SOAPMessage JavaDoc;
31 import javax.xml.soap.SOAPPart JavaDoc;
32
33 import org.apache.log4j.Logger;
34
35 import com.tonbeller.wcf.utils.SoftException;
36
37 /**
38  * static SOAP utility methods
39  */

40 public class SoapUtil {
41
42   public static final Logger logger = Logger.getLogger(SoapUtil.class);
43
44   /**
45    * add a list of Restrictions/Properties ...
46    */

47   public static void addParameterList(
48     SOAPEnvelope JavaDoc envelope,
49     SOAPElement JavaDoc eParent,
50     String JavaDoc typeName,
51     String JavaDoc listName,
52     Map JavaDoc params)
53     throws SOAPException JavaDoc {
54     Name JavaDoc nPara = envelope.createName(typeName);
55     SOAPElement JavaDoc eType = eParent.addChildElement(nPara);
56     nPara = envelope.createName(listName);
57     SOAPElement JavaDoc eList = eType.addChildElement(nPara);
58     if (params == null)
59       return;
60     Set JavaDoc keys = params.keySet();
61     Iterator JavaDoc it = keys.iterator();
62     while (it.hasNext()) {
63       String JavaDoc tag = (String JavaDoc) it.next();
64       String JavaDoc value = (String JavaDoc) params.get(tag);
65       nPara = envelope.createName(tag);
66       SOAPElement JavaDoc eTag = eList.addChildElement(nPara);
67       eTag.addTextNode(value);
68     }
69   }
70
71   /**
72    * print reply to output to System.out
73    */

74   public static void printReply(SOAPMessage JavaDoc reply) throws SOAPException JavaDoc {
75     // Document source, do a transform.
76
System.out.println("Reply:");
77     SOAPPart JavaDoc sp = reply.getSOAPPart();
78     SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
79     SOAPBody JavaDoc body = envelope.getBody();
80     Iterator JavaDoc itBody = body.getChildElements();
81     while (itBody.hasNext()) {
82       SOAPElement JavaDoc element = (SOAPElement JavaDoc) itBody.next();
83       printElement(element);
84     }
85     System.out.println();
86   }
87
88   /**
89    * recursively print element
90    * @param el
91    */

92   static private void printElement(SOAPElement JavaDoc el) {
93
94     System.out.println(el.getElementName() + el.getValue());
95     Iterator JavaDoc itAtt = el.getAllAttributes();
96     if (itAtt.hasNext()) {
97       System.out.print("<" + el.getElementName());
98       while (itAtt.hasNext()) {
99         SOAPElement JavaDoc att = (SOAPElement JavaDoc) itAtt.next();
100         System.out.print(" " + att.getElementName() + "=" + att.getValue());
101       }
102       System.out.println(">");
103     } else {
104       System.out.println("<" + el.getElementName() + ">");
105     }
106
107     System.out.println(el.getValue());
108     System.out.println("</" + el.getElementName() + ">");
109     
110     Iterator JavaDoc it = el.getChildElements();
111     while (it.hasNext()) {
112       SOAPElement JavaDoc element = (SOAPElement JavaDoc) it.next();
113       printElement(element);
114     }
115   }
116
117   /**
118    * run Discover request
119    */

120   public static SOAPMessage JavaDoc createDiscoverMsg(
121     String JavaDoc dataSource,
122     String JavaDoc catalog,
123     String JavaDoc request,
124     Map JavaDoc restrictMap)
125     throws SOAPException JavaDoc {
126     MessageFactory JavaDoc mf = MessageFactory.newInstance();
127     SOAPMessage JavaDoc msg = mf.createMessage();
128
129     MimeHeaders JavaDoc mh = msg.getMimeHeaders();
130     mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Discover\"");
131
132     SOAPPart JavaDoc soapPart = msg.getSOAPPart();
133     SOAPEnvelope JavaDoc envelope = soapPart.getEnvelope();
134     SOAPBody JavaDoc body = envelope.getBody();
135     Name JavaDoc nDiscover = envelope.createName("Discover", "", "urn:schemas-microsoft-com:xml-analysis");
136
137     SOAPElement JavaDoc eDiscover = body.addChildElement(nDiscover);
138
139     // add the parameters
140

141     // <RequestType>request</RequestType>
142
Name JavaDoc nPara = envelope.createName("RequestType");
143     SOAPElement JavaDoc eRequestType = eDiscover.addChildElement(nPara);
144     eRequestType.addTextNode(request);
145     SoapUtil.addParameterList(envelope, eDiscover, "Restrictions", "RestrictionList", restrictMap);
146
147     // <Properties>
148
// <PropertyList>
149
// <DataSourceInfo>Provider=MSOLAP;Data Source=local</DataSourceInfo>
150
// <Catalog>Foodmart 2000</Catalog>
151
// <Format>Tabular</Format>
152
// <Content>SchemaData</Content>
153
// </PropertyList>
154
// </Properties>
155
HashMap JavaDoc pHash = new HashMap JavaDoc();
156     pHash.put("DataSourceInfo", dataSource);
157     pHash.put("Catalog", catalog);
158
159     pHash.put("Format", "Tabular");
160     pHash.put("Content", "SchemaData");
161     SoapUtil.addParameterList(envelope, eDiscover, "Properties", "PropertyList", pHash);
162
163     msg.saveChanges();
164     return msg;
165   }
166
167   /**
168    * check SOAP reply for Error, return fault Code
169    * @param reply the message to check
170    * @param aReturn ArrayList containing faultcode,faultstring,faultactor
171    */

172   public static boolean soapFault(SOAPMessage JavaDoc reply, String JavaDoc[] faults) throws SOAPException JavaDoc {
173     SOAPPart JavaDoc sp = reply.getSOAPPart();
174     SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
175     SOAPBody JavaDoc body = envelope.getBody();
176     if (!body.hasFault())
177       return false;
178     SOAPFault JavaDoc fault = body.getFault();
179
180     faults[0] = fault.getFaultCode();
181     faults[1] = fault.getFaultString();
182     faults[2] = fault.getFaultActor();
183
184     return true;
185
186   }
187   
188   
189   public static URL JavaDoc addUserPassword(URL JavaDoc url, String JavaDoc user, String JavaDoc password) {
190     try {
191       if (user != null && user.length() > 0) {
192         String JavaDoc newUri = url.getProtocol() + "://" + user;
193         if (password != null && password.length() > 0) {
194           newUri += ":" + password;
195         }
196         newUri += "@" + url.getHost() + ":" + url.getPort() + url.getPath();
197         return new URL JavaDoc(newUri);
198       }
199       return url;
200     } catch (MalformedURLException JavaDoc e) {
201       logger.error("?", e);
202       throw new SoftException(e);
203     }
204   }
205
206 } // End SoapUtil
207
Popular Tags