isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1156064 - in /incubator/isis/trunk/framework/viewer/json: json-applib/src/main/java/org/apache/isis/viewer/json/applib/ json-applib/src/main/java/org/apache/isis/viewer/json/applib/blocks/ json-tck/src/test/java/org/apache/isis/viewer/json...
Date Wed, 10 Aug 2011 08:18:28 GMT
Author: danhaywood
Date: Wed Aug 10 08:18:28 2011
New Revision: 1156064

URL: http://svn.apache.org/viewvc?rev=1156064&view=rev
Log:
ISIS-109: RepresentationWalker can now walk links that take a body

Modified:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RepresentationWalker.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulClient.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/blocks/Link.java
    incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/ResourceRepresentationTest.java

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RepresentationWalker.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RepresentationWalker.java?rev=1156064&r1=1156063&r2=1156064&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RepresentationWalker.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RepresentationWalker.java
Wed Aug 10 08:18:28 2011
@@ -79,7 +79,7 @@ public class RepresentationWalker {
     }
 
     public void walk(String key) {
-        Step previousStep = previousStep();
+        Step previousStep = currentStep();
         if(previousStep.error!=null) {
             return;
         }
@@ -117,62 +117,110 @@ public class RepresentationWalker {
     }
 
     public void walkXpath(String linkXpath) {
-        JsonRepresentation entity;
-        try {
-            entity = getEntity();
-        } catch (Exception e) {
-            Step previousStep = previousStep();
-            previousStep.error = "exception: " + e.getMessage();
-            previousStep.exception = e;
-            return;
-        }
+        walkXpath(linkXpath, null);
+    }
 
+    public void walkXpath(String linkXpath, JsonRepresentation invokeBody) {
+        JsonRepresentation entity = getEntityElseMarkStepInError();
         if(entity == null) {
             return;
         }
         
-        JsonRepresentation matching;
+        Link link = null;
         try {
-            matching = entity.xpath(linkXpath);
+            JsonRepresentation matching = entity.xpath(linkXpath);
+            if (matching == null) {
+                addStep(linkXpath, null, null, null, "no such link '" + linkXpath + "'",
null);
+                return;
+            }
+
+            link = matching.asLink();
+            if(link.getHref() == null) {
+                addStep(linkXpath, link, null, null, "key does not identify a link '" + linkXpath
+ "'", null);
+                return;
+            }
+
+            Response response;
+            if(invokeBody != null) {
+                response = restfulClient.follow(link, invokeBody);
+            } else {
+                response = restfulClient.follow(link);
+            }
+            addStep(linkXpath, link, null, JsonResponse.of(response, JsonRepresentation.class),
null, null);
             
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
+            // if xpath fails
             addStep(linkXpath, null, null, null, "exception: " + e.getMessage(), e);
             return;
-        }
-
-        if (matching == null) {
-            addStep(linkXpath, null, null, null, "no such link '" + linkXpath + "'", null);
+        } catch (Exception e) {
+            // if follow fails
+            addStep(linkXpath, link, null, null, "failed to follow link: " + e.getMessage(),
e);
             return;
         }
+    }
 
-        Link link = matching.asLink();
-        if(link.getHref() == null) {
-            addStep(linkXpath, link, null, null, "key does not identify a link '" + linkXpath
+ "'", null);
-            return;
-        }
-        
-        Response response;
+    private JsonRepresentation getEntityElseMarkStepInError() {
         try {
-            response = restfulClient.follow(link);
+            return getEntity();
         } catch (Exception e) {
-            addStep(linkXpath, link, null, null, "failed to follow link: " + e.getMessage(),
e);
-            return;
+            Step previousStep = currentStep();
+            previousStep.error = "exception: " + e.getMessage();
+            previousStep.exception = e;
+            return null;
         }
-        
-        addStep(linkXpath, link, null, JsonResponse.of(response, JsonRepresentation.class),
null, null);
     }
 
+    /**
+     * The entity returned from the previous walk.
+     * 
+     * <p>
+     * Will return null if the previous walk returned an error.
+     */
     public JsonRepresentation getEntity() throws JsonParseException, JsonMappingException,
IOException {
-        Step previousStep = previousStep();
-        if (previousStep.response == null || previousStep.error != null) {
+        Step currentStep = currentStep();
+        if (currentStep.response == null || currentStep.error != null) {
             return null;
         }
-        return previousStep.response.getEntity();
+        return currentStep.response.getEntity();
     }
 
-    private Step previousStep() {
-        return steps.get(0);
+    /**
+     * The response returned from the previous walk.
+     * 
+     * <p>
+     * Once a walk/performed has been attempted, is guaranteed to return a non-null value.
+     * (Conversely, will return <tt>null</tt> immediately after instantiation
and prior 
+     * to a walk being attempted/performed).  
+     */
+    public JsonResponse<?> getResponse()  {
+        Step currentStep = currentStep();
+        return currentStep != null? currentStep.response: null;
+    }
+
+    /**
+     * The error (if any) that occurred from the previous walk.
+     */
+    public String getError()  {
+        Step currentStep = currentStep();
+        return currentStep != null? currentStep.error: null;
     }
 
+    /**
+     * The exception (if any) that occurred from the previous walk.
+     * 
+     * <p>
+     * Will only ever be populated if {@link #getError()} is non-null.
+     */
+    public Exception getException()  {
+        Step currentStep = currentStep();
+        return currentStep != null? currentStep.exception: null;
+    }
+
+    /**
+     * The step that has just been walked.
+     */
+    private Step currentStep() {
+        return steps.get(0);
+    }
 
 }

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulClient.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulClient.java?rev=1156064&r1=1156063&r2=1156064&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulClient.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulClient.java
Wed Aug 10 08:18:28 2011
@@ -91,7 +91,11 @@ public class RestfulClient {
     }
 
     public Response follow(Link link) throws Exception {
-        Response response = link.follow(executor);
+        return follow(link, null);
+    }
+
+    public Response follow(Link link, JsonRepresentation requestBody) throws Exception {
+        Response response = link.follow(executor, requestBody);
         // this is a bit hacky
         @SuppressWarnings("unchecked")
         BaseClientResponse<String> restEasy = (BaseClientResponse<String>)response;

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/blocks/Link.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/blocks/Link.java?rev=1156064&r1=1156063&r2=1156064&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/blocks/Link.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/blocks/Link.java
Wed Aug 10 08:18:28 2011
@@ -45,12 +45,18 @@ public class Link {
     public void setBody(final JsonNode body) {
         this.body = new JsonRepresentation(body);
     }
-    
 
     public <T> Response follow(ClientExecutor executor) throws Exception {
+        return follow(executor, null);
+    }
+
+    public <T> Response follow(ClientExecutor executor, JsonRepresentation requestBody)
throws Exception {
         ClientRequest restEasyRequest = executor.createRequest(href);
         restEasyRequest.setHttpMethod(method.name());
         restEasyRequest.accept(MediaType.APPLICATION_JSON_TYPE);
+        if(requestBody != null) {
+            restEasyRequest.body(MediaType.APPLICATION_JSON, requestBody.toString());
+        }
         @SuppressWarnings("unchecked")
         ClientResponse<T> restEasyResponse = executor.execute(restEasyRequest);
         return restEasyResponse;

Modified: incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/ResourceRepresentationTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/ResourceRepresentationTest.java?rev=1156064&r1=1156063&r2=1156064&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/ResourceRepresentationTest.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/ResourceRepresentationTest.java
Wed Aug 10 08:18:28 2011
@@ -420,16 +420,33 @@ public class ResourceRepresentationTest 
     @Test
     public void walkResources() throws Exception {
     
-        // given an initial representation
+        // given a response for an initial resource
         HomePageResource homePageResource = client.getHomePageResource();
-
-        RepresentationWalker walker = client.createWalker(homePageResource.resources());
+        Response homePageResp = homePageResource.resources();
+        
+        // and given a walker starting from this response
+        RepresentationWalker walker = client.createWalker(homePageResp);
+        
+        // when walk the home pages' 'services' link
         walker.walk("services");
+        
+        // and when locate the ApplibValues repo and walk the its 'object' link
         walker.walkXpath("/*[title='ApplibValues']/link[rel='object']");
+        
+        // and when locate the AppLibValues repo's "newEntity" action and walk to its details
         walker.walkXpath("/newEntity[memberType='action']/details");
-        JsonRepresentation jsonRepresentation = walker.getEntity();
         
-        assertThat(jsonRepresentation.getString("_self.link.href"), matches(".+/objects/OID:1/actions/newEntity"));

+        // and when find the invoke body for the "newEntity" action and then walk the action
using the body 
+        JsonRepresentation newEntityActionDetails = walker.getEntity();
+        JsonRepresentation newEntityActionInvokeBody = newEntityActionDetails.getArray("invoke.body");
+        walker.walkXpath("/invoke", newEntityActionInvokeBody);
+        
+        // and when walk the link to the returned object
+        walker.walk("link");
+        
+        // then the returned object is created with its OID
+        JsonRepresentation newEntityDomainObject = walker.getEntity();
+        assertThat(newEntityDomainObject.getString("_self.link.href"), matches(".+/objects/OID:[\\d]+$"));

     }
 
 }



Mime
View raw message