[jackson-jaxrs-providers] 71/162: Fixed #14

Timo Aaltonen tjaalton at moszumanska.debian.org
Mon Sep 8 22:16:29 UTC 2014


This is an automated email from the git hooks/post-receive script.

tjaalton pushed a commit to branch master
in repository jackson-jaxrs-providers.

commit fa284b83e2f1dd7aa0380ed6ee7f5f4af41b7157
Author: Tatu Saloranta <tsaloranta at gmail.com>
Date:   Sat May 25 16:32:43 2013 -0700

    Fixed #14
---
 .../fasterxml/jackson/jaxrs/base/ProviderBase.java | 32 ++++++++--
 .../jackson/jaxrs/json/JacksonJsonProvider.java    | 24 +++++++-
 .../jackson/jaxrs/json/dw/TestSimpleEndpoint.java  | 71 ++++++++++++++++++----
 release-notes/VERSION                              |  2 +
 4 files changed, 111 insertions(+), 18 deletions(-)

diff --git a/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java b/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
index 34775e5..d0f58b2 100644
--- a/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
+++ b/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
@@ -305,13 +305,37 @@ public abstract class ProviderBase<
     /* Abstract methods sub-classes need to implement
     /**********************************************************
      */
+
+    /**
+     * Helper method used to check whether given media type
+     * is supported by this provider for read operations
+     * (when binding input data such as POST body).
+     *<p>
+     * Default implementation simply calls {@link #hasMatchingMediaType}.
+     * 
+     * @since 2.3
+     */
+    protected boolean hasMatchingMediaTypeForReading(MediaType mediaType) {
+        return hasMatchingMediaType(mediaType);
+    }
+
+    /**
+     * Helper method used to check whether given media type
+     * is supported by this provider for writing operations,
+     * such as when converting response object to response
+     * body of request (like GET or POST).
+     *<p>
+     * Default implementation simply calls {@link #hasMatchingMediaType}.
+     * 
+     * @since 2.3
+     */
+    protected boolean hasMatchingMediaTypeForWriting(MediaType mediaType) {
+        return hasMatchingMediaType(mediaType);
+    }
     
     /**
      * Helper method used to check whether given media type
-     * is JSON type or sub type.
-     * Current implementation essentially checks to see whether
-     * {@link MediaType#getSubtype} returns "json" or something
-     * ending with "+json".
+     * is supported by this provider.
      * 
      * @since 2.2
      */
diff --git a/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java b/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java
index 923207f..b6c7568 100644
--- a/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java
+++ b/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java
@@ -51,6 +51,10 @@ public class JacksonJsonProvider
         ObjectMapper,
         JsonEndpointConfig, JsonMapperConfigurator>
 {
+    public final static String MIME_JAVASCRIPT = "application/javascript";
+
+    public final static String MIME_JAVASCRIPT_MS = "application/x-javascript";
+    
     /**
      * Default annotation sets to use, if not explicitly defined during
      * construction: only Jackson annotations are used for the base
@@ -158,7 +162,16 @@ public class JacksonJsonProvider
     protected boolean isJsonType(MediaType mediaType) {
         return hasMatchingMediaType(mediaType);
     }
-    
+
+    /**
+     * Helper method used to check whether given media type
+     * is supported by this provider.
+     * Current implementation essentially checks to see whether
+     * {@link MediaType#getSubtype} returns "json" or something
+     * ending with "+json".
+     * 
+     * @since 2.2
+     */
     @Override
     protected boolean hasMatchingMediaType(MediaType mediaType)
     {
@@ -171,10 +184,15 @@ public class JacksonJsonProvider
         if (mediaType != null) {
             // Ok: there are also "xxx+json" subtypes, which count as well
             String subtype = mediaType.getSubtype();
-            return "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json");
+            // [Issue#6]: also allow 'application/javascript'
+           return "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json")
+                   || "javascript".equals(subtype)
+                   // apparently Microsoft once again has interesting alternative types?
+                   || "x-javascript".equals(subtype)
+                   ;
         }
         /* Not sure if this can happen; but it seems reasonable
-         * that we can at least produce json without media type?
+         * that we can at least produce JSON without media type?
          */
         return true;
     }
