[java-support] branch master updated: Add support method for validating a servlet request's Content-Type.
Brent Putman
putmanb at georgetown.edu
Wed Oct 19 22:09:04 EDT 2016
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch master
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=786264b69f220271f7e544772ad77bc8281b1ef3
The following commit(s) were added to refs/heads/master by this push:
new 786264b Add support method for validating a servlet request's Content-Type.
786264b is described below
commit 786264b69f220271f7e544772ad77bc8281b1ef3
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Wed Oct 19 22:09:02 2016 -0400
Add support method for validating a servlet request's Content-Type.
---
.../java/support/net/HttpServletSupport.java | 67 ++++++++++++++++-
.../net/StripMediaTypeParametersFunction.java | 37 +++++++++
.../java/support/net/HttpServletSupportTest.java | 87 ++++++++++++++++++++++
3 files changed, 189 insertions(+), 2 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java b/src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java
index 4b48c60..93266a0 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java
@@ -18,17 +18,26 @@
package net.shibboleth.utilities.java.support.net;
import java.net.URI;
+import java.util.HashSet;
+import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import net.shibboleth.utilities.java.support.primitive.StringSupport;
-
import com.google.common.annotations.Beta;
+import com.google.common.base.Function;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Collections2;
+import com.google.common.net.MediaType;
+
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
/** Utilities for working with HTTP Servlet requests and responses. */
@Beta
public final class HttpServletSupport {
+
+ /** Function to strip MediaType parameters. */
+ private static final Function<MediaType, MediaType> STRIP_PARAMS = new StripMediaTypeParametersFunction();
/** Constructor. */
private HttpServletSupport() {
@@ -99,4 +108,58 @@ public final class HttpServletSupport {
return URI.create(requestUrl.toString());
}
+
+ /**
+ * Validate the Content-Type of the specified request.
+ *
+ * <p>
+ * 2 strategies are supported for evaluating the request's parsed content type:
+ * <ol>
+ * <li>
+ * If isOneOfStrategy is true, then the {@link MediaType} parsed from the request is compared to each
+ * of the specified valid types via {@link MediaType#is(MediaType)}. If any pass, the type is considered
+ * valid. This allows use of MediaType's support for wildcard and parameter evaluation.
+ * </li>
+ * <li>
+ * If isOneOfStrategy is false, then the {@link MediaType} parsed from the request is stripped
+ * of its parameters, as is each of the valid types. Then a simple evaluation is done that the
+ * request type is equal to one of the passed types. In this case, only literal types and subtypes
+ * should be passed as valid types; wildcards should not be used.
+ * </li>
+ * </ol>
+ * </p>
+ *
+ * @param request the request to be validated
+ * @param validTypes the set of valid media types
+ * @param noContentTypeIsValid flag whether the case of a missing/empty Content-Type header is considered valid
+ * @param isOneOfStrategy flag for the strategy used in the validation (see above for details)
+ * @return true if the content type is valid, false if not
+ */
+ public static boolean validateContentType(final HttpServletRequest request, final Set<MediaType> validTypes,
+ final boolean noContentTypeIsValid, final boolean isOneOfStrategy) {
+
+ final String contentType = StringSupport.trimOrNull(request.getContentType());
+ if (contentType != null) {
+ if (isOneOfStrategy) {
+ final MediaType mediaType = MediaType.parse(contentType);
+ for (final MediaType validType : validTypes) {
+ if (mediaType.is(validType)) {
+ return true;
+ }
+ }
+ return false;
+ } else {
+ final MediaType mediaType = MediaType.parse(contentType).withoutParameters();
+ final Set<MediaType> validTypesWithoutParameters = new HashSet<>();
+ validTypesWithoutParameters.addAll(Collections2.filter(
+ Collections2.transform(validTypes, STRIP_PARAMS),
+ Predicates.notNull()));
+ return validTypesWithoutParameters.contains(mediaType);
+ }
+ } else {
+ return noContentTypeIsValid;
+ }
+
+ }
+
}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/utilities/java/support/net/StripMediaTypeParametersFunction.java b/src/main/java/net/shibboleth/utilities/java/support/net/StripMediaTypeParametersFunction.java
new file mode 100644
index 0000000..c9e3ad5
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/net/StripMediaTypeParametersFunction.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You 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 net.shibboleth.utilities.java.support.net;
+
+import com.google.common.base.Function;
+import com.google.common.net.MediaType;
+
+/**
+ * Simple function to strip the parameters from a {@link MediaType}.
+ */
+public class StripMediaTypeParametersFunction implements Function<MediaType, MediaType> {
+
+ /** {@inheritDoc} */
+ public MediaType apply(final MediaType input) {
+ if (input == null) {
+ return null;
+ } else {
+ return input.withoutParameters();
+ }
+ }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/net/HttpServletSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/net/HttpServletSupportTest.java
index 6d7e227..e291838 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/net/HttpServletSupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/net/HttpServletSupportTest.java
@@ -17,10 +17,14 @@
package net.shibboleth.utilities.java.support.net;
+import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.testng.Assert;
import org.testng.annotations.Test;
+import com.google.common.collect.Sets;
+import com.google.common.net.MediaType;
+
/** {@link HttpServletSupport} unit test. */
public class HttpServletSupportTest {
@@ -61,4 +65,87 @@ public class HttpServletSupportTest {
@Test public void testSetUTF8Encoding(){
}
+
+ @Test public void testValidateContentType() {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+
+ // No Content-type
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8),
+ true,
+ false));
+
+ Assert.assertFalse(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8),
+ false,
+ false));
+
+ // With charset parameter
+ request.setContentType("text/xml; charset=utf-8");
+
+ Assert.assertFalse(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8.withoutParameters(), MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.ANY_TEXT_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.ANY_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ // No parameters
+ request.setContentType("text/xml");
+
+ Assert.assertFalse(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ // Not valid, because the text/xml valid type includes parameters
+ Assert.assertFalse(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.XML_UTF_8.withoutParameters(), MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.ANY_TEXT_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(HttpServletSupport.validateContentType(request,
+ Sets.newHashSet(MediaType.ANY_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ }
}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list