@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testStrictDisallowUnknown() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_JSON_CONTENT_TYPES, "text/plain"); }
public static void checkContentTypes(Set<String> allowedContentTypes, String contentType) throws InvalidContentTypeException { if (Strings.isNullOrEmpty(contentType)) { throw new InvalidContentTypeException( "No Content-Type specified. One of " + Joiner.on(", ").join(allowedContentTypes) + " is required"); } contentType = ContentTypes.extractMimePart(contentType); if (ContentTypes.FORBIDDEN_CONTENT_TYPES.contains(contentType)) { throw new InvalidContentTypeException( "Cannot use disallowed Content-Type " + contentType); } if (allowedContentTypes.contains(contentType)) { return; } throw new InvalidContentTypeException( "Unsupported Content-Type " + contentType + ". One of " + Joiner.on(", ").join(allowedContentTypes) + " is required"); }
@Test public void testAllowJsonRpc() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_JSON_CONTENT_TYPES, "application/json-rpc"); }
public static void checkContentTypes(Set<String> allowedContentTypes, String contentType) throws InvalidContentTypeException { if (Strings.isNullOrEmpty(contentType)) { throw new InvalidContentTypeException( "No Content-Type specified. One of " + Joiner.on(", ").join(allowedContentTypes) + " is required"); } contentType = ContentTypes.extractMimePart(contentType); if (ContentTypes.FORBIDDEN_CONTENT_TYPES.contains(contentType)) { throw new InvalidContentTypeException( "Cannot use disallowed Content-Type " + contentType); } if (allowedContentTypes.contains(contentType)) { return; } throw new InvalidContentTypeException( "Unsupported Content-Type " + contentType + ". One of " + Joiner.on(", ").join(allowedContentTypes) + " is required"); }
@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testForbidden() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_JSON_CONTENT_TYPES, "application/x-www-form-urlencoded"); }
public static void checkContentTypes(Set<String> allowedContentTypes, String contentType) throws InvalidContentTypeException { if (Strings.isNullOrEmpty(contentType)) { throw new InvalidContentTypeException( "No Content-Type specified. One of " + Joiner.on(", ").join(allowedContentTypes) + " is required"); } contentType = ContentTypes.extractMimePart(contentType); if (ContentTypes.FORBIDDEN_CONTENT_TYPES.contains(contentType)) { throw new InvalidContentTypeException( "Cannot use disallowed Content-Type " + contentType); } if (allowedContentTypes.contains(contentType)) { return; } throw new InvalidContentTypeException( "Unsupported Content-Type " + contentType + ". One of " + Joiner.on(", ").join(allowedContentTypes) + " is required"); }
@Override protected void doPut(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { try { ContentTypes.checkContentTypes(ALLOWED_CONTENT_TYPES, servletRequest.getContentType()); executeRequest(servletRequest, servletResponse); } catch (ContentTypes.InvalidContentTypeException icte) { sendError(servletResponse, new ResponseItem(HttpServletResponse.SC_BAD_REQUEST, icte.getMessage())); } }
public static void checkContentTypes(Set<String> allowedContentTypes, String contentType) throws InvalidContentTypeException { if (StringUtils.isEmpty(contentType)) { throw new InvalidContentTypeException( "No Content-Type specified. One of " + StringUtils.join(allowedContentTypes, ", ") + " is required"); } contentType = ContentTypes.extractMimePart(contentType); if (ContentTypes.FORBIDDEN_CONTENT_TYPES.contains(contentType)) { throw new InvalidContentTypeException( "Cannot use disallowed Content-Type " + contentType); } if (allowedContentTypes.contains(contentType)) { return; } throw new InvalidContentTypeException( "Unsupported Content-Type " + contentType + ". One of " + StringUtils.join(allowedContentTypes, ", ") + " is required"); }
@Test public void testAllowJsonRpc() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_JSON_CONTENT_TYPES, "application/json-rpc"); }
public static void checkContentTypes(Set<String> allowedContentTypes, String contentType) throws InvalidContentTypeException { if (StringUtils.isEmpty(contentType)) { throw new InvalidContentTypeException( "No Content-Type specified. One of " + StringUtils.join(allowedContentTypes, ", ") + " is required"); } contentType = ContentTypes.extractMimePart(contentType); if (ContentTypes.FORBIDDEN_CONTENT_TYPES.contains(contentType)) { throw new InvalidContentTypeException( "Cannot use disallowed Content-Type " + contentType); } if (allowedContentTypes.contains(contentType)) { return; } throw new InvalidContentTypeException( "Unsupported Content-Type " + contentType + ". One of " + StringUtils.join(allowedContentTypes, ", ") + " is required"); }
@Test public void testAllowMultipart() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_MULTIPART_CONTENT_TYPES, ContentTypes.MULTIPART_FORM_CONTENT_TYPE); }
@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testExtractMimePartEmptyString() throws Exception { ContentTypes.extractMimePart(""); } }
@Test public void testAllowJsonRpc() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_JSON_CONTENT_TYPES, "application/json-rpc"); }
@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testExtractMimePartEmptyString() throws Exception { ContentTypes.extractMimePart(""); } }
@Override protected void doPut(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { try { ContentTypes.checkContentTypes(ALLOWED_CONTENT_TYPES, servletRequest.getContentType()); executeRequest(servletRequest, servletResponse); } catch (ContentTypes.InvalidContentTypeException icte) { sendError(servletResponse, new ResponseItem(HttpServletResponse.SC_BAD_REQUEST, icte.getMessage())); } }
@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testExtractMimePartNull() throws Exception { ContentTypes.extractMimePart(null); }
@Test public void testAllowJson() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_JSON_CONTENT_TYPES, ContentTypes.OUTPUT_JSON_CONTENT_TYPE); }
@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testExtractMimePartEmptyString() throws Exception { ContentTypes.extractMimePart(""); } }
@Test public void testAllowXml() throws Exception { ContentTypes.checkContentTypes(ContentTypes.ALLOWED_XML_CONTENT_TYPES, ContentTypes.OUTPUT_XML_CONTENT_TYPE); }
@Test(expected=ContentTypes.InvalidContentTypeException.class) public void testExtractMimePartNull() throws Exception { ContentTypes.extractMimePart(null); }