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

Add AbstractVssApi, get/put/list api handlers #10

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ version '1.0'
dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"

implementation('org.glassfish.jersey.containers:jersey-container-servlet:3.1.0')

//jOOQ & Postgres impl deps
implementation "org.jooq:jooq:$jooqVersion"
implementation "org.jooq:jooq-meta:$jooqVersion"
Expand All @@ -42,6 +44,9 @@ dependencies {

implementation "com.google.inject:guice:$guiceVersion"

compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'

testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "org.hamcrest:hamcrest-library:2.2"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/vss/VSSApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.vss;

import jakarta.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/")
public class VSSApplication extends ResourceConfig {
public VSSApplication() {
}
}
48 changes: 48 additions & 0 deletions app/src/main/java/org/vss/api/AbstractVssApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.vss.api;

import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.InvalidProtocolBufferException;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.vss.ErrorCode;
import org.vss.ErrorResponse;
import org.vss.KVStore;
import org.vss.exception.ConflictException;

public abstract class AbstractVssApi {
final KVStore kvStore;

@Inject
public AbstractVssApi(KVStore kvStore) {
this.kvStore = kvStore;
}

Response toResponse(GeneratedMessageV3 protoResponse) {

return Response
.status(Response.Status.OK)
.entity(protoResponse.toByteArray())
.build();
}

Response toErrorResponse(Exception e) {
ErrorCode errorCode;
if (e instanceof ConflictException) {
errorCode = ErrorCode.CONFLICT_EXCEPTION;
} else if (e instanceof IllegalArgumentException
|| e instanceof InvalidProtocolBufferException) {
errorCode = ErrorCode.INVALID_REQUEST_EXCEPTION;
} else {
errorCode = ErrorCode.INTERNAL_SERVER_EXCEPTION;
}

ErrorResponse errorResponse = ErrorResponse.newBuilder()
.setErrorCode(errorCode)
.setMessage(e.getMessage())
.build();

return Response.status(errorCode.getNumber())
.entity(errorResponse.toByteArray())
.build();
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/org/vss/api/GetObjectApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.vss.api;

import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.vss.GetObjectRequest;
import org.vss.GetObjectResponse;
import org.vss.KVStore;

@Path(VssApiEndpoint.GET_OBJECT)
@Slf4j
public class GetObjectApi extends AbstractVssApi {

@Inject
public GetObjectApi(KVStore kvstore) {
super(kvstore);
}

@POST
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response execute(byte[] payload) {
try {
GetObjectRequest request = GetObjectRequest.parseFrom(payload);
GetObjectResponse response = kvStore.get(request);
return toResponse(response);
} catch (Exception e) {
log.error("Exception in GetObjectApi: ", e);
return toErrorResponse(e);
}
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/org/vss/api/ListKeyVersionsApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.vss.api;

import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.vss.KVStore;
import org.vss.ListKeyVersionsRequest;
import org.vss.ListKeyVersionsResponse;

@Path(VssApiEndpoint.LIST_KEY_VERSIONS)
@Slf4j
public class ListKeyVersionsApi extends AbstractVssApi {

@Inject
public ListKeyVersionsApi(KVStore kvStore) {
super(kvStore);
}

@POST
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response execute(byte[] payload) {
try {
ListKeyVersionsRequest request = ListKeyVersionsRequest.parseFrom(payload);
ListKeyVersionsResponse response = kvStore.listKeyVersions(request);
return toResponse(response);
} catch (Exception e) {
log.error("Exception in ListKeyVersionsApi: ", e);
return toErrorResponse(e);
}
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/org/vss/api/PutObjectsApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.vss.api;

import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.vss.KVStore;
import org.vss.PutObjectRequest;
import org.vss.PutObjectResponse;

@Path(VssApiEndpoint.PUT_OBJECTS)
@Slf4j
public class PutObjectsApi extends AbstractVssApi {

@Inject
public PutObjectsApi(KVStore kvStore) {
super(kvStore);
}

@POST
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response execute(byte[] payload) {
try {
PutObjectRequest putObjectRequest = PutObjectRequest.parseFrom(payload);
PutObjectResponse response = kvStore.put(putObjectRequest);
return toResponse(response);
} catch (Exception e) {
log.error("Exception in PutObjectsApi: ", e);
return toErrorResponse(e);
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/org/vss/api/VssApiEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.vss.api;

public class VssApiEndpoint {
public static final String GET_OBJECT = "/getObject";
public static final String PUT_OBJECTS = "/putObjects";
public static final String LIST_KEY_VERSIONS = "/listKeyVersions";
}