KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > google > spellcheck > FormModel


1 package sample.google.spellcheck;
2
3 import java.net.MalformedURLException JavaDoc;
4 import java.net.URL JavaDoc;
5
6 import javax.xml.namespace.QName JavaDoc;
7
8 import org.apache.axis2.addressing.AddressingConstants;
9 import org.apache.axis2.addressing.EndpointReference;
10 import org.apache.axis2.clientapi.AsyncResult;
11 import org.apache.axis2.clientapi.Call;
12 import org.apache.axis2.clientapi.Callback;
13 import org.apache.axis2.engine.AxisFault;
14 import org.apache.axis2.om.OMAbstractFactory;
15 import org.apache.axis2.om.OMElement;
16 import org.apache.axis2.om.OMNamespace;
17 import org.apache.axis2.soap.SOAPBody;
18 import org.apache.axis2.soap.SOAPEnvelope;
19 import org.apache.axis2.soap.SOAPFactory;
20
21 import sample.google.common.util.PropertyLoader;
22
23 /**
24  * class sample.google.spellcheck.FormModel
25  * This is the Impementation of the Asynchronous Client
26  * @author Nadana Gunarathna
27  *
28  */

29 public class FormModel {
30
31     Observer observer;
32     private static final String JavaDoc PROTOCOL = "http";
33
34     public FormModel(Observer observer)
35     {
36         this.observer = observer;
37     }
38
39     private OMElement getElement(String JavaDoc word){
40         SOAPFactory omfactory=OMAbstractFactory.getSOAP11Factory();
41         OMNamespace opN = omfactory.createOMNamespace("urn:GoogleSearch","ns1");
42         OMNamespace emptyNs=omfactory.createOMNamespace("", null);
43
44         OMElement method = omfactory.createOMElement("doSpellingSuggestion", opN);
45         method.declareNamespace("http://www.w3.org/1999/XMLSchema-instance","xsi");
46         method.declareNamespace("http://www.w3.org/1999/XMLSchema","xsd");
47
48         //reqEnv.getBody().addChild(method);
49
method.addAttribute("soapenv:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/",null);
50         OMElement value1 = omfactory.createOMElement("key",emptyNs);
51         OMElement value2=omfactory.createOMElement("phrase",emptyNs);
52         value1.addAttribute("xsi:type","xsd:string",null);
53         value2.addAttribute("xsi:type","xsd:string",null);
54         value1.addChild(omfactory.createText(value1,PropertyLoader.getGoogleKey() ));
55         value2.addChild(omfactory.createText(value2,word));
56
57         method.addChild(value1);method.addChild(value2);
58         return method;
59     }
60
61
62     public void doAsyncSpellingSuggestion(String JavaDoc word)
63     {
64         OMElement requestElement = getElement(word);
65         Call call = null;
66         try {
67             call = new Call();
68         } catch (AxisFault axisFault) {
69             observer.updateError(axisFault.getMessage());
70         }
71         URL JavaDoc url = null;
72         try {
73             url = new URL JavaDoc(PROTOCOL,PropertyLoader.getGoogleEndpointURL(),PropertyLoader.getGoogleEndpointServiceName());
74             //url=new URL( "http","127.0.0.1",7070,"/search/beta2");
75
} catch (MalformedURLException JavaDoc e) {
76             observer.updateError(e.getMessage());;
77         }
78
79         call.setTo(new EndpointReference(AddressingConstants.WSA_TO, url.toString()));
80         try {
81             call.invokeNonBlocking("doGoogleSpellingSugg",requestElement,new GoogleCallBack(word));
82         } catch (AxisFault axisFault) {
83             observer.updateError(axisFault.getMessage());
84         }
85
86     }
87     public void doSyncSpellingSuggestion(String JavaDoc word)
88     {
89         OMElement responseElement=null;
90         OMElement requestElement = getElement(word);
91         Call call = null;
92         try {
93             call = new Call();
94         } catch (AxisFault axisFault) {
95             observer.updateError(axisFault.getMessage());
96         }
97         URL JavaDoc url = null;
98         try {
99             url = new URL JavaDoc(PROTOCOL,PropertyLoader.getGoogleEndpointURL(),PropertyLoader.getGoogleEndpointServiceName());
100             //url=new URL( "http","127.0.0.1",7070,"/search/beta2");
101
} catch (MalformedURLException JavaDoc e) {
102             observer.updateError(e.getMessage());
103         }
104
105         call.setTo(new EndpointReference(AddressingConstants.WSA_TO, url.toString()));
106         try {
107             responseElement=(OMElement)call.invokeBlocking("doGoogleSpellingSugg",requestElement);
108         } catch (AxisFault axisFault) {
109             observer.updateError(axisFault.getMessage());
110         }
111
112
113         this.getResponseFromElement(responseElement);
114     }
115
116     public String JavaDoc getResponseFromElement(OMElement responseElement){
117
118         OMElement val = responseElement.getFirstElement();
119         String JavaDoc sugession = val.getText();
120         this.observer.update(sugession);
121         return sugession;
122     }
123
124
125     public String JavaDoc getResponse(SOAPEnvelope responseEnvelope){
126         QName JavaDoc qName1 = new QName JavaDoc("urn:GoogleSearch", "doSpellingSuggestionResponse");
127         QName JavaDoc qName2 = new QName JavaDoc("urn:GoogleSearch", "return");
128               
129
130         SOAPBody body = responseEnvelope.getBody();
131         if (body.hasFault()) {
132             observer.updateError(body.getFault().getException().getMessage());
133             return null;
134         } else{
135             OMElement root = body.getFirstChildWithName(qName1);
136             OMElement val =null;
137             if (root!=null){
138                 // val = root.getFirstChildWithName(qName2);
139
val = root.getFirstElement();
140             }else{
141                 observer.updateError("Correct response not received!");
142                 return null;
143             }
144
145             String JavaDoc sugession = val.getText();
146             if ((sugession==null) ||(sugession.trim().equals(""))){
147                 return null;
148             }else{
149                 return sugession;
150             }
151
152         }
153     }
154
155     private class GoogleCallBack extends Callback{
156         private String JavaDoc originalWord;
157         public GoogleCallBack(String JavaDoc originalWord) {
158             this.originalWord = originalWord;
159         }
160
161         public void onComplete(AsyncResult result) {
162             String JavaDoc suggestion = getResponse(result.getResponseEnvelope());
163             if (suggestion==null){
164                 observer.update(originalWord);
165                 observer.updateError("No suggestions found for "+originalWord);
166             }else{
167                 observer.update(suggestion);
168             }
169         }
170
171         public void reportError(Exception JavaDoc e) {
172             observer.updateError(e.getMessage());
173         }
174     }
175
176 }
177
Popular Tags