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

Correct context path vs servlet path #280

Merged
2 commits merged into from Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,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 @@ -22,7 +22,7 @@
import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceKey;
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);
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class RequestPath {

public static final String RESOURCE_ID_PARAM = "RESOURCE_ID";

public static final String API_BASE_PATH = "/api";
public static final String RPC_BASE_PATH = API_BASE_PATH + "/rpc";
public static final String REST_BASE_PATH = API_BASE_PATH + "/rest";
public static final String API_SERVLET_PATH = "/api";
public static final String RPC_SERVLET_PATH = API_SERVLET_PATH + "/rpc";
public static final String REST_SERVLET_PATH = API_SERVLET_PATH + "/rest";

private final RequestType type;

Expand All @@ -28,22 +28,22 @@ public class RequestPath {
private final String actionName;

private RequestPath(HttpServletRequest request) {
String contextPath = request != null && request.getContextPath() != null
? request.getContextPath()
String servletPath = request != null && request.getServletPath() != null
? request.getServletPath()
: EMPTY;
String pathInfo = request != null && request.getPathInfo() != null
? request.getPathInfo()
: EMPTY;

pathParts = pathInfo.split("/");

if (contextPath.toLowerCase().contains(RPC_BASE_PATH)) {
if (servletPath.toLowerCase().contains(RPC_SERVLET_PATH)) {
type = RequestType.RPC;
actionName = pathParts.length > 1 ? pathParts[1] : null;
resourceVersion = null;
resourceName = null;
resourceId = null;
} else if (contextPath.toLowerCase().contains(REST_BASE_PATH)) {
} else if (servletPath.toLowerCase().contains(REST_SERVLET_PATH)) {
type = RequestType.REST;
resourceVersion = pathParts.length > 1 ? pathParts[1] : null;
resourceName = pathParts.length > 2 ? pathParts[2] : null;
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 org.apache.commons.lang3.StringUtils.EMPTY;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -40,7 +40,6 @@
public class RESTEndpointTest {

private final static String PATH_INFO = "/1/test";
private final static String CONTEXT_PATH = REST_BASE_PATH + PATH_INFO;

private Map<String, String[]> params = new HashMap<>();

Expand Down Expand Up @@ -173,7 +172,7 @@ private void run(String method) {

private void prepareMocks(String method, String actionName) {
when(request.getMethod()).thenReturn(method);
when(request.getContextPath()).thenReturn(CONTEXT_PATH);
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn(PATH_INFO);

when(action.run(any(OperationData.class)))
Expand Down Expand Up @@ -206,7 +205,7 @@ private void prepareMocksRPCNotImplemented(String method, String actionName) {

private void prepareMocksCustomAction(String method, String actionName) {
when(request.getMethod()).thenReturn(method);
when(request.getContextPath()).thenReturn(CONTEXT_PATH + "/" + actionName);
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn(PATH_INFO + "/" + actionName);

when(action.run(any(OperationData.class)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.cornell.mannlib.vitro.webapp.dynapi;

import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_SERVLET_PATH;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static javax.servlet.http.HttpServletResponse.SC_NOT_IMPLEMENTED;
Expand Down Expand Up @@ -36,8 +37,7 @@
@RunWith(Parameterized.class)
public class RPCEndpointIT extends ServletContextIT {

private final static String URI_CONTEXT = "/api/rpc/";
private final static String URI_BASE = "http://localhost" + URI_CONTEXT;
private final static String URI_BASE = "http://localhost:8080" + RPC_SERVLET_PATH;

private RPCEndpoint rpcEndpoint;

Expand Down Expand Up @@ -92,13 +92,13 @@ public void beforeEach() throws Exception {
loadDefaultModel();

when(request.getServletContext()).thenReturn(servletContext);
when(request.getContextPath()).thenReturn(URI_CONTEXT);
when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH);

actionPool.init(request.getServletContext());
actionPool.reload();

if (testAction != null) {
StringBuffer buffer = new StringBuffer(URI_BASE + testAction);
StringBuffer buffer = new StringBuffer(URI_BASE + "/" + testAction);
when(request.getRequestURL()).thenReturn(buffer);
when(request.getPathInfo()).thenReturn("/" + testAction);
}
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 static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -30,7 +30,6 @@
public class RPCEndpointTest {

private final static String PATH_INFO = "/test";
private final static String CONTEXT_PATH = RPC_BASE_PATH + PATH_INFO;

private Map<String, String[]> params;

Expand Down Expand Up @@ -80,7 +79,7 @@ public void doGetTest() {
public void doPostTest() {
OperationResult result = new OperationResult(HttpServletResponse.SC_OK);

when(request.getContextPath()).thenReturn(CONTEXT_PATH);
when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH);
when(request.getPathInfo()).thenReturn(PATH_INFO);
when(action.run(any(OperationData.class))).thenReturn(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public void setup() {
loader = new ConfigurationBeanLoader(ontModel, servletContext);
}

protected void loadModelsFromN3(String fileFormat, String... paths) throws IOException {
for(String path : paths){
loadModel(
new RDFFile(fileFormat, path)
);
}
}

protected void loadTestModel() throws IOException {
// all actions reuse testSparqlQuery1 from testing action
loadModel(
Expand Down Expand Up @@ -83,6 +75,12 @@ protected void loadModel(RDFFile... rdfFiles) throws IOException {
}
}

protected void loadModels(String fileFormat, String... paths) throws IOException {
for (String path : paths) {
loadModel(new RDFFile(fileFormat, path));
}
}

protected String readFile(String path) throws IOException {
Path p = new File(path).toPath();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import edu.cornell.mannlib.vitro.webapp.application.ApplicationImpl;
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Action;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.SolrQuery;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngine;
Expand All @@ -28,11 +27,11 @@ public class SolrQueryTest extends ServletContextTest{
private final static String TEST_DATA_PATH="src/test/resources/rdf/abox/filegraph/dynamic-api-individuals-solr-test.n3";
private final static String TEST_SOLR_QUERY_URI="https://vivoweb.org/ontology/vitro-dynamic-api/solrQuery/genericSolrTextQuery";

private static MockedStatic<ApplicationUtils> applicationUtils;

@Spy
private SolrQuery solrQuery;

private static MockedStatic<ApplicationUtils> applicationUtils;

@Mock
ApplicationImpl application;

Expand Down Expand Up @@ -69,7 +68,7 @@ public void setupQuery(){
@Test
public void testLoadingAndPropertiesSetup() throws IOException, ConfigurationBeanLoaderException {
loadDefaultModel();
loadModelsFromN3(TEST_DATA_PATH.split("\\.")[1],TEST_DATA_PATH);
loadModels(TEST_DATA_PATH.split("\\.")[1], TEST_DATA_PATH);

SolrQuery query = loader.loadInstance(TEST_SOLR_QUERY_URI, SolrQuery.class);
assertNotNull(query);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.cornell.mannlib.vitro.webapp.dynapi.request;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -27,7 +30,7 @@ public class RequestPathTest {

@Test
public void testRpc() {
when(request.getContextPath()).thenReturn("/api/rpc/create");
when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/create");

RequestPath requestPath = RequestPath.from(request);
Expand All @@ -41,33 +44,33 @@ public void testRpc() {
assertEquals(null, requestPath.getResourceId());


when(request.getContextPath()).thenReturn("/api/rpc/");
when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/");

assertFalse(RequestPath.from(request).isValid());


when(request.getContextPath()).thenReturn(null);
when(request.getServletPath()).thenReturn(null);
when(request.getPathInfo()).thenReturn("/create");

assertFalse(RequestPath.from(request).isValid());


when(request.getContextPath()).thenReturn("/api/rpc");
when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH);
when(request.getPathInfo()).thenReturn(null);

assertFalse(RequestPath.from(request).isValid());


when(request.getContextPath()).thenReturn(null);
when(request.getServletPath()).thenReturn(null);
when(request.getPathInfo()).thenReturn(null);

assertFalse(RequestPath.from(request).isValid());
}

@Test
public void testRestCollection() {
when(request.getContextPath()).thenReturn("/api/rest/1/persons");
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/1/persons");

RequestPath requestPath = RequestPath.from(request);
Expand All @@ -81,33 +84,33 @@ public void testRestCollection() {
assertEquals(null, requestPath.getActionName());


when(request.getContextPath()).thenReturn("/api/rest/");
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/");

assertFalse(RequestPath.from(request).isValid());


when(request.getContextPath()).thenReturn(null);
when(request.getServletPath()).thenReturn(null);
when(request.getPathInfo()).thenReturn("/1/persons");

assertFalse(RequestPath.from(request).isValid());


when(request.getContextPath()).thenReturn("/api/rest");
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn(null);

assertFalse(RequestPath.from(request).isValid());


when(request.getContextPath()).thenReturn(null);
when(request.getServletPath()).thenReturn(null);
when(request.getPathInfo()).thenReturn(null);

assertFalse(RequestPath.from(request).isValid());
}

@Test
public void testRestCustomAction() {
when(request.getContextPath()).thenReturn("/api/rest/1/persons/dedupe");
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/1/persons/dedupe");

RequestPath requestPath = RequestPath.from(request);
Expand All @@ -123,7 +126,7 @@ public void testRestCustomAction() {

@Test
public void testRestIndividual() {
when(request.getContextPath()).thenReturn("/api/rest/1/persons/resource:" + encodedResourceId);
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/1/persons/resource:" + encodedResourceId);

RequestPath requestPath = RequestPath.from(request);
Expand All @@ -139,7 +142,7 @@ public void testRestIndividual() {

@Test
public void testRestIndividualCustomAction() {
when(request.getContextPath()).thenReturn("/api/rest/1/persons/resource:" + encodedResourceId + "/patch");
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/1/persons/resource:" + encodedResourceId + "/patch");

RequestPath requestPath = RequestPath.from(request);
Expand All @@ -155,17 +158,17 @@ public void testRestIndividualCustomAction() {

@Test
public void testNotFound() {
when(request.getContextPath()).thenReturn("/api/rest/1/persons/resource:" + encodedResourceId + "/patch/foo");
when(request.getServletPath()).thenReturn(REST_SERVLET_PATH);
when(request.getPathInfo()).thenReturn("/1/persons/resource:" + encodedResourceId + "/patch/foo");

assertFalse(RequestPath.from(request).isValid());

when(request.getContextPath()).thenReturn("/api/bar/1/persons");
when(request.getServletPath()).thenReturn("/api/bar");
when(request.getPathInfo()).thenReturn("/1/persons");

assertFalse(RequestPath.from(request).isValid());

when(request.getContextPath()).thenReturn("/some/random/path");
when(request.getServletPath()).thenReturn("/some/random/path");
when(request.getPathInfo()).thenReturn("/3/2/1");

assertFalse(RequestPath.from(request).isValid());
Expand Down