incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1179083 - in /incubator/isis/trunk/framework/viewer/json: json-applib/src/main/java/org/apache/isis/viewer/json/applib/ json-applib/src/test/java/org/apache/isis/viewer/json/applib/ json-viewer/src/main/java/org/apache/isis/viewer/json/vie...
Date Wed, 05 Oct 2011 07:44:09 GMT
Author: danhaywood
Date: Wed Oct  5 07:44:08 2011
New Revision: 1179083

URL: http://svn.apache.org/viewvc?rev=1179083&view=rev
Log:
ISIS-109: utility methods to support parsing of x-ro-follow-links query param

Modified:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/ParserTest.java
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/home/HomePageResourceServerside.java

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java?rev=1179083&r1=1179082&r2=1179083&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
Wed Oct  5 07:44:08 2011
@@ -2,7 +2,6 @@ package org.apache.isis.viewer.json.appl
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
@@ -187,7 +186,61 @@ public abstract class Parser<T> {
             }
         };
     }
-    
+
+    public static Parser<List<List<String>>> forListOfListOfStrings() {
+        return new Parser<List<List<String>>>() {
+
+            @Override
+            public List<List<String>> valueOf(List<String> str) {
+                if(str == null) {
+                    return null;
+                }
+                if(str.size()==0)
+                    return null;
+                List<List<String>> listOfLists = Lists.newArrayList();
+                for (String s : str) {
+                    final Iterable<String> split = Splitter.on('.').split(s);
+                    listOfLists.add(Lists.newArrayList(split));
+                }
+                return listOfLists;
+            }
+            
+            @Override
+            public List<List<String>> valueOf(String[] str) {
+                if(str == null) {
+                    return null;
+                }
+                if(str.length==0)
+                    return null;
+                return valueOf(Arrays.asList(str));
+            }
+
+            @Override
+            public List<List<String>> valueOf(String str) {
+                final Iterable<String> listOfStrings = Splitter.on(',').split(str);
+                return Lists.transform(Lists.newArrayList(listOfStrings), new Function<String,
List<String>>() {
+
+                    @Override
+                    public List<String> apply(String input) {
+                        return Lists.newArrayList(Splitter.on('.').split(input));
+                    }
+                });
+            }
+
+            @Override
+            public String asString(List<List<String>> listOfLists) {
+                List<String> listOfStrings = Lists.transform(listOfLists, new Function<List<String>,
String>() {
+                    @Override
+                    public String apply(List<String> listOfStrings) {
+                        return Joiner.on('.').join(listOfStrings);
+                    }
+                });
+                return Joiner.on(',').join(listOfStrings);
+            }
+        };
+    }
+
+
     public static Parser<String[]> forArrayOfStrings() {
         return new Parser<String[]>() {
 
@@ -261,5 +314,5 @@ public abstract class Parser<T> {
             }
         };
     }
