Skip to content

Commit

Permalink
Returning 400 Bad Request error for unparseable JSON, adding ITs for …
Browse files Browse the repository at this point in the history
…parsing and mime type errors
  • Loading branch information
escowles committed May 21, 2014
1 parent ef798d9 commit a1c0e81
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
Expand Up @@ -340,11 +340,14 @@ protected int getStatus(final HttpUriRequest method)



protected int postRoles(final String path, final String json_roles)
throws ParseException, IOException {
protected int postRoles(final String path, final String json_roles ) throws ParseException, IOException {
return postRoles(path, json_roles, "application/json");
}
protected int postRoles(final String path, final String json_roles, final String contentType )
throws ParseException, IOException {
final HttpPost method = postRolesMethod(path);
setAuth(method, "fedoraAdmin");
method.addHeader("Content-Type", "application/json");
method.addHeader("Content-Type", contentType);
final StringEntity entity = new StringEntity(json_roles, "utf-8");
method.setEntity(entity);
final HttpResponse response = client.execute(method);
Expand Down
Expand Up @@ -15,9 +15,11 @@
*/
package org.fcrepo.auth.roles.common.integration;

import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.Status.OK;
import static javax.ws.rs.core.Response.Status.UNSUPPORTED_MEDIA_TYPE;
import static org.junit.Assert.assertEquals;

import java.io.IOException;
Expand All @@ -30,6 +32,7 @@
/**
* @author Gregory Jansen
* @author Scott Prater
* @author Esme Cowles
*/
public class AccessRolesIT extends AbstractCommonRolesIT {

Expand Down Expand Up @@ -137,4 +140,15 @@ public void testGetEffectiveRoles() throws ClientProtocolException,
getEffectiveRoles("testcommonobj1/testchildobj1"));

}

@Test
public void testInvalidAccessRoles() throws Exception {
assertEquals(BAD_REQUEST.getStatusCode(), postRoles("testcommonobj1", "invalid roles"));
}

@Test
public void testInvalidMimeType() throws Exception {
assertEquals(UNSUPPORTED_MEDIA_TYPE.getStatusCode(), postRoles("testcommonobj1", test_json_roles,
"text/plain"));
}
}
@@ -0,0 +1,39 @@
/**
* Copyright 2014 DuraSpace, Inc.
*
* Licensed 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 org.fcrepo.http.commons.exceptionhandlers;

import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.JsonParseException;

/**
* If an injected JSON resource fails to parse, return an HTTP 400 Bad Request.
*
* @author Esme Cowles
* @since 2014-05-21
*/
@Provider
public class JsonParseExceptionMapper implements ExceptionMapper<JsonParseException> {

@Override
public Response toResponse(final JsonParseException ex) {
return status(BAD_REQUEST).entity(ex.getMessage()).build();
}
}
Expand Up @@ -27,6 +27,8 @@
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.JsonParseException;

import org.fcrepo.kernel.exception.TransactionMissingException;
import org.slf4j.Logger;

Expand Down Expand Up @@ -80,6 +82,10 @@ public Response toResponse(final Exception e) {
.toResponse((TransactionMissingException) e.getCause());
}

if (e instanceof JsonParseException) {
return new JsonParseExceptionMapper().toResponse((JsonParseException)e);
}

if ( e.getCause() instanceof RepositoryException) {
return new RepositoryExceptionMapper()
.toResponse((RepositoryException)e.getCause());
Expand Down

0 comments on commit a1c0e81

Please sign in to comment.