[exo-jcr-commits] exo-jcr SVN: r2143 - in ws/branches/2.2.x: exo.ws.rest.core/src/main/java/org/exoplatform/services/rest and 3 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Mar 25 12:33:35 EDT 2010


Author: yakimenko
Date: 2010-03-25 12:33:34 -0400 (Thu, 25 Mar 2010)
New Revision: 2143

Added:
   ws/branches/2.2.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java
Modified:
   ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java
   ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
   ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/service/RestServicesList.java
   ws/branches/2.2.x/pom.xml
Log:
EVERREST-1 : merge last changes from trunk

Modified: ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java	2010-03-25 16:33:32 UTC (rev 2142)
+++ ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java	2010-03-25 16:33:34 UTC (rev 2143)
@@ -175,5 +175,11 @@
     * Field Definitions sec. 14.41 Transfer-Encoding</a>.
     */
    public static final String TRANSFER_ENCODING = "Transfer-Encoding";
-
+   
+   /**
+    * This header indicates that body is provided via JAXR framework.
+    * Value of header MAY contain additional information about the nature
+    * of body's content, for example: 'Error-Message'. 
+    */
+   public static final String JAXRS_BODY_PROVIDED = "JAXRS-Body-Provided";
 }

Modified: ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java	2010-03-25 16:33:32 UTC (rev 2142)
+++ ws/branches/2.2.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java	2010-03-25 16:33:34 UTC (rev 2143)
@@ -27,6 +27,7 @@
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.StreamingOutput;
+import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.ext.ExceptionMapper;
 
 import org.exoplatform.services.log.ExoLogger;
@@ -41,6 +42,7 @@
 import org.exoplatform.services.rest.RequestHandler;
 import org.exoplatform.services.rest.ResourceBinder;
 import org.exoplatform.services.rest.ResponseFilter;
+import org.exoplatform.services.rest.ExtHttpHeaders;
 
 /**
  * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
@@ -77,7 +79,6 @@
          e.printStackTrace(pw);
          pw.flush();
       }
-
    }
 
    /**
@@ -90,12 +91,12 @@
     * and may be accessible via method {@link ApplicationContextImpl#getProperties()}. 
     */
    private static final Map<String, String> properties = new HashMap<String, String>();
-   
+
    public static final String getProperty(String name)
    {
       return properties.get(name);
    }
