Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5 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 af6aa461 Christos V. Stathis
package gr.ebs.gss.web.client.rest;
20 a52ea5e4 pastith
21 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.GSS;
22 af6aa461 Christos V. Stathis
import gr.ebs.gss.common.exceptions.ObjectNotFoundException;
23 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.FileResource;
24 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.FolderResource;
25 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.GroupResource;
26 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.GroupUserResource;
27 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.GroupsResource;
28 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.RestResource;
29 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.SharedResource;
30 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.client.rest.resource.TrashResource;
31 af6aa461 Christos V. Stathis
import gr.ebs.gss.web.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 023f6f1e Panagiotis Astithas
import com.google.gwt.http.client.RequestBuilder;
36 a52ea5e4 pastith
import com.google.gwt.http.client.Response;
37 a52ea5e4 pastith
38 a52ea5e4 pastith
39 a52ea5e4 pastith
/**
40 a52ea5e4 pastith
 * @author kman
41 a52ea5e4 pastith
 *
42 a52ea5e4 pastith
 */
43 86c951b2 Panagiotis Astithas
public  abstract class HeadCommand<T extends RestResource> extends RestCommand{
44 a52ea5e4 pastith
45 a52ea5e4 pastith
        boolean complete = false;
46 a52ea5e4 pastith
        T result = null;
47 a52ea5e4 pastith
        Class<T> aclass;
48 62f168b2 Giannis Koutsoubos
        private boolean requestSent = false;
49 62f168b2 Giannis Koutsoubos
        T cached;
50 62f168b2 Giannis Koutsoubos
        final String path;
51 86c951b2 Panagiotis Astithas
52 86c951b2 Panagiotis Astithas
        public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
53 86c951b2 Panagiotis Astithas
                this(theclass, pathToGet, true, theCached);
54 aa8608d3 koutsoub
        }
55 86c951b2 Panagiotis Astithas
56 86c951b2 Panagiotis Astithas
        public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
57 aa8608d3 koutsoub
                setShowLoadingIndicator(showLoading);
58 86c951b2 Panagiotis Astithas
                this.aclass = theClass;
59 aa8608d3 koutsoub
                if(isShowLoadingIndicator())
60 c9592525 koutsoub
                        GSS.get().showLoadingIndicator("Getting ",pathToGet);
61 62f168b2 Giannis Koutsoubos
62 86c951b2 Panagiotis Astithas
                if(theClass.equals(FileResource.class))
63 a52ea5e4 pastith
                        path = pathToGet;
64 a52ea5e4 pastith
                else
65 a52ea5e4 pastith
                        path = fixPath(pathToGet);
66 86c951b2 Panagiotis Astithas
                this.cached = theCached;
67 a52ea5e4 pastith
68 62f168b2 Giannis Koutsoubos
        }
69 62f168b2 Giannis Koutsoubos
70 62f168b2 Giannis Koutsoubos
        private void sendRequest(){
71 62f168b2 Giannis Koutsoubos
                if(requestSent)
72 62f168b2 Giannis Koutsoubos
                        return;
73 62f168b2 Giannis Koutsoubos
                requestSent=true;
74 023f6f1e Panagiotis Astithas
                RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, path);
75 62f168b2 Giannis Koutsoubos
                if(cached!=null && cached.getLastModifiedSince() != null){
76 62f168b2 Giannis Koutsoubos
                        GWT.log("ADDING IF MODIFIED HEADERS", null);
77 62f168b2 Giannis Koutsoubos
                        builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
78 62f168b2 Giannis Koutsoubos
                }
79 a52ea5e4 pastith
                try {
80 a52ea5e4 pastith
                        handleHeaders(builder, path);
81 a52ea5e4 pastith
                        builder.sendRequest("", new RestCallback(path) {
82 a52ea5e4 pastith
83 86c951b2 Panagiotis Astithas
                                @Override
84 a52ea5e4 pastith
                                public Object deserialize(Response response) {
85 a52ea5e4 pastith
                                        return deserializeResponse(path, response);
86 a52ea5e4 pastith
                                }
87 a52ea5e4 pastith
88 86c951b2 Panagiotis Astithas
                                @Override
89 a52ea5e4 pastith
                                public void handleError(Request request, Throwable exception) {
90 62f168b2 Giannis Koutsoubos
                                        if(exception instanceof RestException)
91 62f168b2 Giannis Koutsoubos
                                                if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
92 62f168b2 Giannis Koutsoubos
                                                        GWT.log("Using cache:"+cached.getUri(), null);
93 62f168b2 Giannis Koutsoubos
                                                        handleSuccess(cached);
94 62f168b2 Giannis Koutsoubos
                                                        return;
95 62f168b2 Giannis Koutsoubos
                                                }
96 a52ea5e4 pastith
                                        complete = true;
97 895035a2 pastith
                                        HeadCommand.this.onError(exception);
98 a52ea5e4 pastith
                                }
99 a52ea5e4 pastith
100 86c951b2 Panagiotis Astithas
                                @Override
101 a52ea5e4 pastith
                                public void handleSuccess(Object object) {
102 a52ea5e4 pastith
                                        result = (T) object;
103 a52ea5e4 pastith
                                        complete = true;
104 a52ea5e4 pastith
                                }
105 a52ea5e4 pastith
106 a52ea5e4 pastith
                        });
