You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/07/09 11:51:21 UTC

[40/41] git commit: - contributed JAX-RS error handling from our own project to Marmotta

- contributed JAX-RS error handling from our own project to Marmotta


Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/75e6832e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/75e6832e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/75e6832e

Branch: refs/heads/develop
Commit: 75e6832e35b3c650fbb5f1956e8929b18659e63c
Parents: 00f64a0
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Tue Jul 9 11:46:21 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Tue Jul 9 11:46:21 2013 +0200

----------------------------------------------------------------------
 .../core/api/jaxrs/ExceptionMapperService.java  |  28 +++++
 .../platform/core/jaxrs/CDIExceptionMapper.java |  11 ++
 .../platform/core/jaxrs/ErrorResponse.java      | 116 +++++++++++++++++++
 .../core/jaxrs/IllegalStateExceptionMapper.java |  30 +++++
 .../core/jaxrs/MarmottaExceptionMapper.java     |  34 ++++++
 .../jaxrs/MarmottaImportExceptionMapper.java    |  32 +++++
 .../core/jaxrs/RepositoryExceptionMapper.java   |  33 ++++++
 .../core/jaxrs/URISyntaxExceptionMapper.java    |  29 +++++
 .../UnsupportedOperationExceptionMapper.java    |  30 +++++
 .../jaxrs/ExceptionMapperServiceImpl.java       |  52 +++++++++
 10 files changed, 395 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jaxrs/ExceptionMapperService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jaxrs/ExceptionMapperService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jaxrs/ExceptionMapperService.java
