01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.data;
09:
10: import com.google.gwt.http.client.Request;
11: import com.google.gwt.http.client.RequestBuilder;
12: import com.google.gwt.http.client.RequestCallback;
13: import com.google.gwt.http.client.Response;
14:
15: /**
16: * A concrete <code>DataProxy</code> that retrieves data using
17: * <code>RequestBulder</code> instances.
18: *
19: * @see RequestBuilder
20: */
21: public class HTTPProxy implements DataProxy, RequestCallback {
22:
23: private RequestBuilder builder;
24: private DataReader reader;
25: private DataCallback callback;
26: private LoadConfig config;
27:
28: public HTTPProxy(RequestBuilder builder) {
29: this .builder = builder;
30: }
31:
32: public void load(DataReader reader, LoadConfig cfg, DataCallback cb) {
33: this .reader = reader;
34: this .config = cfg;
35: callback = cb;
36: try {
37: builder.sendRequest(cfg.toString(), this );
38: } catch (Exception e) {
39: LoadResult result = new LoadResult();
40: result.success = false;
41: callback.setResult(result);
42: }
43: }
44:
45: public void onError(Request request, Throwable exception) {
46: LoadResult result = new LoadResult();
47: result.success = false;
48: callback.setResult(result);
49: }
50:
51: public void onResponseReceived(Request request, Response response) {
52: String text = response.getText();
53: LoadResult result = reader.read(text);
54: result.success = true;
55: result.cursor = config.start;
56: callback.setResult(result);
57: }
58:
59: }
|