root / src / gr / grnet / pithos / web / client / rest / HeadCommand.java @ a853017c
History | View | Annotate | Download (4.4 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
package gr.grnet.pithos.web.client.rest; |
5 |
|
6 |
import gr.grnet.pithos.web.client.GSS; |
7 |
import gr.grnet.pithos.web.client.ObjectNotFoundException; |
8 |
import gr.grnet.pithos.web.client.rest.resource.FileResource; |
9 |
import gr.grnet.pithos.web.client.rest.resource.FolderResource; |
10 |
import gr.grnet.pithos.web.client.rest.resource.GroupResource; |
11 |
import gr.grnet.pithos.web.client.rest.resource.GroupUserResource; |
12 |
import gr.grnet.pithos.web.client.rest.resource.GroupsResource; |
13 |
import gr.grnet.pithos.web.client.rest.resource.RestResource; |
14 |
import gr.grnet.pithos.web.client.rest.resource.SharedResource; |
15 |
import gr.grnet.pithos.web.client.rest.resource.TrashResource; |
16 |
import gr.grnet.pithos.web.client.rest.resource.UserResource; |
17 |
|
18 |
import com.google.gwt.core.client.GWT; |
19 |
import com.google.gwt.http.client.Request; |
20 |
import com.google.gwt.http.client.RequestBuilder; |
21 |
import com.google.gwt.http.client.Response; |
22 |
|
23 |
|
24 |
public abstract class HeadCommand<T extends RestResource> extends RestCommand{ |
25 |
|
26 |
boolean complete = false; |
27 |
T result = null;
|
28 |
Class<T> aclass;
|
29 |
private boolean requestSent = false; |
30 |
T cached; |
31 |
final String path; |
32 |
|
33 |
public HeadCommand(Class<T> theclass, String pathToGet, T theCached){ |
34 |
this(theclass, pathToGet, true, theCached); |
35 |
} |
36 |
|
37 |
public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){ |
38 |
setShowLoadingIndicator(showLoading); |
39 |
this.aclass = theClass;
|
40 |
if(isShowLoadingIndicator())
|
41 |
GSS.get().showLoadingIndicator("Getting ",pathToGet);
|
42 |
|
43 |
if(theClass.equals(FileResource.class))
|
44 |
path = pathToGet; |
45 |
else
|
46 |
path = fixPath(pathToGet); |
47 |
this.cached = theCached;
|
48 |
|
49 |
} |
50 |
|
51 |
private void sendRequest(){ |
52 |
if(requestSent)
|
53 |
return;
|
54 |
requestSent=true;
|
55 |
RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, path);
|
56 |
if(cached!=null && cached.getLastModifiedSince() != null){ |
57 |
GWT.log("ADDING IF MODIFIED HEADERS", null); |
58 |
builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
|
59 |
} |
60 |
try {
|
61 |
handleHeaders(builder, path); |
62 |
builder.sendRequest("", new RestCallback(path) { |
63 |
|
64 |
@Override
|
65 |
public Object deserialize(Response response) { |
66 |
return deserializeResponse(path, response);
|
67 |
} |
68 |
|
69 |
@Override
|
70 |
public void handleError(Request request, Throwable exception) { |
71 |
if(exception instanceof RestException) |
72 |
if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){ |
73 |
GWT.log("Using cache:"+cached.getUri(), null); |
74 |
handleSuccess(cached); |
75 |
return;
|
76 |
} |
77 |
complete = true;
|
78 |
HeadCommand.this.onError(exception); |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void handleSuccess(Object object) { |
83 |
result = (T) object; |
84 |
complete = true;
|
85 |
} |
86 |
|
87 |
}); |
88 |
} catch (Exception ex) { |
89 |
complete = true;
|
90 |
onError(ex); |
91 |
} |
92 |
} |
93 |
|
94 |
public boolean isComplete() { |
95 |
return complete;
|
96 |
} |
97 |
|
98 |
public T getResult(){
|
99 |
return result;
|
100 |
} |
101 |
|
102 |
@Override
|
103 |
public boolean execute() { |
104 |
if(!requestSent)
|
105 |
sendRequest(); |
106 |
boolean com = isComplete();
|
107 |
if(com){
|
108 |
if(isShowLoadingIndicator())
|
109 |
GSS.get().hideLoadingIndicator(); |
110 |
if(getResult() != null) |
111 |
onComplete(); |
112 |
else
|
113 |
onError(new ObjectNotFoundException("Resource Not Found")); |
114 |
return false; |
115 |
} |
116 |
return true; |
117 |
} |
118 |
|
119 |
public Object deserializeResponse(String aPath, Response response){ |
120 |
RestResource result1 = null;
|
121 |
if(aclass.equals(FolderResource.class)){
|
122 |
result1 = new FolderResource(aPath);
|
123 |
result1.createFromJSON(response.getText()); |
124 |
|
125 |
} |
126 |
else if(aclass.equals(FileResource.class)){ |
127 |
result1 = new FileResource(aPath);
|
128 |
result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
|
129 |
} |
130 |
else if(aclass.equals(GroupsResource.class)){ |
131 |
result1 = new GroupsResource(aPath);
|
132 |
result1.createFromJSON(response.getText()); |
133 |
} |
134 |
else if(aclass.equals(TrashResource.class)){ |
135 |
result1 = new TrashResource(aPath);
|
136 |
result1.createFromJSON(response.getText()); |
137 |
|
138 |
} |
139 |
else if(aclass.equals(SharedResource.class)){ |
140 |
result1 = new SharedResource(aPath);
|
141 |
result1.createFromJSON(response.getText()); |
142 |
|
143 |
} |
144 |
else if(aclass.equals(GroupResource.class)){ |
145 |
result1 = new GroupResource(aPath);
|
146 |
result1.createFromJSON(response.getText()); |
147 |
|
148 |
} |
149 |
else if(aclass.equals(GroupUserResource.class)){ |
150 |
result1 = new GroupUserResource(aPath);
|
151 |
result1.createFromJSON(response.getText()); |
152 |
|
153 |
} |
154 |
else if(aclass.equals(UserResource.class)){ |
155 |
result1 = new UserResource(aPath);
|
156 |
result1.createFromJSON(response.getText()); |
157 |
|
158 |
} |
159 |
return result1;
|
160 |
} |
161 |
} |