Repository: marmotta Updated Branches: refs/heads/develop 85b93523d -> 477a53866
MARMOTTA-562: built common resource errors with the requested format Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/477a5386 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/477a5386 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/477a5386 Branch: refs/heads/develop Commit: 477a538666a42f7d922932f320b689b25a9f3097 Parents: 85b9352 Author: Sergio Fernández <[email protected]> Authored: Wed Nov 5 16:41:58 2014 +0100 Committer: Sergio Fernández <[email protected]> Committed: Wed Nov 5 16:50:21 2014 +0100 ---------------------------------------------------------------------- .../platform/core/exception/HttpErrorException.java | 13 +++++++++++++ .../core/webservices/resource/MetaWebService.java | 10 +++++----- .../core/webservices/resource/ResourceWebService.java | 3 ++- .../core/test/jaxrs/ExceptionHandlingTest.java | 8 +++----- 4 files changed, 23 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/477a5386/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/exception/HttpErrorException.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/exception/HttpErrorException.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/exception/HttpErrorException.java index aba26cd..dc69f8b 100644 --- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/exception/HttpErrorException.java +++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/exception/HttpErrorException.java @@ -17,8 +17,10 @@ */ package org.apache.marmotta.platform.core.exception; +import com.google.common.collect.ImmutableMap; import edu.emory.mathcs.backport.java.util.Collections; +import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Response; import java.util.HashMap; import java.util.Map; @@ -77,6 +79,17 @@ public class HttpErrorException extends Exception { * Constructs an instance with the specified details * * @param status http status code + * @param request http servlet request + * @param e exception + */ + public HttpErrorException(Response.Status status, HttpServletRequest request, Exception e) { + this(status.getStatusCode(), status.getReasonPhrase(), request.getRequestURI(), e.getMessage(), ImmutableMap.of("Accept", request.getHeader("Accept"))); + } + + /** + * Constructs an instance with the specified details + * + * @param status http status code * @param reason reason phrase * @param uri resource uri * @param msg message http://git-wip-us.apache.org/repos/asf/marmotta/blob/477a5386/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java index e599e9a..019b69d 100644 --- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java +++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java @@ -240,7 +240,7 @@ public class MetaWebService { } if (r == null || !ResourceUtils.isUsed(conn, r)) { - throw new HttpErrorException(Response.Status.NOT_FOUND, resource, "the requested resource could not be found in Marmotta right now, but may be available again in the future"); + throw new HttpErrorException(Response.Status.NOT_FOUND, resource, "the requested resource could not be found in Marmotta right now, but may be available again in the future", ImmutableMap.of("Accept", mimetype)); } // create parser @@ -331,7 +331,7 @@ public class MetaWebService { return response; } if (request.getContentLength() == 0) { - throw new HttpErrorException(Status.BAD_REQUEST, uri, "content may not be empty in resource update"); + throw new HttpErrorException(Status.BAD_REQUEST, uri, "content may not be empty in resource update", ImmutableMap.of("Accept", mimetype)); } // a intercepting connection that filters out all triples that have @@ -355,11 +355,11 @@ public class MetaWebService { } return Response.ok().build(); } catch (URISyntaxException e) { - throw new HttpErrorException(Status.INTERNAL_SERVER_ERROR, uri, "invalid target context"); + throw new HttpErrorException(Status.INTERNAL_SERVER_ERROR, uri, "invalid target context", ImmutableMap.of("Accept", mimetype)); } catch (IOException | RDFParseException e) { - throw new HttpErrorException(Status.NOT_ACCEPTABLE, uri, "could not parse request body"); + throw new HttpErrorException(Status.NOT_ACCEPTABLE, uri, "could not parse request body", ImmutableMap.of("Accept", mimetype)); } catch (RepositoryException e) { - throw new HttpErrorException(Status.INTERNAL_SERVER_ERROR, uri, e.getMessage()); + throw new HttpErrorException(Status.INTERNAL_SERVER_ERROR, request, e); } } http://git-wip-us.apache.org/repos/asf/marmotta/blob/477a5386/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java index 77456a1..991c1f2 100644 --- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java +++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java @@ -17,6 +17,7 @@ */ package org.apache.marmotta.platform.core.webservices.resource; +import com.google.common.collect.ImmutableMap; import org.apache.commons.lang3.StringUtils; import org.apache.marmotta.commons.http.ContentType; import org.apache.marmotta.commons.http.ETagGenerator; @@ -373,7 +374,7 @@ public class ResourceWebService { } } if (r == null) { - throw new HttpErrorException(Status.NOT_FOUND, resource, "the requested resource could not be found in Marmotta right now, but may be available again in the future"); + throw new HttpErrorException(Status.NOT_FOUND, resource, "the requested resource could not be found in Marmotta right now, but may be available again in the future", ImmutableMap.of("Accept", types)); } // FIXME String appendix = uuid == null ? "?uri=" + URLEncoder.encode(uri, "utf-8") : // "/" + uuid; http://git-wip-us.apache.org/repos/asf/marmotta/blob/477a5386/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/jaxrs/ExceptionHandlingTest.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/jaxrs/ExceptionHandlingTest.java b/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/jaxrs/ExceptionHandlingTest.java index d3a0f5b..e34a40f 100644 --- a/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/jaxrs/ExceptionHandlingTest.java +++ b/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/jaxrs/ExceptionHandlingTest.java @@ -27,7 +27,7 @@ public class ExceptionHandlingTest { private static JettyMarmotta marmotta; @BeforeClass - public static void setUp() throws RepositoryException, IOException, RDFParseException { + public static void setUp() { marmotta = new JettyMarmotta("/marmotta", ResourceWebService.class); RestAssured.baseURI = "http://localhost"; @@ -41,10 +41,9 @@ public class ExceptionHandlingTest { } @Test - public void testNotFound() throws MarmottaException, IOException { + public void testNotFound() { final ResponseBody response = expect(). - //log().ifError(). statusCode(404). contentType("text/html"). given(). @@ -52,10 +51,9 @@ public class ExceptionHandlingTest { when(). get(ConfigurationService.RESOURCE_PATH + "/foo"). getBody(); - response.print(); + //response.print(); final ResponseBody responseJson = expect(). - //log().ifError(). statusCode(404). contentType("application/json"). given().
