Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / GetRequest.java @ b2fc16aa

History | View | Annotate | Download (4.2 kB)

1 9e8e14e4 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 9e8e14e4 Christos Stathis
 */
35 9e8e14e4 Christos Stathis
36 9e8e14e4 Christos Stathis
package gr.grnet.pithos.web.client.rest;
37 9e8e14e4 Christos Stathis
38 9e8e14e4 Christos Stathis
import com.google.gwt.core.client.GWT;
39 9e8e14e4 Christos Stathis
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
40 9e8e14e4 Christos Stathis
import com.google.gwt.http.client.Request;
41 9e8e14e4 Christos Stathis
import com.google.gwt.http.client.RequestBuilder;
42 9e8e14e4 Christos Stathis
import com.google.gwt.http.client.RequestException;
43 9e8e14e4 Christos Stathis
import com.google.gwt.http.client.Response;
44 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
45 9e8e14e4 Christos Stathis
import java.util.HashMap;
46 9e8e14e4 Christos Stathis
import java.util.Map;
47 9e8e14e4 Christos Stathis
48 9e8e14e4 Christos Stathis
public abstract class GetRequest<T extends Resource> implements ScheduledCommand {
49 9e8e14e4 Christos Stathis
50 9e8e14e4 Christos Stathis
    private Class<T> aClass;
51 9e8e14e4 Christos Stathis
52 9e8e14e4 Christos Stathis
    private String path;
53 9e8e14e4 Christos Stathis
54 9e8e14e4 Christos Stathis
    private int okCode;
55 9e8e14e4 Christos Stathis
    
56 9e8e14e4 Christos Stathis
    private String username;
57 9e8e14e4 Christos Stathis
58 9e8e14e4 Christos Stathis
    private T cached;
59 9e8e14e4 Christos Stathis
60 5cd18037 Christos Stathis
    private T result;
61 5cd18037 Christos Stathis
62 9e8e14e4 Christos Stathis
    private Map<String, String> headers = new HashMap<String, String>();
63 9e8e14e4 Christos Stathis
64 9e8e14e4 Christos Stathis
    public abstract void onSuccess(T result);
65 9e8e14e4 Christos Stathis
66 9e8e14e4 Christos Stathis
    public abstract void onError(Throwable t);
67 9e8e14e4 Christos Stathis
68 5cd18037 Christos Stathis
    public GetRequest(Class<T> aClass, String path, int okCode, T result) {
69 9e8e14e4 Christos Stathis
        this.aClass = aClass;
70 9e8e14e4 Christos Stathis
        this.path = path;
71 9e8e14e4 Christos Stathis
        this.okCode = okCode;
72 5cd18037 Christos Stathis
        this.result = result;
73 9e8e14e4 Christos Stathis
    }
74 9e8e14e4 Christos Stathis
75 9e8e14e4 Christos Stathis
    public GetRequest(Class<T> aClass, String path) {
76 5cd18037 Christos Stathis
        this(aClass, path, -1, null);
77 5cd18037 Christos Stathis
    }
78 5cd18037 Christos Stathis
79 5cd18037 Christos Stathis
    public GetRequest(Class<T> aClass, String path, T result) {
80 5cd18037 Christos Stathis
        this(aClass, path, -1, result);
81 9e8e14e4 Christos Stathis
    }
82 9e8e14e4 Christos Stathis
83 9e8e14e4 Christos Stathis
    @Override
84 9e8e14e4 Christos Stathis
    public void execute() {
85 9e8e14e4 Christos Stathis
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
86 b2fc16aa Christos Stathis
        for (String header : headers.keySet()) {
87 b2fc16aa Christos Stathis
            builder.setHeader("If-Modified-Since", "0");
88 9e8e14e4 Christos Stathis
            builder.setHeader(header, headers.get(header));
89 b2fc16aa Christos Stathis
        }
90 9e8e14e4 Christos Stathis
        try {
91 9e8e14e4 Christos Stathis
            builder.sendRequest("", new RestRequestCallback<T>(path, okCode) {
92 9e8e14e4 Christos Stathis
                @Override
93 9e8e14e4 Christos Stathis
                public void onSuccess(T object) {
94 9e8e14e4 Christos Stathis
                    GetRequest.this.onSuccess(object);
95 9e8e14e4 Christos Stathis
                }
96 9e8e14e4 Christos Stathis
97 9e8e14e4 Christos Stathis
                @Override
98 9e8e14e4 Christos Stathis
                public T deserialize(Response response) {
99 5cd18037 Christos Stathis
                    return Resource.createFromResponse(aClass, response, result);
100 9e8e14e4 Christos Stathis
                }
101 9e8e14e4 Christos Stathis
102 9e8e14e4 Christos Stathis
                @Override
103 9e8e14e4 Christos Stathis
                public void onError(Request request, Throwable throwable) {
104 9e8e14e4 Christos Stathis
                    if (throwable instanceof RestException) {
105 9e8e14e4 Christos Stathis
                        if (((RestException) throwable).getHttpStatusCode() == 304 && cached != null){
106 1ac430a1 Christos Stathis
                            GWT.log("Using cache: " + cached.toString(), null);
107 9e8e14e4 Christos Stathis
                            onSuccess(cached);
108 9e8e14e4 Christos Stathis
                            return;
109 9e8e14e4 Christos Stathis
                        }
110 9e8e14e4 Christos Stathis
                    }
111 9e8e14e4 Christos Stathis
                    GetRequest.this.onError(throwable);
112 9e8e14e4 Christos Stathis
                }
113 9e8e14e4 Christos Stathis
            });
114 9e8e14e4 Christos Stathis
        }
115 9e8e14e4 Christos Stathis
        catch (RequestException e) {
116 9e8e14e4 Christos Stathis
        }
117 9e8e14e4 Christos Stathis
    }
118 9e8e14e4 Christos Stathis
119 9e8e14e4 Christos Stathis
    public void setHeader(String header, String value) {
120 9e8e14e4 Christos Stathis
        headers.put(header, value);
121 9e8e14e4 Christos Stathis
    }
122 9e8e14e4 Christos Stathis
}