root / src / gr / ebs / gss / client / rest / HeadCommand.java @ bddcde7b
History | View | Annotate | Download (4.9 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
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.
|
10 |
*
|
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.
|
15 |
*
|
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/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.client.rest; |
20 |
|
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; |
32 |
|
33 |
import com.google.gwt.core.client.GWT; |
34 |
import com.google.gwt.http.client.Request; |
35 |
import com.google.gwt.http.client.Response; |
36 |
|
37 |
|
38 |
/**
|
39 |
* @author kman
|
40 |
*
|
41 |
*/
|
42 |
public abstract class HeadCommand<T extends RestResource> extends RestCommand{ |
43 |
|
44 |
boolean complete = false; |
45 |
T result = null;
|
46 |
Class<T> aclass;
|
47 |
private boolean requestSent = false; |
48 |
T cached; |
49 |
final String path; |
50 |
|
51 |
public HeadCommand(Class<T> theclass, String pathToGet, T theCached){ |
52 |
this(theclass, pathToGet, true, theCached); |
53 |
} |
54 |
|
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(); |
60 |
|
61 |
if(theClass.equals(FileResource.class))
|
62 |
path = pathToGet; |
63 |
else
|
64 |
path = fixPath(pathToGet); |
65 |
this.cached = theCached;
|
66 |
|
67 |
} |
68 |
|
69 |
private void sendRequest(){ |
70 |
if(requestSent)
|
71 |
return;
|
72 |
requestSent=true;
|
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());
|
77 |
} |
78 |
try {
|
79 |
handleHeaders(builder, path); |
80 |
builder.sendRequest("", new RestCallback(path) { |
81 |
|
82 |
@Override
|
83 |
public Object deserialize(Response response) { |
84 |
return deserializeResponse(path, response);
|
85 |
} |
86 |
|
87 |
@Override
|
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); |
93 |
return;
|
94 |
} |
95 |
complete = true;
|
96 |
HeadCommand.this.onError(exception); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void handleSuccess(Object object) { |
101 |
result = (T) object; |
102 |
complete = true;
|
103 |
} |
104 |
|
105 |
}); |
106 |
} catch (Exception ex) { |
107 |
complete = true;
|
108 |
onError(ex); |
109 |
} |
110 |
} |
111 |
|
112 |
public boolean isComplete() { |
113 |
return complete;
|
114 |
} |
115 |
|
116 |
public T getResult(){
|
117 |
return result;
|
118 |
} |
119 |
|
120 |
public boolean execute() { |
121 |
if(!requestSent)
|
122 |
sendRequest(); |
123 |
boolean com = isComplete();
|
124 |
if(com){
|
125 |
if(isShowLoadingIndicator())
|
126 |
GSS.get().hideLoadingIndicator(); |
127 |
if(getResult() != null) |
128 |
onComplete(); |
129 |
else
|
130 |
onError(new ObjectNotFoundException("Resource Not Found")); |
131 |
return false; |
132 |
} |
133 |
return true; |
134 |
} |
135 |
|
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()); |
141 |
|
142 |
} |
143 |
else if(aclass.equals(FileResource.class)){ |
144 |
result1 = new FileResource(aPath);
|
145 |
result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
|
146 |
} |
147 |
else if(aclass.equals(GroupsResource.class)){ |
148 |
result1 = new GroupsResource(aPath);
|
149 |
result1.createFromJSON(response.getText()); |
150 |
} |
151 |
else if(aclass.equals(TrashResource.class)){ |
152 |
result1 = new TrashResource(aPath);
|
153 |
result1.createFromJSON(response.getText()); |
154 |
|
155 |
} |
156 |
else if(aclass.equals(SharedResource.class)){ |
157 |
result1 = new SharedResource(aPath);
|
158 |
result1.createFromJSON(response.getText()); |
159 |
|
160 |
} |
161 |
else if(aclass.equals(GroupResource.class)){ |
162 |
result1 = new GroupResource(aPath);
|
163 |
result1.createFromJSON(response.getText()); |
164 |
|
165 |
} |
166 |
else if(aclass.equals(GroupUserResource.class)){ |
167 |
result1 = new GroupUserResource(aPath);
|
168 |
result1.createFromJSON(response.getText()); |
169 |
|
170 |
} |
171 |
else if(aclass.equals(UserResource.class)){ |
172 |
result1 = new UserResource(aPath);
|
173 |
result1.createFromJSON(response.getText()); |
174 |
|
175 |
} |
176 |
return result1;
|
177 |
} |
178 |
} |