Skip to content
Snippets Groups Projects
Commit 25d74fab authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Added sane handling of invalid JSON input

parent 39f96795
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ public class VIPSCoreManagerApplication extends Application
private void addRestResourceClassesManually(Set<Class<?>> resources) {
//resources.add(no.bioforsk.vips.core.service.ModelResource.class);
resources.add(no.bioforsk.vips.coremanager.service.ManagerResourceImpl.class);
resources.add(no.bioforsk.vips.coremanager.service.JsonParseExceptionMapper.class);
}
/**
......@@ -37,6 +38,7 @@ public class VIPSCoreManagerApplication extends Application
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(no.bioforsk.vips.core.service.ModelResource.class);
resources.add(no.bioforsk.vips.coremanager.service.JsonParseExceptionMapper.class);
resources.add(no.bioforsk.vips.coremanager.service.ManagerResource.class);
resources.add(no.bioforsk.vips.coremanager.service.ManagerResourceImpl.class);
}
......
package no.bioforsk.vips.coremanager.service;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.JsonParseException;
/**
* Catches when user sends in invalid JSON that causes exception to be thrown
* in Jackson's JSON parser.
* @copyright 2013 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
@Provider
public class JsonParseExceptionMapper implements ExceptionMapper<JsonParseException>{
@Override
public Response toResponse(JsonParseException exception) {
String errorMessage = "Error with input: Invalid JSON. Details: " + exception.getMessage();
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build();
}
}
......@@ -34,6 +34,7 @@ import no.bioforsk.vips.entity.ModelConfiguration;
import no.bioforsk.vips.entity.Result;
import no.bioforsk.vips.model.Model;
import no.bioforsk.vips.util.ServletUtil;
import org.codehaus.jackson.JsonParseException;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
......@@ -220,6 +221,7 @@ public class ManagerResourceImpl implements ManagerResource{
@Path("models/{modelId}/run")
@Consumes("application/json")
@Produces("application/json")
@Override
public Response runModel(@PathParam("modelId") String modelId, Map<String,Object> input)
{
try
......@@ -274,9 +276,10 @@ public class ManagerResourceImpl implements ManagerResource{
ControllerGetter.getUserController().storeModelUsageLog(log);
return Response.ok().entity(result).build();
}
} catch(IOException e)
}
catch( IOException ioe)
{
throw new WebApplicationException(Response.serverError().entity(e).build());
throw new WebApplicationException(Response.serverError().entity(ioe).build());
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment