KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > amazon > 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.amazon.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.OMNamespace;
27 import org.apache.axis2.soap.SOAPEnvelope;
28 import org.apache.axis2.soap.SOAPFactory;
29
30 /**
31  * Builds the MessageContext as called by AsynchronousClient
32  * First build the request soap envilope
33  * then build a messageContext and soap envelope is attached to it
34  *
35  * @auther Gayan Asanka (gayan@opensource.lk)
36  */

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

42     private static MessageContext msgContext;
43
44     /**
45      * method getMessageContext
46      *
47      * @return msgContext
48      */

49     public static MessageContext getMessageContext() {
50         OMNamespace namespace,nulNS;
51         OMElement operation,value1,value2;
52         OMElement subValue1,subValue2,subValue3,subValue4,subValue5;
53
54         SOAPFactory omFactory = OMAbstractFactory.getSOAP11Factory();
55         SOAPEnvelope reqEnv = omFactory.getDefaultEnvelope();
56         namespace = reqEnv.declareNamespace("http://schemas.xmlsoap.org/soap/envelope/",
57                 "SOAP-ENV");
58         namespace = reqEnv.declareNamespace("http://schemas.xmlsoap.org/soap/encoding/",
59                 "SOAP-ENC");
60         namespace = reqEnv.declareNamespace("http://www.w3.org/1999/XMLSchema-instance/", "xsi");
61         namespace = reqEnv.declareNamespace("http://www.w3.org/2001/XMLSchema", "xsd");
62         namespace = reqEnv.declareNamespace("http://schemas.xmlsoap.org/wsdl/soap/",
63                 "soap");
64         namespace = reqEnv.declareNamespace("http://schemas.xmlsoap.org/wsdl/",
65                 "wsdl");
66         namespace = reqEnv.declareNamespace("http://webservices.amazon.com/AWSAlexa/2005-02-01",
67                 "tns");
68
69         nulNS = omFactory.createOMNamespace("", "");
70
71         operation = omFactory.createOMElement("Search",
72                 "http://webservices.amazon.com/AWSAlexa/2005-02-01", "ns1");
73         reqEnv.getBody().addChild(operation);
74         operation.addAttribute("encordingStyle", "http://schemas.xmlsoap.org/soap/encoding/", null);
75
76
77         value1 = omFactory.createOMElement("SubscriptionId", nulNS);
78         value1.addChild(omFactory.createText(AsynchronousClient.amazonkey));
79         //this is a valid sample key :- "0Y6WJGPB6TW8AVAHGFR2"));
80

81         value2 = omFactory.createOMElement("Request", nulNS);
82
83         subValue1= omFactory.createOMElement("ResponseGroup", nulNS);
84         subValue1.addChild(omFactory.createText("Web"));
85
86         subValue2 = omFactory.createOMElement("Query", nulNS);
87         subValue2.addChild(omFactory.createText(AsynchronousClient.search));
88
89         subValue3 = omFactory.createOMElement("Count", nulNS);
90         subValue3.addChild(omFactory.createText(AsynchronousClient.maxResults));
91
92         subValue4 = omFactory.createOMElement("IgnoreWords", nulNS);
93         subValue4.addChild(omFactory.createText("90"));
94
95         subValue5 = omFactory.createOMElement("AdultFilter", nulNS);
96         subValue5.addChild(omFactory.createText("yes"));
97
98         value2.addChild(subValue5);
99         value2.addChild(subValue4);
100         value2.addChild(subValue3);
101         value2.addChild(subValue2);
102         value2.addChild(subValue1);
103         operation.addChild(value2);
104         operation.addChild(value1);
105
106         ConfigurationContextFactory fac = new ConfigurationContextFactory();
107         ConfigurationContext configContext = null;
108         try {
109             configContext = fac.buildClientConfigurationContext("Search");
110         } catch (DeploymentException e) {
111             e.printStackTrace();
112         }
113         try {
114             msgContext = new MessageContext(configContext);
115         } catch (AxisFault axisFault) {
116             axisFault.printStackTrace();
117         }
118         msgContext.setEnvelope(reqEnv);
119         return msgContext;
120     }
121
122
123 }
124
125
126
127
Popular Tags