2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client.rest;
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.client.rest.resource.FileResource;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.GroupResource;
26 import gr.ebs.gss.client.rest.resource.GroupUserResource;
27 import gr.ebs.gss.client.rest.resource.GroupsResource;
28 import gr.ebs.gss.client.rest.resource.RestResource;
29 import gr.ebs.gss.client.rest.resource.SharedResource;
30 import gr.ebs.gss.client.rest.resource.TrashResource;
31 import gr.ebs.gss.client.rest.resource.UserResource;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.http.client.Request;
35 import com.google.gwt.http.client.Response;
42 public abstract class HeadCommand<T extends RestResource> extends RestCommand{
44 boolean complete = false;
47 private boolean requestSent = false;
51 public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
52 this(theclass, pathToGet, true, theCached);
55 public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
56 setShowLoadingIndicator(showLoading);
57 this.aclass = theClass;
58 if(isShowLoadingIndicator())
59 GSS.get().showLoadingIndicator();
61 if(theClass.equals(FileResource.class))
64 path = fixPath(pathToGet);
65 this.cached = theCached;
69 private void sendRequest(){
73 RestRequestBuilder builder = new RestRequestBuilder("HEAD", path);
74 if(cached!=null && cached.getLastModifiedSince() != null){
75 GWT.log("ADDING IF MODIFIED HEADERS", null);
76 builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
79 handleHeaders(builder, path);
80 builder.sendRequest("", new RestCallback(path) {
83 public Object deserialize(Response response) {
84 return deserializeResponse(path, response);
88 public void handleError(Request request, Throwable exception) {
89 if(exception instanceof RestException)
90 if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
91 GWT.log("Using cache:"+cached.getUri(), null);
92 handleSuccess(cached);
96 HeadCommand.this.onError(exception);
100 public void handleSuccess(Object object) {
106 } catch (Exception ex) {
112 public boolean isComplete() {
116 public T getResult(){
120 public boolean execute() {
123 boolean com = isComplete();
125 if(isShowLoadingIndicator())
126 GSS.get().hideLoadingIndicator();
127 if(getResult() != null)
130 onError(new ObjectNotFoundException("Resource Not Found"));
136 public Object deserializeResponse(String aPath, Response response){
137 RestResource result1 = null;
138 if(aclass.equals(FolderResource.class)){
139 result1 = new FolderResource(aPath);
140 result1.createFromJSON(response.getText());
143 else if(aclass.equals(FileResource.class)){
144 result1 = new FileResource(aPath);
145 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
147 else if(aclass.equals(GroupsResource.class)){
148 result1 = new GroupsResource(aPath);
149 result1.createFromJSON(response.getText());
151 else if(aclass.equals(TrashResource.class)){
152 result1 = new TrashResource(aPath);
153 result1.createFromJSON(response.getText());
156 else if(aclass.equals(SharedResource.class)){
157 result1 = new SharedResource(aPath);
158 result1.createFromJSON(response.getText());
161 else if(aclass.equals(GroupResource.class)){
162 result1 = new GroupResource(aPath);
163 result1.createFromJSON(response.getText());
166 else if(aclass.equals(GroupUserResource.class)){
167 result1 = new GroupUserResource(aPath);
168 result1.createFromJSON(response.getText());
171 else if(aclass.equals(UserResource.class)){
172 result1 = new UserResource(aPath);
173 result1.createFromJSON(response.getText());