-   
+
    public static final void setProperty(String name, String value)
    {
       if (value == null)
@@ -107,14 +108,14 @@
     * See {@link RequestDispatcher}.
     */
    private final RequestDispatcher dispatcher;
-   
+
    /**
     * ResourceBinder.
     */
    private final ResourceBinder binder;
-   
+
    private final DependencySupplier depInjector;
-   
+
    public RequestHandlerImpl(ResourceBinder binder, DependencySupplier depInjector)
    {
       this.binder = binder;
@@ -154,7 +155,14 @@
          {
 
             dispatcher.dispatch(request, response);
-
+            if (response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED) == null)
+            {
+               String jaxrsHeader = getJaxrsHeader(response.getStatus());
+               if (jaxrsHeader != null)
+               {
+                  response.getHttpHeaders().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
+               }
+            }
          }
          catch (Exception e)
          {
@@ -164,65 +172,50 @@
                Response errorResponse = ((WebApplicationException)e).getResponse();
                ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(WebApplicationException.class);
 
+               int errorStatus = errorResponse.getStatus();
                // should be some of 4xx status
-               if (errorResponse.getStatus() < 500)
+               if (errorStatus < 500)
                {
+                  // Warn about error in debug mode only.
                   if (LOG.isDebugEnabled() && e.getCause() != null)
                   {
-                     LOG.warn("WedApplication exception occurs.", e.getCause());
+                     LOG.warn("WebApplication exception occurs.", e.getCause());
                   }
-                  if (errorResponse.getEntity() == null)
+               }
+               else
+               {
+                  if (e.getCause() != null)
                   {
-                     if (excmap != null)
+                     LOG.warn("WebApplication exception occurs.", e.getCause());
+                  }
+               }
+               // -----
+               if (errorResponse.getEntity() == null)
+               {
+                  if (excmap != null)
+                  {
+                     errorResponse = excmap.toResponse(e);
+                  }
+                  else
+                  {
+                     if (e.getMessage() != null)
                      {
-                        errorResponse = excmap.toResponse(e);
+                        errorResponse = createErrorResponse(errorStatus, e.getMessage());
                      }
                   }
-
-                  if (e.getMessage() != null)
-                     errorResponse =
-                        Response.status(errorResponse.getStatus()).entity(new String(e.getMessage())).type(
-                           MediaType.TEXT_PLAIN).header("JAXRS-Message-Provided", "true").build();
-
-                  response.setResponse(errorResponse);
                }
                else
                {
-
-                  if (errorResponse.getEntity() == null)
+                  if (errorResponse.getMetadata().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED) == null)
                   {
-                     if (excmap != null)
+                     String jaxrsHeader = getJaxrsHeader(errorStatus);
+                     if (jaxrsHeader != null)
                      {
-                        if (LOG.isDebugEnabled() && e.getCause() != null)
-                        {
-                           // Hide error message if exception mapper exists.
-                           LOG.warn("WedApplication exception occurs.", e.getCause());
-                        }
-
-                        errorResponse = excmap.toResponse(e);
+                        errorResponse.getMetadata().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
                      }
-                     else
-                     {
-                        if (e.getCause() != null)
-                        {
-                           LOG.warn("WedApplication exception occurs.", e.getCause());
-                        }
-
-                        // print stack trace & adding ex message into body
-                        if (LOG.isDebugEnabled())
-                        {
-                           e.printStackTrace();
-                        }
-                        if (e.getMessage() != null)
-                           errorResponse =
-                              Response.status(errorResponse.getStatus()).entity(new String(e.getMessage())).type(
-                                 MediaType.TEXT_PLAIN).header("JAXRS-Message-Provided", "true").build();
-                        else
-                           errorResponse =  Response.status(errorResponse.getStatus()).header("JAXRS-Message-Provided", "false").build();
-                     }
                   }
-                  response.setResponse(errorResponse);
                }
+               response.setResponse(errorResponse);
             }
             else if (e instanceof InternalException)
             {
@@ -255,7 +248,6 @@
                throw new UnhandledException(e);
             }
          }
-
          for (ObjectFactory<FilterDescriptor> factory : ProviderBinder.getInstance().getResponseFilters(
             context.getPath()))
          {
@@ -273,4 +265,32 @@
       }
    }
 
+   /**
+    * Create error response with specified status and body message.
+    *  
+    * @param status response status
+    * @param message response message
+    * @return response
+    */
+   private Response createErrorResponse(int status, String message)
+   {
+
+      ResponseBuilder responseBuilder = Response.status(status);
+      responseBuilder.entity(message).type(MediaType.TEXT_PLAIN);
+      String jaxrsHeader = getJaxrsHeader(status);
+      if (jaxrsHeader != null)
+         responseBuilder.header(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
+
+      return responseBuilder.build();
+   }
+
+   private String getJaxrsHeader(int status)
+   {
+      if (status >= 400)
+      {
+         return "Error-Message";
+      }
+      // Add required behavior here.
+      return null;
+   }
 }

