[exo-jcr-commits] exo-jcr SVN: r3363 - in ws/branches/2.1.x/exo.ws.rest.core/src: test/java/org/exoplatform/services/rest/impl/provider and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Oct 29 03:11:50 EDT 2010


Author: areshetnyak
Date: 2010-10-29 03:11:50 -0400 (Fri, 29 Oct 2010)
New Revision: 3363

Modified:
   ws/branches/2.1.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/provider/JsonEntityProvider.java
   ws/branches/2.1.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java
Log:
WS-256 : Applied patch. Allow to use a String as entity for a JSON response

Modified: ws/branches/2.1.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/provider/JsonEntityProvider.java
===================================================================
--- ws/branches/2.1.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/provider/JsonEntityProvider.java	2010-10-29 07:04:16 UTC (rev 3362)
+++ ws/branches/2.1.x/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/provider/JsonEntityProvider.java	2010-10-29 07:11:50 UTC (rev 3363)
@@ -30,17 +30,26 @@
 import org.exoplatform.ws.frameworks.json.impl.JsonWriterImpl;
 import org.exoplatform.ws.frameworks.json.value.JsonValue;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.activation.DataSource;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBElement;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
 
 /**
  * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
@@ -59,13 +68,31 @@
    // Or probably enough check only content type 'application/json'
    // and if this content type set trust it and try parse/write
 
+   /** Do not process via JSON "known" JAX-RS types and some more. */
+   private static final Class<?>[] IGNORED =
+      new Class<?>[]{byte[].class, char[].class, DataSource.class, DOMSource.class, File.class, InputStream.class,
+         OutputStream.class, JAXBElement.class, MultivaluedMap.class, Reader.class, Writer.class, SAXSource.class,
+         StreamingOutput.class, StreamSource.class, String.class};
+
+   private static boolean isIgnored(Class<?> type)
+   {
+      for (Class<?> c : IGNORED)
+      {
+         if (c.isAssignableFrom(type))
+         {
+            return true;
+         }
+      }
+      return false;
+   }
+
    /**
     * {@inheritDoc}
     */
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
-      // say as support all objects, see _TODO_ above
-      return Object.class.isAssignableFrom(type);
+      //return Object.class.isAssignableFrom(type);
+      return !isIgnored(type);
    }
 
    /**
@@ -104,8 +131,8 @@
     */
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
-      // say as support all objects, see _TODO_ above
-      return Object.class.isAssignableFrom(type);
+      //return Object.class.isAssignableFrom(type);
+      return !isIgnored(type);
    }
 
    /**

Modified: ws/branches/2.1.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java
===================================================================
--- ws/branches/2.1.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java	2010-10-29 07:04:16 UTC (rev 3362)
+++ ws/branches/2.1.x/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityTest.java	2010-10-29 07:11:50 UTC (rev 3363)
@@ -29,7 +29,9 @@
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
 
 /**
  * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
@@ -77,13 +79,42 @@
       }
    }
 
+   @Path("/")
+   public static class ResourceString
+   {
+      @POST
+      @Consumes("application/json")
+      public void m1(String b)
+      {
+         assertEquals(jsonBook, b);
+      }
+   }
+
+   @Path("/")
+   public static class ResourceString2
+   {
+      @GET
+      @Produces("application/json")
+      public String m1()
+      {
+         return jsonBook;
+      }
+
+      @POST
+      public Response m2()
+      {
+         return Response.ok(jsonBook).type(MediaType.APPLICATION_JSON).build();
+      }
+   }
+
+   private static String jsonBook = "{\"title\":\"Hamlet\", \"author\":\"William Shakespeare\", \"sendByPost\":true}";
+
    private byte[] jsonData;
 
    public void setUp() throws Exception
    {
       super.setUp();
-      jsonData =
-         ("{\"title\":\"Hamlet\"," + "\"author\":\"William Shakespeare\"," + "\"sendByPost\":true}").getBytes("UTF-8");
+      jsonData = jsonBook.getBytes("UTF-8");
    }
 
    public void testJsonEntityParameter() throws Exception
@@ -125,8 +156,44 @@
       assertEquals("Hamlet\n", book.getTitle());
       assertEquals("William Shakespeare\n", book.getAuthor());
       assertFalse(book.isSendByPost());
-//      writer = new ByteArrayContainerResponseWriter();
+      //      writer = new ByteArrayContainerResponseWriter();
       unregistry(r2);
    }
 
+   public void testJsonEntityString() throws Exception
+   {
+      ResourceString r1 = new ResourceString();
+      registry(r1);
+      MultivaluedMap<String, String> h = new MultivaluedMapImpl();
+      h.putSingle("content-type", "application/json");
+      h.putSingle("content-length", "" + jsonData.length);
+      assertEquals(204, service("POST", "/", "", h, jsonData).getStatus());
+      unregistry(r1);
+   }
+
+   public void testJsonReturnString() throws Exception
+   {
+      ResourceString2 r2 = new ResourceString2();
+      registry(r2);
+      MultivaluedMap<String, String> h = new MultivaluedMapImpl();
+      h.putSingle("accept", "application/json");
+      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+
+      // ResourceString2#m1()
+      ContainerResponse response = service("GET", "/", "", h, null, writer);
+      assertEquals(200, response.getStatus());
+      assertEquals("application/json", response.getContentType().toString());
+      assertEquals(jsonBook, response.getEntity());
+      //System.out.println("string: " + new String(writer.getBody()));
+
+      // ResourceString2#m2()
+      writer.reset();
+      response = service("POST", "/", "", h, null, writer);
+      assertEquals(200, response.getStatus());
+      assertEquals("application/json", response.getContentType().toString());
+      assertEquals(jsonBook, response.getEntity());
+      //System.out.println("string: " + new String(writer.getBody()));
+
+      unregistry(r2);
+   }
 }



More information about the exo-jcr-commits mailing list