Skip to content

Commit

Permalink
Switched to reading byte stream using scanner.
Browse files Browse the repository at this point in the history
Contains minor test adjustments.
  • Loading branch information
mderka committed Apr 15, 2016
1 parent 0b168fb commit e8aa3db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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());
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -1948,8 +1949,8 @@ public void onFailure(GoogleJsonError googleJsonError) {
public void onSuccess(ManagedZonesListResponse zones) {
List<ManagedZone> 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2921,5 +2922,6 @@ public void onFailure(GoogleJsonError googleJsonError) {
fail();
}
}, EMPTY_RPC_OPTIONS);
batch.submit();
}
}

0 comments on commit e8aa3db

Please sign in to comment.