Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / HeadRequest.java @ 430898e5

History | View | Annotate | Download (4.7 kB)

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