Skip to content

Commit

Permalink
Co-authored-by: William Welling <8352733+wwelling@users.noreply.githu…
Browse files Browse the repository at this point in the history
…b.com>

Co-authored-by: Kevin Day <kday@library.tamu.edu>
Co-authored-by: Dragan Ivanovic <chenejac@uns.ac.rs>
Co-authored-by: chenejac <chenejac@gmail.com>

correct context path vs servlet path

Refactor Integration Tests to use the full word Test.

This allows for the tests to be properly picked up and executed.

Merge pull request vivo-project#281 from kaladay/dynapi-rpc_endpoint_it_to_itest

[3678] Refactor Integration Tests to use the full word Test.
Merge branch 'sprint-dynapi-2022-feb-staging' of github.com:vivo-project/Vitro into servlet-path

Merge pull request vivo-project#280 from wwelling/servlet-path

Correct context path vs servlet path
Update of the ontology. Introducing structured parameter types (object and array)

rest endpoint corrections and integration tests

Updating binding of the ontology to Java classes and n3 test examples.To be in accordance with changes in the ontology.

Adding files for primitive types and APIInformation java bean.

Adding new line at the end of file and retrieving history of deleted files by using git mv

Adding new line at the end of file

Merge pull request vivo-project#283 from chenejac/3653_definition_of_the_ontology

3653 definition of the ontology
Merge branch 'sprint-dynapi-2022-feb-staging' of github.com:vivo-project/Vitro into 3667-rest-it
  • Loading branch information
