Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3004)

Unified Diff: components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/JsonResponseHandler.java

Issue 677843006: Registration of DevTools bridge in GCD. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gcm-signaling
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/JsonResponseHandler.java
diff --git a/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/JsonResponseHandler.java b/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/JsonResponseHandler.java
new file mode 100644
index 0000000000000000000000000000000000000000..1700cc52f40d442e9b0adeaf3d2d475af94254c3
--- /dev/null
+++ b/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/JsonResponseHandler.java
@@ -0,0 +1,70 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.components.devtools_bridge.apiary;
+
+import android.util.JsonReader;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpResponseException;
+import org.apache.http.client.ResponseHandler;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+/**
+ * Base class for a ResponseHandler that reads response with JsonReader. Like BasicResponseHandler
+ * throws HttpResponseException if response code >= 300.
+ *
+ * It catchs JsonReader's runtime exception (IllegalStateException and IllegalArgumentException)
+ * and wraps them into ResponseFormatException.
+ */
+abstract class JsonResponseHandler<T> implements ResponseHandler<T> {
+ public static void checkStatus(HttpResponse response) throws HttpResponseException {
+ StatusLine statusLine = response.getStatusLine();
+ if (response.getStatusLine().getStatusCode() >= 300) {
+ throw new HttpResponseException(
+ statusLine.getStatusCode(), statusLine.getReasonPhrase());
+ }
+ }
+
+ @Override
+ public final T handleResponse(HttpResponse response)
+ throws IOException, ClientProtocolException {
+ checkStatus(response);
+ HttpEntity entity = response.getEntity();
+ if (entity == null) {
+ throw new ClientProtocolException("Missing content");
+ }
+ JsonReader reader = new JsonReader(new InputStreamReader(entity.getContent()));
+ try {
+ T result = readResponse(reader);
+ reader.close();
+
+ if (result == null) {
+ throw new ClientProtocolException("Missing result");
+ }
+
+ return result;
+ } catch (IllegalStateException e) {
+ throw new ResponseFormatException(e);
+ } catch (IllegalArgumentException e) {
+ throw new ResponseFormatException(e);
+ }
+ }
+
+ public abstract T readResponse(JsonReader reader)
+ throws IOException, ResponseFormatException;
+
+ public static class ResponseFormatException extends ClientProtocolException {
+ public ResponseFormatException(RuntimeException readerException) {
+ super(readerException);
+ }
+
+ public ResponseFormatException() {}
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698