Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.4 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 8ddb4627 Christos KK Loverdos
import gr.grnet.pithos.web.client.Pithos;
39 cc0120ab Christos KK Loverdos
import gr.grnet.pithos.web.client.Resource;
40 ebead1b5 Christos Stathis
41 ebead1b5 Christos Stathis
import java.util.HashMap;
42 ebead1b5 Christos Stathis
import java.util.Map;
43 ebead1b5 Christos Stathis
44 b73a903e Christos Stathis
import com.google.gwt.core.client.Scheduler;
45 f1aabd89 Christos Stathis
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
46 f1aabd89 Christos Stathis
import com.google.gwt.http.client.Request;
47 f1aabd89 Christos Stathis
import com.google.gwt.http.client.RequestBuilder;
48 f1aabd89 Christos Stathis
import com.google.gwt.http.client.RequestException;
49 f1aabd89 Christos Stathis
import com.google.gwt.http.client.Response;
50 f1aabd89 Christos Stathis
51 f1aabd89 Christos Stathis
/**
52 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
53 f1aabd89 Christos Stathis
 * Templates.
54 f1aabd89 Christos Stathis
 */
55 f1aabd89 Christos Stathis
public abstract class DeleteRequest implements ScheduledCommand {
56 bb160e3a Christos Stathis
        
57 bb160e3a Christos Stathis
        protected static final int MAX_RETRIES = 3; 
58 bb160e3a Christos Stathis
59 bb160e3a Christos Stathis
        protected int retries = 0; 
60 bb160e3a Christos Stathis
61 bb160e3a Christos Stathis
        private String api;
62 b51c628b Christos Stathis
63 7811b9d1 Christos Stathis
    protected String owner;
64 b51c628b Christos Stathis
65 f1aabd89 Christos Stathis
    private String path;
66 f1aabd89 Christos Stathis
67 f1aabd89 Christos Stathis
    private Map<String, String> headers = new HashMap<String, String>();
68 f1aabd89 Christos Stathis
69 f1aabd89 Christos Stathis
    public abstract void onSuccess(Resource result);
70 f1aabd89 Christos Stathis
71 f1aabd89 Christos Stathis
    public abstract void onError(Throwable t);
72 f1aabd89 Christos Stathis
73 dc8cc18a Christos KK Loverdos
    public DeleteRequest(String api, String owner, String path) {
74 b51c628b Christos Stathis
        this.api = api;
75 b51c628b Christos Stathis
        this.owner = owner;
76 f1aabd89 Christos Stathis
        this.path = path;
77 f1aabd89 Christos Stathis
    }
78 f1aabd89 Christos Stathis
79 f1aabd89 Christos Stathis
    @Override
80 f1aabd89 Christos Stathis
    public void execute() {
81 8ddb4627 Christos KK Loverdos
        Pithos.LOG("DELETE api = ", api, ", owner = ", owner, ", path = ", path);
82 8ddb4627 Christos KK Loverdos
        Pithos.LOG("   ==> ", api + owner + path);
83 b51c628b Christos Stathis
        RequestBuilder builder = new RequestBuilder(RequestBuilder.DELETE, api + owner + path);
84 f1aabd89 Christos Stathis
        for (String header : headers.keySet()) {
85 f1aabd89 Christos Stathis
            builder.setHeader(header, headers.get(header));
86 f1aabd89 Christos Stathis
        }
87 f1aabd89 Christos Stathis
        try {
88 7811b9d1 Christos Stathis
            builder.sendRequest("", new RestRequestCallback<Resource>(api + owner + path, Response.SC_NO_CONTENT) {
89 f1aabd89 Christos Stathis
                @Override
90 f1aabd89 Christos Stathis
                public void onSuccess(Resource object) {
91 f1aabd89 Christos Stathis
                    DeleteRequest.this.onSuccess(object);
92 f1aabd89 Christos Stathis
                }
93 f1aabd89 Christos Stathis
94 f1aabd89 Christos Stathis
                @Override
95 f1aabd89 Christos Stathis
                public Resource deserialize(Response response) {
96 dc8cc18a Christos KK Loverdos
                    return Resource.createFromResponse(Resource.class, owner, response, null);
97 f1aabd89 Christos Stathis
                }
98 f1aabd89 Christos Stathis
99 f1aabd89 Christos Stathis
                @Override
100 ebead1b5 Christos Stathis
                public void onError(Request request, Throwable throwable) {
101 b73a903e Christos Stathis
                        if (retries >= MAX_RETRIES)
102 b73a903e Christos Stathis
                                DeleteRequest.this.onError(throwable);
103 b73a903e Christos Stathis
                        else
104 b73a903e Christos Stathis
                                Scheduler.get().scheduleDeferred(DeleteRequest.this);
105 f1aabd89 Christos Stathis
                }
106 9539e23d Christos Stathis
107 9539e23d Christos Stathis
                                @Override
108 9539e23d Christos Stathis
                                public void onUnauthorized(Response response) {
109 b73a903e Christos Stathis
                        if (retries >= MAX_RETRIES)
110 b73a903e Christos Stathis
                                DeleteRequest.this.onUnauthorized(response);
111 b73a903e Christos Stathis
                        else
112 b73a903e Christos Stathis
                                Scheduler.get().scheduleDeferred(DeleteRequest.this);
113 9539e23d Christos Stathis
                                }
114 f1aabd89 Christos Stathis
            });
115 b73a903e Christos Stathis
            retries++;
116 f1aabd89 Christos Stathis
        }
117 f1aabd89 Christos Stathis
        catch (RequestException e) {
118 8ddb4627 Christos KK Loverdos
            Pithos.LOG(e);
119 f1aabd89 Christos Stathis
        }
120 f1aabd89 Christos Stathis
    }
121 f1aabd89 Christos Stathis
122 9539e23d Christos Stathis
    protected abstract void onUnauthorized(Response response);
123 9539e23d Christos Stathis
124 9539e23d Christos Stathis
        public void setHeader(String header, String value) {
125 f1aabd89 Christos Stathis
        headers.put(header, value);
126 f1aabd89 Christos Stathis
    }
127 f1aabd89 Christos Stathis
}