litvinovg committed Mar 7, 2024
1 parent f386a3f commit 092ac04
Show file tree
Hide file tree
Showing 68 changed files with 1,946 additions and 1,594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Initializer implements ServletContextListener {
public Initializer() {}

private void initializeResourcePool(ServletContext ctx) {
ResourcePool resourcePool = ResourcePool.getInstance();
resourcePool.init(ctx);
ResourceAPIPool resourceAPIPool = ResourceAPIPool.getInstance();
resourceAPIPool.init(ctx);
}

private void initializeActionPool(ServletContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.cornell.mannlib.vitro.webapp.dynapi;

import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_BASE_PATH;
import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_SERVLET_PATH;
import static java.lang.String.format;

import java.io.IOException;
Expand All @@ -15,19 +15,20 @@

import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Action;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.DefaultResourceAPI;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.HTTPMethod;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.OperationResult;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.RPC;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Resource;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceKey;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceAPI;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceAPIKey;
import edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath;

@WebServlet(name = "RESTEndpoint", urlPatterns = { REST_BASE_PATH + "/*" })
@WebServlet(name = "RESTEndpoint", urlPatterns = { REST_SERVLET_PATH + "/*" })
public class RESTEndpoint extends VitroHttpServlet {

private static final Log log = LogFactory.getLog(RESTEndpoint.class);

private ResourcePool resourcePool = ResourcePool.getInstance();
private ResourceAPIPool resourceAPIPool = ResourceAPIPool.getInstance();
private ActionPool actionPool = ActionPool.getInstance();

@Override
Expand Down Expand Up @@ -64,66 +65,81 @@ public void doPatch(HttpServletRequest request, HttpServletResponse response) {
}

private void process(HttpServletRequest request, HttpServletResponse response) {
String method = request.getMethod();
RequestPath requestPath = RequestPath.from(request);

if (requestPath.isValid()) {
ResourceKey resourceKey = ResourceKey.of(requestPath.getResourceName(), requestPath.getResourceVersion());
if (!requestPath.isValid()) {
log.error(format("Request path %s is not found", request.getPathInfo()));
OperationResult.notFound().prepareResponse(response);
return;
}

if (log.isDebugEnabled()) {
resourcePool.printKeys();
}
Resource resource = resourcePool.get(resourceKey);
ResourceKey key = resource.getKey();
String method = request.getMethod();

RPC rpc = null;

if (requestPath.isCustomRestAction()) {
String customRestActionName = requestPath.getActionName();
try {
rpc = resource.getCustomRestActionRPC(customRestActionName);
} catch (UnsupportedOperationException e) {
log.error(format("Custom REST action %s not implemented for resource %s", customRestActionName, key), e);
OperationResult.notImplemented().prepareResponse(response);
return;
} finally {
resource.removeClient();
}
} else {
try {
rpc = resource.getRestRPC(method);
} catch (UnsupportedOperationException e) {
log.error(format("Method %s not implemented for resource %s", method, key), e);
OperationResult.notImplemented().prepareResponse(response);
return;
} finally {
resource.removeClient();
}
}
if (!requestPath.isMethodAllowed(method)) {
log.error(format("Method %s not allowed at path %s", method, request.getPathInfo()));
OperationResult.methodNotAllowed().prepareResponse(response);
return;
}

HTTPMethod rpcMethod = rpc.getHttpMethod();
ResourceAPIKey resourceAPIKey = ResourceAPIKey.of(requestPath.getResourceName(), requestPath.getResourceVersion());

if (rpcMethod == null || !rpcMethod.getName().toUpperCase().equals(method)) {
log.error(format("Remote Procedure Call not implemented for resource %s with method %s", key, method));
OperationResult.notImplemented().prepareResponse(response);
return;
}
if (log.isDebugEnabled()) {
resourceAPIPool.printKeys();
}
ResourceAPI resourceAPI = resourceAPIPool.get(resourceAPIKey);

if (resourceAPI instanceof DefaultResourceAPI) {
log.error(format("ResourceAPI %s not found", resourceAPIKey));
OperationResult.notFound().prepareResponse(response);
return;
}

String actionName = rpc.getName();
ResourceAPIKey key = resourceAPI.getKey();

if (log.isDebugEnabled()) {
actionPool.printKeys();
}
Action action = actionPool.get(actionName);
OperationData input = new OperationData(request);
RPC rpc = null;

if (requestPath.isCustomRestAction()) {
String customRestActionName = requestPath.getActionName();
try {
OperationResult result = action.run(input);
result.prepareResponse(response);
rpc = resourceAPI.getCustomRestActionRPC(customRestActionName);
} catch (UnsupportedOperationException e) {
log.error(format("Custom REST action %s not implemented for resource %s", customRestActionName, key), e);
OperationResult.methodNotAllowed().prepareResponse(response);
return;
} finally {
action.removeClient();
resourceAPI.removeClient();
}
} else {
OperationResult.notFound().prepareResponse(response);
try {
rpc = resourceAPI.getRestRPC(method);
} catch (UnsupportedOperationException e) {
log.error(format("Method %s not implemented for resource %s", method, key), e);
OperationResult.methodNotAllowed().prepareResponse(response);
return;
} finally {
resourceAPI.removeClient();
}
}

HTTPMethod rpcMethod = rpc.getHttpMethod();

if (rpcMethod == null || !rpcMethod.getName().toUpperCase().equals(method)) {
log.error(format("Remote Procedure Call not implemented for resource %s with method %s", key, method));
OperationResult.methodNotAllowed().prepareResponse(response);
return;
}

String actionName = rpc.getName();

if (log.isDebugEnabled()) {
actionPool.printKeys();
}
Action action = actionPool.get(actionName);
OperationData input = new OperationData(request);
try {
OperationResult result = action.run(input);
result.prepareResponse(response);
} finally {
action.removeClient();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.cornell.mannlib.vitro.webapp.dynapi;

import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_BASE_PATH;
import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_SERVLET_PATH;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -14,7 +14,7 @@
import edu.cornell.mannlib.vitro.webapp.dynapi.components.OperationResult;
import edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath;

@WebServlet(name = "RPCEndpoint", urlPatterns = { RPC_BASE_PATH + "/*" })
@WebServlet(name = "RPCEndpoint", urlPatterns = { RPC_SERVLET_PATH + "/*" })
public class RPCEndpoint extends VitroHttpServlet {

private static final Log log = LogFactory.getLog(RESTEndpoint.class);
Expand All @@ -23,7 +23,7 @@ public class RPCEndpoint extends VitroHttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
OperationResult.notImplemented().prepareResponse(response);
OperationResult.methodNotAllowed().prepareResponse(response);
}

@Override
Expand All @@ -48,12 +48,12 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) {

@Override
public void doDelete(HttpServletRequest request, HttpServletResponse response) {
OperationResult.notImplemented().prepareResponse(response);
OperationResult.methodNotAllowed().prepareResponse(response);
}

@Override
public void doPut(HttpServletRequest request, HttpServletResponse response) {
OperationResult.notImplemented().prepareResponse(response);
OperationResult.methodNotAllowed().prepareResponse(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.cornell.mannlib.vitro.webapp.dynapi;

import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceAPI;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceAPIKey;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.DefaultResourceAPI;

public class ResourceAPIPool extends VersionableAbstractPool<ResourceAPIKey, ResourceAPI, ResourceAPIPool> {

private static ResourceAPIPool INSTANCE = new ResourceAPIPool();

public static ResourceAPIPool getInstance() {
return INSTANCE;
}

@Override
public ResourceAPIPool getPool() {
return getInstance();
}

@Override
public ResourceAPI getDefault() {
return new DefaultResourceAPI();
}

@Override
public Class<ResourceAPI> getType() {
return ResourceAPI.class;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package edu.cornell.mannlib.vitro.webapp.dynapi.components;

import edu.cornell.mannlib.vitro.webapp.dynapi.OperationData;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.validators.Validator;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
import org.apache.jena.datatypes.RDFDatatype;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class APIInformation {

private String title;
private String description;
private String version;

public String getTitle() {
return title;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#title", minOccurs = 1, maxOccurs = 1)
public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#description", maxOccurs = 1)
public void setDescription(String description) {
this.description = description;
}

public String getVersion() {
return version;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#version", minOccurs = 1, maxOccurs = 1)
public void setVersion(String version) {
this.version = version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import edu.cornell.mannlib.vitro.webapp.dynapi.OperationData;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;

Expand All @@ -34,17 +32,17 @@ public void dereference() {
@Override
public OperationResult run(OperationData input) {
if (firstStep == null) {
return new OperationResult(HttpServletResponse.SC_NOT_IMPLEMENTED);
return OperationResult.internalServerError();
}
return firstStep.run(input);
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#firstStep", minOccurs = 1, maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#hasFirstStep", minOccurs = 1, maxOccurs = 1)
public void setStep(OperationalStep step) {
this.firstStep = step;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#assignedRPC", minOccurs = 1, maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#hasAssignedRPC", minOccurs = 1, maxOccurs = 1)
public void setRPC(RPC rpc) {
this.rpc = rpc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public String getName() {
return name;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#customRESTActionName", minOccurs = 1, maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#name", minOccurs = 1, maxOccurs = 1)
public void setName(String name) {
this.name = name;
}
Expand All @@ -26,7 +26,7 @@ public RPC getTargetRPC() {
return targetRPC;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#forwardTo", minOccurs = 1, maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#forwardsTo", minOccurs = 1, maxOccurs = 1)
public void setTargetRPC(RPC rpc) {
this.targetRPC = rpc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -19,7 +17,7 @@ public OperationResult run(Map<String, String[]> map) {
log.debug("param value " + value);
}
}
OperationResult result = new OperationResult(HttpServletResponse.SC_BAD_REQUEST);
return result;

return OperationResult.badRequest();
}
}
Loading

0 comments on commit 092ac04

Please sign in to comment.