|
9 | 9 | import com.google.gson.JsonDeserializer;
|
10 | 10 | import com.google.gson.JsonObject;
|
11 | 11 | import com.koushikdutta.async.future.FutureCallback;
|
| 12 | +import com.koushikdutta.async.http.Headers; |
| 13 | +import com.koushikdutta.async.http.NameValuePair; |
12 | 14 | import com.koushikdutta.ion.Ion;
|
13 | 15 | import com.koushikdutta.ion.Response;
|
14 | 16 |
|
15 | 17 | import java.lang.reflect.Type;
|
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
16 | 20 | import java.util.List;
|
| 21 | +import java.util.Map; |
17 | 22 |
|
18 | 23 | import stacksmashers.smp30connectionlib.delegate.ODataHttpClientCallback;
|
| 24 | +import stacksmashers.smp30connectionlib.delegate.ODataHttpPostClientCallback; |
| 25 | +import stacksmashers.smp30connectionlib.delegate.ODataHttpTokenClientCallback; |
19 | 26 | import stacksmashers.smp30connectionlib.enums.TypeHttpProtocol;
|
20 | 27 | import stacksmashers.smp30connectionlib.exception.SmpExceptionInvalidInput;
|
21 | 28 | import stacksmashers.smp30connectionlib.netutil.IonFactory;
|
@@ -54,6 +61,8 @@ public class ODataHttpClient {
|
54 | 61 | * Handle events from the OData service
|
55 | 62 | **/
|
56 | 63 | private ODataHttpClientCallback delegate;
|
| 64 | + private ODataHttpTokenClientCallback tokenDelegate; |
| 65 | + private ODataHttpPostClientCallback postDelegate; |
57 | 66 |
|
58 | 67 | /**
|
59 | 68 | * Class used to inject json raw data
|
@@ -83,6 +92,16 @@ public void setDelegate(ODataHttpClientCallback delegate) throws SmpExceptionInv
|
83 | 92 | this.delegate = delegate;
|
84 | 93 | }
|
85 | 94 |
|
| 95 | + public void setTokenDelegate(ODataHttpTokenClientCallback tokenDelegate) throws SmpExceptionInvalidInput { |
| 96 | + InputValidator.validateNotNull(tokenDelegate); |
| 97 | + this.tokenDelegate = tokenDelegate; |
| 98 | + } |
| 99 | + |
| 100 | + public void setPostDelegate(ODataHttpPostClientCallback postDelegate) throws SmpExceptionInvalidInput { |
| 101 | + InputValidator.validateNotNull(postDelegate); |
| 102 | + this.postDelegate = postDelegate; |
| 103 | + } |
| 104 | + |
86 | 105 | public void setDeserializer(Class clazz, JsonDeserializer deserializer) throws SmpExceptionInvalidInput {
|
87 | 106 | InputValidator.validateNotNull(clazz);
|
88 | 107 | InputValidator.validateNotNull(deserializer);
|
@@ -149,4 +168,75 @@ private List buildResult(JsonObject raw, Type type) {
|
149 | 168 | return result;
|
150 | 169 | }
|
151 | 170 |
|
| 171 | + public void getXCSRFToken(String url) throws SmpExceptionInvalidInput { |
| 172 | + TypeHttpProtocol protocol = InputValidator.getURLProtocol(url); |
| 173 | + |
| 174 | + try { |
| 175 | + IonFactory ionFactory = new IonFactory(this.context, protocol); |
| 176 | + Ion ion = ionFactory.build(); |
| 177 | + ion.with(ionFactory.getContext()) |
| 178 | + .load(url) |
| 179 | + .addHeader("X-SMP-APPCID", xsmpappcid) |
| 180 | + .addHeader("Accept", "application/json") |
| 181 | + .addHeader("X-CSRF-Token","Fetch") |
| 182 | + .basicAuthentication(username, password) |
| 183 | + .asJsonObject() |
| 184 | + .withResponse() |
| 185 | + .setCallback(new FutureCallback<Response<JsonObject>>() { |
| 186 | + @Override |
| 187 | + public void onCompleted(Exception e, Response<JsonObject> result) { |
| 188 | + try { |
| 189 | + new IonResponseManager(e, result); |
| 190 | + Headers response = result.getHeaders().getHeaders(); |
| 191 | + String token = response.get("x-csrf-token"); |
| 192 | + |
| 193 | + if (tokenDelegate != null) { |
| 194 | + tokenDelegate.onFetchXCSRFTokenSuccessCallback(token); |
| 195 | + } |
| 196 | + } catch (Exception e1) { |
| 197 | + if (tokenDelegate != null) { |
| 198 | + tokenDelegate.onErrorCallback(e, result); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + }); |
| 203 | + } catch (Exception e) { |
| 204 | + throw new SmpExceptionInvalidInput(e.getMessage()); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public void sendODataEntitySet(String url, JsonObject jsonObject, Header... params) throws SmpExceptionInvalidInput { |
| 209 | + TypeHttpProtocol protocol = InputValidator.getURLProtocol(url); |
| 210 | + |
| 211 | + try { |
| 212 | + IonFactory ionFactory = new IonFactory(this.context, protocol); |
| 213 | + Ion ion = ionFactory.build(); |
| 214 | + ion.with(ionFactory.getContext()) |
| 215 | + .load(url) |
| 216 | + .setHeader(params) |
| 217 | + .basicAuthentication(username, password) |
| 218 | + .setJsonObjectBody(jsonObject) |
| 219 | + .asJsonObject() |
| 220 | + .withResponse() |
| 221 | + .setCallback(new FutureCallback<Response<JsonObject>>() { |
| 222 | + @Override |
| 223 | + public void onCompleted(Exception e, Response<JsonObject> result) { |
| 224 | + try { |
| 225 | + new IonResponseManager(e, result); |
| 226 | + if (postDelegate != null) { |
| 227 | + postDelegate.onPostSuccessCallback(); |
| 228 | + } |
| 229 | + } catch (Exception e1) { |
| 230 | + if (postDelegate != null) { |
| 231 | + postDelegate.onErrorCallback(e, result); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + }); |
| 236 | + } catch (Exception e) { |
| 237 | + throw new SmpExceptionInvalidInput(e.getMessage()); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + |
152 | 242 | }
|
0 commit comments