new file mode 100644
index 0000000..d5a9cf4
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jaxrs/ExceptionMapperService.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.marmotta.platform.core.api.jaxrs;
+
+/**
+ * This service auto-registers JAX-RS exception mappers implementing the CDIExceptionMapper interface and
+ * registers them with RESTEasy. This allows applications based on Marmotta to easily implement and register their
+ * own ExceptionMapppers without needing to go into RESTEasy.
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public interface ExceptionMapperService {
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/CDIExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/CDIExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/CDIExceptionMapper.java
new file mode 100644
index 0000000..7d7bf4f
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/CDIExceptionMapper.java
@@ -0,0 +1,11 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import javax.ws.rs.ext.ExceptionMapper;
+
+/**
+ * A marker-interface to allow CDI injection of ExceptionMappers implementing this interface by the ExceptionMapperService.
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public interface CDIExceptionMapper<E extends Throwable> extends ExceptionMapper<E> {
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/ErrorResponse.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/ErrorResponse.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/ErrorResponse.java
new file mode 100644
index 0000000..e2490d0
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/ErrorResponse.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.marmotta.platform.core.jaxrs;
+
+import javax.ws.rs.core.Response;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * Represents a uniform error response for REST service requests.
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@XmlRootElement(name = "Error")
+public class ErrorResponse {
+
+    private int code;
+
+    private String message;
+
+    private String stackTrace;
+
+    public ErrorResponse() {
+    }
+
+    public ErrorResponse(int code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    public ErrorResponse(int code, String message, String stackTrace) {
+        this.code = code;
+        this.message = message;
+        this.stackTrace = stackTrace;
+    }
+
+    public ErrorResponse(int code, Exception ex) {
+        this.code = code;
+        this.message = ex.getMessage();
+
+        StringWriter writer = new StringWriter();
+        ex.printStackTrace(new PrintWriter(writer));
+
+        this.stackTrace = writer.toString();
+    }
+
+
+    @XmlElement(name = "code", required = true)
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    @XmlElement(name = "message", required = true)
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    @XmlElement(name = "stackTrace", required = false)
+    public String getStackTrace() {
+        return stackTrace;
+    }
+
+    public void setStackTrace(String stackTrace) {
+        this.stackTrace = stackTrace;
+    }
+
+    public static Response errorResponse(Response.Status status, String message) {
+        ErrorResponse entity = new ErrorResponse(status.getStatusCode(), message);
+        return Response.status(status).entity(entity).build();
+    }
+
+    public static Response errorResponse(Response.Status status, Exception ex) {
+        ErrorResponse entity = new ErrorResponse(status.getStatusCode(), ex);
+        return Response.status(status).entity(entity).build();
+    }
+
+    public String toString() {
+        StringBuffer b = new StringBuffer();
+        b.append("Code: ");
+        b.append(code);
+        b.append(System.getProperty("line.separator"));
+        b.append("Message: ");
+        b.append(message);
+        if(stackTrace != null) {
+            b.append(System.getProperty("line.separator"));
+            b.append("StackTrace: ");
+            b.append(stackTrace);
+        }
+        return b.toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/IllegalStateExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/IllegalStateExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/IllegalStateExceptionMapper.java
new file mode 100644
index 0000000..983d07e
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/IllegalStateExceptionMapper.java
@@ -0,0 +1,30 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Map MarmottaExceptions to a internal server error and return the default error object
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@Provider
+@ApplicationScoped
+public class IllegalStateExceptionMapper implements CDIExceptionMapper<IllegalStateException> {
+
+    /**
+     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
+     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
+     * response. Throwing a runtime exception results in a
+     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response
+     *
+     * @param exception the exception to map to a response
+     * @return a response mapped from the supplied exception
+     */
+    @Override
+    public Response toResponse(IllegalStateException exception) {
+        return ErrorResponse.errorResponse(Response.Status.SERVICE_UNAVAILABLE, exception.getMessage());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaExceptionMapper.java
new file mode 100644
index 0000000..1dadc6e
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaExceptionMapper.java
@@ -0,0 +1,34 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.exception.MarmottaException;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Map MarmottaExceptions to a internal server error and return the default error object
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@Provider
+@ApplicationScoped
+public class MarmottaExceptionMapper implements CDIExceptionMapper<MarmottaException> {
+
+    /**
+     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
+     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
+     * response. Throwing a runtime exception results in a
+     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response
+     *
+     * @param exception the exception to map to a response
+     * @return a response mapped from the supplied exception
+     */
+    @Override
+    public Response toResponse(MarmottaException exception) {
+        return ErrorResponse.errorResponse(Response.Status.INTERNAL_SERVER_ERROR, exception);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaImportExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaImportExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaImportExceptionMapper.java
new file mode 100644
index 0000000..2bda331
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/MarmottaImportExceptionMapper.java
@@ -0,0 +1,32 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import org.apache.marmotta.platform.core.exception.io.MarmottaImportException;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Map MarmottaExceptions to a internal server error and return the default error object
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@Provider
+@ApplicationScoped
+public class MarmottaImportExceptionMapper implements CDIExceptionMapper<MarmottaImportException> {
+
+    /**
+     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
+     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
+     * response. Throwing a runtime exception results in a
+     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response
+     *
+     * @param exception the exception to map to a response
+     * @return a response mapped from the supplied exception
+     */
+    @Override
+    public Response toResponse(MarmottaImportException exception) {
+        return ErrorResponse.errorResponse(Response.Status.INTERNAL_SERVER_ERROR, exception);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/RepositoryExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/RepositoryExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/RepositoryExceptionMapper.java
new file mode 100644
index 0000000..80dfd71
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/RepositoryExceptionMapper.java
@@ -0,0 +1,33 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import org.openrdf.repository.RepositoryException;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Map RepositoryExceptions to a internal server error and return the default error object
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@Provider
+@ApplicationScoped
+public class RepositoryExceptionMapper implements CDIExceptionMapper<RepositoryException> {
+
+
+    /**
+     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
+     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
+     * response. Throwing a runtime exception results in a
+     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response
+     *
+     * @param exception the exception to map to a response
+     * @return a response mapped from the supplied exception
+     */
+    @Override
+    public Response toResponse(RepositoryException exception) {
+        return ErrorResponse.errorResponse(Response.Status.INTERNAL_SERVER_ERROR, exception);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/URISyntaxExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/URISyntaxExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/URISyntaxExceptionMapper.java
new file mode 100644
index 0000000..858ccaa
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/URISyntaxExceptionMapper.java
@@ -0,0 +1,29 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import java.net.URISyntaxException;
+
+/**
+ * Map MarmottaExceptions to a internal server error and return the default error object
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@Provider
+public class URISyntaxExceptionMapper implements CDIExceptionMapper<URISyntaxException> {
+
+    /**
+     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
+     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
+     * response. Throwing a runtime exception results in a
+     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response
+     *
+     * @param exception the exception to map to a response
+     * @return a response mapped from the supplied exception
+     */
+    @Override
+    public Response toResponse(URISyntaxException exception) {
+        return ErrorResponse.errorResponse(Response.Status.INTERNAL_SERVER_ERROR, exception);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/UnsupportedOperationExceptionMapper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/UnsupportedOperationExceptionMapper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/UnsupportedOperationExceptionMapper.java
new file mode 100644
index 0000000..e62e5b5
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/jaxrs/UnsupportedOperationExceptionMapper.java
@@ -0,0 +1,30 @@
+package org.apache.marmotta.platform.core.jaxrs;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Map MarmottaExceptions to a internal server error and return the default error object
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@Provider
+@ApplicationScoped
+public class UnsupportedOperationExceptionMapper implements CDIExceptionMapper<UnsupportedOperationException> {
+
+    /**
+     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
+     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
+     * response. Throwing a runtime exception results in a
+     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response
+     *
+     * @param exception the exception to map to a response
+     * @return a response mapped from the supplied exception
+     */
+    @Override
+    public Response toResponse(UnsupportedOperationException exception) {
+        return ErrorResponse.errorResponse(Response.Status.fromStatusCode(501), exception.getMessage());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/75e6832e/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jaxrs/ExceptionMapperServiceImpl.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jaxrs/ExceptionMapperServiceImpl.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jaxrs/ExceptionMapperServiceImpl.java
new file mode 100644
index 0000000..88c3bf2
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jaxrs/ExceptionMapperServiceImpl.java
@@ -0,0 +1,52 @@
+package org.apache.marmotta.platform.core.services.jaxrs;
+
+import org.apache.marmotta.platform.core.api.content.ContentReader;
+import org.apache.marmotta.platform.core.api.jaxrs.ExceptionMapperService;
+import org.apache.marmotta.platform.core.events.ConfigurationServiceInitEvent;
+import org.apache.marmotta.platform.core.jaxrs.CDIExceptionMapper;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
+import org.slf4j.Logger;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+/**
+ * Add file description here!
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+@ApplicationScoped
+public class ExceptionMapperServiceImpl implements ExceptionMapperService {
+
+    @Inject
+    private Logger log;
+
+    @Inject
+    private Instance<CDIExceptionMapper> mappers;
+
+    /**
+     * Register Exception Mappers
+     */
+    @PostConstruct
+    public void initialise() {
+        log.info("initialising JAX-RS exception mappers");
+
+        ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
+
+        for(CDIExceptionMapper mapper : mappers) {
+            log.debug("registering exception mapper: {}", mapper.getClass().getName());
+
+            factory.registerProviderInstance(mapper);
+        }
+    }
+
+    // trigger startup once configuration service is finished with initialisation
+    public void initEvent(@Observes ConfigurationServiceInitEvent e) {
+
+    }
+
+
+}