|
@@ -25,6 +25,7 @@ import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.SuppressForbidden;
|
|
|
import org.elasticsearch.common.UUIDs;
|
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
|
+import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
|
import org.elasticsearch.common.io.Streams;
|
|
|
import org.elasticsearch.common.network.InetAddresses;
|
|
@@ -64,7 +65,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|
|
@SuppressForbidden(reason = "Uses a HttpServer to emulate a Google Cloud Storage endpoint")
|
|
|
public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
|
|
|
- private final ConcurrentMap<String, BytesArray> blobs;
|
|
|
+ private final ConcurrentMap<String, BytesReference> blobs;
|
|
|
private final String bucket;
|
|
|
|
|
|
public GoogleCloudStorageHttpHandler(final String bucket) {
|
|
@@ -86,7 +87,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
final Set<String> prefixes = new HashSet<>();
|
|
|
final List<String> listOfBlobs = new ArrayList<>();
|
|
|
|
|
|
- for (final Map.Entry<String, BytesArray> blob : blobs.entrySet()) {
|
|
|
+ for (final Map.Entry<String, BytesReference> blob : blobs.entrySet()) {
|
|
|
final String blobName = blob.getKey();
|
|
|
if (prefix.isEmpty() || blobName.startsWith(prefix)) {
|
|
|
int delimiterPos = (delimiter != null) ? blobName.substring(prefix.length()).indexOf(delimiter) : -1;
|
|
@@ -122,7 +123,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
|
|
|
} else if (Regex.simpleMatch("GET /download/storage/v1/b/" + bucket + "/o/*", request)) {
|
|
|
// Download Object https://cloud.google.com/storage/docs/request-body
|
|
|
- BytesArray blob = blobs.get(exchange.getRequestURI().getPath().replace("/download/storage/v1/b/" + bucket + "/o/", ""));
|
|
|
+ BytesReference blob = blobs.get(exchange.getRequestURI().getPath().replace("/download/storage/v1/b/" + bucket + "/o/", ""));
|
|
|
if (blob != null) {
|
|
|
final String range = exchange.getRequestHeaders().getFirst("Range");
|
|
|
Matcher matcher = Pattern.compile("bytes=([0-9]*)-([0-9]*)").matcher(range);
|
|
@@ -130,7 +131,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
throw new AssertionError("Range bytes header does not match expected format: " + range);
|
|
|
}
|
|
|
|
|
|
- byte[] response = Integer.parseInt(matcher.group(1)) == 0 ? blob.array() : new byte[0];
|
|
|
+ byte[] response = Integer.parseInt(matcher.group(1)) == 0 ? BytesReference.toBytes(blob) : new byte[0];
|
|
|
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
|
|
|
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
|
|
|
exchange.getResponseBody().write(response);
|
|
@@ -141,8 +142,8 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
} else if (Regex.simpleMatch("DELETE /storage/v1/b/" + bucket + "/o/*", request)) {
|
|
|
// Delete Object https://cloud.google.com/storage/docs/json_api/v1/objects/delete
|
|
|
int deletions = 0;
|
|
|
- for (Iterator<Map.Entry<String, BytesArray>> iterator = blobs.entrySet().iterator(); iterator.hasNext(); ) {
|
|
|
- Map.Entry<String, BytesArray> blob = iterator.next();
|
|
|
+ for (Iterator<Map.Entry<String, BytesReference>> iterator = blobs.entrySet().iterator(); iterator.hasNext(); ) {
|
|
|
+ Map.Entry<String, BytesReference> blob = iterator.next();
|
|
|
if (blob.getKey().equals(exchange.getRequestURI().toString())) {
|
|
|
iterator.remove();
|
|
|
deletions++;
|
|
@@ -208,12 +209,11 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
|
|
|
|
|
|
final String blobName = params.get("test_blob_name");
|
|
|
- byte[] blob = blobs.get(blobName).array();
|
|
|
- if (blob == null) {
|
|
|
+ if (blobs.containsKey(blobName) == false) {
|
|
|
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+ byte[] blob = BytesReference.toBytes(blobs.get(blobName));
|
|
|
final String range = exchange.getRequestHeaders().getFirst("Content-Range");
|
|
|
final Integer limit = getContentRangeLimit(range);
|
|
|
final int start = getContentRangeStart(range);
|
|
@@ -249,6 +249,10 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public Map<String, BytesReference> blobs() {
|
|
|
+ return blobs;
|
|
|
+ }
|
|
|
+
|
|
|
private String httpServerUrl(final HttpExchange exchange) {
|
|
|
final InetSocketAddress address = exchange.getLocalAddress();
|
|
|
return "http://" + InetAddresses.toUriString(address.getAddress()) + ":" + address.getPort();
|