2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client.rest;
37 import gr.grnet.pithos.web.client.Pithos;
38 import gr.grnet.pithos.web.client.ObjectNotFoundException;
39 import gr.grnet.pithos.web.client.rest.resource.FileResource;
40 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
41 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
42 import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
43 import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
44 import gr.grnet.pithos.web.client.rest.resource.RestResource;
45 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
46 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
47 import gr.grnet.pithos.web.client.rest.resource.UserResource;
49 import com.google.gwt.core.client.GWT;
50 import com.google.gwt.http.client.Request;
51 import com.google.gwt.http.client.RequestBuilder;
52 import com.google.gwt.http.client.Response;
55 public abstract class HeadCommand<T extends RestResource> extends RestCommand{
57 boolean complete = false;
60 private boolean requestSent = false;
64 public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
65 this(theclass, pathToGet, true, theCached);
68 public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
69 setShowLoadingIndicator(showLoading);
70 this.aclass = theClass;
71 if(isShowLoadingIndicator())
72 Pithos.get().showLoadingIndicator("Getting ",pathToGet);
74 if(theClass.equals(FileResource.class))
77 path = fixPath(pathToGet);
78 this.cached = theCached;
82 private void sendRequest(){
86 RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, path);
87 if(cached!=null && cached.getLastModifiedSince() != null){
88 GWT.log("ADDING IF MODIFIED HEADERS", null);
89 builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
92 handleHeaders(builder, path);
93 builder.sendRequest("", new RestCallback(path) {
96 public Object deserialize(Response response) {
97 return deserializeResponse(path, response);
101 public void handleError(Request request, Throwable exception) {
102 if(exception instanceof RestException)
103 if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
104 GWT.log("Using cache:"+cached.getUri(), null);
105 handleSuccess(cached);
109 HeadCommand.this.onError(exception);
113 public void handleSuccess(Object object) {
119 } catch (Exception ex) {
125 public boolean isComplete() {
129 public T getResult(){
134 public boolean execute() {
137 boolean com = isComplete();
139 if(isShowLoadingIndicator())
140 Pithos.get().hideLoadingIndicator();
141 if(getResult() != null)
144 onError(new ObjectNotFoundException("Resource Not Found"));
150 public Object deserializeResponse(String aPath, Response response){
151 RestResource result1 = null;
152 if(aclass.equals(FolderResource.class)){
153 result1 = new FolderResource(aPath);
154 result1.createFromJSON(response.getText());
157 else if(aclass.equals(FileResource.class)){
158 result1 = new FileResource(aPath);
159 result1.createFromJSON(response.getHeader("X-Pithos-Metadata"));
161 else if(aclass.equals(GroupsResource.class)){
162 result1 = new GroupsResource(aPath);
163 result1.createFromJSON(response.getText());
165 else if(aclass.equals(TrashResource.class)){
166 result1 = new TrashResource(aPath);
167 result1.createFromJSON(response.getText());
170 else if(aclass.equals(SharedResource.class)){
171 result1 = new SharedResource(aPath);
172 result1.createFromJSON(response.getText());
175 else if(aclass.equals(GroupResource.class)){
176 result1 = new GroupResource(aPath);
177 result1.createFromJSON(response.getText());
180 else if(aclass.equals(GroupUserResource.class)){
181 result1 = new GroupUserResource(aPath);
182 result1.createFromJSON(response.getText());
185 else if(aclass.equals(UserResource.class)){
186 result1 = new UserResource(aPath);
187 result1.createFromJSON(response.getText());