1 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 ; 30 31 39 40 public class ClientCallbackHandler extends Callback { 41 42 45 private String beginHTML = "<HTML><HEAD><TITLE>Wow</TITLE></HEAD><BODY>"; 46 47 50 private String endHTML = "</BODY></HTML>"; 51 52 55 private String strURL = beginHTML; 56 57 60 private AsyncResult myResult; 61 62 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 89 private void extractDetails(AsyncResult result) { 90 Iterator 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 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 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 str1 = elem.getLocalName(); 126 System.out.println(str1); 127 if (str1.equals("Alexa")) { 128 System.out.println("Got Alexa"); 129 elem = elem.getFirstElement(); 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 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 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 str5 = elem.getLocalName(); 156 System.out.println(str5); 157 if (str5.equals("NavigableUrl")) { 158 String 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 e) { 187 System.out.println("Error occured after responce is received"); 188 e.printStackTrace(); 189 } 190 } | Popular Tags |