1 13 package com.tonbeller.jpivot.xmla; 14 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import javax.xml.soap.MessageFactory ; 23 import javax.xml.soap.MimeHeaders ; 24 import javax.xml.soap.Name ; 25 import javax.xml.soap.SOAPBody ; 26 import javax.xml.soap.SOAPElement ; 27 import javax.xml.soap.SOAPEnvelope ; 28 import javax.xml.soap.SOAPException ; 29 import javax.xml.soap.SOAPFault ; 30 import javax.xml.soap.SOAPMessage ; 31 import javax.xml.soap.SOAPPart ; 32 33 import org.apache.log4j.Logger; 34 35 import com.tonbeller.wcf.utils.SoftException; 36 37 40 public class SoapUtil { 41 42 public static final Logger logger = Logger.getLogger(SoapUtil.class); 43 44 47 public static void addParameterList( 48 SOAPEnvelope envelope, 49 SOAPElement eParent, 50 String typeName, 51 String listName, 52 Map params) 53 throws SOAPException { 54 Name nPara = envelope.createName(typeName); 55 SOAPElement eType = eParent.addChildElement(nPara); 56 nPara = envelope.createName(listName); 57 SOAPElement eList = eType.addChildElement(nPara); 58 if (params == null) 59 return; 60 Set keys = params.keySet(); 61 Iterator it = keys.iterator(); 62 while (it.hasNext()) { 63 String tag = (String ) it.next(); 64 String value = (String ) params.get(tag); 65 nPara = envelope.createName(tag); 66 SOAPElement eTag = eList.addChildElement(nPara); 67 eTag.addTextNode(value); 68 } 69 } 70 71 74 public static void printReply(SOAPMessage reply) throws SOAPException { 75 System.out.println("Reply:"); 77 SOAPPart sp = reply.getSOAPPart(); 78 SOAPEnvelope envelope = sp.getEnvelope(); 79 SOAPBody body = envelope.getBody(); 80 Iterator itBody = body.getChildElements(); 81 while (itBody.hasNext()) { 82 SOAPElement element = (SOAPElement ) itBody.next(); 83 printElement(element); 84 } 85 System.out.println(); 86 } 87 88 92 static private void printElement(SOAPElement el) { 93 94 System.out.println(el.getElementName() + el.getValue()); 95 Iterator itAtt = el.getAllAttributes(); 96 if (itAtt.hasNext()) { 97 System.out.print("<" + el.getElementName()); 98 while (itAtt.hasNext()) { 99 SOAPElement att = (SOAPElement ) 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 it = el.getChildElements(); 111 while (it.hasNext()) { 112 SOAPElement element = (SOAPElement ) it.next(); 113 printElement(element); 114 } 115 } 116 117 120 public static SOAPMessage createDiscoverMsg( 121 String dataSource, 122 String catalog, 123 String request, 124 Map restrictMap) 125 throws SOAPException { 126 MessageFactory mf = MessageFactory.newInstance(); 127 SOAPMessage msg = mf.createMessage(); 128 129 MimeHeaders mh = msg.getMimeHeaders(); 130 mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Discover\""); 131 132 SOAPPart soapPart = msg.getSOAPPart(); 133 SOAPEnvelope envelope = soapPart.getEnvelope(); 134 SOAPBody body = envelope.getBody(); 135 Name nDiscover = envelope.createName("Discover", "", "urn:schemas-microsoft-com:xml-analysis"); 136 137 SOAPElement eDiscover = body.addChildElement(nDiscover); 138 139 141 Name nPara = envelope.createName("RequestType"); 143 SOAPElement eRequestType = eDiscover.addChildElement(nPara); 144 eRequestType.addTextNode(request); 145 SoapUtil.addParameterList(envelope, eDiscover, "Restrictions", "RestrictionList", restrictMap); 146 147 HashMap pHash = new HashMap (); 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 172 public static boolean soapFault(SOAPMessage reply, String [] faults) throws SOAPException { 173 SOAPPart sp = reply.getSOAPPart(); 174 SOAPEnvelope envelope = sp.getEnvelope(); 175 SOAPBody body = envelope.getBody(); 176 if (!body.hasFault()) 177 return false; 178 SOAPFault 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 addUserPassword(URL url, String user, String password) { 190 try { 191 if (user != null && user.length() > 0) { 192 String 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 (newUri); 198 } 199 return url; 200 } catch (MalformedURLException e) { 201 logger.error("?", e); 202 throw new SoftException(e); 203 } 204 } 205 206 } | Popular Tags |