107 a52ea5e4 pastith
                } catch (Exception ex) {
108 a52ea5e4 pastith
                        complete = true;
109 a52ea5e4 pastith
                        onError(ex);
110 a52ea5e4 pastith
                }
111 a52ea5e4 pastith
        }
112 a52ea5e4 pastith
113 a52ea5e4 pastith
        public boolean isComplete() {
114 a52ea5e4 pastith
                return complete;
115 a52ea5e4 pastith
        }
116 a52ea5e4 pastith
117 a52ea5e4 pastith
        public T getResult(){
118 a52ea5e4 pastith
                return result;
119 a52ea5e4 pastith
        }
120 a52ea5e4 pastith
121 023f6f1e Panagiotis Astithas
        @Override
122 a52ea5e4 pastith
        public boolean execute() {
123 62f168b2 Giannis Koutsoubos
                if(!requestSent)
124 62f168b2 Giannis Koutsoubos
                        sendRequest();
125 a52ea5e4 pastith
                boolean com = isComplete();
126 a52ea5e4 pastith
                if(com){
127 aa8608d3 koutsoub
                        if(isShowLoadingIndicator())
128 aa8608d3 koutsoub
                                GSS.get().hideLoadingIndicator();
129 a52ea5e4 pastith
                        if(getResult() != null)
130 a52ea5e4 pastith
                                onComplete();
131 a52ea5e4 pastith
                        else
132 a52ea5e4 pastith
                                onError(new ObjectNotFoundException("Resource Not Found"));
133 a52ea5e4 pastith
                        return false;
134 a52ea5e4 pastith
                }
135 a52ea5e4 pastith
                return true;
136 a52ea5e4 pastith
        }
137 a52ea5e4 pastith
138 86c951b2 Panagiotis Astithas
        public  Object deserializeResponse(String aPath, Response response){
139 a52ea5e4 pastith
                RestResource result1 = null;
140 a52ea5e4 pastith
                if(aclass.equals(FolderResource.class)){
141 86c951b2 Panagiotis Astithas
                        result1 = new FolderResource(aPath);
142 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
143 a52ea5e4 pastith
144 a52ea5e4 pastith
                }
145 a52ea5e4 pastith
                else if(aclass.equals(FileResource.class)){
146 86c951b2 Panagiotis Astithas
                        result1 = new FileResource(aPath);
147 a52ea5e4 pastith
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
148 a52ea5e4 pastith
                }
149 a52ea5e4 pastith
                else if(aclass.equals(GroupsResource.class)){
150 86c951b2 Panagiotis Astithas
                        result1 = new GroupsResource(aPath);
151 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
152 a52ea5e4 pastith
                }
153 a52ea5e4 pastith
                else if(aclass.equals(TrashResource.class)){
154 86c951b2 Panagiotis Astithas
                        result1 = new TrashResource(aPath);
155 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
156 a52ea5e4 pastith
157 a52ea5e4 pastith
                }
158 a52ea5e4 pastith
                else if(aclass.equals(SharedResource.class)){
159 86c951b2 Panagiotis Astithas
                        result1 = new SharedResource(aPath);
160 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
161 a52ea5e4 pastith
162 a52ea5e4 pastith
                }
163 a52ea5e4 pastith
                else if(aclass.equals(GroupResource.class)){
164 86c951b2 Panagiotis Astithas
                        result1 = new GroupResource(aPath);
165 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
166 a52ea5e4 pastith
167 a52ea5e4 pastith
                }
168 a52ea5e4 pastith
                else if(aclass.equals(GroupUserResource.class)){
169 86c951b2 Panagiotis Astithas
                        result1 = new GroupUserResource(aPath);
170 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
171 a52ea5e4 pastith
172 a52ea5e4 pastith
                }
173 a52ea5e4 pastith
                else if(aclass.equals(UserResource.class)){
174 86c951b2 Panagiotis Astithas
                        result1 = new UserResource(aPath);
175 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
176 a52ea5e4 pastith
177 a52ea5e4 pastith
                }
178 a52ea5e4 pastith
                return result1;
179 a52ea5e4 pastith
        }
180 a52ea5e4 pastith
}