Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying dependency tree for oci-objectstorage-fixture #74

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.opensearch.fixtures.oci;

import com.google.common.base.Preconditions;
import com.oracle.bmc.model.BmcException;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -18,12 +17,8 @@ public class LocalBucket {
Map<String, OSObject> prefixToObjectMap = new ConcurrentHashMap<>();
String name;

public void putObject(String objectName, InputStream inputStream, int contentLength)
throws IOException {
Preconditions.checkArgument(
contentLength < Integer.MAX_VALUE,
"Local object storage is currently not designed to handle very large objects");
final byte[] objectData = IOUtils.readFully(inputStream, contentLength);
public void putObject(String objectName, InputStream inputStream) throws IOException {
final byte[] objectData = IOUtils.toByteArray(inputStream);
final OSObject osObject = new OSObject(objectName, objectData);
prefixToObjectMap.put(objectName, osObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.oracle.bmc.model.Range;
import com.oracle.bmc.objectstorage.model.Bucket;
import com.oracle.bmc.objectstorage.model.CreateBucketDetails;
Expand All @@ -17,16 +18,12 @@
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.*;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import lombok.extern.log4j.Log4j2;
import org.opensearch.common.io.Streams;
import org.opensearch.common.regex.Regex;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.RestUtils;

@Log4j2
public class OciHttpHandler implements HttpHandler {
Expand Down Expand Up @@ -60,17 +57,16 @@ public void handle(HttpExchange exchange) {
if (path.equals("/n/testResource") && exchange.getRequestMethod().equals("GET")) {
// Test resource
final byte[] response = getIt().getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
exchange.getResponseBody().write(response);
exchange.close();
} else if (Regex.simpleMatch("/n/*/b", path)
&& exchange.getRequestMethod().equals("POST")) {
} else if (path.matches("/n/.*/b") && exchange.getRequestMethod().equals("POST")) {
// create bucket
final String namespace = pathParams[2];
final CreateBucketDetails createBucketDetails =
MAPPER.readValue(exchange.getRequestBody(), CreateBucketDetails.class);
createBucket(namespace, createBucketDetails, exchange);
} else if (Regex.simpleMatch("/n/*/b/*/o/*", path)
} else if (path.matches("/n/.*/b/.*/o/.*")
&& exchange.getRequestMethod().equals("PUT")) {
// PUT object
final String namespace = pathParams[2];
Expand All @@ -80,7 +76,7 @@ public void handle(HttpExchange exchange) {
final int contentLength =
Integer.parseInt(exchange.getRequestHeaders().getFirst("Content-Length"));
putObject(namespace, bucket, objectName, contentLength, exchange);
} else if (Regex.simpleMatch("/n/*/b/*/o/*", path)
} else if (path.matches("/n/.*/b/.*/o/.*")
&& exchange.getRequestMethod().equals("GET")) {
// GET object
final String namespace = pathParams[2];
Expand All @@ -96,39 +92,43 @@ public void handle(HttpExchange exchange) {
}

getObject(namespace, bucket, objectName, range, exchange);
} else if (Regex.simpleMatch("/n/*/b/*/o/*", path)
} else if (path.matches("/n/.*/b/.*/o/.*")
&& exchange.getRequestMethod().equals("HEAD")) {
// HEAD object
final String namespace = pathParams[2];
final String bucket = pathParams[4];
final String objectName =
String.join("/", Arrays.copyOfRange(pathParams, 6, pathParams.length));
headObject(namespace, bucket, objectName, exchange);
} else if (Regex.simpleMatch("/n/*/b/*/o/*", path)
} else if (path.matches("/n/.*/b/.*/o/.*")
&& exchange.getRequestMethod().equals("DELETE")) {
// DELETE object
final String namespace = pathParams[2];
final String bucket = pathParams[4];
final String objectName =
String.join("/", Arrays.copyOfRange(pathParams, 6, pathParams.length));
deleteObject(namespace, bucket, objectName, exchange);
} else if (Regex.simpleMatch("/n/*/b/*/o", path)
&& exchange.getRequestMethod().equals("GET")) {
} else if (path.matches("/n/.*/b/.*/o") && exchange.getRequestMethod().equals("GET")) {
// List objects
final String namespace = pathParams[2];
final String bucket = pathParams[4];
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
final Map<String, String> params =
Splitter.on('&')
.trimResults()
.withKeyValueSeparator('=')
.split(exchange.getRequestURI().getQuery());
listObject(namespace, bucket, params.get("prefix"), exchange);
} else if (Regex.simpleMatch("/n/*/b/*", path)
&& exchange.getRequestMethod().equals("GET")) {
} else if (path.matches("/n/.*/b/.*") && exchange.getRequestMethod().equals("GET")) {
// GET bucket
final String namespace = pathParams[2];
final String bucket = pathParams[4];
getBucket(namespace, bucket, exchange);
} else {
sendError(
exchange, RestStatus.METHOD_NOT_ALLOWED, "400", "Method not allowed/found");
exchange,
HttpURLConnection.HTTP_BAD_METHOD,
"400",
"Method not allowed/found");
}
} catch (Exception e) {
log.error("Exception found when processing request", e);
Expand All @@ -137,7 +137,7 @@ public void handle(HttpExchange exchange) {

public static void sendError(
final HttpExchange exchange,
final RestStatus status,
final int status,
final String errorCode,
final String message)
throws IOException {
Expand All @@ -150,7 +150,7 @@ public static void sendError(
}

if (errorCode == null || "HEAD".equals(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(status.getStatus(), -1L);
exchange.sendResponseHeaders(status, -1L);
exchange.close();
} else {
final byte[] response =
Expand All @@ -166,7 +166,7 @@ public static void sendError(
+ "</RequestId>"
+ "</Error>")
.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status.getStatus(), response.length);
exchange.sendResponseHeaders(status, response.length);
exchange.getResponseBody().write(response);
exchange.close();
}
Expand Down Expand Up @@ -194,7 +194,7 @@ private void createBucket(
final String bucketStr = MAPPER.writeValueAsString(bucket);
final byte[] response = bucketStr.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
exchange.getResponseBody().write(response);
exchange.close();
}
Expand All @@ -208,10 +208,10 @@ private void getBucket(String namespace, String bucketName, HttpExchange exchang
final String bucketStr = MAPPER.writeValueAsString(bucket);
final byte[] response = bucketStr.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
exchange.getResponseBody().write(response);
} else {
sendError(exchange, RestStatus.NOT_FOUND, "404", "Bucket not found");
sendError(exchange, HttpURLConnection.HTTP_NOT_FOUND, "404", "Bucket not found");
}

exchange.close();
Expand All @@ -224,21 +224,19 @@ private void putObject(
int contentLength,
HttpExchange exchange)
throws IOException {
final BytesReference requestBody = Streams.readFully(exchange.getRequestBody());
// final BytesReference requestBody = Streams.readFully(exchange.getRequestBody());

Preconditions.checkArgument(buckets.containsKey(bucketName), "Bucket doesn't exist");
final LocalBucket bucket = buckets.get(bucketName);
bucket.putObject(
objectName,
new ByteArrayInputStream(requestBody.toBytesRef().bytes),
contentLength);
bucket.putObject(objectName, exchange.getRequestBody());

log.info(
"Put object with namespaceName:{}, bucketName: {}, objectName: {}",
namespaceName,
bucketName,
objectName);
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);
exchange.close();
}

Expand All @@ -259,7 +257,7 @@ private void getObject(
final OSObject object = bucket.getObject(objectName);
if (object != null) {
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), 0);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
if (range != null) {
exchange.getResponseBody()
.write(
Expand All @@ -273,7 +271,7 @@ private void getObject(

exchange.close();
} else {
sendError(exchange, RestStatus.NOT_FOUND, "404", "Object not found");
sendError(exchange, HttpURLConnection.HTTP_NOT_FOUND, "404", "Object not found");
}
}

Expand All @@ -297,11 +295,11 @@ private void headObject(
final byte[] response = str.getBytes(StandardCharsets.UTF_8);

exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
exchange.getResponseBody().write(response);
exchange.close();
} else {
sendError(exchange, RestStatus.NOT_FOUND, "404", "Object not found");
sendError(exchange, HttpURLConnection.HTTP_NOT_FOUND, "404", "Object not found");
}
}

Expand Down Expand Up @@ -332,7 +330,7 @@ private void listObject(
final byte[] response = str.getBytes(StandardCharsets.UTF_8);

exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
exchange.getResponseBody().write(response);
exchange.close();
}
Expand All @@ -352,7 +350,7 @@ private void deleteObject(

bucket.deleteObject(objectName);
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);
exchange.close();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.opensearch.fixtures.oci;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import com.oracle.bmc.Region;
import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.SimpleAuthenticationDetailsProvider;
Expand All @@ -28,6 +24,11 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import static org.junit.Assert.assertEquals;

@Log4j2
Expand Down Expand Up @@ -56,18 +57,13 @@ public class FixtureTests {

private NonJerseyServer nonJerseyServer;

private WebTarget target;

@Before
public void setup() throws Exception {
// start the server
nonJerseyServer = new NonJerseyServer();
nonJerseyServer.start();

// create the client
Client c = ClientBuilder.newClient();

target = c.target(NonJerseyServer.DEFAULT_BASE_URI);
objectStorage =
ObjectStorageClient.builder()
// This will run after, and in addition to, the default
Expand Down Expand Up @@ -110,9 +106,16 @@ public void tearDown() throws Exception {

/** Test to see that the message "Got it!" is sent in the response. */
@Test
public void testResource() {
String responseMsg = target.path("/n/testResource").request().get(String.class);
assertEquals("Got it!", responseMsg);
public void testResource() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(NonJerseyServer.DEFAULT_BASE_URI+"n/testResource"))
.GET()
.build();

// Send the request and get the response
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals("Got it!", response.body());
}

@Test
Expand Down
Loading