root / web_client / src / gr / grnet / pithos / web / client / rest / MultiplePostCommand.java @ f55cf326
History | View | Annotate | Download (4.7 kB)
1 | ab1eb3f8 | Christos Stathis | /*
|
---|---|---|---|
2 | 58777026 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | 58777026 | Christos Stathis | *
|
4 | 58777026 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 58777026 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 58777026 | Christos Stathis | * conditions are met:
|
7 | 58777026 | Christos Stathis | *
|
8 | 58777026 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 58777026 | Christos Stathis | * disclaimer.
|
11 | 58777026 | Christos Stathis | *
|
12 | 58777026 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 58777026 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 58777026 | Christos Stathis | * provided with the distribution.
|
16 | 58777026 | Christos Stathis | *
|
17 | 58777026 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 58777026 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 58777026 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 58777026 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 58777026 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 58777026 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 58777026 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 58777026 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 58777026 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 58777026 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 58777026 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 58777026 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 58777026 | Christos Stathis | *
|
30 | 58777026 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 58777026 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 58777026 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 58777026 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | ab1eb3f8 | Christos Stathis | */
|
35 | ab1eb3f8 | Christos Stathis | package gr.grnet.pithos.web.client.rest; |
36 | ab1eb3f8 | Christos Stathis | |
37 | f55cf326 | Christos Stathis | import gr.grnet.pithos.web.client.Pithos; |
38 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.InsufficientPermissionsException; |
39 | ab1eb3f8 | Christos Stathis | |
40 | ab1eb3f8 | Christos Stathis | import java.util.ArrayList; |
41 | ab1eb3f8 | Christos Stathis | import java.util.HashMap; |
42 | ab1eb3f8 | Christos Stathis | import java.util.List; |
43 | ab1eb3f8 | Christos Stathis | import java.util.Map; |
44 | ab1eb3f8 | Christos Stathis | |
45 | ab1eb3f8 | Christos Stathis | import com.google.gwt.core.client.GWT; |
46 | ab1eb3f8 | Christos Stathis | import com.google.gwt.http.client.Request; |
47 | ab1eb3f8 | Christos Stathis | import com.google.gwt.http.client.RequestBuilder; |
48 | ab1eb3f8 | Christos Stathis | import com.google.gwt.http.client.RequestCallback; |
49 | ab1eb3f8 | Christos Stathis | import com.google.gwt.http.client.Response; |
50 | ab1eb3f8 | Christos Stathis | |
51 | ab1eb3f8 | Christos Stathis | |
52 | ab1eb3f8 | Christos Stathis | public abstract class MultiplePostCommand extends RestCommand { |
53 | ab1eb3f8 | Christos Stathis | |
54 | ab1eb3f8 | Christos Stathis | |
55 | ab1eb3f8 | Christos Stathis | Map<String, Throwable> errors = new HashMap<String, Throwable>(); |
56 | ab1eb3f8 | Christos Stathis | |
57 | ab1eb3f8 | Christos Stathis | List<String> successPaths = new ArrayList<String>(); |
58 | ab1eb3f8 | Christos Stathis | |
59 | ab1eb3f8 | Christos Stathis | String[] paths; |
60 | ab1eb3f8 | Christos Stathis | |
61 | ab1eb3f8 | Christos Stathis | public MultiplePostCommand(String[] pathToDelete, final int okStatusCode){ |
62 | ab1eb3f8 | Christos Stathis | this(pathToDelete, okStatusCode, true); |
63 | ab1eb3f8 | Christos Stathis | } |
64 | ab1eb3f8 | Christos Stathis | |
65 | ab1eb3f8 | Christos Stathis | public MultiplePostCommand(String[] pathToDelete, String data, final int okStatusCode){ |
66 | ab1eb3f8 | Christos Stathis | this(pathToDelete, data, okStatusCode, true); |
67 | ab1eb3f8 | Christos Stathis | } |
68 | ab1eb3f8 | Christos Stathis | |
69 | ab1eb3f8 | Christos Stathis | public MultiplePostCommand(String[] pathToDelete, final int okStatusCode, boolean showLoading){ |
70 | ab1eb3f8 | Christos Stathis | this(pathToDelete, "", okStatusCode, showLoading); |
71 | ab1eb3f8 | Christos Stathis | } |
72 | ab1eb3f8 | Christos Stathis | |
73 | ab1eb3f8 | Christos Stathis | public MultiplePostCommand(String[] pathToDelete, String data, final int okStatusCode, boolean showLoading){ |
74 | ab1eb3f8 | Christos Stathis | setShowLoadingIndicator(showLoading); |
75 | ab1eb3f8 | Christos Stathis | if(isShowLoadingIndicator())
|
76 | f55cf326 | Christos Stathis | Pithos.get().showLoadingIndicator("Updating "+pathToDelete.length+" items", null); |
77 | ab1eb3f8 | Christos Stathis | paths = pathToDelete; |
78 | ab1eb3f8 | Christos Stathis | for (final String pathg : pathToDelete) { |
79 | ab1eb3f8 | Christos Stathis | GWT.log("[DEL]"+pathg, null); |
80 | ab1eb3f8 | Christos Stathis | RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, pathg);
|
81 | ab1eb3f8 | Christos Stathis | |
82 | ab1eb3f8 | Christos Stathis | try {
|
83 | ab1eb3f8 | Christos Stathis | handleHeaders(builder, pathg); |
84 | ab1eb3f8 | Christos Stathis | builder.sendRequest(data, new RequestCallback() {
|
85 | ab1eb3f8 | Christos Stathis | |
86 | ab1eb3f8 | Christos Stathis | @Override
|
87 | ab1eb3f8 | Christos Stathis | public void onError(Request arg0, Throwable arg1) { |
88 | ab1eb3f8 | Christos Stathis | errors.put(pathg, arg1); |
89 | ab1eb3f8 | Christos Stathis | } |
90 | ab1eb3f8 | Christos Stathis | |
91 | ab1eb3f8 | Christos Stathis | @Override
|
92 | ab1eb3f8 | Christos Stathis | public void onResponseReceived(Request arg0, Response arg1) { |
93 | ab1eb3f8 | Christos Stathis | if (arg1.getStatusCode() == okStatusCode)
|
94 | ab1eb3f8 | Christos Stathis | successPaths.add(pathg); |
95 | ab1eb3f8 | Christos Stathis | else if(arg1.getStatusCode() == 403) |
96 | ab1eb3f8 | Christos Stathis | sessionExpired(); |
97 | ab1eb3f8 | Christos Stathis | else if (arg1.getStatusCode() == 405) |
98 | ab1eb3f8 | Christos Stathis | errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource")); |
99 | ab1eb3f8 | Christos Stathis | else
|
100 | ab1eb3f8 | Christos Stathis | errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
|
101 | ab1eb3f8 | Christos Stathis | } |
102 | ab1eb3f8 | Christos Stathis | |
103 | ab1eb3f8 | Christos Stathis | }); |
104 | ab1eb3f8 | Christos Stathis | } catch (Exception ex) { |
105 | ab1eb3f8 | Christos Stathis | errors.put(pathg, ex); |
106 | ab1eb3f8 | Christos Stathis | } |
107 | ab1eb3f8 | Christos Stathis | } |
108 | ab1eb3f8 | Christos Stathis | } |
109 | ab1eb3f8 | Christos Stathis | |
110 | ab1eb3f8 | Christos Stathis | public boolean isComplete() { |
111 | ab1eb3f8 | Christos Stathis | return errors.size() + successPaths.size() == paths.length;
|
112 | ab1eb3f8 | Christos Stathis | } |
113 | ab1eb3f8 | Christos Stathis | |
114 | ab1eb3f8 | Christos Stathis | @Override
|
115 | ab1eb3f8 | Christos Stathis | public boolean execute() { |
116 | ab1eb3f8 | Christos Stathis | boolean com = isComplete();
|
117 | ab1eb3f8 | Christos Stathis | if (com) {
|
118 | ab1eb3f8 | Christos Stathis | if(hasErrors())
|
119 | ab1eb3f8 | Christos Stathis | for(String p : errors.keySet()) |
120 | ab1eb3f8 | Christos Stathis | onError(p, errors.get(p)); |
121 | ab1eb3f8 | Christos Stathis | onComplete(); |
122 | ab1eb3f8 | Christos Stathis | if(isShowLoadingIndicator())
|
123 | f55cf326 | Christos Stathis | Pithos.get().hideLoadingIndicator(); |
124 | ab1eb3f8 | Christos Stathis | return false; |
125 | ab1eb3f8 | Christos Stathis | } |
126 | ab1eb3f8 | Christos Stathis | return true; |
127 | ab1eb3f8 | Christos Stathis | } |
128 | ab1eb3f8 | Christos Stathis | |
129 | ab1eb3f8 | Christos Stathis | |
130 | ab1eb3f8 | Christos Stathis | /**
|
131 | ab1eb3f8 | Christos Stathis | * @param p
|
132 | ab1eb3f8 | Christos Stathis | * @param throwable
|
133 | ab1eb3f8 | Christos Stathis | */
|
134 | ab1eb3f8 | Christos Stathis | public abstract void onError(String p, Throwable throwable); |
135 | ab1eb3f8 | Christos Stathis | |
136 | ab1eb3f8 | Christos Stathis | public boolean hasErrors(){ |
137 | ab1eb3f8 | Christos Stathis | return errors.size() >0; |
138 | ab1eb3f8 | Christos Stathis | } |
139 | ab1eb3f8 | Christos Stathis | |
140 | ab1eb3f8 | Christos Stathis | |
141 | ab1eb3f8 | Christos Stathis | /**
|
142 | ab1eb3f8 | Christos Stathis | * Retrieve the errors.
|
143 | ab1eb3f8 | Christos Stathis | *
|
144 | ab1eb3f8 | Christos Stathis | * @return the errors
|
145 | ab1eb3f8 | Christos Stathis | */
|
146 | ab1eb3f8 | Christos Stathis | public Map<String, Throwable> getErrors() { |
147 | ab1eb3f8 | Christos Stathis | return errors;
|
148 | ab1eb3f8 | Christos Stathis | } |
149 | ab1eb3f8 | Christos Stathis | |
150 | ab1eb3f8 | Christos Stathis | public void debug(){ |
151 | ab1eb3f8 | Christos Stathis | GWT.log("-ERRORS-->"+getErrors().size(), null); |
152 | ab1eb3f8 | Christos Stathis | for(String p : getErrors().keySet()) |
153 | ab1eb3f8 | Christos Stathis | GWT.log("error:"+p, getErrors().get(p));
|
154 | ab1eb3f8 | Christos Stathis | } |
155 | ab1eb3f8 | Christos Stathis | |
156 | ab1eb3f8 | Christos Stathis | @Override
|
157 | ab1eb3f8 | Christos Stathis | public void onError(Throwable t) { |
158 | ab1eb3f8 | Christos Stathis | |
159 | ab1eb3f8 | Christos Stathis | |
160 | ab1eb3f8 | Christos Stathis | } |
161 | ab1eb3f8 | Christos Stathis | |
162 | ab1eb3f8 | Christos Stathis | } |