Skip to content

Commit

Permalink
Issue 53: Allow namespace as a path param (#54)
Browse files Browse the repository at this point in the history
Signed-off-by: Shivesh Ranjan <[email protected]>
  • Loading branch information
shiveshr authored Jul 20, 2020
1 parent 3868d72 commit a82a400
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.ws.rs.core.UriBuilder;

import io.pravega.common.security.JKSHelper;
import io.pravega.schemaregistry.server.rest.filter.NamespaceRedirectFilter;
import io.pravega.schemaregistry.server.rest.resources.GroupResourceImpl;
import io.pravega.schemaregistry.server.rest.resources.PingImpl;
import io.pravega.schemaregistry.server.rest.resources.SchemaResourceImpl;
Expand Down Expand Up @@ -49,6 +50,7 @@ public RestServer(SchemaRegistryService registryService, ServiceConfig restServe
this.baseUri = UriBuilder.fromUri("http://" + restServerConfig.getHost()).port(restServerConfig.getPort()).build();

final Set<Object> resourceObjs = new HashSet<>();
resourceObjs.add(new NamespaceRedirectFilter());
resourceObjs.add(new PingImpl());
resourceObjs.add(new GroupResourceImpl(registryService, restServerConfig, executor()));
resourceObjs.add(new SchemaResourceImpl(registryService, restServerConfig, executor()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.pravega.schemaregistry.server.rest.filter;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
import java.util.List;

/**
* A request filter that converts namespace expressed as a path param into a query param.
*
* This allows users to express either "/v1/namespace/ns/groups" "/v1/groups?namespace=ns" and both are equivalent.
*
* If both path param and query param are used to supply values for namespace, then the path param overrides the query param.
* So if someone expresses the uri as "/v1/namespace/ns1/groups?namespace=ns2". This will result in namespace being
* set to ns1 for the request.
*/
@Provider
@PreMatching
public class NamespaceRedirectFilter implements ContainerRequestFilter {
private static final String NAMESPACE = "namespace";
private static final String GROUPS = "groups";

@Override
public void filter(ContainerRequestContext containerRequest)
throws WebApplicationException {
UriInfo uriInfo = containerRequest.getUriInfo();
UriBuilder uriBuilder = uriInfo.getRequestUriBuilder();
List<PathSegment> pathSegments = uriInfo.getPathSegments();
// if path has namespace defined as path segment. convert it to query param.
// namespace path param is converted to query param only when namespace name is followed by groups path segment.
if (pathSegments.size() > 3 && pathSegments.get(1).getPath().equals(NAMESPACE)
&& pathSegments.get(3).getPath().equals(GROUPS)) {
handleNamespacePath(uriBuilder, pathSegments);
}
containerRequest.setRequestUri(uriBuilder.build());
}

private void handleNamespacePath(UriBuilder uriBuilder, List<PathSegment> pathSegments) {
StringBuilder pathBuilder = new StringBuilder();
appendPath(pathSegments.get(0), pathBuilder);
// pathsegments(1) is namspace and pathsegments(2) is name of the namespace.
String namespace = pathSegments.get(2).getPath();

for (int i = 3; i < pathSegments.size(); i++) {
appendPath(pathSegments.get(i), pathBuilder);
}

uriBuilder.replacePath(pathBuilder.toString());
uriBuilder.replaceQueryParam(NAMESPACE, namespace);
}

private void appendPath(PathSegment pathSegment, StringBuilder pathBuilder) {
pathBuilder.append("/").append(pathSegment.getPath());
pathSegment.getMatrixParameters().forEach((x, y) -> pathBuilder.append(";").append(x).append("=").append(y));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.pravega.schemaregistry.contract.transform.ModelHelper;
import io.pravega.schemaregistry.server.rest.RegistryApplication;
import io.pravega.schemaregistry.server.rest.ServiceConfig;
import io.pravega.schemaregistry.server.rest.filter.NamespaceRedirectFilter;
import io.pravega.schemaregistry.service.SchemaRegistryService;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
Expand Down Expand Up @@ -50,6 +51,7 @@

public class SchemaRegistryResourceTest extends JerseyTest {
private static final String GROUPS = "v1/groups";
private static final String NAMESPACE_FORMAT = "v1/namespace/%s/groups";
private SchemaRegistryService service;
private ScheduledExecutorService executor;

Expand All @@ -59,6 +61,7 @@ protected Application configure() {
forceSet(TestProperties.CONTAINER_PORT, "0");
service = mock(SchemaRegistryService.class);
final Set<Object> resourceObjs = new HashSet<>();
resourceObjs.add(new NamespaceRedirectFilter());
resourceObjs.add(new GroupResourceImpl(service, ServiceConfig.builder().build(), executor));
resourceObjs.add(new SchemaResourceImpl(service, ServiceConfig.builder().build(), executor));

Expand Down Expand Up @@ -86,6 +89,12 @@ public void groups() throws ExecutionException, InterruptedException {
assertEquals(response.getStatus(), 200);
ListGroupsResponse list = response.readEntity(ListGroupsResponse.class);
assertEquals(list.getGroups().size(), 1);

future = target(String.format(NAMESPACE_FORMAT, "ns")).queryParam("limit", 100).request().async().get();
response = future.get();
assertEquals(response.getStatus(), 200);
list = response.readEntity(ListGroupsResponse.class);
assertEquals(list.getGroups().size(), 1);

// region create group
// endregion
Expand Down

0 comments on commit a82a400

Please sign in to comment.