Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / PostRequest.java @ 7529fcd7

History | View | Annotate | Download (3.3 kB)

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