KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > google > search > ClientUtil


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
32  * Builds the MessageContext as called by AsynchronousClient
33  * First build the request soap envilope
34  * then build a messageContext and soap envelope is attached to it
35  *
36  * @author Gayan Asanka (gayan@opensource.lk)
37  */

38 public class ClientUtil {
39
40     /** Soap request is included to this and pass it to sendMsg() in AsynchronousClient */
41     //static MessageContext msgContext;
42

43     /**
44      * method getMessageContext
45      *
46      * @return msgContext
47      */

48     public static MessageContext getMessageContext(AsynchronousClient asyncClient)
49             throws DeploymentException {
50         OMNamespace defNs;
51         OMElement operation;
52         MessageContext msgContext = null;
53
54         String JavaDoc 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 JavaDoc elementName,
93                                           String JavaDoc type, String JavaDoc 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