diff --git a/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestSimpleEndpoint.java b/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestSimpleEndpoint.java
index 7a09be8..1bc5aa3 100644
--- a/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestSimpleEndpoint.java
+++ b/json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/TestSimpleEndpoint.java
@@ -1,7 +1,7 @@
 package com.fasterxml.jackson.jaxrs.json.dw;
 
 import java.io.*;
-import java.net.URL;
+import java.net.*;
 import java.util.*;
 
 import javax.ws.rs.GET;
@@ -12,6 +12,7 @@ import javax.ws.rs.core.MediaType;
 
 import org.eclipse.jetty.server.Server;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
 import com.fasterxml.jackson.jaxrs.json.JaxrsTestBase;
 
@@ -19,14 +20,21 @@ public class TestSimpleEndpoint extends JaxrsTestBase
 {
     static class Point {
         public int x, y;
+
+        protected Point() { }
+        public Point(int x, int y) {
+            this.x = x;
+            this.y = y;
+        }
     }
 
     @Path("/point")
-    public static class SimpleResource {
+    public static class SimpleResource
+    {
         @GET
-        @Produces(MediaType.APPLICATION_JSON)
+        @Produces({ MediaType.APPLICATION_JSON, "application/javascript" })
         public Point getPoint() {
-            return new Point();
+            return new Point(1, 2);
         }
     }
 
@@ -57,16 +65,57 @@ public class TestSimpleEndpoint extends JaxrsTestBase
     
     public void testStandardJson() throws Exception
     {
+        final ObjectMapper mapper = new ObjectMapper();
         Server server = startServer(6061, SimpleResourceApp.class);
         InputStream in = new URL("http://localhost:6061/point").openStream();
-        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
-        int i;
-        
-        while ((i = in.read()) >= 0) {
-            bytes.write((byte) i);
+        Point p;
+
+        try {
+            p = mapper.readValue(in, Point.class);
+        } finally {
+            in.close();
+            server.stop();
         }
-//        System.out.println("Bytes: "+bytes.size()+" -> "+bytes.toString("UTF-8"));
-        server.stop();
+        // ensure we got a valid Point
+        assertNotNull(p);
+        assertEquals(1, p.x);
+        assertEquals(2, p.y);
     }
 
+    public void testAcceptJavascriptType() throws Exception
+    {
+        final ObjectMapper mapper = new ObjectMapper();
+        Server server = startServer(6062, SimpleResourceApp.class);
+        URL url = new URL("http://localhost:6062/point");
+
+        try {
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+
+            // First: verify that weird types are not supported...
+            conn.setRequestProperty("Accept", "foo/bar");
+            conn.connect();
+            assertEquals(HttpURLConnection.HTTP_NOT_ACCEPTABLE, conn.getResponseCode());
+            conn.disconnect();
+
+            // try again with somewhat non-standard, but supported JSON-like type (application/javascript)
+            conn = (HttpURLConnection) url.openConnection();
+            conn.setRequestProperty("Accept", "application/javascript");
+//            conn.setRequestProperty("Accept", "application/json");
+            assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
+            InputStream in = conn.getInputStream();
+            Point p;
+            try {
+                p = mapper.readValue(in, Point.class);
+            } finally {
+                in.close();
+            }
+            assertNotNull(p);
+            assertEquals(1, p.x);
+            assertEquals(2, p.y);
+        } finally {
+            server.stop();
+        }
+        
+    }
+    
 }
diff --git a/release-notes/VERSION b/release-notes/VERSION
index ac6f818..7211873 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -11,6 +11,8 @@ Changes:
  (reported by Bill Burke (from Resteasy))
 #12: OSGi imports missing dependency from json/smile/xml to base package
  (reported by Matt Bishop)
+#14: Allow "application/javascript" type for JSON provider
+ (requested by Stephan202 at github)
 
 ------------------------------------------------------------------------
 === History: ===

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jackson-jaxrs-providers.git



More information about the pkg-java-commits mailing list