Statistics
| Branch: | Tag: | Revision:

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

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.RequestBuilder;
36
import com.google.gwt.http.client.Response;
37

    
38

    
39
/**
40
 * @author kman
41
 *
42
 */
43
public  abstract class HeadCommand<T extends RestResource> extends RestCommand{
44

    
45
        boolean complete = false;
46
        T result = null;
47
        Class<T> aclass;
48
        private boolean requestSent = false;
49
        T cached;
50
        final String path;
51

    
52
        public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
53
                this(theclass, pathToGet, true, theCached);
54
        }
55

    
56
        public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
57
                setShowLoadingIndicator(showLoading);
58
                this.aclass = theClass;
59
                if(isShowLoadingIndicator())
60
                        GSS.get().showLoadingIndicator();
61

    
62
                if(theClass.equals(FileResource.class))
63
                        path = pathToGet;
64
                else
65
                        path = fixPath(pathToGet);
66
                this.cached = theCached;
67

    
68
        }
69

    
70
        private void sendRequest(){
71
                if(requestSent)
72
                        return;
73
                requestSent=true;
74
                RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, path);
75
                if(cached!=null && cached.getLastModifiedSince() != null){
76
                        GWT.log("ADDING IF MODIFIED HEADERS", null);
77
                        builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
78
                }
79
                try {
80
                        handleHeaders(builder, path);
81
                        builder.sendRequest("", new RestCallback(path) {
82

    
83
                                @Override
84
                                public Object deserialize(Response response) {
85
                                        return deserializeResponse(path, response);
86
                                }
87

    
88
                                @Override
89
                                public void handleError(Request request, Throwable exception) {
90
                                        if(exception instanceof RestException)
91
                                                if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
92
                                                        GWT.log("Using cache:"+cached.getUri(), null);
93
                                                        handleSuccess(cached);
94
                                                        return;
95
                                                }
96
                                        complete = true;
97
                                        HeadCommand.this.onError(exception);
98
                                }
99

    
100
                                @Override
101
                                public void handleSuccess(Object object) {
102
                                        result = (T) object;
103
                                        complete = true;
104
                                }
105

    
106
                        });
107
                } catch (Exception ex) {
108
                        complete = true;
109
                        onError(ex);
110
                }
111
        }
112

    
113
        public boolean isComplete() {
114
                return complete;
115
        }
116

    
117
        public T getResult(){
118
                return result;
119
        }
120

    
121
        @Override
122
        public boolean execute() {
123
                if(!requestSent)
124
                        sendRequest();
125
                boolean com = isComplete();
126
                if(com){
127
                        if(isShowLoadingIndicator())
128
                                GSS.get().hideLoadingIndicator();
129
                        if(getResult() != null)
130
                                onComplete();
131
                        else
132
                                onError(new ObjectNotFoundException("Resource Not Found"));
133
                        return false;
134
                }
135
                return true;
136
        }
137

    
138
        public  Object deserializeResponse(String aPath, Response response){
139
                RestResource result1 = null;
140
                if(aclass.equals(FolderResource.class)){
141
                        result1 = new FolderResource(aPath);
142
                        result1.createFromJSON(response.getText());
143

    
144
                }
145
                else if(aclass.equals(FileResource.class)){
146
                        result1 = new FileResource(aPath);
147
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
148
                }
149
                else if(aclass.equals(GroupsResource.class)){
150
                        result1 = new GroupsResource(aPath);
151
                        result1.createFromJSON(response.getText());
152
                }
153
                else if(aclass.equals(TrashResource.class)){
154
                        result1 = new TrashResource(aPath);
155
                        result1.createFromJSON(response.getText());
156

    
157
                }
158
                else if(aclass.equals(SharedResource.class)){
159
                        result1 = new SharedResource(aPath);
160
                        result1.createFromJSON(response.getText());
161

    
162
                }
163
                else if(aclass.equals(GroupResource.class)){
164
                        result1 = new GroupResource(aPath);
165
                        result1.createFromJSON(response.getText());
166

    
167
                }
168
                else if(aclass.equals(GroupUserResource.class)){
169
                        result1 = new GroupUserResource(aPath);
170
                        result1.createFromJSON(response.getText());
171

    
172
                }
173
                else if(aclass.equals(UserResource.class)){
174
                        result1 = new UserResource(aPath);
175
                        result1.createFromJSON(response.getText());
176

    
177
                }
178
                return result1;
179
        }
180
}