Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / HeadCommand.java @ bddcde7b

History | View | Annotate | Download (4.9 kB)

1 a52ea5e4 pastith
/*
2 a52ea5e4 pastith
 * Copyright 2009 Electronic Business Systems Ltd.
3 a52ea5e4 pastith
 *
4 a52ea5e4 pastith
 * This file is part of GSS.
5 a52ea5e4 pastith
 *
6 a52ea5e4 pastith
 * GSS is free software: you can redistribute it and/or modify
7 a52ea5e4 pastith
 * it under the terms of the GNU General Public License as published by
8 a52ea5e4 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 a52ea5e4 pastith
 * (at your option) any later version.
10 a52ea5e4 pastith
 *
11 a52ea5e4 pastith
 * GSS is distributed in the hope that it will be useful,
12 a52ea5e4 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 a52ea5e4 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 a52ea5e4 pastith
 * GNU General Public License for more details.
15 a52ea5e4 pastith
 *
16 a52ea5e4 pastith
 * You should have received a copy of the GNU General Public License
17 a52ea5e4 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 a52ea5e4 pastith
 */
19 a52ea5e4 pastith
package gr.ebs.gss.client.rest;
20 a52ea5e4 pastith
21 a52ea5e4 pastith
import gr.ebs.gss.client.GSS;
22 a52ea5e4 pastith
import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FileResource;
24 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FolderResource;
25 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupResource;
26 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupUserResource;
27 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupsResource;
28 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.RestResource;
29 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.SharedResource;
30 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.TrashResource;
31 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.UserResource;
32 a52ea5e4 pastith
33 62f168b2 Giannis Koutsoubos
import com.google.gwt.core.client.GWT;
34 a52ea5e4 pastith
import com.google.gwt.http.client.Request;
35 a52ea5e4 pastith
import com.google.gwt.http.client.Response;
36 a52ea5e4 pastith
37 a52ea5e4 pastith
38 a52ea5e4 pastith
/**
39 a52ea5e4 pastith
 * @author kman
40 a52ea5e4 pastith
 *
41 a52ea5e4 pastith
 */
42 86c951b2 Panagiotis Astithas
public  abstract class HeadCommand<T extends RestResource> extends RestCommand{
43 a52ea5e4 pastith
44 a52ea5e4 pastith
        boolean complete = false;
45 a52ea5e4 pastith
        T result = null;
46 a52ea5e4 pastith
        Class<T> aclass;
47 62f168b2 Giannis Koutsoubos
        private boolean requestSent = false;
48 62f168b2 Giannis Koutsoubos
        T cached;
49 62f168b2 Giannis Koutsoubos
        final String path;
50 86c951b2 Panagiotis Astithas
51 86c951b2 Panagiotis Astithas
        public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
52 86c951b2 Panagiotis Astithas
                this(theclass, pathToGet, true, theCached);
53 aa8608d3 koutsoub
        }
54 86c951b2 Panagiotis Astithas
55 86c951b2 Panagiotis Astithas
        public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
56 aa8608d3 koutsoub
                setShowLoadingIndicator(showLoading);
57 86c951b2 Panagiotis Astithas
                this.aclass = theClass;
58 aa8608d3 koutsoub
                if(isShowLoadingIndicator())
59 aa8608d3 koutsoub
                        GSS.get().showLoadingIndicator();
60 62f168b2 Giannis Koutsoubos
61 86c951b2 Panagiotis Astithas
                if(theClass.equals(FileResource.class))
62 a52ea5e4 pastith
                        path = pathToGet;
63 a52ea5e4 pastith
                else
64 a52ea5e4 pastith
                        path = fixPath(pathToGet);
65 86c951b2 Panagiotis Astithas
                this.cached = theCached;
66 a52ea5e4 pastith
67 62f168b2 Giannis Koutsoubos
        }
68 62f168b2 Giannis Koutsoubos
69 62f168b2 Giannis Koutsoubos
        private void sendRequest(){
70 62f168b2 Giannis Koutsoubos
                if(requestSent)
71 62f168b2 Giannis Koutsoubos
                        return;
72 62f168b2 Giannis Koutsoubos
                requestSent=true;
73 62f168b2 Giannis Koutsoubos
                RestRequestBuilder builder = new RestRequestBuilder("HEAD", path);
74 62f168b2 Giannis Koutsoubos
                if(cached!=null && cached.getLastModifiedSince() != null){
75 62f168b2 Giannis Koutsoubos
                        GWT.log("ADDING IF MODIFIED HEADERS", null);
76 62f168b2 Giannis Koutsoubos
                        builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
77 62f168b2 Giannis Koutsoubos
                }
78 a52ea5e4 pastith
                try {
79 a52ea5e4 pastith
                        handleHeaders(builder, path);
80 a52ea5e4 pastith
                        builder.sendRequest("", new RestCallback(path) {
81 a52ea5e4 pastith
82 86c951b2 Panagiotis Astithas
                                @Override
83 a52ea5e4 pastith
                                public Object deserialize(Response response) {
84 a52ea5e4 pastith
                                        return deserializeResponse(path, response);
85 a52ea5e4 pastith
                                }
86 a52ea5e4 pastith
87 86c951b2 Panagiotis Astithas
                                @Override
88 a52ea5e4 pastith
                                public void handleError(Request request, Throwable exception) {
89 62f168b2 Giannis Koutsoubos
                                        if(exception instanceof RestException)
90 62f168b2 Giannis Koutsoubos
                                                if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
91 62f168b2 Giannis Koutsoubos
                                                        GWT.log("Using cache:"+cached.getUri(), null);
92 62f168b2 Giannis Koutsoubos
                                                        handleSuccess(cached);
93 62f168b2 Giannis Koutsoubos
                                                        return;
94 62f168b2 Giannis Koutsoubos
                                                }
95 a52ea5e4 pastith
                                        complete = true;
96 895035a2 pastith
                                        HeadCommand.this.onError(exception);
97 a52ea5e4 pastith
                                }
98 a52ea5e4 pastith
99 86c951b2 Panagiotis Astithas
                                @Override
100 a52ea5e4 pastith
                                public void handleSuccess(Object object) {
101 a52ea5e4 pastith
                                        result = (T) object;
102 a52ea5e4 pastith
                                        complete = true;
103 a52ea5e4 pastith
                                }
104 a52ea5e4 pastith
105 a52ea5e4 pastith
                        });
106 a52ea5e4 pastith
                } catch (Exception ex) {
107 a52ea5e4 pastith
                        complete = true;
108 a52ea5e4 pastith
                        onError(ex);
109 a52ea5e4 pastith
                }
