Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.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 a52ea5e4 pastith
package gr.ebs.gss.client.rest;
20 a52ea5e4 pastith
21 a52ea5e4 pastith
import gr.ebs.gss.client.GSS;
22 a52ea5e4 pastith
23 a52ea5e4 pastith
import com.google.gwt.http.client.Request;
24 a52ea5e4 pastith
import com.google.gwt.http.client.RequestCallback;
25 a52ea5e4 pastith
import com.google.gwt.http.client.Response;
26 a52ea5e4 pastith
27 a52ea5e4 pastith
28 a52ea5e4 pastith
/**
29 a52ea5e4 pastith
 * @author kman
30 a52ea5e4 pastith
 *
31 a52ea5e4 pastith
 */
32 895035a2 pastith
public abstract class PostCommand extends RestCommand{
33 a52ea5e4 pastith
        boolean complete = false;
34 a52ea5e4 pastith
        String postBody=null;
35 a52ea5e4 pastith
36 9a8f9536 Panagiotis Astithas
        public PostCommand(final String path, String data, final int okStatusCode) {
37 aa8608d3 koutsoub
                this(path, data, okStatusCode, true);
38 aa8608d3 koutsoub
        }
39 9a8f9536 Panagiotis Astithas
40 9a8f9536 Panagiotis Astithas
        public PostCommand(final String path, String data, final int okStatusCode, boolean showLoading) {
41 aa8608d3 koutsoub
                setShowLoadingIndicator(showLoading);
42 aa8608d3 koutsoub
                if(isShowLoadingIndicator())
43 aa8608d3 koutsoub
                        GSS.get().showLoadingIndicator();
44 a52ea5e4 pastith
45 a52ea5e4 pastith
                RestRequestBuilder builder = new RestRequestBuilder("POST", path);
46 a52ea5e4 pastith
47 a52ea5e4 pastith
                try {
48 a52ea5e4 pastith
                        handleHeaders(builder, path);
49 a52ea5e4 pastith
                        builder.sendRequest(data, new RequestCallback() {
50 a52ea5e4 pastith
51 a52ea5e4 pastith
                                public void onError(Request arg0, Throwable arg1) {
52 a52ea5e4 pastith
                                        complete = true;
53 895035a2 pastith
                                        PostCommand.this.onError(arg1);
54 a52ea5e4 pastith
                                }
55 a52ea5e4 pastith
56 9a8f9536 Panagiotis Astithas
                                public void onResponseReceived(Request req, Response resp) {
57 a52ea5e4 pastith
                                        complete=true;
58 9a8f9536 Panagiotis Astithas
                                        int status = resp.getStatusCode();
59 9a8f9536 Panagiotis Astithas
                                        // Normalize IE status 1223 to a regular 204.
60 9a8f9536 Panagiotis Astithas
                                        if (status == 1223)
61 9a8f9536 Panagiotis Astithas
                                                status = 204;
62 9a8f9536 Panagiotis Astithas
63 9a8f9536 Panagiotis Astithas
                                        if (status == okStatusCode) {
64 9a8f9536 Panagiotis Astithas
                                                postBody = resp.getText();
65 a52ea5e4 pastith
                                                onComplete();
66 9a8f9536 Panagiotis Astithas
                                        } else if (status == 403)
67 05f65955 koutsoub
                                                sessionExpired();
68 a52ea5e4 pastith
                                        else
69 9a8f9536 Panagiotis Astithas
                                                PostCommand.this.onError(new RestException(path, status, resp.getStatusText(), resp.getText()));
70 a52ea5e4 pastith
                                }
71 a52ea5e4 pastith
72 a52ea5e4 pastith
                        });
73 a52ea5e4 pastith
                } catch (Exception ex) {
74 a52ea5e4 pastith
                        complete=true;
75 a52ea5e4 pastith
                        onError(ex);
76 a52ea5e4 pastith
                }
77 a52ea5e4 pastith
        }
78 a52ea5e4 pastith
79 a52ea5e4 pastith
        public boolean isComplete() {
80 a52ea5e4 pastith
                return complete;
81 a52ea5e4 pastith
        }
82 a52ea5e4 pastith
83 a52ea5e4 pastith
        public boolean execute() {
84 a52ea5e4 pastith
                boolean com = isComplete();
85 9a8f9536 Panagiotis Astithas
                if (com) {
86 9a8f9536 Panagiotis Astithas
                        if (isShowLoadingIndicator())
87 aa8608d3 koutsoub
                                GSS.get().hideLoadingIndicator();
88 a52ea5e4 pastith
                        return false;
89 a52ea5e4 pastith
                }
90 a52ea5e4 pastith
                return true;
91 a52ea5e4 pastith
        }
92 a52ea5e4 pastith
93 a52ea5e4 pastith
        public String getPostBody() {
94 a52ea5e4 pastith
                return postBody;
95 a52ea5e4 pastith
        }
96 a52ea5e4 pastith
97 a52ea5e4 pastith
}