From e8aa3db0ab7f2dee7e80996d606933a500b299c9 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 15 Apr 2016 07:32:07 -0700 Subject: [PATCH] Switched to reading byte stream using scanner. Contains minor test adjustments. --- .../cloud/dns/testing/LocalDnsHelper.java | 46 +++++++++---------- .../cloud/dns/testing/LocalDnsHelperTest.java | 8 ++-- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java index 8c9a6b04eb78..14632869f6ba 100644 --- a/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java @@ -47,12 +47,9 @@ import org.apache.commons.fileupload.MultipartStream; import org.joda.time.format.ISODateTimeFormat; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.math.BigInteger; import java.net.InetSocketAddress; @@ -69,6 +66,7 @@ import java.util.NavigableMap; import java.util.NavigableSet; import java.util.Random; +import java.util.Scanner; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; @@ -119,15 +117,9 @@ public class LocalDnsHelper { private static final ScheduledExecutorService EXECUTORS = Executors.newScheduledThreadPool(2, Executors.defaultThreadFactory()); private static final String PROJECT_ID = "dummyprojectid"; - private static final String responseBoundary = "____THIS_IS_HELPERS_BOUNDARY____"; - private static final String responseSeparator = new StringBuilder("--") - .append(responseBoundary) - .append("\r\n") - .toString(); - private static final String responseEnd = new StringBuilder("--") - .append(responseBoundary) - .append("--\r\n\r\n") - .toString(); + private static final String RESPONSE_BOUNDARY = "____THIS_IS_HELPERS_BOUNDARY____"; + private static final String RESPONSE_SEPARATOR = "--" + RESPONSE_BOUNDARY + "\r\n"; + private static final String RESPONSE_END = "--" + RESPONSE_BOUNDARY + "--\r\n\r\n"; static { try { @@ -333,7 +325,6 @@ private Response pickHandler(HttpExchange exchange, CallRegex regex) { try { return handleBatch(exchange); } catch (IOException ex) { - ex.printStackTrace(); return Error.BAD_REQUEST.response(ex.getMessage()); } default: @@ -377,19 +368,22 @@ private Response handleBatch(final HttpExchange exchange) throws IOException { OutputStream socketOutput = socket.getOutputStream(); ByteArrayOutputStream section = new ByteArrayOutputStream(); multipartStream.readBodyData(section); - BufferedReader reader = new BufferedReader( - new InputStreamReader(new ByteArrayInputStream(section.toByteArray()))); String line; String contentId = null; - while (!(line = reader.readLine()).isEmpty()) { - if (line.toLowerCase().startsWith("content-id")) { + Scanner scanner = new Scanner(new String(section.toByteArray())); + while (scanner.hasNextLine()) { + line = scanner.nextLine(); + if(line.isEmpty()) { + break; + } else if (line.toLowerCase().startsWith("content-id")) { contentId = line.split(":")[1].trim(); } } - String requestLine = reader.readLine(); + String requestLine = scanner.nextLine(); socketOutput.write((requestLine + " \r\n").getBytes()); socketOutput.write("Connection: close \r\n".getBytes()); - while ((line = reader.readLine()) != null) { + while(scanner.hasNextLine()) { + line = scanner.nextLine(); socketOutput.write(line.getBytes()); if (!line.isEmpty()) { socketOutput.write(" \r\n".getBytes()); @@ -400,7 +394,7 @@ private Response handleBatch(final HttpExchange exchange) throws IOException { socketOutput.flush(); InputStream in = socket.getInputStream(); int length; - out.write(responseSeparator.getBytes()); + out.write(RESPONSE_SEPARATOR.getBytes()); out.write("Content-Type: application/http \r\n".getBytes()); out.write(("Content-ID: " + contentId + " \r\n\r\n").getBytes()); try { @@ -411,8 +405,10 @@ private Response handleBatch(final HttpExchange exchange) throws IOException { // this handles connection reset error } } - out.write(responseEnd.getBytes()); + out.write(RESPONSE_END.getBytes()); writeBatchResponse(exchange, out); + } else { + return Error.BAD_REQUEST.response("Content-type header was not provided for batch."); } return null; } @@ -520,14 +516,14 @@ private static void writeResponse(HttpExchange exchange, Response response) { } } - private static void writeBatchResponse(HttpExchange exchange, ByteArrayOutputStream out) { + private static void writeBatchResponse(HttpExchange exchange, ByteArrayOutputStream output) { exchange.getResponseHeaders().set( - "Content-type", "multipart/mixed; boundary=" + responseBoundary); + "Content-type", "multipart/mixed; boundary=" + RESPONSE_BOUNDARY); try { exchange.getResponseHeaders().add("Connection", "close"); - exchange.sendResponseHeaders(200, out.toByteArray().length); + exchange.sendResponseHeaders(200, output.toByteArray().length); OutputStream responseBody = exchange.getResponseBody(); - out.writeTo(responseBody); + output.writeTo(responseBody); responseBody.close(); } catch (IOException e) { log.log(Level.WARNING, "IOException encountered when sending response.", e); diff --git a/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java b/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java index 93c8acd4c554..142a24f372ad 100644 --- a/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java +++ b/gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java @@ -1940,6 +1940,7 @@ public void onFailure(GoogleJsonError googleJsonError) { }, EMPTY_RPC_OPTIONS); batch.submit(); // some zones exists + final ManagedZone first = RPC.create(ZONE1, EMPTY_RPC_OPTIONS); final ManagedZone second = RPC.create(ZONE2, EMPTY_RPC_OPTIONS); batch = RPC.createBatch(); @@ -1948,8 +1949,8 @@ public void onFailure(GoogleJsonError googleJsonError) { public void onSuccess(ManagedZonesListResponse zones) { List results = zones.getManagedZones(); assertEquals(2, results.size()); - assertEquals(first, results.get(1)); - assertEquals(second, results.get(0)); + assertTrue(results.contains(first)); + assertTrue(results.contains(second)); } @Override @@ -2890,7 +2891,7 @@ public void onFailure(GoogleJsonError googleJsonError) { public void onSuccess(ChangesListResponse response) { assertEquals(1, response.getChanges().size()); assertEquals(RPC.getChangeRequest(created.getName(), "0", EMPTY_RPC_OPTIONS), - response.getChanges().get(0).getId()); + response.getChanges().get(0)); } @Override @@ -2921,5 +2922,6 @@ public void onFailure(GoogleJsonError googleJsonError) { fail(); } }, EMPTY_RPC_OPTIONS); + batch.submit(); } }