[java-support] branch master updated: Refactor Content-Type validation to new String-based support method.
Brent Putman
putmanb at georgetown.edu
Fri Oct 21 15:56:12 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=c6f887118313011b103140a2331c586fd06ba3d8
The following commit(s) were added to refs/heads/master by this push:
new c6f8871 Refactor Content-Type validation to new String-based support method.
c6f8871 is described below
commit c6f887118313011b103140a2331c586fd06ba3d8
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Oct 21 15:56:10 2016 -0400
Refactor Content-Type validation to new String-based support method.
---
.../java/support/net/HttpServletSupport.java | 31 +-----
...tpServletSupport.java => MediaTypeSupport.java} | 111 ++++----------------
.../java/support/net/MediaTypeSupportTest.java | 114 +++++++++++++++++++++
3 files changed, 136 insertions(+), 120 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 93266a0..ecf27e0 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,16 +18,12 @@
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 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;
@@ -35,9 +31,6 @@ 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() {
@@ -138,28 +131,8 @@ public final class HttpServletSupport {
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;
- }
-
+ return MediaTypeSupport.validateContentType(request.getContentType(), validTypes,
+ noContentTypeIsValid, isOneOfStrategy);
}
}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java b/src/main/java/net/shibboleth/utilities/java/support/net/MediaTypeSupport.java
similarity index 50%
copy from src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java
copy to src/main/java/net/shibboleth/utilities/java/support/net/MediaTypeSupport.java
index 93266a0..1c59b41 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/net/HttpServletSupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/net/MediaTypeSupport.java
@@ -17,14 +17,9 @@
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 com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
@@ -32,116 +27,50 @@ import com.google.common.net.MediaType;
import net.shibboleth.utilities.java.support.primitive.StringSupport;
-/** Utilities for working with HTTP Servlet requests and responses. */
- at Beta
-public final class HttpServletSupport {
+/**
+ * Support methods for Guava {@link MediaType}.
+ */
+public final class MediaTypeSupport {
/** Function to strip MediaType parameters. */
private static final Function<MediaType, MediaType> STRIP_PARAMS = new StripMediaTypeParametersFunction();
-
- /** Constructor. */
- private HttpServletSupport() {
- }
-
- /**
- * Adds Cache-Control and Pragma headers meant to disable caching.
- *
- * @param response transport to add headers to
- */
- public static void addNoCacheHeaders(final HttpServletResponse response) {
- response.setHeader("Cache-control", "no-cache, no-store");
- response.setHeader("Pragma", "no-cache");
- }
-
- /**
- * Sets the character encoding of the transport to UTF-8.
- *
- * @param response transport to set character encoding type
- */
- public static void setUTF8Encoding(final HttpServletResponse response) {
- response.setCharacterEncoding("UTF-8");
- }
-
- /**
- * Sets the MIME content type of the response.
- *
- * @param response the transport to set content type on
- * @param contentType the content type to set
- */
- public static void setContentType(final HttpServletResponse response, final String contentType) {
- response.setHeader("Content-Type", contentType);
- }
-
- /**
- * Gets the request URI as returned by {@link HttpServletRequest#getRequestURI()} but without the servlet context
- * path.
- *
- * @param request request to get the URI from
- *
- * @return constructed URI
- */
- public static String getRequestPathWithoutContext(final HttpServletRequest request) {
- final String servletPath = request.getServletPath();
-
- if (request.getPathInfo() == null) {
- return servletPath;
- } else {
- return servletPath + request.getPathInfo();
- }
- }
-
- /**
- * Gets the URL that was requested to generate this request. This includes the scheme, host, port, path, and query
- * string.
- *
- * @param request current request
- *
- * @return URL that was requested to generate this request
- */
- public static URI getFullRequestURI(final HttpServletRequest request) {
- final StringBuffer requestUrl = request.getRequestURL();
-
- final String encodedQuery = StringSupport.trimOrNull(request.getQueryString());
- if (encodedQuery != null) {
- requestUrl.append("?").append(encodedQuery);
- }
-
- return URI.create(requestUrl.toString());
- }
+
+ /** * Constructor. */
+ private MediaTypeSupport() { }
/**
- * Validate the Content-Type of the specified request.
+ * Validate the specified Content-Type.
*
* <p>
- * 2 strategies are supported for evaluating the request's parsed content type:
+ * 2 strategies are supported for evaluating a content type:
* <ol>
* <li>
- * If isOneOfStrategy is true, then the {@link MediaType} parsed from the request is compared to each
+ * If isOneOfStrategy is true, then the {@link MediaType} parsed from the content type value 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
+ * If isOneOfStrategy is false, then the {@link MediaType} parsed from the value 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
+ * specified content 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 contentType the contentType 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,
+ public static boolean validateContentType(final String contentType, final Set<MediaType> validTypes,
final boolean noContentTypeIsValid, final boolean isOneOfStrategy) {
- final String contentType = StringSupport.trimOrNull(request.getContentType());
- if (contentType != null) {
+ final String contentTypeValue = StringSupport.trimOrNull(contentType);
+ if (contentTypeValue != null) {
if (isOneOfStrategy) {
- final MediaType mediaType = MediaType.parse(contentType);
+ final MediaType mediaType = MediaType.parse(contentTypeValue);
for (final MediaType validType : validTypes) {
if (mediaType.is(validType)) {
return true;
@@ -149,7 +78,7 @@ public final class HttpServletSupport {
}
return false;
} else {
- final MediaType mediaType = MediaType.parse(contentType).withoutParameters();
+ final MediaType mediaType = MediaType.parse(contentTypeValue).withoutParameters();
final Set<MediaType> validTypesWithoutParameters = new HashSet<>();
validTypesWithoutParameters.addAll(Collections2.filter(
Collections2.transform(validTypes, STRIP_PARAMS),
@@ -161,5 +90,5 @@ public final class HttpServletSupport {
}
}
-
-}
\ No newline at end of file
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/net/MediaTypeSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/net/MediaTypeSupportTest.java
new file mode 100644
index 0000000..30cfa3a
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/net/MediaTypeSupportTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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 org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.Sets;
+import com.google.common.net.MediaType;
+
+/**
+ *
+ */
+public class MediaTypeSupportTest {
+
+ @Test public void testValidateContentType() {
+ String contentType = null;
+
+ // No Content-type
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8),
+ true,
+ false));
+
+ Assert.assertFalse(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8),
+ false,
+ false));
+
+ // With charset parameter
+ contentType = "text/xml; charset=utf-8";
+
+ Assert.assertFalse(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8.withoutParameters(), MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.ANY_TEXT_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.ANY_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ // No parameters
+ contentType = "text/xml";
+
+ Assert.assertFalse(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ false));
+
+ // Not valid, because the text/xml valid type includes parameters
+ Assert.assertFalse(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.XML_UTF_8.withoutParameters(), MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.ANY_TEXT_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ Assert.assertTrue(MediaTypeSupport.validateContentType(contentType,
+ Sets.newHashSet(MediaType.ANY_TYPE, MediaType.create("application", "foobar")),
+ true,
+ true));
+
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list