-
+    
 }

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java?rev=1179083&r1=1179082&r2=1179083&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java
Wed Oct  5 07:44:08 2011
@@ -35,7 +35,7 @@ public final class RestfulRequest {
     
     public static class QueryParameter<Q> {
 
-        public static QueryParameter<List<String>> FOLLOW_LINKS = new QueryParameter<List<String>>("x-ro-follow-links",
Parser.forListOfStrings());
+        public static QueryParameter<List<List<String>>> FOLLOW_LINKS =
new QueryParameter<List<List<String>>>("x-ro-follow-links", Parser.forListOfListOfStrings());
         public static QueryParameter<Integer> PAGE = new QueryParameter<Integer>("x-ro-page",
Parser.forInteger());
         public static QueryParameter<Integer> PAGE_SIZE = new QueryParameter<Integer>("x-ro-page-size",
Parser.forInteger());
         public static QueryParameter<List<String>> SORT_BY = new QueryParameter<List<String>>("x-ro-sort-by",
Parser.forListOfStrings());

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/ParserTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/ParserTest.java?rev=1179083&r1=1179082&r2=1179083&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/ParserTest.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/ParserTest.java
Wed Oct  5 07:44:08 2011
@@ -2,6 +2,7 @@ package org.apache.isis.viewer.json.appl
 
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
 
 import java.util.Arrays;
@@ -21,23 +22,34 @@ public class ParserTest {
 
     @Test
     public void forBoolean() {
+        Parser<Boolean> parser = Parser.forBoolean();
         for (Boolean v : new Boolean[] {Boolean.TRUE, Boolean.FALSE}) {
-            assertRoundTrips(Parser.forBoolean(), v);
+            final String asString = parser.asString(v);
+            final Boolean valueOf = parser.valueOf(asString);
+            assertThat(v, is(equalTo(valueOf)));
         }
+        
+        final Boolean valueOf = parser.valueOf(Arrays.asList(parser.asString(Boolean.TRUE),
parser.asString(Boolean.FALSE)));
+        assertThat(valueOf, is(Boolean.TRUE));
     }
 
     @Test
     public void forString() {
+        Parser<String> parser = Parser.forString();
+        
         for (String v : new String[] {"", "foo", "foz"}) {
-            assertRoundTrips(Parser.forString(), v);
+            final String asString = parser.asString(v);
+            final String valueOf = parser.valueOf(asString);
+            assertThat(v, is(equalTo(valueOf)));
         }
     }
 
     @Test
     public void forListOfStrings() {
         Parser<List<String>> parser = Parser.forListOfStrings();
-        String[] strings = { "", "foo", "foz" };
-        List<String> v = Arrays.asList(strings);
+        
+        List<String> v = Arrays.asList("", "foo", "foz" );
+        
         final String asString = parser.asString(v);
         final List<String> valueOf = parser.valueOf(asString);
         
@@ -45,52 +57,122 @@ public class ParserTest {
     }
 
     @Test
+    public void forListOfListOfStringsDottedNotation() {
+        Parser<List<List<String>>> parser = Parser.forListOfListOfStrings();
+        
+        final List<List<String>> valueOf = parser.valueOf(Arrays.asList("a",
"b.c", "d.e.f" ));
+        
+        assertThat(valueOf.size(), is(3));
+        assertThat(valueOf.get(0).size(), is(1));
+        assertThat(valueOf.get(0).get(0), is("a"));
+        assertThat(valueOf.get(1).size(), is(2));
+        assertThat(valueOf.get(1).get(0), is("b"));
+        assertThat(valueOf.get(1).get(1), is("c"));
+        assertThat(valueOf.get(2).size(), is(3));
+        assertThat(valueOf.get(2).get(0), is("d"));
+        assertThat(valueOf.get(2).get(1), is("e"));
+        assertThat(valueOf.get(2).get(2), is("f"));
+        
+        assertThat(parser.asString(valueOf), is("a,b.c,d.e.f"));
+    }
+
+    @Test
+    public void forListOfListOfStringsCommaSeparated() {
+        Parser<List<List<String>>> parser = Parser.forListOfListOfStrings();
+        
+        final List<List<String>> valueOf = parser.valueOf("a,b.c,d.e.f");
+        
+        assertThat(valueOf.size(), is(3));
+        assertThat(valueOf.get(0).size(), is(1));
+        assertThat(valueOf.get(0).get(0), is("a"));
+        assertThat(valueOf.get(1).size(), is(2));
+        assertThat(valueOf.get(1).get(0), is("b"));
+        assertThat(valueOf.get(1).get(1), is("c"));
+        assertThat(valueOf.get(2).size(), is(3));
+        assertThat(valueOf.get(2).get(0), is("d"));
+        assertThat(valueOf.get(2).get(1), is("e"));
+        assertThat(valueOf.get(2).get(2), is("f"));
+    }
+
+    @Test
+    public void forMediaTypes() {
+        Parser<MediaType> parser = Parser.forMediaType();
+        MediaType mediaType = RepresentationType.DOMAIN_OBJECT.getMediaType();
+        final String asString = parser.asString(mediaType);
+        final MediaType valueOf = parser.valueOf(asString);
+        
+        assertThat(valueOf, is(mediaType));
+    }
+
+
+    @Test
+    public void forListOfMediaTypes() {
+        Parser<List<MediaType>> parser = Parser.forListOfMediaTypes();
+        MediaType mediaType = RepresentationType.DOMAIN_OBJECT.getMediaType();
+        List<MediaType> v = Arrays.asList(mediaType, MediaType.APPLICATION_JSON_TYPE);
+        final String asString = parser.asString(v);
+        final List<MediaType> valueOf = parser.valueOf(asString);
+        
+        assertThat(v, sameContentsAs(valueOf));
+    }
+
+    @Test
     public void forDate() {
+        Parser<Date> parser = Parser.forDate();
+        
         final Calendar cal = Calendar.getInstance();
         cal.setTime(new Date());
         cal.set(Calendar.MILLISECOND, 0);
         Date nowToNearestSecond = cal.getTime();
         for (Date v : new Date[] {nowToNearestSecond}) {
-            assertRoundTrips(Parser.forDate(), v);
+            final String asString = parser.asString(v);
+            final Date valueOf = parser.valueOf(asString);
+            assertThat(v, is(equalTo(valueOf)));
         }
     }
 
     @Test
     public void forInteger() {
+        Parser<Integer> parser = Parser.forInteger();
+        
         for (Integer v : new Integer[] {1, 2, 3, -5, -100, 0, Integer.MAX_VALUE, Integer.MIN_VALUE})
{
-            assertRoundTrips(Parser.forInteger(), v);
+            final String asString = parser.asString(v);
+            final Integer valueOf = parser.valueOf(asString);
+            assertThat(v, is(equalTo(valueOf)));
         }
     }
 
     @Test
     public void forMediaType() {
+        Parser<MediaType> parser = Parser.forMediaType();
+        
         for (MediaType v : new MediaType[] { 
                 MediaType.APPLICATION_ATOM_XML_TYPE, 
                 MediaType.APPLICATION_JSON_TYPE, 
                 MediaType.APPLICATION_XHTML_XML_TYPE, 
                 MediaType.valueOf(RestfulMediaType.APPLICATION_JSON_DOMAIN_OBJECT) }) {
-            assertRoundTrips(Parser.forMediaType(), v);
+            final String asString = parser.asString(v);
+            final MediaType valueOf = parser.valueOf(asString);
+            assertThat(v, is(equalTo(valueOf)));
         }
     }
 
 
     @Test
     public void forCacheControl() {
+        Parser<CacheControl> parser = Parser.forCacheControl();
+        
         final CacheControl cc1 = createCacheControl();
         cc1.setMaxAge(2000);
         final CacheControl cc2 = createCacheControl();
         cc2.setNoCache(true);
         for (CacheControl v : new CacheControl[] { cc1, cc2 }) {
-            assertRoundTrips(Parser.forCacheControl(), v);
+            final String asString = parser.asString(v);
+            final CacheControl valueOf = parser.valueOf(asString);
+            assertThat(v, is(equalTo(valueOf)));
         }
     }
 
-    private static <T> void assertRoundTrips(final Parser<T> parser, T v) {
-        final String asString = parser.asString(v);
-        final T valueOf = parser.valueOf(asString);
-        assertThat(v, is(equalTo(valueOf)));
-    }
-
     private static CacheControl createCacheControl() {
         final CacheControl cacheControl = new CacheControl();
         cacheControl.getCacheExtension(); // workaround for bug in CacheControl's equals()
method

Modified: incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/home/HomePageResourceServerside.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/home/HomePageResourceServerside.java?rev=1179083&r1=1179082&r2=1179083&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/home/HomePageResourceServerside.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/resources/home/HomePageResourceServerside.java
Wed Oct  5 07:44:08 2011
@@ -19,6 +19,8 @@
 package org.apache.isis.viewer.json.viewer.resources.home;
 
 
+import java.util.List;
+
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
@@ -26,6 +28,8 @@ import javax.ws.rs.core.Response;
 import org.apache.isis.viewer.json.applib.JsonRepresentation;
 import org.apache.isis.viewer.json.applib.RepresentationType;
 import org.apache.isis.viewer.json.applib.RestfulMediaType;
+import org.apache.isis.viewer.json.applib.RestfulRequest.Header;
+import org.apache.isis.viewer.json.applib.RestfulRequest.QueryParameter;
 import org.apache.isis.viewer.json.applib.homepage.HomePageResource;
 import org.apache.isis.viewer.json.viewer.representations.RendererFactory;
 import org.apache.isis.viewer.json.viewer.resources.ResourceAbstract;
@@ -42,7 +46,7 @@ public class HomePageResourceServerside 
         RepresentationType representationType = RepresentationType.HOME_PAGE;
         init(representationType);
         
-        getResourceContext().ensureCompatibleAcceptHeader(representationType);
+        //final List<List<String>> arg = getResourceContext().getArg(QueryParameter.FOLLOW_LINKS);
         
         final RendererFactory factory = rendererFactoryRegistry.find(representationType);
         final HomePageReprRenderer renderer = 



Mime
View raw message