HDDS-679. Add query parameter to the constructed query in VirtualHostStyleFilter.

Contributed by Bharat Viswanadham.

(cherry picked from commit d7b012e560)
This commit is contained in:
Anu Engineer 2018-10-19 11:40:11 -07:00
parent 69cf9e66e2
commit 6417a51192
2 changed files with 53 additions and 12 deletions

View File

@ -22,14 +22,14 @@ import javax.ws.rs.container.ContainerRequestContext;
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 class VirtualHostStyleFilter implements ContainerRequestFilter {
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<String, String> 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);
}
}

View File

@ -31,6 +31,7 @@ import javax.ws.rs.core.HttpHeaders;
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 class TestVirtualHostStyleFilter {
* @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 class TestVirtualHostStyleFilter {
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 class TestVirtualHostStyleFilter {
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 class TestVirtualHostStyleFilter {
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 class TestVirtualHostStyleFilter {
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 class TestVirtualHostStyleFilter {
virtualHostStyleFilter.setConfiguration(conf);
ContainerRequest containerRequest = createContainerRequest("mybucket" +
"localhost:9878", null, true);
"localhost:9878", null, null, true);
try {
virtualHostStyleFilter.filter(containerRequest);
fail("testIncorrectVirtualHostStyle failed");