changeset 40:af22d4de275c

Log RuntimeExceptions in REST calls to log4j. artifacts/trunk@112 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Sep 2009 08:27:35 +0000
parents 075cb3778d71
children 5e4bc24ea438
files Changelog artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactOutResource.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseResource.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java
diffstat 6 files changed, 103 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/Changelog	Mon Sep 21 11:56:10 2009 +0000
+++ b/Changelog	Wed Sep 23 08:27:35 2009 +0000
@@ -1,5 +1,18 @@
-2009-09-21  Tim Englich  <tim.englich@intevation.de>
-   * Added some IDE-specific files to the Ignorelist.
+2009-09-23	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseResource.java:
+	New. Base class to log RuntimeExceptions to log4j error log.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java,
+	artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java,
+	artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java,
+	artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactOutResource.java:
+	Adjusted to extend from BaseResource.
+
+2009-09-21	Tim Englich	<tim.englich@intevation.de>
+
+	* Added some IDE-specific files to the Ignorelist.
+
 2009-09-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/doc/schema.sql: removed.
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactOutResource.java	Mon Sep 21 11:56:10 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactOutResource.java	Wed Sep 23 08:27:35 2009 +0000
@@ -2,9 +2,7 @@
 
 import org.apache.log4j.Logger;
 
-import org.restlet.resource.Get;  
-import org.restlet.resource.Post;
-import org.restlet.resource.ServerResource;
+import org.restlet.resource.ResourceException;
 
 import org.restlet.data.Request;
 import org.restlet.data.Response;
@@ -30,7 +28,7 @@
  * @author Sascha L. Teichmann (sascha.teichmann@intevation)
  */
 public class ArtifactOutResource
