[Yanel-commits] rev 25624 - in public/yanel/trunk/src: contributions/resources/nutch/src/test/junit/org/wyona/yanel/impl/resources contributions/resources/resource-creator/src/java/org/wyona/yanel/impl/resources core/java/org/wyona/yanel/core core/java/org/wyona/yanel/core/api/attributes core/java/org/wyona/yanel/core/attributes/translatable core/java/org/wyona/yanel/core/source core/java/org/wyona/yanel/core/workflow resources/file/src/java/org/wyona/yanel/impl/resources resources/translation/src/java/org/wyona/yanel/impl/resources resources/xml/src/java/org/wyona/yanel/impl/resources webapp/src/java/org/wyona/yanel/servlet

josias at wyona.com josias at wyona.com
Fri Jun 29 16:27:14 CEST 2007


Author: josias
Date: 2007-06-29 16:27:13 +0200 (Fri, 29 Jun 2007)
New Revision: 25624

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/Area.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/Environment.java
Modified:
   public/yanel/trunk/src/contributions/resources/nutch/src/test/junit/org/wyona/yanel/impl/resources/NutchResourceTest.java
   public/yanel/trunk/src/contributions/resources/resource-creator/src/java/org/wyona/yanel/impl/resources/ResourceCreatorResource.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/api/attributes/VersionableV2.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/AbstractPathTranslationManager.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/workflow/WorkflowHelper.java
   public/yanel/trunk/src/resources/file/src/java/org/wyona/yanel/impl/resources/NodeResource.java
   public/yanel/trunk/src/resources/translation/src/java/org/wyona/yanel/impl/resources/TranslationResource.java
   public/yanel/trunk/src/resources/xml/src/java/org/wyona/yanel/impl/resources/XMLResource.java
   public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java
Log:
added environment object which contains information about the environment from which a resource has been called (it contains e.g. the request, the response, the area, the identity). the environment will be created by the servlet and passed on to the resources, and it will be recursively passed to included resources. this can be used e.g. to find out the correct area (authoring/live) inside of an included resource. see bug #5357

Modified: public/yanel/trunk/src/contributions/resources/nutch/src/test/junit/org/wyona/yanel/impl/resources/NutchResourceTest.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/nutch/src/test/junit/org/wyona/yanel/impl/resources/NutchResourceTest.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/contributions/resources/nutch/src/test/junit/org/wyona/yanel/impl/resources/NutchResourceTest.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -26,6 +26,7 @@
 
 import org.wyona.yanel.core.Path;
 import org.wyona.yanel.core.Resource;
+import org.wyona.yanel.core.Environment;
 import org.wyona.yanel.core.api.attributes.ModifiableV2;
 import org.wyona.yanel.core.map.Map;
 import org.wyona.yanel.core.map.Realm;
@@ -47,7 +48,8 @@
         Map map = yanel.getMap();
         Realm realm = yanel.getMap().getRealm(url);
         String path = yanel.getMap().getPath(realm, url);
-        this.resource = (NutchResource)yanel.getResourceManager().getResource(null, null, realm, path);
+        Environment environment = new Environment(null, null, null, null);
+        this.resource = (NutchResource)yanel.getResourceManager().getResource(environment, realm, path);
         confDir = this.resource.getRTD().getConfigFile().getParentFile().getAbsolutePath() + 
                     File.separator + "conf" + File.separator;
     }

Modified: public/yanel/trunk/src/contributions/resources/resource-creator/src/java/org/wyona/yanel/impl/resources/ResourceCreatorResource.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/resource-creator/src/java/org/wyona/yanel/impl/resources/ResourceCreatorResource.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/contributions/resources/resource-creator/src/java/org/wyona/yanel/impl/resources/ResourceCreatorResource.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -454,7 +454,7 @@
         String rtps = resourceType;
         String resNamespace = rtps.substring(0, rtps.indexOf("::"));
         String resName = rtps.substring(rtps.indexOf("::") + 2);
