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.RequestBuilder;
36 import com.google.gwt.http.client.Response;
43 public abstract class HeadCommand<T extends RestResource> extends RestCommand{
45 boolean complete = false;
48 private boolean requestSent = false;
52 public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
53 this(theclass, pathToGet, true, theCached);
56 public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
57 setShowLoadingIndicator(showLoading);
58 this.aclass = theClass;
59 if(isShowLoadingIndicator())
60 GSS.get().showLoadingIndicator();
62 if(theClass.equals(FileResource.class))
65 path = fixPath(pathToGet);
66 this.cached = theCached;
70 private void sendRequest(){
74 RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, path);
75 if(cached!=null && cached.getLastModifiedSince() != null){
76 GWT.log("ADDING IF MODIFIED HEADERS", null);
77 builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
80 handleHeaders(builder, path);
81 builder.sendRequest("", new RestCallback(path) {
84 public Object deserialize(Response response) {
85 return deserializeResponse(path, response);
89 public void handleError(Request request, Throwable exception) {
90 if(exception instanceof RestException)
91 if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
92 GWT.log("Using cache:"+cached.getUri(), null);
93 handleSuccess(cached);
97 HeadCommand.this.onError(exception);
101 public void handleSuccess(Object object) {
107 } catch (Exception ex) {
113 public boolean isComplete() {
117 public T getResult(){
122 public boolean execute() {
125 boolean com = isComplete();
127 if(isShowLoadingIndicator())
128 GSS.get().hideLoadingIndicator();
129 if(getResult() != null)
132 onError(new ObjectNotFoundException("Resource Not Found"));
138 public Object deserializeResponse(String aPath, Response response){
139 RestResource result1 = null;
140 if(aclass.equals(FolderResource.class)){
141 result1 = new FolderResource(aPath);
142 result1.createFromJSON(response.getText());
145 else if(aclass.equals(FileResource.class)){
146 result1 = new FileResource(aPath);
147 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
149 else if(aclass.equals(GroupsResource.class)){
150 result1 = new GroupsResource(aPath);
151 result1.createFromJSON(response.getText());
153 else if(aclass.equals(TrashResource.class)){
154 result1 = new TrashResource(aPath);
155 result1.createFromJSON(response.getText());
158 else if(aclass.equals(SharedResource.class)){
159 result1 = new SharedResource(aPath);
160 result1.createFromJSON(response.getText());
163 else if(aclass.equals(GroupResource.class)){
164 result1 = new GroupResource(aPath);
165 result1.createFromJSON(response.getText());
168 else if(aclass.equals(GroupUserResource.class)){
169 result1 = new GroupUserResource(aPath);
170 result1.createFromJSON(response.getText());
173 else if(aclass.equals(UserResource.class)){
174 result1 = new UserResource(aPath);
175 result1.createFromJSON(response.getText());