Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / DeleteRequest.java @ e6e9f6e6

History | View | Annotate | Download (4.2 kB)

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