Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / rest / PostCommand.java @ 1206:292dec4eae08

History | View | Annotate | Download (2.6 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 org.gss_project.gss.web.client.rest;
20

    
21
import org.gss_project.gss.web.client.GSS;
22

    
23
import com.google.gwt.http.client.Request;
24
import com.google.gwt.http.client.RequestBuilder;
25
import com.google.gwt.http.client.RequestCallback;
26
import com.google.gwt.http.client.Response;
27

    
28

    
29
/**
30
 * @author kman
31
 *
32
 */
33
public abstract class PostCommand extends RestCommand{
34
        boolean complete = false;
35
        String postBody=null;
36

    
37
        public PostCommand(final String path, String data, final int okStatusCode) {
38
                this(path, data, okStatusCode, true);
39
        }
40

    
41
        public PostCommand(final String path, String data, final int okStatusCode, boolean showLoading) {
42
                setShowLoadingIndicator(showLoading);
43
                if(isShowLoadingIndicator())
44
                        GSS.get().showLoadingIndicator("Updating ",path);
45

    
46
                RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
47

    
48
                try {
49
                        handleHeaders(builder, path);
50
                        builder.sendRequest(data, new RequestCallback() {
51

    
52
                                @Override
53
                                public void onError(Request arg0, Throwable arg1) {
54
                                        complete = true;
55
                                        PostCommand.this.onError(arg1);
56
                                }
57

    
58
                                @Override
59
                                public void onResponseReceived(Request req, Response resp) {
60
                                        complete=true;
61
                                        int status = resp.getStatusCode();
62
                                        // Normalize IE status 1223 to a regular 204.
63
                                        if (status == 1223)
64
                                                status = 204;
65

    
66
                                        if (status == okStatusCode) {
67
                                                postBody = resp.getText();
68
                                                onComplete();
69
                                        } else if (status == 403)
70
                                                sessionExpired();
71
                                        else
72
                                                PostCommand.this.onError(new RestException(path, status, resp.getStatusText(), resp.getText()));
73
                                }
74

    
75
                        });
76
                } catch (Exception ex) {
77
                        complete=true;
78
                        onError(ex);
79
                }
80
        }
81

    
82
        public boolean isComplete() {
83
                return complete;
84
        }
85

    
86
        @Override
87
        public boolean execute() {
88
                boolean com = isComplete();
89
                if (com) {
90
                        if (isShowLoadingIndicator())
91
                                GSS.get().hideLoadingIndicator();
92
                        return false;
93
                }
94
                return true;
95
        }
96

    
97
        public String getPostBody() {
98
                return postBody;
99
        }
100

    
101
}