Skip to content

Commit d7a69f9

Browse files
authored
Merge pull request #3 from Y2JChamp/master
X-CSRF-Token FETCH
2 parents 19c9392 + 0cdb986 commit d7a69f9

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package stacksmashers.smp30connectionlib.delegate;
2+
3+
import com.google.gson.JsonObject;
4+
import com.koushikdutta.ion.Response;
5+
6+
/**
7+
* Created by Y2J on 03/05/17.
8+
*/
9+
10+
public interface ODataHttpPostClientCallback<T> {
11+
/**
12+
* Invoked on network or HTTP service error
13+
**/
14+
void onErrorCallback(Exception ex, Response<JsonObject> response);
15+
16+
/**
17+
* Invoked on success
18+
*/
19+
void onPostSuccessCallback();
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package stacksmashers.smp30connectionlib.delegate;
2+
3+
import com.google.gson.JsonObject;
4+
import com.koushikdutta.ion.Response;
5+
6+
/**
7+
* Created by Y2J on 03/05/17.
8+
*/
9+
10+
public interface ODataHttpTokenClientCallback<T> {
11+
/**
12+
* Invoked on network or HTTP service error
13+
**/
14+
void onErrorCallback(Exception ex, Response<JsonObject> response);
15+
16+
/**
17+
* Invoked on success of token fetch
18+
*/
19+
void onFetchXCSRFTokenSuccessCallback(String token);
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package stacksmashers.smp30connectionlib.smp;
2+
3+
import com.koushikdutta.async.http.NameValuePair;
4+
5+
/**
6+
* Created by Y2J on 03/05/17.
7+
*/
8+
9+
public class Header implements NameValuePair {
10+
11+
private String name;
12+
private String value;
13+
14+
public Header(String name, String value){
15+
this.name = name;
16+
this.value =value;
17+
}
18+
19+
@Override
20+
public String getName() {
21+
return name;
22+
}
23+
24+
@Override
25+
public String getValue() {
26+
return value;
27+
}
28+
}

smp3.0connectionlib/src/main/java/stacksmashers/smp30connectionlib/smp/ODataHttpClient.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99
import com.google.gson.JsonDeserializer;
1010
import com.google.gson.JsonObject;
1111
import com.koushikdutta.async.future.FutureCallback;
12+
import com.koushikdutta.async.http.Headers;
13+
import com.koushikdutta.async.http.NameValuePair;
1214
import com.koushikdutta.ion.Ion;
1315
import com.koushikdutta.ion.Response;
1416

1517
import java.lang.reflect.Type;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
1620
import java.util.List;
21+
import java.util.Map;
1722

1823
import stacksmashers.smp30connectionlib.delegate.ODataHttpClientCallback;
24+
import stacksmashers.smp30connectionlib.delegate.ODataHttpPostClientCallback;
25+
import stacksmashers.smp30connectionlib.delegate.ODataHttpTokenClientCallback;
1926
import stacksmashers.smp30connectionlib.enums.TypeHttpProtocol;
2027
import stacksmashers.smp30connectionlib.exception.SmpExceptionInvalidInput;
2128
import stacksmashers.smp30connectionlib.netutil.IonFactory;
@@ -54,6 +61,8 @@ public class ODataHttpClient {
5461
* Handle events from the OData service
5562
**/
5663
private ODataHttpClientCallback delegate;
64+
private ODataHttpTokenClientCallback tokenDelegate;
65+
private ODataHttpPostClientCallback postDelegate;
5766

5867
/**
5968
* Class used to inject json raw data
@@ -83,6 +92,16 @@ public void setDelegate(ODataHttpClientCallback delegate) throws SmpExceptionInv
8392
this.delegate = delegate;
8493
}
8594

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+
86105
public void setDeserializer(Class clazz, JsonDeserializer deserializer) throws SmpExceptionInvalidInput {
87106
InputValidator.validateNotNull(clazz);
88107
InputValidator.validateNotNull(deserializer);
@@ -149,4 +168,75 @@ private List buildResult(JsonObject raw, Type type) {
149168
return result;
150169
}
151170

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+
152242
}

0 commit comments

Comments
 (0)