Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.4 kB)

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