use of io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo in project apiman by apiman.
the class GatewayServlet method readRequest.
/**
* Reads a {@link ApiRequest} from information found in the inbound
* portion of the http request.
* @param request the undertow http server request
* @return a valid {@link ApiRequest}
* @throws IOException
*/
protected ApiRequest readRequest(HttpServletRequest request) throws Exception {
// parseApiRequestPath(request);
ApiRequestPathInfo pathInfo = getEngine().getApiRequestPathParser().parseEndpoint(request.getPathInfo(), wrapMultiMap(request));
if (pathInfo.orgId == null) {
// $NON-NLS-1$
throw new Exception(Messages.i18n.format("GatewayServlet.InvalidApiEndpoint"));
}
QueryMap queryParams = parseApiRequestQueryParams(request.getQueryString());
String apiKey = getApiKey(request, queryParams);
ApiRequest srequest = GatewayThreadContext.getApiRequest();
srequest.setApiKey(apiKey);
srequest.setApiOrgId(pathInfo.orgId);
srequest.setApiId(pathInfo.apiId);
srequest.setApiVersion(pathInfo.apiVersion);
srequest.setUrl(request.getRequestURL().toString());
srequest.setDestination(pathInfo.resource);
srequest.setQueryParams(queryParams);
readHeaders(srequest, request);
srequest.setRawRequest(request);
srequest.setRemoteAddr(request.getRemoteAddr());
srequest.setTransportSecure(request.isSecure());
return srequest;
}
use of io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo in project apiman by apiman.
the class GatewayServletTest method testParseApiRequestPath.
/**
* Test method for {@link io.apiman.gateway.platforms.servlet.GatewayServlet#parseApiRequestPath(javax.servlet.http.HttpServletRequest)}.
*/
@Test
public void testParseApiRequestPath() {
ApiRequestPathInfo info = parseApiRequestPath(null);
info = parseApiRequestPath("/invalidpath");
Assert.assertNull(info.orgId);
Assert.assertNull(info.apiId);
Assert.assertNull(info.apiVersion);
Assert.assertNull(info.resource);
info = parseApiRequestPath("/invalid/path");
Assert.assertNull(info.orgId);
Assert.assertNull(info.apiId);
Assert.assertNull(info.apiVersion);
Assert.assertNull(info.resource);
info = parseApiRequestPath("/Org1/Api1/1.0");
Assert.assertEquals("Org1", info.orgId);
Assert.assertEquals("Api1", info.apiId);
Assert.assertEquals("1.0", info.apiVersion);
Assert.assertNull(info.resource);
info = parseApiRequestPath("/MyOrg/Api-99/2.7");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Api-99", info.apiId);
Assert.assertEquals("2.7", info.apiVersion);
Assert.assertNull(info.resource);
info = parseApiRequestPath("/MyOrg/Api-99/2.7/resource");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Api-99", info.apiId);
Assert.assertEquals("2.7", info.apiVersion);
Assert.assertEquals("/resource", info.resource);
info = parseApiRequestPath("/MyOrg/Api-99/2.7/path/to/resource");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Api-99", info.apiId);
Assert.assertEquals("2.7", info.apiVersion);
Assert.assertEquals("/path/to/resource", info.resource);
info = parseApiRequestPath("/MyOrg/Api-99/2.7/path/to/resource?query=1234");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Api-99", info.apiId);
Assert.assertEquals("2.7", info.apiVersion);
Assert.assertEquals("/path/to/resource?query=1234", info.resource);
info = parseApiRequestPath("/MyOrg/Api-99/path/to/resource?query=1234", null, "2.7");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Api-99", info.apiId);
Assert.assertEquals("2.7", info.apiVersion);
Assert.assertEquals("/path/to/resource?query=1234", info.resource);
info = parseApiRequestPath("/MyOrg/Api-99/path/to/resource?query=1234", "application/apiman.2.7+json", null);
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Api-99", info.apiId);
Assert.assertEquals("2.7", info.apiVersion);
Assert.assertEquals("/path/to/resource?query=1234", info.resource);
}
use of io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo in project apiman by apiman.
the class ApimanPathUtilsTest method testParseApiRequestPath.
@Test
public void testParseApiRequestPath() {
ApiRequestPathInfo path = ApimanPathUtils.parseApiRequestPath(null, "application/json", "/OrgId/ApiId/VER/path/to/thing");
Assert.assertEquals("OrgId", path.orgId);
Assert.assertEquals("ApiId", path.apiId);
Assert.assertEquals("VER", path.apiVersion);
Assert.assertEquals("/path/to/thing", path.resource);
path = ApimanPathUtils.parseApiRequestPath(null, "application/json", "/OrgId/ApiId/VER");
Assert.assertEquals("OrgId", path.orgId);
Assert.assertEquals("ApiId", path.apiId);
Assert.assertEquals("VER", path.apiVersion);
Assert.assertEquals(null, path.resource);
path = ApimanPathUtils.parseApiRequestPath(null, "application/json", "/OrgId/ApiId/VER/");
Assert.assertEquals("OrgId", path.orgId);
Assert.assertEquals("ApiId", path.apiId);
Assert.assertEquals("VER", path.apiVersion);
Assert.assertEquals("/", path.resource);
path = ApimanPathUtils.parseApiRequestPath("VER", "application/json", "/OrgId/ApiId/path/to/thing");
Assert.assertEquals("OrgId", path.orgId);
Assert.assertEquals("ApiId", path.apiId);
Assert.assertEquals("VER", path.apiVersion);
Assert.assertEquals("/path/to/thing", path.resource);
path = ApimanPathUtils.parseApiRequestPath("VER", "application/json", "/OrgId/ApiId");
Assert.assertEquals("OrgId", path.orgId);
Assert.assertEquals("ApiId", path.apiId);
Assert.assertEquals("VER", path.apiVersion);
Assert.assertEquals(null, path.resource);
}
use of io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo in project apiman by apiman.
the class HttpApiFactory method parsePath.
private static void parsePath(HttpServerRequest request, ApiRequest apimanRequest) {
// NB: The apiman version of the headers has already been parsed, so the headers have already been filtered/modified.
// Therefore we wrap the original inbound headers (just get) to efficiently access the necessary data.
ApiRequestPathInfo parsedPath = requestPathParser.parseEndpoint(request.path(), wrapMultiMap(request.headers()));
apimanRequest.setApiOrgId(parsedPath.orgId);
apimanRequest.setApiId(parsedPath.apiId);
apimanRequest.setApiVersion(parsedPath.apiVersion);
apimanRequest.setUrl(request.absoluteURI());
apimanRequest.setDestination(parsedPath.resource);
}
use of io.apiman.common.util.ApimanPathUtils.ApiRequestPathInfo in project apiman-plugins by apiman.
the class CustomPathParserImpl method parseEndpoint.
@Override
public ApiRequestPathInfo parseEndpoint(String path, HeaderMap headers) {
String[] split = StringUtils.split(path, "/", 3);
if (split == null || split.length < 2) {
throw new IllegalArgumentException("Invalid path format, expected /apiId/apiVersion/<resource path>");
}
ApiRequestPathInfo parsed = new ApiRequestPathInfo();
// Let's set the org name manually as our configured `defaultOrgName`
parsed.orgId = defaultOrgName;
parsed.apiId = split[0];
parsed.apiVersion = split[1];
if (split.length > 2) {
parsed.resource = "/" + split[2];
} else {
parsed.resource = "/";
}
return parsed;
}
Aggregations