From d7b012e5600fa19b330d61a2572499f14fe9bb61 Mon Sep 17 00:00:00 2001 From: Anu Engineer Date: Fri, 19 Oct 2018 11:40:11 -0700 Subject: [PATCH] HDDS-679. Add query parameter to the constructed query in VirtualHostStyleFilter. Contributed by Bharat Viswanadham. --- .../ozone/s3/VirtualHostStyleFilter.java | 13 +++-- .../ozone/s3/TestVirtualHostStyleFilter.java | 52 ++++++++++++++++--- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java index 4cf78b6ff7..f1d52a6283 100644 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java +++ b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java @@ -22,14 +22,14 @@ import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.PreMatching; import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.ext.Provider; import java.io.IOException; import java.net.URI; import java.util.Arrays; -import java.util.regex.Matcher; -import java.util.regex.Pattern; + import com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.fs.InvalidRequestException; @@ -95,11 +95,16 @@ public void filter(ContainerRequestContext requestContext) throws URI baseURI = requestContext.getUriInfo().getBaseUri(); String currentPath = requestContext.getUriInfo().getPath(); - String newPath = bucketName; + String newPath = bucketName; if (currentPath != null) { newPath += String.format("%s", currentPath); } - URI requestAddr = UriBuilder.fromUri(baseURI).path(newPath).build(); + MultivaluedMap queryParams = requestContext.getUriInfo() + .getQueryParameters(); + UriBuilder requestAddrBuilder = UriBuilder.fromUri(baseURI).path(newPath); + queryParams.forEach((k, v) -> requestAddrBuilder.queryParam(k, + v.toArray())); + URI requestAddr = requestAddrBuilder.build(); requestContext.setRequestUri(baseURI, requestAddr); } } diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java index 5548c7783b..3fe6e1a50e 100644 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java @@ -31,6 +31,7 @@ import javax.ws.rs.core.SecurityContext; import java.net.URI; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** @@ -55,16 +56,27 @@ public void setup() { * @throws Exception */ public ContainerRequest createContainerRequest(String host, String path, + String queryParams, boolean virtualHostStyle) throws Exception { URI baseUri = new URI("http://" + s3HttpAddr); URI virtualHostStyleUri; - if (path == null) { + if (path == null && queryParams == null) { virtualHostStyleUri = new URI("http://" + s3HttpAddr); - } else { + } else if (path != null && queryParams == null) { virtualHostStyleUri = new URI("http://" + s3HttpAddr + path); + } else if (path !=null && queryParams != null) { + virtualHostStyleUri = new URI("http://" + s3HttpAddr + path + + queryParams); + } else { + virtualHostStyleUri = new URI("http://" + s3HttpAddr + queryParams); + } + URI pathStyleUri; + if (queryParams == null) { + pathStyleUri = new URI("http://" + s3HttpAddr + path); + } else { + pathStyleUri = new URI("http://" + s3HttpAddr + path + queryParams); } - URI pathStyleUri = new URI("http://" + s3HttpAddr + path); String httpMethod = "DELETE"; SecurityContext securityContext = Mockito.mock(SecurityContext.class); PropertiesDelegate propertiesDelegate = Mockito.mock(PropertiesDelegate @@ -89,7 +101,7 @@ public void testVirtualHostStyle() throws Exception { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - ".localhost:9878", "/myfile", true); + ".localhost:9878", "/myfile", null, true); virtualHostStyleFilter.filter(containerRequest); URI expected = new URI("http://" + s3HttpAddr + "/mybucket/myfile"); @@ -104,7 +116,7 @@ public void testPathStyle() throws Exception { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest(s3HttpAddr, - "/mybucket/myfile", false); + "/mybucket/myfile", null, false); virtualHostStyleFilter.filter(containerRequest); URI expected = new URI("http://" + s3HttpAddr + "/mybucket/myfile"); @@ -120,13 +132,37 @@ public void testVirtualHostStyleWithCreateBucketRequest() throws Exception { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - ".localhost:9878", null, true); + ".localhost:9878", null, null, true); virtualHostStyleFilter.filter(containerRequest); URI expected = new URI("http://" + s3HttpAddr + "/mybucket"); Assert.assertEquals(expected, containerRequest.getRequestUri()); } + @Test + public void testVirtualHostStyleWithQueryParams() throws Exception { + + VirtualHostStyleFilter virtualHostStyleFilter = + new VirtualHostStyleFilter(); + virtualHostStyleFilter.setConfiguration(conf); + + ContainerRequest containerRequest = createContainerRequest("mybucket" + + ".localhost:9878", null, "?prefix=bh", true); + virtualHostStyleFilter.filter(containerRequest); + URI expected = new URI("http://" + s3HttpAddr + "/mybucket?prefix=bh"); + assertTrue(expected.toString().contains(containerRequest.getRequestUri() + .toString())); + + containerRequest = createContainerRequest("mybucket" + + ".localhost:9878", null, "?prefix=bh&type=dir", true); + virtualHostStyleFilter.filter(containerRequest); + expected = new URI("http://" + s3HttpAddr + + "/mybucket?prefix=bh&type=dir"); + assertTrue(expected.toString().contains(containerRequest.getRequestUri() + .toString())); + + } + @Test public void testVirtualHostStyleWithNoMatchingDomain() throws Exception { @@ -135,7 +171,7 @@ public void testVirtualHostStyleWithNoMatchingDomain() throws Exception { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - ".localhost:9999", null, true); + ".localhost:9999", null, null, true); try { virtualHostStyleFilter.filter(containerRequest); } catch (InvalidRequestException ex) { @@ -153,7 +189,7 @@ public void testIncorrectVirtualHostStyle() throws virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - "localhost:9878", null, true); + "localhost:9878", null, null, true); try { virtualHostStyleFilter.filter(containerRequest); fail("testIncorrectVirtualHostStyle failed");