Skip to content

Commit e8aa3db

Browse files
committed
Switched to reading byte stream using scanner.
Contains minor test adjustments.
1 parent 0b168fb commit e8aa3db

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

gcloud-java-dns/src/main/java/com/google/cloud/dns/testing/LocalDnsHelper.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@
4747
import org.apache.commons.fileupload.MultipartStream;
4848
import org.joda.time.format.ISODateTimeFormat;
4949

50-
import java.io.BufferedReader;
51-
import java.io.ByteArrayInputStream;
5250
import java.io.ByteArrayOutputStream;
5351
import java.io.IOException;
5452
import java.io.InputStream;
55-
import java.io.InputStreamReader;
5653
import java.io.OutputStream;
5754
import java.math.BigInteger;
5855
import java.net.InetSocketAddress;
@@ -69,6 +66,7 @@
6966
import java.util.NavigableMap;
7067
import java.util.NavigableSet;
7168
import java.util.Random;
69+
import java.util.Scanner;
7270
import java.util.Set;
7371
import java.util.SortedMap;
7472
import java.util.TreeMap;
@@ -119,15 +117,9 @@ public class LocalDnsHelper {
119117
private static final ScheduledExecutorService EXECUTORS =
120118
Executors.newScheduledThreadPool(2, Executors.defaultThreadFactory());
121119
private static final String PROJECT_ID = "dummyprojectid";
122-
private static final String responseBoundary = "____THIS_IS_HELPERS_BOUNDARY____";
123-
private static final String responseSeparator = new StringBuilder("--")
124-
.append(responseBoundary)
125-
.append("\r\n")
126-
.toString();
127-
private static final String responseEnd = new StringBuilder("--")
128-
.append(responseBoundary)
129-
.append("--\r\n\r\n")
130-
.toString();
120+
private static final String RESPONSE_BOUNDARY = "____THIS_IS_HELPERS_BOUNDARY____";
121+
private static final String RESPONSE_SEPARATOR = "--" + RESPONSE_BOUNDARY + "\r\n";
122+
private static final String RESPONSE_END = "--" + RESPONSE_BOUNDARY + "--\r\n\r\n";
131123

132124
static {
133125
try {
@@ -333,7 +325,6 @@ private Response pickHandler(HttpExchange exchange, CallRegex regex) {
333325
try {
334326
return handleBatch(exchange);
335327
} catch (IOException ex) {
336-
ex.printStackTrace();
337328
return Error.BAD_REQUEST.response(ex.getMessage());
338329
}
339330
default:
@@ -377,19 +368,22 @@ private Response handleBatch(final HttpExchange exchange) throws IOException {
377368
OutputStream socketOutput = socket.getOutputStream();
378369
ByteArrayOutputStream section = new ByteArrayOutputStream();
379370
multipartStream.readBodyData(section);
380-
BufferedReader reader = new BufferedReader(
381-
new InputStreamReader(new ByteArrayInputStream(section.toByteArray())));
382371
String line;
383372
String contentId = null;
384-
while (!(line = reader.readLine()).isEmpty()) {
385-
if (line.toLowerCase().startsWith("content-id")) {
373+
Scanner scanner = new Scanner(new String(section.toByteArray()));
374+
while (scanner.hasNextLine()) {
375+
line = scanner.nextLine();
376+
if(line.isEmpty()) {
377+
break;
378+
} else if (line.toLowerCase().startsWith("content-id")) {
386379
contentId = line.split(":")[1].trim();
387380
}
388381
}
389-
String requestLine = reader.readLine();
382+
String requestLine = scanner.nextLine();
390383
socketOutput.write((requestLine + " \r\n").getBytes());
391384
socketOutput.write("Connection: close \r\n".getBytes());
392-
while ((line = reader.readLine()) != null) {
385+
while(scanner.hasNextLine()) {
386+
line = scanner.nextLine();
393387
socketOutput.write(line.getBytes());
394388
if (!line.isEmpty()) {
395389
socketOutput.write(" \r\n".getBytes());
@@ -400,7 +394,7 @@ private Response handleBatch(final HttpExchange exchange) throws IOException {
400394
socketOutput.flush();
401395
InputStream in = socket.getInputStream();
402396
int length;
403-
out.write(responseSeparator.getBytes());
397+
out.write(RESPONSE_SEPARATOR.getBytes());
404398
out.write("Content-Type: application/http \r\n".getBytes());
405399
out.write(("Content-ID: " + contentId + " \r\n\r\n").getBytes());
406400
try {
@@ -411,8 +405,10 @@ private Response handleBatch(final HttpExchange exchange) throws IOException {
411405
// this handles connection reset error
412406
}
413407
}
414-
out.write(responseEnd.getBytes());
408+
out.write(RESPONSE_END.getBytes());
415409
writeBatchResponse(exchange, out);
410+
} else {
411+
return Error.BAD_REQUEST.response("Content-type header was not provided for batch.");
416412
}
417413
return null;
418414
}
@@ -520,14 +516,14 @@ private static void writeResponse(HttpExchange exchange, Response response) {
520516
}
521517
}
522518

523-
private static void writeBatchResponse(HttpExchange exchange, ByteArrayOutputStream out) {
519+
private static void writeBatchResponse(HttpExchange exchange, ByteArrayOutputStream output) {
524520
exchange.getResponseHeaders().set(
525-
"Content-type", "multipart/mixed; boundary=" + responseBoundary);
521+
"Content-type", "multipart/mixed; boundary=" + RESPONSE_BOUNDARY);
526522
try {
527523
exchange.getResponseHeaders().add("Connection", "close");
528-
exchange.sendResponseHeaders(200, out.toByteArray().length);
524+
exchange.sendResponseHeaders(200, output.toByteArray().length);
529525
OutputStream responseBody = exchange.getResponseBody();
530-
out.writeTo(responseBody);
526+
output.writeTo(responseBody);
531527
responseBody.close();
532528
} catch (IOException e) {
533529
log.log(Level.WARNING, "IOException encountered when sending response.", e);

gcloud-java-dns/src/test/java/com/google/cloud/dns/testing/LocalDnsHelperTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,7 @@ public void onFailure(GoogleJsonError googleJsonError) {
19401940
}, EMPTY_RPC_OPTIONS);
19411941
batch.submit();
19421942
// some zones exists
1943+
19431944
final ManagedZone first = RPC.create(ZONE1, EMPTY_RPC_OPTIONS);
19441945
final ManagedZone second = RPC.create(ZONE2, EMPTY_RPC_OPTIONS);
19451946
batch = RPC.createBatch();
@@ -1948,8 +1949,8 @@ public void onFailure(GoogleJsonError googleJsonError) {
19481949
public void onSuccess(ManagedZonesListResponse zones) {
19491950
List<ManagedZone> results = zones.getManagedZones();
19501951
assertEquals(2, results.size());
1951-
assertEquals(first, results.get(1));
1952-
assertEquals(second, results.get(0));
1952+
assertTrue(results.contains(first));
1953+
assertTrue(results.contains(second));
19531954
}
19541955

19551956
@Override
@@ -2890,7 +2891,7 @@ public void onFailure(GoogleJsonError googleJsonError) {
28902891
public void onSuccess(ChangesListResponse response) {
28912892
assertEquals(1, response.getChanges().size());
28922893
assertEquals(RPC.getChangeRequest(created.getName(), "0", EMPTY_RPC_OPTIONS),
2893-
response.getChanges().get(0).getId());
2894+
response.getChanges().get(0));
28942895
}
28952896

28962897
@Override
@@ -2921,5 +2922,6 @@ public void onFailure(GoogleJsonError googleJsonError) {
29212922
fail();
29222923
}
29232924
}, EMPTY_RPC_OPTIONS);
2925+
batch.submit();
29242926
}
29252927
}

0 commit comments

Comments
 (0)