Skip to content

Commit

Permalink
Custom REST actions fixes to support specified http methods and provi…
Browse files Browse the repository at this point in the history
…de correct documentation (#413)

* Custom REST actions fixes to support specified http methods and provide correct documentation

* fix: hide RESTEndpoint expections while running tests

* fix: test message in RESTEndpointTest

* improve log error message

* fix for prev commit

---------

Co-authored-by: Georgy Litvinov <[email protected]>
  • Loading branch information
litvinovg and litvinovg authored Aug 11, 2023
1 parent 8e926dc commit a3722d8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,24 @@ private PathItem customRESTActionPathItem(CustomRESTAction customRESTAction, Tag

if (targetRPC != null) {
try (Procedure action = actionPool.get(targetRPC)) {
pathItem.setPut(customRESTActionPutOperation(action, tag));
String httpMethodName = customRESTAction.getHttpMethodName();
switch (httpMethodName) {
case "POST":
pathItem.setPost(customRESTActionPostOperation(action, tag));
break;
case "GET":
pathItem.setGet(customRESTActionGetOperation(action, tag));
break;
case "DELETE":
pathItem.setDelete(customRESTActionDeleteOperation(action, tag));
break;
case "PUT":
pathItem.setPut(customRESTActionPutOperation(action, tag));
break;
case "PATCH":
pathItem.setPatch(customRESTActionPatchOperation(action, tag));
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,11 @@ private void process(HttpServletRequest request, HttpServletResponse response) {
String procedureUri = null;

if (requestPath.isCustomRestAction()) {

if (!"PUT".equals(method)) {
resourceAPI.removeClient();
OperationResult.methodNotAllowed().prepareResponse(response); return;
}
String actionName = requestPath.getCustomRestActionName();
try {
procedureUri = resourceAPI.getProcedureUriByActionName(actionName);
procedureUri = resourceAPI.getProcedureUriByActionName(method, actionName);
} catch (UnsupportedOperationException e) {
log.error(format("Custom REST action %s not implemented for resource %s", actionName, key), e);
log.error(format("Custom REST action %s not implemented for resource %s and method %s", actionName, key, method), e);
OperationResult.methodNotAllowed().prepareResponse(response);
return;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class CustomRESTAction implements Removable {

private String name;
private String targetProcedureUri;
private String httpMethodName = "PUT";

@Override
public void dereference() {
Expand All @@ -20,6 +21,11 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#hasDefaultMethod")
public void setHttpMethod(HTTPMethod httpMethod) {
this.httpMethodName = httpMethod.getName();
}

public String getTargetProcedureUri() {
return targetProcedureUri;
Expand All @@ -29,5 +35,9 @@ public String getTargetProcedureUri() {
public void setTargetProcedureUri(String procedureUri) {
this.targetProcedureUri = procedureUri;
}

public String getHttpMethodName() {
return httpMethodName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@ private String getProcedureUriByMethod(String method, boolean isResourceRequest)
}
}

public String getProcedureUriByActionName(String name) {
String uri = getProcedureUriByCustomActionName(name);
public String getProcedureUriByActionName(String method, String actionName) {
String uri = getProcedureUriByCustomActionName(method, actionName);
if (uri != null) {
return uri;
}
throw new UnsupportedOperationException("Unsupported custom action");
}

private String getProcedureUriByCustomActionName(String name) {
private String getProcedureUriByCustomActionName(String method, String name) {
String uri = null;
for (CustomRESTAction customRestAction : customRESTActions) {
if (customRestAction.getName().equals(name)) {
if (customRestAction.getName().equals(name) && method.equals(customRestAction.getHttpMethodName())) {
uri = customRestAction.getTargetProcedureUri();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ private void restoreLogging(Class clazz) {
}

public static void offLogs() {
offLog(RESTEndpoint.class);
offLog(ResourceAPIPool.class);
offLog(RPCPool.class);
offLog(ProcedurePool.class);
offLog(ConfigurationBeanLoader.class);
}

public static void restoreLogs() {
restoreLog(RESTEndpoint.class);
restoreLog(ResourceAPIPool.class);
restoreLog(RPCPool.class);
restoreLog(ProcedurePool.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ public void doTest() throws IOException {

String procedureUri = "resource_uri";
when(resourceAPI.getProcedureUri(testMethod, false)).thenReturn(procedureUri);
when(resourceAPI.getProcedureUriByActionName(testActionName)).thenReturn(procedureUri);
when(resourceAPI.getProcedureUriByActionName(testMethod, testActionName)).thenReturn(procedureUri);
doNothing().when(resourceAPI).removeClient();

run(testMethod);

verify(resourceAPI, times(testExpectedCounts[0])).getProcedureUri(any(), anyBoolean());
verify(resourceAPI, times(testExpectedCounts[1])).getProcedureUriByActionName(any());
verify(resourceAPI, times(testExpectedCounts[1])).getProcedureUriByActionName(testMethod, testActionName);
verify(resourceAPI, times(testExpectedCounts[2])).removeClient();
verify(procedure, times(testExpectedCounts[3])).run(any());
verify(procedure, times(testExpectedCounts[4])).removeClient();
Expand Down Expand Up @@ -218,8 +218,8 @@ public static Collection<Object[]> requests() {
{ "PATCH", PATH_INFO, actionName, new int[] { 0, 0, 0, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Cannot patch on resource collection" },
{ "DELETE", PATH_INFO, actionName, new int[] { 0, 0, 0, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Cannot delete on resource collection" },

{ "POST", customRestActionPathInfo, actionName, new int[] { 0, 0, 1, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Resource found with supported method" },
{ "GET", customRestActionPathInfo, actionName, new int[] { 0, 0, 1, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Resource found with unsupported method" },
{ "POST", customRestActionPathInfo, actionName, new int[] { 0, 1, 1, 1, 1, 1 }, SC_METHOD_NOT_ALLOWED, "Resource found with supported method" },
{ "GET", customRestActionPathInfo, actionName, new int[] { 0, 1, 1, 1, 1, 1 }, SC_METHOD_NOT_ALLOWED, "Resource found with supported method" },
{ "PUT", customRestActionPathInfo, actionName, new int[] { 0, 0, 0, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Method unsupported by custom REST action" },
{ "PATCH", customRestActionPathInfo, actionName, new int[] { 0, 0, 0, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Method unsupported by custom REST action" },
{ "DELETE", customRestActionPathInfo, actionName, new int[] { 0, 0, 0, 0, 0, 1 }, SC_METHOD_NOT_ALLOWED, "Method unsupported by custom REST action" }
Expand Down

0 comments on commit a3722d8

Please sign in to comment.