-        Resource newResource = yanel.getResourceManager().getResource(request, response, realm, pathOfNewResource.toString(), new ResourceConfiguration(resName, resNamespace, null));
+        Resource newResource = yanel.getResourceManager().getResource(getEnvironment(), realm, pathOfNewResource.toString(), new ResourceConfiguration(resName, resNamespace, null));
         
         if (newResource != null) {
             if (ResourceAttributeHelper.hasAttributeImplemented(newResource, "Creatable", "2")) {
@@ -647,7 +647,7 @@
                 for (int i = 0; i < children.length; i++) {
                     String resourceTypeName;
                     try {
-                        resourceTypeName = yanel.getResourceManager().getResource(request, response, realm, children[i].getPath()).getResourceTypeLocalName();
+                        resourceTypeName = yanel.getResourceManager().getResource(getEnvironment(), realm, children[i].getPath()).getResourceTypeLocalName();
                     } catch (Exception e) {
                         log.error(e.getMessage(), e);
                         resourceTypeName = "?";

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Area.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Area.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Area.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.wyona.yanel.core;
+
+/**
+ *
+ */
+public class Area {
+
+    public static final String AUTHORING = "authoring";
+    
+    public static final String LIVE = "live";
+    
+}


Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Area.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Environment.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Environment.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Environment.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.wyona.yanel.core;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.Category;
+import org.wyona.security.core.api.Identity;
+
+/**
+ * The environment stores items which belong to the environment from which a resource
+ * is called, i.e. the request, the response, the identity, and the area.
+ */
+public class Environment {
+
+    private static Category log = Category.getInstance(Environment.class);
+
+    private HttpServletRequest request;
+    private HttpServletResponse response;
+    private Identity identity;
+    private String area;
+    
+    public Environment(HttpServletRequest request, HttpServletResponse response, Identity identity, String area) {
+        this.request = request;
+        this.response = response;
+        this.identity = identity;
+        this.area = area;
+    }
+
+    public Identity getIdentity() {
+        return identity;
+    }
+
+    public HttpServletRequest getRequest() {
+        return request;
+    }
+
+    public HttpServletResponse getResponse() {
+        return response;
+    }
+
+    public String getArea() {
+        return area;
+    }
+
+    public void setArea(String area) {
+        this.area = area;
+    }
+
+    public void setIdentity(Identity identity) {
+        this.identity = identity;
+    }
+
+    public void setRequest(HttpServletRequest request) {
+        this.request = request;
+    }
+
+    public void setResponse(HttpServletResponse response) {
+        this.response = response;
+    }
+
+}


Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Environment.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -35,6 +35,7 @@
 
     private static Category log = Category.getInstance(Resource.class);
 
+    // TODO: make protected members private (access in subclasses via getter/setter methods)
     protected ResourceTypeDefinition rtd;
     protected ResourceTypeIdentifier rti;
     protected ResourceConfiguration rc;
@@ -43,6 +44,7 @@
     protected Realm realm;
     protected HttpServletRequest request;
     protected HttpServletResponse response;
+    private Environment environment;
     protected Map parameters;
 
     /**
@@ -150,20 +152,32 @@
         return rc;
     }
 
+    /**
+     * @deprecated use getEnvironment().getRequest() instead  
+     */
     public HttpServletRequest getRequest() {
-        return request;
+        return environment.getRequest();
     }
 
+    /**
+     * @deprecated use getEnvironment().setRequest() instead 
+     */
     public void setRequest(HttpServletRequest request) {
-        this.request = request;
+        environment.setRequest(request);
     }
 
+    /**
+     * @deprecated use getEnvironment().getResponse() instead  
+     */
     public HttpServletResponse getResponse() {
-        return response;
+        return environment.getResponse();
     }
 
+    /**
+     * @deprecated use getEnvironment().setResponse() instead
+     */
     public void setResponse(HttpServletResponse response) {
-        this.response = response;
+        environment.setResponse(response);
     }
 
     /**
@@ -198,10 +212,35 @@
      * @return parameter string or null if no parameter with this name exists.
      */
     public String getParameterAsString(String name) {
-        return this.parameters.get(name).toString();
+        Object param = this.parameters.get(name);
+        if (param == null) {
+            return null;
+        } else {
+            return param.toString();
+        }
     }
 
     /**
+     * Gets the parameter values with the given name as a string array.
+     * @param name
+     * @return array containing the parameter values, or empty array if parameter does not exist.
+     */
+    public String[] getParameterAsStringValues(String name) {
+        Object param = this.parameters.get(name);
+        if (param == null) {
+            return new String[0];
+        }
+        if (param instanceof String) {
+            String[] values = new String[1];
+            values[0] = (String)param;
+            return values;
+        } else if (param instanceof String[]) {
+            return (String [])param;
+        }
+        return null;
+    }
+
+    /**
      * Sets the parameter with the given name and value.
      * @param name
      * @param value
@@ -275,5 +314,15 @@
         return getRTI().getProperties(name);
     }
 
+    public Environment getEnvironment() {
+        return environment;
+    }
 
+    public void setEnvironment(Environment environment) {
+        this.environment = environment;
+        this.request = environment.getRequest();
+        this.response = environment.getResponse();
+    }
+
+
 }

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -53,8 +53,8 @@
      *
      * @param path Path relative to realm (e.g. yanel.getMap().getPath(realm, request.getServletPath()))
      */
-    public Resource getResource(HttpServletRequest request, HttpServletResponse response, 
-            Realm realm, String path, ResourceTypeDefinition rtd, ResourceTypeIdentifier rti) 
+    public Resource getResource(Environment environment, Realm realm, String path, 
+            ResourceTypeDefinition rtd, ResourceTypeIdentifier rti) 
     throws Exception {
         String universalName = rtd.getResourceTypeUniversalName();
         if (rtd != null) {
@@ -65,10 +65,9 @@
             resource.setRealm(realm);
             resource.setPath(path);
             resource.setRTI(rti);
-            resource.setRequest(request);
-            resource.setResponse(response);
+            resource.setEnvironment(environment);
             
-            passParameters(resource, request);
+            passParameters(resource, environment.getRequest());
             
             return resource;
         } else {
@@ -82,7 +81,7 @@
      *
      * @param path Path relative to realm (e.g. yanel.getMap().getPath(realm, request.getServletPath()))
      */
-    public Resource getResource(HttpServletRequest request, HttpServletResponse response, Realm realm, String path, ResourceConfiguration rc) throws Exception {
+    public Resource getResource(Environment environment, Realm realm, String path, ResourceConfiguration rc) throws Exception {
         ResourceTypeDefinition rtd = rtRegistry.getResourceTypeDefinition(rc.getUniversalName());
 
         if (rtd != null) {
@@ -94,10 +93,9 @@
             resource.setRealm(realm);
             resource.setPath(path);
             resource.setConfiguration(rc);
-            resource.setRequest(request);
-            resource.setResponse(response);
+            resource.setEnvironment(environment);
             
-            passParameters(resource, request);
+            passParameters(resource, environment.getRequest());
             
             return resource;
         } else {
@@ -111,10 +109,10 @@
      *
      * @param path Path relative to realm (e.g. yanel.getMap().getPath(realm, request.getServletPath()))
      */
-    public Resource getResource(HttpServletRequest request, HttpServletResponse response, Realm realm, String path) throws Exception {
+    public Resource getResource(Environment environment, Realm realm, String path) throws Exception {
         if (realm.getRTIRepository().exists(new Path(PathUtil.getRCPath(path)))) {
             ResourceConfiguration rc = new ResourceConfiguration(realm.getRTIRepository().getInputStream(new Path(PathUtil.getRCPath(path))));
-            if (rc != null) return getResource(request, response, realm, path, rc);
+            if (rc != null) return getResource(environment, realm, path, rc);
         }
 
         if (realm.getRTIRepository().exists(new Path(PathUtil.getRTIPath(path)))) {
@@ -122,16 +120,16 @@
             log.warn("DEPRECATED: RTI should be replaced by ResourceConfiguration: " + realm + ", " + path);
             ResourceTypeIdentifier rti = getResourceTypeIdentifier(realm, path);
             ResourceTypeDefinition rtd = rtRegistry.getResourceTypeDefinition(rti.getUniversalName());
-            return getResource(request, response, realm, path, rtd, rti);
+            return getResource(environment, realm, path, rtd, rti);
         } 
 
         String rcPath = ResourceConfigurationMap.getRCPath(realm, path);
         if (rcPath != null && realm.getRTIRepository().exists(new Path(rcPath))) {
             ResourceConfiguration rc = new ResourceConfiguration(realm.getRTIRepository().getInputStream(new Path(ResourceConfigurationMap.getRCPath(realm, path))));
-            if (rc != null) return getResource(request, response, realm, path, rc);
+            if (rc != null) return getResource(environment, realm, path, rc);
         } 
         
-        return getResource(request, response, realm, path, new ResourceConfiguration("file", "http://www.wyona.org/yanel/resource/1.0", null));
+        return getResource(environment, realm, path, new ResourceConfiguration("file", "http://www.wyona.org/yanel/resource/1.0", null));
         
     }
 

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -143,7 +143,7 @@
      * use getResourceManager().getResource() instead 
      */
     public Resource getResource(Realm realm, String path) throws Exception {
-        return resourceManager.getResource(null, null, realm, path);
+        return resourceManager.getResource(null, realm, path);
     }
 
     /**

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/api/attributes/VersionableV2.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/api/attributes/VersionableV2.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/api/attributes/VersionableV2.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -27,8 +27,9 @@
 public interface VersionableV2 {
 
     /**
-     * Gets an array of revision information objects.
-     * @return informations of the various revisions
+     * Gets an array of revision information objects, oldest revision at the first array
+     * position, newest at the last position.
+     * @return informations of the various revisions, or empty array if there are no revisions.
      * @throws Exception
      */
     public RevisionInformation[] getRevisions() throws Exception;

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/AbstractPathTranslationManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/AbstractPathTranslationManager.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/AbstractPathTranslationManager.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -93,7 +93,7 @@
     protected Resource getResource(Resource resource, String language) throws Exception {
         String possiblePath = getPath(resource.getPath(), language);
         if (possiblePath != null) {
-            Resource possibleResource = getResourceManager().getResource(resource.getRequest(), resource.getResponse(), resource.getRealm(), possiblePath);
+            Resource possibleResource = getResourceManager().getResource(resource.getEnvironment(), resource.getRealm(), possiblePath);
             if (ResourceAttributeHelper.hasAttributeImplemented(possibleResource, "Viewable", "2")) {
                 ViewableV2 viewable = (ViewableV2)possibleResource;
                 if (viewable.exists()) {
@@ -141,7 +141,7 @@
         if (page != null && page.containsKey(language)) {
             LanguageVersion target = (LanguageVersion)page.get(language);
             try {
-                return getResourceManager().getResource(resource.getRequest(), resource.getResponse(), resource.getRealm(), target.path);
+                return getResourceManager().getResource(resource.getEnvironment(), resource.getRealm(), target.path);
             } catch (Exception e) {
                 throw new TranslationException(e.getMessage(), e);
             }

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -116,7 +116,7 @@
             ResourceManager resourceManager;
             try {
                 resourceManager = Yanel.getInstance().getResourceManager();
-                return resourceManager.getResource(resource.getRequest(), resource.getResponse(), resource.getRealm(), target.path);
+                return resourceManager.getResource(resource.getEnvironment(), resource.getRealm(), target.path);
             } catch (Exception e) {
                 throw new TranslationException(e.getMessage(), e);
             }

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -55,7 +55,7 @@
                 uri = uri.substring(0, uri.indexOf("?"));
             }
             ResourceManager manager = Yanel.getInstance().getResourceManager();
-            Resource targetResource = manager.getResource(resource.getRequest(), resource.getResponse(), 
+            Resource targetResource = manager.getResource(resource.getEnvironment(), 
                     resource.getRealm(), uri);
             targetResource.setParameters(parameters);
             if (ResourceAttributeHelper.hasAttributeImplemented(targetResource, "Viewable", "1")) {

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/workflow/WorkflowHelper.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/workflow/WorkflowHelper.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/workflow/WorkflowHelper.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -21,8 +21,10 @@
 import org.apache.log4j.Category;
 import org.wyona.yanel.core.Resource;
 import org.wyona.yanel.core.api.attributes.VersionableV2;
+import org.wyona.yanel.core.api.attributes.ViewableV2;
 import org.wyona.yanel.core.api.attributes.WorkflowableV1;
 import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
+import org.wyona.yanel.core.attributes.viewable.View;
 import org.wyona.yanel.core.util.DateUtil;
 import org.wyona.yarep.core.Node;
 import org.wyona.yarep.core.Property;
@@ -30,7 +32,7 @@
 
 /**
  * This class provides some helper methods related to workflow for resources 
- * which are WorkflowableV1 and VersionableV2 and which implement workflow
+ * which are WorkflowableV1, VersionableV2, and ViewableV2 and which implement workflow
  * according to the following conditions:
  * - there is a resource config property 'workflow-schema' which points to the schema
  * - the workflow information is stored as properties of a repository node in the repository
@@ -108,17 +110,35 @@
     public static String getLiveRevision(Resource resource) throws WorkflowException {
         try {
             WorkflowableV1 workflowable = (WorkflowableV1)resource;
-            return workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY);
+            if (getWorkflow(resource) == null) {
+                RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
+                if (revisions.length == 0) {
+                    return null;
+                } else {
+                    return revisions[revisions.length-1].getName();
+                }
+            } else {
+                return workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY);
+            }
         } catch (Exception e) {
             log.error(e, e);
             throw new WorkflowException(e.getMessage(), e);
         }
     }
 
+    /**
+     * Indicates whether the given resource is live, i.e. if the workflow variable
+     * &quot;live-revision&quot; has a defined value.
+     * If a resource does not have an associated workflow, it is considered as live.
+     * @param resource
+     * @return true if live, false otherwise
+     * @throws WorkflowException
+     */
     public static boolean isLive(Resource resource) throws WorkflowException {
         try {
             WorkflowableV1 workflowable = (WorkflowableV1)resource;
-            if (workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY) != null) {
+            if (getWorkflow(resource) == null ||
+                    workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY) != null) {
                 return true;
             } else {
                 return false;
@@ -129,6 +149,20 @@
         }
     }
     
+    public static View getLiveView(Resource resource, String viewid) throws Exception {
+        if (((WorkflowableV1)resource).isLive()) {
+            String liveRevision = getLiveRevision(resource);
+            if (liveRevision != null) {
+                return ((VersionableV2)resource).getView(viewid, liveRevision);
+            } else {
+                // if there are no revisions, show the normal view:
+                return ((ViewableV2)resource).getView(viewid);
+            }
+        } else {
+            return null;
+        }
+    }
+
     public static Workflow getWorkflow(Resource resource) throws WorkflowException {
         try {
             String workflowSchema = resource.getResourceConfigProperty("workflow-schema");

Modified: public/yanel/trunk/src/resources/file/src/java/org/wyona/yanel/impl/resources/NodeResource.java
===================================================================
--- public/yanel/trunk/src/resources/file/src/java/org/wyona/yanel/impl/resources/NodeResource.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/resources/file/src/java/org/wyona/yanel/impl/resources/NodeResource.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -224,22 +224,20 @@
     }
     
     /**
-     * Get revisions
+     * @see org.wyona.yanel.core.api.attributes.VersionableV2#getRevisions()
      */
     public RevisionInformation[] getRevisions() throws Exception {
         Revision[] revisions = getRealm().getRepository().getNode(getPath()).getRevisions();
 
-        if (revisions != null && revisions.length > 0) {
-            RevisionInformation[] revisionInfos = new RevisionInformation[revisions.length];
-       
-            for (int i = 0; i < revisions.length; i++) {
-                revisionInfos[i] = new RevisionInformation(revisions[i]);
-            }
-            return revisionInfos;
-        } else {
+        RevisionInformation[] revisionInfos = new RevisionInformation[revisions.length];
+   
+        for (int i = 0; i < revisions.length; i++) {
+            revisionInfos[i] = new RevisionInformation(revisions[i]);
+        }
+        if (revisions.length > 0) {
             log.warn("Node \"" + getPath() + "\" does not seem to have any revisions! The repository \"" + getRealm().getRepository() + "\"  might not support revisions!");
-            return null;
         }
+        return revisionInfos;
     }
    
     public void checkin(String comment) throws Exception {
@@ -444,12 +442,7 @@
      *
      */
     public View getLiveView(String viewid) throws Exception {
-        if (WorkflowHelper.isLive(this)) {
-            String liveRevision = WorkflowHelper.getLiveRevision(this);
-            return getView(viewid, liveRevision);
-        } else {
-            return null;
-        }
+        return WorkflowHelper.getLiveView(this, viewid);
     }
 
     /**

Modified: public/yanel/trunk/src/resources/translation/src/java/org/wyona/yanel/impl/resources/TranslationResource.java
===================================================================
--- public/yanel/trunk/src/resources/translation/src/java/org/wyona/yanel/impl/resources/TranslationResource.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/resources/translation/src/java/org/wyona/yanel/impl/resources/TranslationResource.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -100,7 +100,7 @@
         
         
         try {
-            Resource resource = getYanel().getResourceManager().getResource(getRequest(), getResponse(), getRealm(), currentPath);
+            Resource resource = getYanel().getResourceManager().getResource(getEnvironment(), getRealm(), currentPath);
             
             Repository repo = getRealm().getRepository();
 

Modified: public/yanel/trunk/src/resources/xml/src/java/org/wyona/yanel/impl/resources/XMLResource.java
===================================================================
--- public/yanel/trunk/src/resources/xml/src/java/org/wyona/yanel/impl/resources/XMLResource.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/resources/xml/src/java/org/wyona/yanel/impl/resources/XMLResource.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -213,7 +213,8 @@
     private InputStream getContentXML(Repository repo, String yanelPath, String revisionName) throws Exception {
         if (yanelPath != null) {
             log.debug("Yanel Path: " + yanelPath);
-            Resource res = yanel.getResourceManager().getResource(getRequest(), getResponse(), getRealm(), yanelPath);
+            Resource res = yanel.getResourceManager().getResource(getEnvironment(), 
+                    getRealm(), yanelPath);
             if (ResourceAttributeHelper.hasAttributeImplemented(res, "Viewable", "1")) {
                 // TODO: Pass the request ...
                 String viewV1path = getRealm().getMountPoint() + yanelPath.substring(1);
@@ -346,7 +347,7 @@
     }
 
     /**
-     *
+     * @see org.wyona.yanel.core.api.attributes.VersionableV2#getRevisions()
      */
     public RevisionInformation[] getRevisions() throws Exception {
         Revision[] revisions = getRealm().getRepository().getNode(getPath()).getRevisions();
@@ -650,12 +651,7 @@
     }
     
     public View getLiveView(String viewid) throws Exception {
-        if (WorkflowHelper.isLive(this)) {
-            String liveRevision = WorkflowHelper.getLiveRevision(this);
-            return getView(viewid, liveRevision);
-        } else {
-            return null;
-        }
+        return WorkflowHelper.getLiveView(this, viewid);
     }
 
     public boolean isLive() throws WorkflowException {

Modified: public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java
===================================================================
--- public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java	2007-06-29 13:22:02 UTC (rev 25623)
+++ public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java	2007-06-29 14:27:13 UTC (rev 25624)
@@ -31,6 +31,8 @@
 import javax.xml.transform.sax.TransformerHandler;
 import javax.xml.transform.stream.StreamSource;
 
+import org.wyona.yanel.core.Area;
+import org.wyona.yanel.core.Environment;
 import org.wyona.yanel.core.Path;
 import org.wyona.yanel.core.Resource;
 import org.wyona.yanel.core.ResourceConfiguration;
@@ -329,10 +331,12 @@
 
         String usecase = request.getParameter("yanel.resource.usecase");
 
+        
         Resource res = null;
         long lastModified = -1;
         long size = -1;
             try {
+                Environment environment = getEnvironment(request, response);
                 res = getResource(request, response);
                 if (res != null) {
 
@@ -422,6 +426,20 @@
                             String revisionName = request.getParameter("yanel.resource.revision");
                             if (ResourceAttributeHelper.hasAttributeImplemented(res, "Versionable", "2") && revisionName != null) {
                                 view = ((VersionableV2) res).getView(viewId, revisionName);
+                            } else if (ResourceAttributeHelper.hasAttributeImplemented(res, "Workflowable", "1") && environment.getArea().equals(Area.LIVE)) {
+                                WorkflowableV1 workflowable = (WorkflowableV1)res;
+                                if (workflowable.isLive()) {
+                                    view = workflowable.getLiveView(viewId);
+                                } else {
+                                    String message = "This document has not been published yet.";
+                                    log.warn(message);
+                                    Element exceptionElement = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "exception"));
+                                    exceptionElement.appendChild(doc.createTextNode(message));
+                                    exceptionElement.setAttributeNS(NAMESPACE, "status", "404");
+                                    response.setStatus(javax.servlet.http.HttpServletResponse.SC_NOT_FOUND);
+                                    setYanelOutput(request, response, doc);
+                                    return;
+                                }
                             } else {
                                 view = ((ViewableV2) res).getView(viewId);
                             }
@@ -492,8 +510,7 @@
                         if (ResourceAttributeHelper.hasAttributeImplemented(res, "Versionable", "2")) {
                             // note: this will throw an exception if the document is checked out already
                             // by another user.
-                            Identity identity = getIdentity(request);
-                            String userID = identity.getUsername();
+                            String userID = environment.getIdentity().getUsername();
                             VersionableV2 versionable = (VersionableV2)res;
                             if (versionable.isCheckedOut()) {
                                 String checkoutUserID = versionable.getCheckoutUserID(); 
@@ -644,7 +661,7 @@
                     String newEntryPath = yanel.getMap().getPath(realm, request.getServletPath() + "/" + new java.util.Date().getTime() + ".xml");
 
                     log.error("DEBUG: Realm and Path of new Atom entry: " + realm + " " + newEntryPath);
-                    Resource atomEntryResource = yanel.getResourceManager().getResource(request, response, realm, newEntryPath, new ResourceTypeRegistry().getResourceTypeDefinition(atomEntryUniversalName), new ResourceTypeIdentifier(atomEntryUniversalName, null));
+                    Resource atomEntryResource = yanel.getResourceManager().getResource(getEnvironment(request, response), realm, newEntryPath, new ResourceTypeRegistry().getResourceTypeDefinition(atomEntryUniversalName), new ResourceTypeIdentifier(atomEntryUniversalName, null));
                     
                     ((ModifiableV2)atomEntryResource).write(in);
 
@@ -706,7 +723,7 @@
 
                     log.error("DEBUG: Realm and Path of new Atom entry: " + realm + " " + entryPath);
 
-                    Resource atomEntryResource = yanel.getResourceManager().getResource(request, response, realm, entryPath, new ResourceTypeRegistry().getResourceTypeDefinition(atomEntryUniversalName), new ResourceTypeIdentifier(atomEntryUniversalName, null));
+                    Resource atomEntryResource = yanel.getResourceManager().getResource(getEnvironment(request, response), realm, entryPath, new ResourceTypeRegistry().getResourceTypeDefinition(atomEntryUniversalName), new ResourceTypeIdentifier(atomEntryUniversalName, null));
                     
                     // TODO: There seems to be a problem ...
                     ((ModifiableV2)atomEntryResource).write(in);
@@ -774,7 +791,7 @@
             String path = map.getPath(realm, request.getServletPath());
             HttpRequest httpRequest = new HttpRequest(request);
             HttpResponse httpResponse = new HttpResponse(response);
-            Resource res = yanel.getResourceManager().getResource(httpRequest, httpResponse, realm, path);
+            Resource res = yanel.getResourceManager().getResource(getEnvironment(httpRequest, httpResponse), realm, path);
             
             return res;
         } catch(Exception e) {
@@ -786,6 +803,35 @@
     }
 
     /**
+     *
+     */
+    private Environment getEnvironment(HttpServletRequest request, HttpServletResponse response) throws ServletException {
+        Identity identity;
+        try {
+            identity = getIdentity(request);
+            if (identity == null) {
+                identity = new Identity(); // world
+            }
+            // TODO: implement detection of area
+            String area = Area.AUTHORING;
+            //String area = map.getArea(request.getServletPath());
+            //System.out.println("url: " + request.getServletPath());
+            //System.out.println("area: " + area);
+            /*String area = null;
+            Object toolbarAttr = request.getSession().getAttribute(TOOLBAR_KEY); 
+            if (toolbarAttr != null && toolbarAttr.equals("on")) {
+                area = "authoring";
+            } else {
+                area = "live";
+            }*/
+            Environment environment = new Environment(request, response, identity, area);
+            return environment;
+        } catch (Exception e) {
+            throw new ServletException(e.getMessage(), e);
+        }
+    }
+
+    /**
      * Save data
      */
     private void save(HttpServletRequest request, HttpServletResponse response, boolean doCheckin) throws ServletException, IOException {
@@ -1965,7 +2011,7 @@
                 properties.put("user", userName);
                 ResourceConfiguration rc = new ResourceConfiguration("yanel-user", "http://www.wyona.org/yanel/resource/1.0", properties);
                 Realm realm = yanel.getMap().getRealm(request.getServletPath());
-                Resource yanelUserResource = yanel.getResourceManager().getResource(request, response, realm, path, rc);
+                Resource yanelUserResource = yanel.getResourceManager().getResource(getEnvironment(request, response), realm, path, rc);
                 View view = ((ViewableV2) yanelUserResource).getView(viewId);
                 if (view != null) {
                     if (generateResponse(view, yanelUserResource, request, response, getDocument(NAMESPACE, "yanel"), -1, -1) != null) return;
@@ -1991,7 +2037,7 @@
                 //properties.put("user", userName);
                 ResourceConfiguration rc = new ResourceConfiguration("data-repo-sitetree", "http://www.wyona.org/yanel/resource/1.0", properties);
                 Realm realm = yanel.getMap().getRealm(request.getServletPath());
-                Resource sitetreeResource = yanel.getResourceManager().getResource(request, response, realm, path, rc);
+                Resource sitetreeResource = yanel.getResourceManager().getResource(getEnvironment(request, response), realm, path, rc);
                 View view = ((ViewableV2) sitetreeResource).getView(viewId);
                 if (view != null) {
                     if (generateResponse(view, sitetreeResource, request, response, getDocument(NAMESPACE, "yanel"), -1, -1) != null) return;
@@ -2010,7 +2056,7 @@
                 java.util.Map properties = new HashMap();
                 Realm realm = yanel.getMap().getRealm(request.getServletPath());
                 ResourceConfiguration rc = new ResourceConfiguration(name, namespace, properties);
-                Resource resourceOfPrefix = yanel.getResourceManager().getResource(request, response, realm, path, rc);
+                Resource resourceOfPrefix = yanel.getResourceManager().getResource(getEnvironment(request, response), realm, path, rc);
                 File resourceFile = org.wyona.commons.io.FileUtil.file(resourceOfPrefix.getRTD().getConfigFile().getParentFile().getAbsolutePath(), "htdocs" + htdocsPath);
 
                 if (resourceFile.exists()) {




More information about the Yanel-commits mailing list