110 a52ea5e4 pastith
        }
111 a52ea5e4 pastith
112 a52ea5e4 pastith
        public boolean isComplete() {
113 a52ea5e4 pastith
                return complete;
114 a52ea5e4 pastith
        }
115 a52ea5e4 pastith
116 a52ea5e4 pastith
        public T getResult(){
117 a52ea5e4 pastith
                return result;
118 a52ea5e4 pastith
        }
119 a52ea5e4 pastith
120 a52ea5e4 pastith
        public boolean execute() {
121 62f168b2 Giannis Koutsoubos
                if(!requestSent)
122 62f168b2 Giannis Koutsoubos
                        sendRequest();
123 a52ea5e4 pastith
                boolean com = isComplete();
124 a52ea5e4 pastith
                if(com){
125 aa8608d3 koutsoub
                        if(isShowLoadingIndicator())
126 aa8608d3 koutsoub
                                GSS.get().hideLoadingIndicator();
127 a52ea5e4 pastith
                        if(getResult() != null)
128 a52ea5e4 pastith
                                onComplete();
129 a52ea5e4 pastith
                        else
130 a52ea5e4 pastith
                                onError(new ObjectNotFoundException("Resource Not Found"));
131 a52ea5e4 pastith
                        return false;
132 a52ea5e4 pastith
                }
133 a52ea5e4 pastith
                return true;
134 a52ea5e4 pastith
        }
135 a52ea5e4 pastith
136 86c951b2 Panagiotis Astithas
        public  Object deserializeResponse(String aPath, Response response){
137 a52ea5e4 pastith
                RestResource result1 = null;
138 a52ea5e4 pastith
                if(aclass.equals(FolderResource.class)){
139 86c951b2 Panagiotis Astithas
                        result1 = new FolderResource(aPath);
140 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
141 a52ea5e4 pastith
142 a52ea5e4 pastith
                }
143 a52ea5e4 pastith
                else if(aclass.equals(FileResource.class)){
144 86c951b2 Panagiotis Astithas
                        result1 = new FileResource(aPath);
145 a52ea5e4 pastith
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
146 a52ea5e4 pastith
                }
147 a52ea5e4 pastith
                else if(aclass.equals(GroupsResource.class)){
148 86c951b2 Panagiotis Astithas
                        result1 = new GroupsResource(aPath);
149 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
150 a52ea5e4 pastith
                }
151 a52ea5e4 pastith
                else if(aclass.equals(TrashResource.class)){
152 86c951b2 Panagiotis Astithas
                        result1 = new TrashResource(aPath);
153 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
154 a52ea5e4 pastith
155 a52ea5e4 pastith
                }
156 a52ea5e4 pastith
                else if(aclass.equals(SharedResource.class)){
157 86c951b2 Panagiotis Astithas
                        result1 = new SharedResource(aPath);
158 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
159 a52ea5e4 pastith
160 a52ea5e4 pastith
                }
161 a52ea5e4 pastith
                else if(aclass.equals(GroupResource.class)){
162 86c951b2 Panagiotis Astithas
                        result1 = new GroupResource(aPath);
163 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
164 a52ea5e4 pastith
165 a52ea5e4 pastith
                }
166 a52ea5e4 pastith
                else if(aclass.equals(GroupUserResource.class)){
167 86c951b2 Panagiotis Astithas
                        result1 = new GroupUserResource(aPath);
168 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
169 a52ea5e4 pastith
170 a52ea5e4 pastith
                }
171 a52ea5e4 pastith
                else if(aclass.equals(UserResource.class)){
172 86c951b2 Panagiotis Astithas
                        result1 = new UserResource(aPath);
173 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
174 a52ea5e4 pastith
175 a52ea5e4 pastith
                }
176 a52ea5e4 pastith
                return result1;
177 a52ea5e4 pastith
        }
178 a52ea5e4 pastith
}