1 16 17 package sample.google.search; 18 19 import org.apache.axis2.context.ConfigurationContext; 20 import org.apache.axis2.context.ConfigurationContextFactory; 21 import org.apache.axis2.context.MessageContext; 22 import org.apache.axis2.deployment.DeploymentException; 23 import org.apache.axis2.engine.AxisFault; 24 import org.apache.axis2.om.OMAbstractFactory; 25 import org.apache.axis2.om.OMElement; 26 import org.apache.axis2.om.OMFactory; 27 import org.apache.axis2.om.OMNamespace; 28 import org.apache.axis2.soap.SOAPEnvelope; 29 import org.apache.axis2.soap.SOAPFactory; 30 31 38 public class ClientUtil { 39 40 41 43 48 public static MessageContext getMessageContext(AsynchronousClient asyncClient) 49 throws DeploymentException { 50 OMNamespace defNs; 51 OMElement operation; 52 MessageContext msgContext = null; 53 54 String str_ST_index = Integer.toString(asyncClient.getStartIndex()); 55 56 defNs = OMAbstractFactory.getSOAP11Factory().createOMNamespace("", ""); 57 SOAPFactory omFactory = OMAbstractFactory.getSOAP11Factory(); 58 SOAPEnvelope envelope = omFactory.getDefaultEnvelope(); 59 envelope.declareNamespace("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV"); 60 envelope.declareNamespace("http://schemas.xmlsoap.org/soap/encoding/", "SOAP-ENC"); 61 envelope.declareNamespace("http://www.w3.org/1999/XMLSchema-instance/", "xsi"); 62 envelope.declareNamespace("http://www.w3.org/1999/XMLSchema", 63 "xsd"); 64 65 operation = omFactory.createOMElement("doGoogleSearch", "urn:GoogleSearch", "ns1"); 66 envelope.getBody().addChild(operation); 67 operation.addAttribute("SOAP-ENV:encordingStyle", 68 "http://schemas.xmlsoap.org/soap/encoding/", null); 69 70 operation.addChild(getOMElement(omFactory, defNs, "key", "xsd:string", asyncClient.getKey())); 71 operation.addChild(getOMElement(omFactory, defNs, "q", "xsd:string", asyncClient.getSearch())); 72 operation.addChild(getOMElement(omFactory, defNs, "start", "xsd:int", str_ST_index)); 73 operation.addChild(getOMElement(omFactory, defNs, "maxResults", "xsd:int", asyncClient.getMaxResults())); 74 operation.addChild(getOMElement(omFactory, defNs, "filter", "xsd:boolean", "true")); 75 operation.addChild(getOMElement(omFactory, defNs, "restrict", "xsd:string", "")); 76 operation.addChild(getOMElement(omFactory, defNs, "safeSearch", "xsd:boolean", "false")); 77 operation.addChild(getOMElement(omFactory, defNs, "lr", "xsd:string", "")); 78 operation.addChild(getOMElement(omFactory, defNs, "ie", "xsd:string", "latin1")); 79 operation.addChild(getOMElement(omFactory, defNs, "oe", "xsd:string", "latin1")); 80 81 ConfigurationContextFactory fac = new ConfigurationContextFactory(); 82 ConfigurationContext configContext = fac.buildClientConfigurationContext("doGoogleSearch"); 83 try { 84 msgContext = new MessageContext(configContext); 85 } catch (AxisFault axisFault) { 86 axisFault.printStackTrace(); 87 } 88 msgContext.setEnvelope(envelope); 89 return msgContext; 90 } 91 92 private static OMElement getOMElement(OMFactory factory, OMNamespace ns, String elementName, 93 String type, String text) { 94 OMElement part = factory.createOMElement(elementName, ns); 95 part.addAttribute("xsi:type", type, null); 96 part.addChild(factory.createText(text)); 97 return part; 98 } 99 } 100 101 102 103 | Popular Tags |