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