commit f45d55bea8b71051c1941fd6d9d605818eecc09c Author: Hashik Donthineni HashikDonthineni@gmail.com Date: Wed Jun 17 13:25:48 2020 +0530
Removed hardcoded headers from service and added them dynamically --- .../main/java/org/torproject/snowflake/GlobalApplication.java | 11 +++++++++++ .../java/org/torproject/snowflake/MyPersistentService.java | 6 +++--- .../org/torproject/snowflake/services/GetOfferService.java | 8 ++++++-- .../org/torproject/snowflake/services/SendAnswerService.java | 8 ++++++-- 4 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/app/src/main/java/org/torproject/snowflake/GlobalApplication.java b/app/src/main/java/org/torproject/snowflake/GlobalApplication.java index 288733d..42e3b7f 100644 --- a/app/src/main/java/org/torproject/snowflake/GlobalApplication.java +++ b/app/src/main/java/org/torproject/snowflake/GlobalApplication.java @@ -2,10 +2,21 @@ package org.torproject.snowflake;
import android.app.Application;
+import java.util.HashMap; +import java.util.Map; + public class GlobalApplication extends Application { private final static String BROKER_URL = "http://10.0.2.2:8080"; //10.0.2.2 is used to access computer's local host from Android Emulator.
public static String getBrokerUrl() { return BROKER_URL; } + + public static Map<String, String> getHeadersMap() { + HashMap<String, String> map = new HashMap<>(); + map.put("Content-type", "application/json"); + //http or https shouldn't be part of header or els broker will throw error. + map.put("Host", getBrokerUrl().replace("http://", "")); + return map; + } } diff --git a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java index 60cd0e0..91550d6 100644 --- a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java +++ b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java @@ -286,7 +286,7 @@ public class MyPersistentService extends Service { @Override public void dataChannelStateChange(final DataChannel.State STATE) { Log.d(TAG, "dataChannelStateChange: Data Channel State: " + STATE); - if(STATE == DataChannel.State.OPEN){ + if (STATE == DataChannel.State.OPEN) { updateNotification("Connection Established. Serving one client."); } } @@ -326,7 +326,7 @@ public class MyPersistentService extends Service { Log.d(TAG, "fetchOffer: Fetching offer from broker."); ///Retrofit call final GetOfferService getOfferService = RetroServiceGenerator.createService(GetOfferService.class); - Observable<SDPOfferResponse> offer = getOfferService.getOffer(new OfferRequestBody("555")); //TODO:Randomly Generate SID. + Observable<SDPOfferResponse> offer = getOfferService.getOffer(GlobalApplication.getHeadersMap(), new OfferRequestBody("555")); //TODO:Randomly Generate SID. serviceDisposable = offer.subscribeOn(Schedulers.io()) .delaySubscription(5000, TimeUnit.MILLISECONDS) //Delay of 5 seconds before sending request to avoid sending too many requests in case of a failure. .observeOn(AndroidSchedulers.mainThread()) @@ -381,7 +381,7 @@ public class MyPersistentService extends Service { bodySDP.setSdp(SDPSerializer.serializeAnswer(sessionDescription)); AnswerBody body = new AnswerBody("555", bodySDP.toString()); //TODO:Use randomly Generate SID from sendRequest SendAnswerService service = RetroServiceGenerator.createService(SendAnswerService.class); - Observable<AnsResponse> response = service.sendAnswer(body); + Observable<AnsResponse> response = service.sendAnswer(GlobalApplication.getHeadersMap(), body); serviceDisposable = response.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()).subscribe(this::answerResponseSuccess, this::answerResponseFailure); } diff --git a/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java b/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java index 317bcc5..40a7cb6 100644 --- a/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java +++ b/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java @@ -3,13 +3,17 @@ package org.torproject.snowflake.services; import org.torproject.snowflake.pojo.OfferRequestBody; import org.torproject.snowflake.pojo.SDPOfferResponse;
+import java.util.Map; + import io.reactivex.rxjava3.core.Observable; import retrofit2.http.Body; +import retrofit2.http.HeaderMap; import retrofit2.http.Headers; import retrofit2.http.POST;
public interface GetOfferService { @POST("proxy") - @Headers({"Content-type: application/json", "Host: 10.0.2.2:8080"}) - Observable<SDPOfferResponse> getOffer(@Body OfferRequestBody body); + Observable<SDPOfferResponse> getOffer( + @HeaderMap Map<String, String> headersMap, + @Body OfferRequestBody body); } diff --git a/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java b/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java index 8d2f153..af14726 100644 --- a/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java +++ b/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java @@ -3,13 +3,17 @@ package org.torproject.snowflake.services; import org.torproject.snowflake.pojo.AnsResponse; import org.torproject.snowflake.pojo.AnswerBody;
+import java.util.Map; + import io.reactivex.rxjava3.core.Observable; import retrofit2.http.Body; +import retrofit2.http.HeaderMap; import retrofit2.http.Headers; import retrofit2.http.POST;
public interface SendAnswerService { @POST("answer") - @Headers({"Content-type: application/json", "Host: 10.0.2.2:8080"}) - Observable<AnsResponse> sendAnswer(@Body AnswerBody body); + Observable<AnsResponse> sendAnswer( + @HeaderMap Map<String, String> headersMap, + @Body AnswerBody body); }