Added: ws/branches/2.2.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java	                        (rev 0)
+++ ws/branches/2.2.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java	2010-03-25 16:33:34 UTC (rev 2143)
@@ -0,0 +1,775 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.exoplatform.services.rest.impl.AbstractResourceTest;
+import org.exoplatform.services.rest.ExtHttpHeaders;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.UnhandledException;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+
+/**
+ * Created by The eXo Platform SAS. <br/>
+ * Date: 24 Dec 2009
+ * 
+ * @author <a href="mailto:max.shaposhnik at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id: WebApplicationExceptionTest.java
+ */
+public class ExceptionsTest extends AbstractResourceTest
+{
+
+   @Path("a")
+   public static class Resource1
+   {
+
+      @GET
+      @Path("0")
+      public void m0() throws WebApplicationException
+      {
+         Exception e = new Exception(errorMessage);
+         throw new WebApplicationException(e, 500);
+      }
+
+      @GET
+      @Path("1")
+      public void m1() throws WebApplicationException
+      {
+         Response response = Response.status(500).entity(errorMessage).type("text/plain").build();
+         throw new WebApplicationException(new Exception(), response);
+      }
+
+      @GET
+      @Path("2")
+      public Response m2() throws WebApplicationException
+      {
+         throw new WebApplicationException(500);
+      }
+
+      @GET
+      @Path("3")
+      public void m3() throws Exception
+      {
+         throw new RuntimeException("Runtime exception");
+      }
+
+      @GET
+      @Path("4")
+      public Response m4() throws Exception
+      {
+         return Response.status(500).entity(errorMessage).type("text/plain").build();
+      }
+
+   }
+
+   private static String errorMessage = "test-error-message";
+
+   private Resource1 resource;
+
+   public void setUp() throws Exception
+   {
+      super.setUp();
+      resource = new Resource1();
+      registry(resource);
+   }
+
+   public void tearDown() throws Exception
+   {
+      unregistry(resource);
+      super.tearDown();
+   }
+   
+   public void testErrorResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/4", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testUncheckedException() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      try
+      {
+         service("GET", "/a/3", "", null, null, writer);
+         fail("UnhandledException should be throw by RequstHandlerImpl");
+      }
+      catch (UnhandledException e)
+      {
+         // OK
+      }
+   }
+
+   public void testWebApplicationExceptionWithCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/0", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(new Exception(errorMessage).toString(), entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithoutCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/2", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      assertNull(response.getEntity());
+      assertNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/1", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+}
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.exoplatform.services.rest.impl.AbstractResourceTest;
+import org.exoplatform.services.rest.ExtHttpHeaders;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.UnhandledException;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+
+/**
+ * Created by The eXo Platform SAS. <br/>
+ * Date: 24 Dec 2009
+ * 
+ * @author <a href="mailto:max.shaposhnik at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id: WebApplicationExceptionTest.java
+ */
+public class ExceptionsTest extends AbstractResourceTest
+{
+
+   @Path("a")
+   public static class Resource1
+   {
+
+      @GET
+      @Path("0")
+      public void m0() throws WebApplicationException
+      {
+         Exception e = new Exception(errorMessage);
+         throw new WebApplicationException(e, 500);
+      }
+
+      @GET
+      @Path("1")
+      public void m1() throws WebApplicationException
+      {
+         Response response = Response.status(500).entity(errorMessage).type("text/plain").build();
+         throw new WebApplicationException(new Exception(), response);
+      }
+
+      @GET
+      @Path("2")
+      public Response m2() throws WebApplicationException
+      {
+         throw new WebApplicationException(500);
+      }
+
+      @GET
+      @Path("3")
+      public void m3() throws Exception
+      {
+         throw new RuntimeException("Runtime exception");
+      }
+
+      @GET
+      @Path("4")
+      public Response m4() throws Exception
+      {
+         return Response.status(500).entity(errorMessage).type("text/plain").build();
+      }
+
+   }
+
+   private static String errorMessage = "test-error-message";
+
+   private Resource1 resource;
+
+   public void setUp() throws Exception
+   {
+      super.setUp();
+      resource = new Resource1();
+      registry(resource);
+   }
+
+   public void tearDown() throws Exception
+   {
+      unregistry(resource);
+      super.tearDown();
+   }
+   
+   public void testErrorResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/4", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testUncheckedException() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      try
+      {
+         service("GET", "/a/3", "", null, null, writer);
+         fail("UnhandledException should be throw by RequstHandlerImpl");
+      }
+      catch (UnhandledException e)
+      {
+         // OK
+      }
+   }
+
+   public void testWebApplicationExceptionWithCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/0", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(new Exception(errorMessage).toString(), entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithoutCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/2", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      assertNull(response.getEntity());
+      assertNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/1", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+}
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.exoplatform.services.rest.impl.AbstractResourceTest;
+import org.exoplatform.services.rest.ExtHttpHeaders;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.UnhandledException;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+
+/**
+ * Created by The eXo Platform SAS. <br/>
+ * Date: 24 Dec 2009
+ * 
+ * @author <a href="mailto:max.shaposhnik at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id: WebApplicationExceptionTest.java
+ */
+public class ExceptionsTest extends AbstractResourceTest
+{
+
+   @Path("a")
+   public static class Resource1
+   {
+
+      @GET
+      @Path("0")
+      public void m0() throws WebApplicationException
+      {
+         Exception e = new Exception(errorMessage);
+         throw new WebApplicationException(e, 500);
+      }
+
+      @GET
+      @Path("1")
+      public void m1() throws WebApplicationException
+      {
+         Response response = Response.status(500).entity(errorMessage).type("text/plain").build();
+         throw new WebApplicationException(new Exception(), response);
+      }
+
+      @GET
+      @Path("2")
+      public Response m2() throws WebApplicationException
+      {
+         throw new WebApplicationException(500);
+      }
+
+      @GET
+      @Path("3")
+      public void m3() throws Exception
+      {
+         throw new RuntimeException("Runtime exception");
+      }
+
+      @GET
+      @Path("4")
+      public Response m4() throws Exception
+      {
+         return Response.status(500).entity(errorMessage).type("text/plain").build();
+      }
+
+   }
+
+   private static String errorMessage = "test-error-message";
+
+   private Resource1 resource;
+
+   public void setUp() throws Exception
+   {
+      super.setUp();
+      resource = new Resource1();
+      registry(resource);
+   }
+
+   public void tearDown() throws Exception
+   {
+      unregistry(resource);
+      super.tearDown();
+   }
+   
+   public void testErrorResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/4", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testUncheckedException() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      try
+      {
+         service("GET", "/a/3", "", null, null, writer);
+         fail("UnhandledException should be throw by RequstHandlerImpl");
+      }
+      catch (UnhandledException e)
+      {
+         // OK
+      }
+   }
+
+   public void testWebApplicationExceptionWithCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/0", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(new Exception(errorMessage).toString(), entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithoutCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/2", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      assertNull(response.getEntity());
+      assertNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/1", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+}
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.exoplatform.services.rest.impl.AbstractResourceTest;
+import org.exoplatform.services.rest.ExtHttpHeaders;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.UnhandledException;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+
+/**
+ * Created by The eXo Platform SAS. <br/>
+ * Date: 24 Dec 2009
+ * 
+ * @author <a href="mailto:max.shaposhnik at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id: WebApplicationExceptionTest.java
+ */
+public class ExceptionsTest extends AbstractResourceTest
+{
+
+   @Path("a")
+   public static class Resource1
+   {
+
+      @GET
+      @Path("0")
+      public void m0() throws WebApplicationException
+      {
+         Exception e = new Exception(errorMessage);
+         throw new WebApplicationException(e, 500);
+      }
+
+      @GET
+      @Path("1")
+      public void m1() throws WebApplicationException
+      {
+         Response response = Response.status(500).entity(errorMessage).type("text/plain").build();
+         throw new WebApplicationException(new Exception(), response);
+      }
+
+      @GET
+      @Path("2")
+      public Response m2() throws WebApplicationException
+      {
+         throw new WebApplicationException(500);
+      }
+
+      @GET
+      @Path("3")
+      public void m3() throws Exception
+      {
+         throw new RuntimeException("Runtime exception");
+      }
+
+      @GET
+      @Path("4")
+      public Response m4() throws Exception
+      {
+         return Response.status(500).entity(errorMessage).type("text/plain").build();
+      }
+
+   }
+
+   private static String errorMessage = "test-error-message";
+
+   private Resource1 resource;
+
+   public void setUp() throws Exception
+   {
+      super.setUp();
+      resource = new Resource1();
+      registry(resource);
+   }
+
+   public void tearDown() throws Exception
+   {
+      unregistry(resource);
+      super.tearDown();
+   }
+   
+   public void testErrorResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/4", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testUncheckedException() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      try
+      {
+         service("GET", "/a/3", "", null, null, writer);
+         fail("UnhandledException should be throw by RequstHandlerImpl");
+      }
+      catch (UnhandledException e)
+      {
+         // OK
+      }
+   }
+
+   public void testWebApplicationExceptionWithCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/0", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(new Exception(errorMessage).toString(), entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithoutCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/2", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      assertNull(response.getEntity());
+      assertNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/1", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+}
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import org.exoplatform.services.rest.impl.AbstractResourceTest;
+import org.exoplatform.services.rest.ExtHttpHeaders;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.UnhandledException;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+
+/**
+ * Created by The eXo Platform SAS. <br/>
+ * Date: 24 Dec 2009
+ * 
+ * @author <a href="mailto:max.shaposhnik at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id: WebApplicationExceptionTest.java
+ */
+public class ExceptionsTest extends AbstractResourceTest
+{
+
+   @Path("a")
+   public static class Resource1
+   {
+
+      @GET
+      @Path("0")
+      public void m0() throws WebApplicationException
+      {
+         Exception e = new Exception(errorMessage);
+         throw new WebApplicationException(e, 500);
+      }
+
+      @GET
+      @Path("1")
+      public void m1() throws WebApplicationException
+      {
+         Response response = Response.status(500).entity(errorMessage).type("text/plain").build();
+         throw new WebApplicationException(new Exception(), response);
+      }
+
+      @GET
+      @Path("2")
+      public Response m2() throws WebApplicationException
+      {
+         throw new WebApplicationException(500);
+      }
+
+      @GET
+      @Path("3")
+      public void m3() throws Exception
+      {
+         throw new RuntimeException("Runtime exception");
+      }
+
+      @GET
+      @Path("4")
+      public Response m4() throws Exception
+      {
+         return Response.status(500).entity(errorMessage).type("text/plain").build();
+      }
+
+   }
+
+   private static String errorMessage = "test-error-message";
+
+   private Resource1 resource;
+
+   public void setUp() throws Exception
+   {
+      super.setUp();
+      resource = new Resource1();
+      registry(resource);
+   }
+
+   public void tearDown() throws Exception
+   {
+      unregistry(resource);
+      super.tearDown();
+   }
+   
+   public void testErrorResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/4", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testUncheckedException() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      try
+      {
+         service("GET", "/a/3", "", null, null, writer);
+         fail("UnhandledException should be throw by RequstHandlerImpl");
+      }
+      catch (UnhandledException e)
+      {
+         // OK
+      }
+   }
+
+   public void testWebApplicationExceptionWithCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/0", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(new Exception(errorMessage).toString(), entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithoutCause() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/2", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      assertNull(response.getEntity());
+      assertNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+   public void testWebApplicationExceptionWithResponse() throws Exception
+   {
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+      ContainerResponse response = service("GET", "/a/1", "", null, null, writer);
+      assertEquals(500, response.getStatus());
+      String entity = new String(writer.getBody());
+      assertEquals(errorMessage, entity);
+      assertNotNull(response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED));
+   }
+
+}