-extends      ServerResource
+extends      BaseResource
 {
     public static final String PATH = "/artifact/{uuid}/{type}";
 
@@ -41,13 +39,9 @@
 
     private static Logger logger = Logger.getLogger(ArtifactOutResource.class);
 
-    @Post
-    public Representation represent() {
-
-        Request request = getRequest();
-
-        Representation requestRepr = request.getEntity();
-
+    protected Representation innerPost(Representation requestRepr)
+    throws    ResourceException
+    {
         Document inputDocument = null;
         try {
             DomRepresentation input = new DomRepresentation(requestRepr);
@@ -63,6 +57,8 @@
         ArtifactDatabase db = (ArtifactDatabase)getContext()
             .getAttributes().get("database");
 
+        Request request = getRequest();
+
         String identifier = (String)request.getAttributes().get("uuid");
 
         if (logger.isDebugEnabled()) {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java	Mon Sep 21 11:56:10 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java	Wed Sep 23 08:27:35 2009 +0000
@@ -1,8 +1,6 @@
 package de.intevation.artifactdatabase.rest;
 
-import org.restlet.resource.Get;  
-import org.restlet.resource.Post;  
-import org.restlet.resource.ServerResource;
+import org.restlet.resource.ResourceException;
 
 import org.restlet.representation.Representation;
 import org.restlet.representation.EmptyRepresentation;
@@ -31,7 +29,7 @@
  * @author Sascha L. Teichmann (sascha.teichmann@intevation)
  */
 public class ArtifactResource
-extends      ServerResource
+extends      BaseResource
 {
     private static Logger logger = Logger.getLogger(ArtifactResource.class);
 
@@ -47,9 +45,9 @@
     public static final String ADVANCE = "advance";
     public static final String FEED    = "feed";
 
-    @Get
-    public Representation represent() {
-
+    protected Representation innerGet()
+    throws                   ResourceException
+    {
         Request request = getRequest();
 
         String identifier = (String)request.getAttributes().get("uuid");
@@ -105,11 +103,7 @@
         return new DomRepresentation(MediaType.APPLICATION_XML, out);
     }
 
-    @Post
-    public Representation representPost() {
-        Request request = getRequest();
-
-        Representation requestRepr = request.getEntity();
+    protected Representation innerPost(Representation requestRepr) {
 
         Document inputDocument = null;
         try {
@@ -135,6 +129,8 @@
             return new EmptyRepresentation();
         }
 
+        Request request = getRequest();
+
         String identifier = (String)request.getAttributes().get("uuid");
 
         ArtifactDatabase db = (ArtifactDatabase)getContext()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseResource.java	Wed Sep 23 08:27:35 2009 +0000
@@ -0,0 +1,63 @@
+package de.intevation.artifactdatabase.rest;
+
+import org.apache.log4j.Logger;
+
+import org.restlet.resource.ServerResource;
+import org.restlet.resource.ResourceException;
+
+import org.restlet.representation.Representation;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation)
+ */
+public class BaseResource
+extends      ServerResource
+{
+    private static Logger logger = Logger.getLogger(BaseResource.class);
+
+    public BaseResource() {
+    }
+
+    protected Representation post(Representation requestRepr)
+    throws    ResourceException
+    {
+        try {
+            return innerPost(requestRepr);
+        }
+        catch (ResourceException re) {
+            throw re;
+        }
+        catch (RuntimeException re) {
+            logger.error(re.getLocalizedMessage(), re);
+            throw re;
+        }
+    }
+
+    protected Representation innerPost(Representation requestRepr)
+    throws    ResourceException
+    {
+        return super.post(requestRepr);
+    }
+
+    protected Representation get() 
+    throws    ResourceException
+    {
+        try {
+            return innerGet();
+        }
+        catch (ResourceException re) {
+            throw re;
+        }
+        catch (RuntimeException re) {
+            logger.error(re.getLocalizedMessage(), re);
+            throw re;
+        }
+    }
+
+    protected Representation innerGet()
+    throws    ResourceException
+    {
+        return super.get();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java	Mon Sep 21 11:56:10 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java	Wed Sep 23 08:27:35 2009 +0000
@@ -4,10 +4,8 @@
 
 import org.restlet.ext.xml.DomRepresentation;
 
-import org.restlet.resource.Post;  
-import org.restlet.resource.ServerResource;
+import org.restlet.resource.ResourceException;
 
-import org.restlet.data.Request;
 import org.restlet.data.Response;
 import org.restlet.data.Status;
 
@@ -30,7 +28,7 @@
  * @author Sascha L. Teichmann (sascha.teichmann@intevation)
  */
 public class CreateResource
-extends      ServerResource
+extends      BaseResource
 {
     private static Logger logger = Logger.getLogger(CreateResource.class);
 
@@ -42,13 +40,9 @@
 
     public static final String NO_ARTIFACT_CREATED = "No artifact created";
 
-    @Post
-    public Representation represent() {
-
-        Request request = getRequest();
-
-        Representation requestRepr = request.getEntity();
-
+    protected Representation innerPost(Representation requestRepr)
+    throws ResourceException
+    {
         Document inputDocument = null;
         try {
             DomRepresentation input = new DomRepresentation(requestRepr);
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java	Mon Sep 21 11:56:10 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java	Wed Sep 23 08:27:35 2009 +0000
@@ -5,8 +5,7 @@
 
 import org.restlet.data.MediaType;
 
-import org.restlet.resource.Get;  
-import org.restlet.resource.ServerResource;
+import org.restlet.resource.ResourceException;
 
 import org.restlet.ext.xml.DomRepresentation;
 
@@ -24,15 +23,15 @@
  * @author Sascha L. Teichmann (sascha.teichmann@intevation)
  */
 public class FactoriesResource
-extends      ServerResource
+extends      BaseResource
 {
     private static Logger logger = Logger.getLogger(FactoriesResource.class);
     
     public static final String PATH = "/factories";
 
-    @Get
-    public Representation represent() {
-
+    protected Representation innerGet()
+    throws                   ResourceException
+    {
         Document document = XMLUtils.newDocument();
 
         ElementCreator ec = new ElementCreator(

http://dive4elements.wald.intevation.org