From 5eb986d598b1d16205ff46f60f35e62164636395 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Mon, 20 Aug 2018 02:14:09 -0700 Subject: [PATCH] Use UUID instead of new Random().nextInt(...) (#238) Summary: Minor simplification, mostly cleanup. Arguably, better to avoid the birthday paradox e.g. collision with > a few thousand requests. Tested via $ ./gradlew :sample:installDebug Pull Request resolved: https://github.com/facebook/flipper/pull/238 Reviewed By: danielbuechele Differential Revision: D9359941 Pulled By: passy fbshipit-source-id: d2988b58f41d84a52aca060b5c583e055b8b203a --- .../network/SonarOkhttpInterceptor.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/com/facebook/sonar/plugins/network/SonarOkhttpInterceptor.java b/android/src/main/java/com/facebook/sonar/plugins/network/SonarOkhttpInterceptor.java index d05119bcd..0ff5155f5 100644 --- a/android/src/main/java/com/facebook/sonar/plugins/network/SonarOkhttpInterceptor.java +++ b/android/src/main/java/com/facebook/sonar/plugins/network/SonarOkhttpInterceptor.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Set; +import java.util.UUID; import javax.annotation.Nullable; import okhttp3.Headers; import okhttp3.Interceptor; @@ -38,11 +39,11 @@ public class SonarOkhttpInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); - int randInt = randInt(1, Integer.MAX_VALUE); - plugin.reportRequest(convertRequest(request, randInt)); + String identifier = UUID.randomUUID().toString(); + plugin.reportRequest(convertRequest(request, identifier)); Response response = chain.proceed(request); ResponseBody body = response.body(); - ResponseInfo responseInfo = convertResponse(response, body, randInt); + ResponseInfo responseInfo = convertResponse(response, body, identifier); plugin.reportResponse(responseInfo); // Creating new response as can't used response.body() more than once if (responseInfo.body != null) { @@ -67,10 +68,10 @@ public class SonarOkhttpInterceptor implements Interceptor { } } - private RequestInfo convertRequest(Request request, int identifier) { + private RequestInfo convertRequest(Request request, String identifier) { List headers = convertHeader(request.headers()); RequestInfo info = new RequestInfo(); - info.requestId = String.valueOf(identifier); + info.requestId = identifier; info.timeStamp = System.currentTimeMillis(); info.headers = headers; info.method = request.method(); @@ -82,11 +83,11 @@ public class SonarOkhttpInterceptor implements Interceptor { return info; } - private ResponseInfo convertResponse(Response response, ResponseBody body, int identifier) { + private ResponseInfo convertResponse(Response response, ResponseBody body, String identifier) { List headers = convertHeader(response.headers()); ResponseInfo info = new ResponseInfo(); - info.requestId = String.valueOf(identifier); + info.requestId = identifier; info.timeStamp = response.receivedResponseAtMillis(); info.statusCode = response.code(); info.headers = headers; @@ -107,10 +108,4 @@ public class SonarOkhttpInterceptor implements Interceptor { } return list; } - - private int randInt(int min, int max) { - Random rand = new Random(); - int randomNum = rand.nextInt((max - min) + 1) + min; - return randomNum; - } }