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

Contributed by Bharat Viswanadham.
This commit is contained in:
Anu Engineer 2018-10-19 11:40:11 -07:00
parent c456d6b3a5
commit d7b012e560
2 changed files with 53 additions and 12 deletions

View File

@ -22,14 +22,14 @@
import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching; import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.fs.InvalidRequestException; import org.apache.hadoop.fs.InvalidRequestException;
@ -95,11 +95,16 @@ public void filter(ContainerRequestContext requestContext) throws
URI baseURI = requestContext.getUriInfo().getBaseUri(); URI baseURI = requestContext.getUriInfo().getBaseUri();
String currentPath = requestContext.getUriInfo().getPath(); String currentPath = requestContext.getUriInfo().getPath();
String newPath = bucketName; String newPath = bucketName;
if (currentPath != null) { if (currentPath != null) {
newPath += String.format("%s", currentPath); 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); requestContext.setRequestUri(baseURI, requestAddr);
} }
} }

View File

@ -31,6 +31,7 @@
import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.SecurityContext;
import java.net.URI; import java.net.URI;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -55,16 +56,27 @@ public void setup() {
* @throws Exception * @throws Exception
*/ */
public ContainerRequest createContainerRequest(String host, String path, public ContainerRequest createContainerRequest(String host, String path,
String queryParams,
boolean virtualHostStyle) boolean virtualHostStyle)
throws Exception { throws Exception {
URI baseUri = new URI("http://" + s3HttpAddr); URI baseUri = new URI("http://" + s3HttpAddr);
URI virtualHostStyleUri; URI virtualHostStyleUri;
if (path == null) { if (path == null && queryParams == null) {
virtualHostStyleUri = new URI("http://" + s3HttpAddr); virtualHostStyleUri = new URI("http://" + s3HttpAddr);
} else { } else if (path != null && queryParams == null) {
virtualHostStyleUri = new URI("http://" + s3HttpAddr + path); 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"; String httpMethod = "DELETE";
SecurityContext securityContext = Mockito.mock(SecurityContext.class); SecurityContext securityContext = Mockito.mock(SecurityContext.class);
PropertiesDelegate propertiesDelegate = Mockito.mock(PropertiesDelegate PropertiesDelegate propertiesDelegate = Mockito.mock(PropertiesDelegate
@ -89,7 +101,7 @@ public void testVirtualHostStyle() throws Exception {
virtualHostStyleFilter.setConfiguration(conf); virtualHostStyleFilter.setConfiguration(conf);
ContainerRequest containerRequest = createContainerRequest("mybucket" + ContainerRequest containerRequest = createContainerRequest("mybucket" +
".localhost:9878", "/myfile", true); ".localhost:9878", "/myfile", null, true);
virtualHostStyleFilter.filter(containerRequest); virtualHostStyleFilter.filter(containerRequest);
URI expected = new URI("http://" + s3HttpAddr + URI expected = new URI("http://" + s3HttpAddr +
"/mybucket/myfile"); "/mybucket/myfile");
@ -104,7 +116,7 @@ public void testPathStyle() throws Exception {
virtualHostStyleFilter.setConfiguration(conf); virtualHostStyleFilter.setConfiguration(conf);
ContainerRequest containerRequest = createContainerRequest(s3HttpAddr, ContainerRequest containerRequest = createContainerRequest(s3HttpAddr,
"/mybucket/myfile", false); "/mybucket/myfile", null, false);
virtualHostStyleFilter.filter(containerRequest); virtualHostStyleFilter.filter(containerRequest);
URI expected = new URI("http://" + s3HttpAddr + URI expected = new URI("http://" + s3HttpAddr +
"/mybucket/myfile"); "/mybucket/myfile");
@ -120,13 +132,37 @@ public void testVirtualHostStyleWithCreateBucketRequest() throws Exception {
virtualHostStyleFilter.setConfiguration(conf); virtualHostStyleFilter.setConfiguration(conf);
ContainerRequest containerRequest = createContainerRequest("mybucket" + ContainerRequest containerRequest = createContainerRequest("mybucket" +
".localhost:9878", null, true); ".localhost:9878", null, null, true);
virtualHostStyleFilter.filter(containerRequest); virtualHostStyleFilter.filter(containerRequest);
URI expected = new URI("http://" + s3HttpAddr + "/mybucket"); URI expected = new URI("http://" + s3HttpAddr + "/mybucket");
Assert.assertEquals(expected, containerRequest.getRequestUri()); 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 @Test
public void testVirtualHostStyleWithNoMatchingDomain() throws Exception { public void testVirtualHostStyleWithNoMatchingDomain() throws Exception {
@ -135,7 +171,7 @@ public void testVirtualHostStyleWithNoMatchingDomain() throws Exception {
virtualHostStyleFilter.setConfiguration(conf); virtualHostStyleFilter.setConfiguration(conf);
ContainerRequest containerRequest = createContainerRequest("mybucket" + ContainerRequest containerRequest = createContainerRequest("mybucket" +
".localhost:9999", null, true); ".localhost:9999", null, null, true);
try { try {
virtualHostStyleFilter.filter(containerRequest); virtualHostStyleFilter.filter(containerRequest);
} catch (InvalidRequestException ex) { } catch (InvalidRequestException ex) {
@ -153,7 +189,7 @@ public void testIncorrectVirtualHostStyle() throws
virtualHostStyleFilter.setConfiguration(conf); virtualHostStyleFilter.setConfiguration(conf);
ContainerRequest containerRequest = createContainerRequest("mybucket" + ContainerRequest containerRequest = createContainerRequest("mybucket" +
"localhost:9878", null, true); "localhost:9878", null, null, true);
try { try {
virtualHostStyleFilter.filter(containerRequest); virtualHostStyleFilter.filter(containerRequest);
fail("testIncorrectVirtualHostStyle failed"); fail("testIncorrectVirtualHostStyle failed");