Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.8 kB)

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