Modified: ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/service/RestServicesList.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/service/RestServicesList.java	2010-03-25 16:33:32 UTC (rev 2142)
+++ ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/service/RestServicesList.java	2010-03-25 16:33:34 UTC (rev 2143)
@@ -19,7 +19,7 @@
 package org.exoplatform.services.rest.ext.service;
 
 import org.exoplatform.services.rest.ObjectFactory;
-import org.exoplatform.services.rest.impl.ResourceBinder;
+import org.exoplatform.services.rest.ResourceBinder;
 import org.exoplatform.services.rest.resource.AbstractResourceDescriptor;
 
 import java.io.ByteArrayOutputStream;

Modified: ws/branches/2.2.x/pom.xml
===================================================================
--- ws/branches/2.2.x/pom.xml	2010-03-25 16:33:32 UTC (rev 2142)
+++ ws/branches/2.2.x/pom.xml	2010-03-25 16:33:34 UTC (rev 2143)
@@ -32,7 +32,6 @@
    <artifactId>ws-parent</artifactId>
    <version>2.2.0-SNAPSHOT</version>
    <packaging>pom</packaging>
-
    <name>eXo WS</name>
 
    <scm>
@@ -45,8 +44,8 @@
       <exo.product.name>exo-ws</exo.product.name>
       <exo.product.specification>2.1</exo.product.specification>
       
-      <org.exoplatform.kernel.version>2.2.0-Beta02</org.exoplatform.kernel.version>
-      <org.exoplatform.core.version>2.3.0-Beta02</org.exoplatform.core.version>
+      <org.exoplatform.kernel.version>2.2.0-GA</org.exoplatform.kernel.version>
+      <org.exoplatform.core.version>2.3.0-GA</org.exoplatform.core.version>
 
       <exo.test.includes>*Test*</exo.test.includes>
    </properties>



More information about the exo-jcr-commits mailing list