diff options
author | Kiwon Park <kiwonp@google.com> | 2022-08-24 23:20:27 +0000 |
---|---|---|
committer | Kiwon Park <kiwonp@google.com> | 2022-10-04 00:10:11 +0000 |
commit | 00d7763e6db1568259872680725025e12234d816 (patch) | |
tree | 2949634f84828094397442aa8cd97b305070dd49 /java/com/android/libraries/entitlement/http | |
parent | 5d4fc15dc48a6361155178c6cb56be4c0eace72c (diff) | |
download | service_entitlement-00d7763e6db1568259872680725025e12234d816.tar.gz |
Add ability to save the HTTP request/responses.
Bug: 223430297
Test: HttpClientTest
Change-Id: I395c26dff80e44a1531b70ca8310de1298b95613
Merged-In: I395c26dff80e44a1531b70ca8310de1298b95613
Diffstat (limited to 'java/com/android/libraries/entitlement/http')
-rw-r--r-- | java/com/android/libraries/entitlement/http/HttpClient.java | 28 | ||||
-rw-r--r-- | java/com/android/libraries/entitlement/http/HttpResponse.java | 18 |
2 files changed, 28 insertions, 18 deletions
diff --git a/java/com/android/libraries/entitlement/http/HttpClient.java b/java/com/android/libraries/entitlement/http/HttpClient.java index 9ccb5ee..39275e8 100644 --- a/java/com/android/libraries/entitlement/http/HttpClient.java +++ b/java/com/android/libraries/entitlement/http/HttpClient.java @@ -47,6 +47,7 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -55,9 +56,19 @@ public class HttpClient { private static final String TAG = "ServiceEntitlement"; private HttpURLConnection mConnection; + private boolean mSaveHistory; + private ArrayList<String> mHistory; + + public HttpClient(boolean saveHistory) { + mSaveHistory = saveHistory; + mHistory = new ArrayList<>(); + } @WorkerThread public HttpResponse request(HttpRequest request) throws ServiceEntitlementException { + if (mSaveHistory) { + mHistory.add(request.toString()); + } logPii("HttpClient.request url: " + request.url()); createConnection(request); logPii("HttpClient.request headers (partial): " + mConnection.getRequestProperties()); @@ -74,6 +85,9 @@ public class HttpClient { mConnection.connect(); // This is to trigger SocketTimeoutException early HttpResponse response = getHttpResponse(mConnection); Log.d(TAG, "HttpClient.response : " + response); + if (mSaveHistory) { + mHistory.add(response.toString()); + } return response; } catch (IOException ioe) { throw new ServiceEntitlementException( @@ -85,6 +99,20 @@ public class HttpClient { } } + /** + * Retrieves the history of past HTTP request and responses. + */ + public List<String> getHistory() { + return mHistory; + } + + /** + * Clears the history of past HTTP request and responses. + */ + public void clearHistory() { + mHistory.clear(); + } + private void createConnection(HttpRequest request) throws ServiceEntitlementException { try { URL url = new URL(request.url()); diff --git a/java/com/android/libraries/entitlement/http/HttpResponse.java b/java/com/android/libraries/entitlement/http/HttpResponse.java index f495578..f76fdd6 100644 --- a/java/com/android/libraries/entitlement/http/HttpResponse.java +++ b/java/com/android/libraries/entitlement/http/HttpResponse.java @@ -73,22 +73,4 @@ public abstract class HttpResponse { .setResponseMessage("") .setCookies(ImmutableList.of()); } - - @Override - public final String toString() { - return new StringBuilder("HttpResponse{") - .append("contentType=") - .append(contentType()) - .append(" body=(") - .append(body().length()) - .append(" characters)") - .append(" responseCode=") - .append(responseCode()) - .append(" responseMessage=") - .append(responseMessage()) - .append(" cookies=[") - .append(cookies().size()) - .append(" cookies]}") - .toString(); - } } |