Author: aparfonov
Date: 2010-02-01 07:22:56 -0500 (Mon, 01 Feb 2010)
New Revision: 1629
Added:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java
Removed:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/MethodExceptionTest.java
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/WebApplicationExceptionTest.java
Modified:
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java
Log:
EXOJCR-458 :
Modified:
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java 2010-02-01
11:15:06 UTC (rev 1628)
+++
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ExtHttpHeaders.java 2010-02-01
12:22:56 UTC (rev 1629)
@@ -120,6 +120,13 @@
* Headers for Distributed Authoring</a> section 9 for more information.
*/
public static final String IF = "If";
+
+ /**
+ * 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";
/**
* WebDav "Timeout" header. See <a
href='http://www.ietf.org/rfc/rfc2518.txt'>
Modified:
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java 2010-02-01
11:15:06 UTC (rev 1628)
+++
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java 2010-02-01
12:22:56 UTC (rev 1629)
@@ -30,6 +30,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.container.component.ComponentPlugin;
@@ -38,6 +39,7 @@
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.ApplicationContext;
+import org.exoplatform.services.rest.ExtHttpHeaders;
import org.exoplatform.services.rest.FilterDescriptor;
import org.exoplatform.services.rest.GenericContainerRequest;
import org.exoplatform.services.rest.GenericContainerResponse;
@@ -73,12 +75,12 @@
* See {@link RequestDispatcher}.
*/
private final RequestDispatcher dispatcher;
-
+
public static final String getProperty(String name)
{
return properties.get(name);
}
-
+
public static final void setProperty(String name, String value)
{
if (value == null)
@@ -132,6 +134,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)
@@ -142,65 +152,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("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("WebApplication exception occurs.",
e.getCause());
- }
-
- errorResponse = excmap.toResponse(e);
+
errorResponse.getMetadata().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
}
- else
- {
- if (e.getCause() != null)
- {
- LOG.warn("WebApplication 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)
{
@@ -251,6 +246,41 @@
}
}
+ /**
+ * 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();
+ }
+
+ /**
+ * Get JAXR header for response status.
+ *
+ * @param status response status
+ * @return JAXRS header or null.
+ */
+ private String getJaxrsHeader(int status)
+ {
+ if (status >= 400)
+ {
+ return "Error-Message";
+ }
+ // Add required behavior here.
+ return null;
+ }
+
//
/**
Copied:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java
(from rev 1626,
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/WebApplicationExceptionTest.java)
===================================================================
---
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java
(rev 0)
+++
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java 2010-02-01
12:22:56 UTC (rev 1629)
@@ -0,0 +1,155 @@
+/*
+ * 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.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@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));
+ }
+
+}
Property changes on:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/ExceptionsTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Deleted:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/MethodExceptionTest.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/MethodExceptionTest.java 2010-02-01
11:15:06 UTC (rev 1628)
+++
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/MethodExceptionTest.java 2010-02-01
12:22:56 UTC (rev 1629)
@@ -1,101 +0,0 @@
-/*
- * 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.method;
-
-import org.exoplatform.services.rest.AbstractResourceTest;
-import org.exoplatform.services.rest.impl.UnhandledException;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Response;
-
-/**
- * Created by The eXo Platform SAS. <br/>
- * Date: 21 Jan 2009
- *
- * @author <a href="mailto:dmitry.kataev@exoplatform.com.ua">Dmytro
Katayev</a>
- * @version $Id: MethodExceptionTest.java
- */
-public class MethodExceptionTest extends AbstractResourceTest
-{
-
- @SuppressWarnings("serial")
- public static class UncheckedException extends Exception
- {
-
- public UncheckedException()
- {
- super();
- }
-
- public UncheckedException(String msg)
- {
- super(msg);
- }
-
- }
-
- @Path("/a")
- public static class Resource1
- {
-
- @GET
- @Path("/0")
- public void m0() throws WebApplicationException
- {
- throw new WebApplicationException();
- }
-
- @GET
- @Path("/1")
- public Response m1() throws WebApplicationException
- {
- return new WebApplicationException().getResponse();
- }
-
- @GET
- @Path("/2")
- public void m2() throws Exception
- {
- throw new UncheckedException("Unchecked exception");
- }
-
- }
-
- public void testExceptionProcessing() throws Exception
- {
- Resource1 resource = new Resource1();
- registry(resource);
-
- assertEquals(500, service("GET", "/a/0", "", null,
null).getStatus());
- assertEquals(500, service("GET", "/a/1", "", null,
null).getStatus());
- try
- {
- assertEquals(500, service("GET", "/a/2", "", null,
null).getStatus());
- fail();
- }
- catch (UnhandledException e)
- {
- }
- unregistry(resource);
- }
-
- //
-}
Deleted:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/WebApplicationExceptionTest.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/WebApplicationExceptionTest.java 2010-02-01
11:15:06 UTC (rev 1628)
+++
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/method/WebApplicationExceptionTest.java 2010-02-01
12:22:56 UTC (rev 1629)
@@ -1,81 +0,0 @@
-/*
- * 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.method;
-
-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.AbstractResourceTest;
-import org.exoplatform.services.rest.impl.method.MethodExceptionTest.UncheckedException;
-
-/**
- * Created by The eXo Platform SAS. <br/>
- * Date: 24 Dec 2009
- *
- * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
- * @version $Id: WebApplicationExceptionTest.java
- */
-public class WebApplicationExceptionTest extends AbstractResourceTest
-{
-
- @Path("/a")
- public static class Resource1
- {
-
- @GET
- @Path("/0")
- public void m0() throws WebApplicationException
- {
- Exception e = new Exception("testmsg");
- throw new WebApplicationException(e, 500);
- }
-
- @GET
- @Path("/1")
- public Response m1() throws WebApplicationException
- {
- throw new WebApplicationException(500);
- }
-
- @GET
- @Path("/2")
- public void m2() throws Exception
- {
- throw new UncheckedException("Unchecked exception");
- }
-
- }
-
- public void testExceptionMessage() throws Exception
- {
- Resource1 resource = new Resource1();
- registry(resource);
-
- assertEquals(500, service("GET", "/a/0", "", null,
null).getStatus());
- String entity = (String)service("GET", "/a/0", "",
null, null).getEntity();
- assertTrue(entity.indexOf("testmsg") > 0);
-
- assertEquals(500, service("GET", "/a/1", "", null,
null).getStatus());
- assertEquals(null, service("GET", "/a/1", "", null,
null).getEntity());
- unregistry(resource);
- }
-
-}
Modified:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java 2010-02-01
11:15:06 UTC (rev 1628)
+++
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java 2010-02-01
12:22:56 UTC (rev 1629)
@@ -125,7 +125,7 @@
assertEquals("Hamlet\n", book.getTitle());
assertEquals("William Shakespeare\n", book.getAuthor());
assertFalse(book.isSendByPost());
- writer = new ByteArrayContainerResponseWriter();
+// writer = new ByteArrayContainerResponseWriter();
unregistry(r2);
}