Skip to content

Commit

Permalink
added support for sending the API version via a custom header or via …
Browse files Browse the repository at this point in the history
…the Accept header
  • Loading branch information
EricWittmann committed Jul 27, 2015
1 parent e588d3d commit a49df3d
Show file tree
Hide file tree
Showing 4 changed files with 752 additions and 14 deletions.
@@ -0,0 +1,25 @@
/*
* Copyright 2015 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apiman.gateway.engine;

/**
*
*
* @author eric.wittmann@redhat.com
*/
public class MongoDbRegistry {

}
Expand Up @@ -249,7 +249,7 @@ public void handle(ISignalWriteStream connectorStream) {
* @throws IOException
*/
protected ServiceRequest readRequest(HttpServletRequest request) throws Exception {
ServiceRequestPathInfo pathInfo = parseServiceRequestPath(request.getPathInfo());
ServiceRequestPathInfo pathInfo = parseServiceRequestPath(request);
if (pathInfo.orgId == null) {
throw new Exception(Messages.i18n.format("GatewayServlet.InvalidServiceEndpoint")); //$NON-NLS-1$
}
Expand Down Expand Up @@ -382,17 +382,38 @@ protected void writeError(HttpServletResponse resp, Throwable error) {
* @param pathInfo
* @return the path info parsed into its component parts
*/
protected static final ServiceRequestPathInfo parseServiceRequestPath(String pathInfo) {
protected static final ServiceRequestPathInfo parseServiceRequestPath(HttpServletRequest request) {
String pathInfo = request.getPathInfo();
ServiceRequestPathInfo info = new ServiceRequestPathInfo();

boolean versionFound = false;

String apiVersionHeader = request.getHeader("X-API-Version"); //$NON-NLS-1$
if (apiVersionHeader != null && apiVersionHeader.trim().length() > 0) {
info.serviceVersion = apiVersionHeader;
versionFound = true;
} else {
String acceptHeader = request.getHeader("Accept"); //$NON-NLS-1$
if (acceptHeader != null && acceptHeader.startsWith("application/apiman.")) { //$NON-NLS-1$
String [] split = acceptHeader.split("\\+"); //$NON-NLS-1$
info.serviceVersion = split[0].substring("application/apiman.".length()); //$NON-NLS-1$
versionFound = true;
}
}

int minParts = versionFound ? 3 : 4;

if (pathInfo != null) {
String[] split = pathInfo.split("/"); //$NON-NLS-1$
if (split.length >= 4) {
if (split.length >= minParts) {
info.orgId = split[1];
info.serviceId = split[2];
info.serviceVersion = split[3];
if (split.length > 4) {
if (!versionFound) {
info.serviceVersion = split[3];
}
if (split.length > minParts) {
StringBuilder resource = new StringBuilder();
for (int idx = 4; idx < split.length; idx++) {
for (int idx = minParts; idx < split.length; idx++) {
resource.append('/');
resource.append(split[idx]);
}
Expand Down
Expand Up @@ -35,49 +35,85 @@ public class GatewayServletTest {
*/
@Test
public void testParseServiceRequestPath() {
ServiceRequestPathInfo info = GatewayServlet.parseServiceRequestPath(null);
ServiceRequestPathInfo info = parseServiceRequestPath(null);

info = GatewayServlet.parseServiceRequestPath("/invalidpath");
info = parseServiceRequestPath("/invalidpath");
Assert.assertNull(info.orgId);
Assert.assertNull(info.serviceId);
Assert.assertNull(info.serviceVersion);
Assert.assertNull(info.resource);

info = GatewayServlet.parseServiceRequestPath("/invalid/path");
info = parseServiceRequestPath("/invalid/path");
Assert.assertNull(info.orgId);
Assert.assertNull(info.serviceId);
Assert.assertNull(info.serviceVersion);
Assert.assertNull(info.resource);

info = GatewayServlet.parseServiceRequestPath("/Org1/Service1/1.0");
info = parseServiceRequestPath("/Org1/Service1/1.0");
Assert.assertEquals("Org1", info.orgId);
Assert.assertEquals("Service1", info.serviceId);
Assert.assertEquals("1.0", info.serviceVersion);
Assert.assertNull(info.resource);

info = GatewayServlet.parseServiceRequestPath("/MyOrg/Service-99/2.7");
info = parseServiceRequestPath("/MyOrg/Service-99/2.7");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Service-99", info.serviceId);
Assert.assertEquals("2.7", info.serviceVersion);
Assert.assertNull(info.resource);

info = GatewayServlet.parseServiceRequestPath("/MyOrg/Service-99/2.7/resource");
info = parseServiceRequestPath("/MyOrg/Service-99/2.7/resource");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Service-99", info.serviceId);
Assert.assertEquals("2.7", info.serviceVersion);
Assert.assertEquals("/resource", info.resource);

info = GatewayServlet.parseServiceRequestPath("/MyOrg/Service-99/2.7/path/to/resource");
info = parseServiceRequestPath("/MyOrg/Service-99/2.7/path/to/resource");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Service-99", info.serviceId);
Assert.assertEquals("2.7", info.serviceVersion);
Assert.assertEquals("/path/to/resource", info.resource);

info = GatewayServlet.parseServiceRequestPath("/MyOrg/Service-99/2.7/path/to/resource?query=1234");
info = parseServiceRequestPath("/MyOrg/Service-99/2.7/path/to/resource?query=1234");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Service-99", info.serviceId);
Assert.assertEquals("2.7", info.serviceVersion);
Assert.assertEquals("/path/to/resource?query=1234", info.resource);

info = parseServiceRequestPath("/MyOrg/Service-99/path/to/resource?query=1234", null, "2.7");
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Service-99", info.serviceId);
Assert.assertEquals("2.7", info.serviceVersion);
Assert.assertEquals("/path/to/resource?query=1234", info.resource);

info = parseServiceRequestPath("/MyOrg/Service-99/path/to/resource?query=1234", "application/apiman.2.7+json", null);
Assert.assertEquals("MyOrg", info.orgId);
Assert.assertEquals("Service-99", info.serviceId);
Assert.assertEquals("2.7", info.serviceVersion);
Assert.assertEquals("/path/to/resource?query=1234", info.resource);

}

/**
* @param path
*/
private ServiceRequestPathInfo parseServiceRequestPath(String path) {
return parseServiceRequestPath(path, null, null);
}

/**
* @param path
* @param acceptHeader
* @param apiVersionHeader
*/
private ServiceRequestPathInfo parseServiceRequestPath(String path, String acceptHeader, String apiVersionHeader) {
MockHttpServletRequest mockReq = new MockHttpServletRequest(path);
if (acceptHeader != null) {
mockReq.setHeader("Accept", acceptHeader);
}
if (apiVersionHeader != null) {
mockReq.setHeader("X-API-Version", apiVersionHeader);
}
return GatewayServlet.parseServiceRequestPath(mockReq);
}

/**
Expand Down

0 comments on commit a49df3d

Please sign in to comment.