KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > amazon > search > ClientCallbackHandler


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.clientapi.AsyncResult;
20 import org.apache.axis2.clientapi.Callback;
21 import org.apache.axis2.om.OMElement;
22 import org.apache.axis2.om.OMNode;
23 import org.apache.axis2.om.OMOutput;
24 import org.apache.axis2.soap.SOAPEnvelope;
25
26 import javax.xml.stream.FactoryConfigurationError;
27 import javax.xml.stream.XMLOutputFactory;
28 import javax.xml.stream.XMLStreamException;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * This class implements the onComplete method extended by call back
33  * recieves the Response
34  * process the soap with OM to extract the data
35  * Find the <NavigationURL> element and get the text from it
36  *
37  * @auther Gayan Asanka (gayan@opensource.lk)
38  */

39
40 public class ClientCallbackHandler extends Callback {
41
42     /**
43      * HTML Header to desplay snippet text
44      */

45     private String JavaDoc beginHTML = "<HTML><HEAD><TITLE>Wow</TITLE></HEAD><BODY>";
46
47     /**
48      * HTML footer
49      */

50     private String JavaDoc endHTML = "</BODY></HTML>";
51
52     /**
53      * Store the URLs read by soap
54      */

55     private String JavaDoc strURL = beginHTML;
56
57     /**
58      * Stores the response
59      */

60     private AsyncResult myResult;
61
62     /**
63      * method onComplete
64      *
65      * @param result
66      */

67     public void onComplete(AsyncResult result) {
68          System.out.println("Responce message received to the ClientCallbackHandler ...");
69         try {
70             OMOutput omOutput = new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out));
71             result.getResponseEnvelope().serialize(omOutput);
72             omOutput.flush();
73         } catch (XMLStreamException e) {
74             System.out.println("Error occured after responce is received");
75             e.printStackTrace();
76         } catch (FactoryConfigurationError e) {
77             System.out.println("Error occured after responce is received");
78             e.printStackTrace();
79         }
80         myResult = result;
81         extractDetails(myResult);
82     }
83
84     /**
85      * method extractDetails
86      *
87      * @param result
88      */

89     private void extractDetails(AsyncResult result) {
90         Iterator JavaDoc iterator0, iterator1, iterator2, iterator3, iterator4;
91         SOAPEnvelope resEnvilop;
92         OMElement body;
93         OMElement operation;
94         OMNode node;
95         OMElement elem;
96
97         resEnvilop = result.getResponseEnvelope();
98         body = resEnvilop.getBody();
99         operation = body.getFirstElement();
100
101         String JavaDoc opLocalName = operation.getLocalName();
102         if (opLocalName.equals("Fault")) {
103             System.out.println("A Fault message recieved, Check your Licence key");
104             strURL =
105                     strURL +
106                     "A Fault message recieved, Check your Licence key. Else you have reached the " +
107                     "daily limit of 1000 requests";
108         } else {
109             System.out.println("this is opera: " + operation.getLocalName());
110             iterator0 = operation.getChildren();
111
112             while (iterator0.hasNext()) {
113                 node = (OMNode) iterator0.next();
114                 if (node.getType() == OMNode.ELEMENT_NODE) {
115                     elem = (OMElement) node;
116                     String JavaDoc str = elem.getLocalName();
117                     System.out.println(str);
118                     if (str.equals("SearchResult")) {
119                         System.out.println("Got Search Results");
120                         iterator1 = elem.getChildren();
121                         while (iterator1.hasNext()) {
122                             node = (OMNode) iterator1.next();
123                             if (node.getType() == OMNode.ELEMENT_NODE) {
124                                 elem = (OMElement) node;
125                                 String JavaDoc str1 = elem.getLocalName();
126                                 System.out.println(str1);
127                                 if (str1.equals("Alexa")) {
128                                     System.out.println("Got Alexa");
129                                     elem = elem.getFirstElement(); //elem -> websearch
130
System.out.println("Should be WebSearch " +
131                                             elem.getLocalName());
132                                     iterator2 = elem.getChildren();
133                                     while (iterator2.hasNext()) {
134                                         node = (OMNode) iterator2.next();
135                                         if (node.getType() == OMNode.ELEMENT_NODE) {
136                                             elem = (OMElement) node;
137                                             String JavaDoc str3 = elem.getLocalName();
138                                             System.out.println(str3);
139                                             if (str3.equals("Results")) {
140                                                 System.out.println("Got Results");
141                                                 iterator3 = elem.getChildren();
142                                                 while (iterator3.hasNext()) {
143                                                     node = (OMNode) iterator3.next();
144                                                     if (node.getType() == OMNode.ELEMENT_NODE) {
145                                                         elem = (OMElement) node;
146                                                         String JavaDoc str4 = elem.getLocalName();
147                                                         System.out.println(str4);
148                                                         if (str4.equals("Result")) {
149                                                             iterator4 = elem.getChildren();
150                                                             while (iterator4.hasNext()) {
151                                                                 node = (OMNode) iterator4.next();
152                                                                 if (node.getType() ==
153                                                                         OMNode.ELEMENT_NODE) {
154                                                                     elem = (OMElement) node;
155                                                                     String JavaDoc str5 = elem.getLocalName();
156                                                                     System.out.println(str5);
157                                                                     if (str5.equals("NavigableUrl")) {
158                                                                         String JavaDoc txt = elem.getText();
159                                                                         strURL = strURL +
160                                                                                 "<a HREF= " +
161                                                                                 txt +
162                                                                                 ">" +
163                                                                                 txt +
164                                                                                 "</a><br>";
165                                                                         System.out.println(strURL);
166                                                                     }
167                                                                 }
168                                                             }
169                                                         }
170                                                     }
171                                                 }
172                                             }
173                                         }
174                                     }
175                                 }
176                             }
177                         }
178                     }
179                 }
180             }
181         }
182         System.out.println("Outside of loop");
183         GUIHandler.showResults(strURL + endHTML);
184     }
185
186     public void reportError(Exception JavaDoc e) {
187         System.out.println("Error occured after responce is received");
188         e.printStackTrace();
189     }
190 }
Popular Tags