svn commit: r679424 - in /lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source: SiteSource.java SiteSourceFactory.java

[email protected]
Newsgroups gmane.comp.cms.lenya.cvs
Message-ID <[email protected]>
Author: andreas
Date: Thu Jul 24 07:36:20 2008
New Revision: 679424

URL: http://svn.apache.org/viewvc?rev=679424&view=rev
Log:
Call source resolver from SiteSourceFactory directly instead of building a SiteSource object. This way, the source can be properly released.

Removed:
    lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/SiteSource.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/SiteSourceFactory.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/SiteSourceFactory.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/SiteSourceFactory.java?rev=679424&r1=679423&r2=679424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/SiteSourceFactory.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/SiteSourceFactory.java Thu Jul 24 07:36:20 2008
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.util.Map;
+import java.util.StringTokenizer;
 
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.context.ContextException;
@@ -35,6 +36,15 @@
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceException;
 import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentUtil;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.URLInformation;
+import org.apache.lenya.cms.site.SiteStructure;
+import org.apache.lenya.util.ServletHelper;
 
 /**
  * <p>
@@ -61,10 +71,9 @@
 public class SiteSourceFactory extends AbstractLogEnabled implements SourceFactory, ThreadSafe,
         Contextualizable, Serviceable {
 
-    protected static final String SCHEME = "site";
-
     private Context context;
     private ServiceManager manager;
+    private SourceResolver resolver;
 
     /**
      * Used for resolving the object model.
@@ -88,14 +97,71 @@
             IOException, SourceException {
         Map objectModel = ContextHelper.getObjectModel(this.context);
         Request request = ObjectModelHelper.getRequest(objectModel);
-        return new SiteSource(this.manager, request, location, getLogger());
+
+        String areaName = null;
+        String pubId;
+
+        StringTokenizer locationSteps = new StringTokenizer(location, "?");
+        String completePath = locationSteps.nextToken();
+
+        String relativePath;
+        try {
+            this.resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+
+            String scheme = completePath.substring(0, completePath.indexOf(":") + 1);
+            final String absolutePath = completePath.substring(scheme.length());
+            if (absolutePath.startsWith("//")) {
+                final String fullPath = absolutePath.substring(2);
+                StringTokenizer steps = new StringTokenizer(fullPath, "/");
+                pubId = steps.nextToken();
+                areaName = steps.nextToken();
+                String prefix = pubId + "/" + areaName;
+                relativePath = fullPath.substring(prefix.length());
+            } else if (absolutePath.startsWith("/")) {
+                String webappUrl = ServletHelper.getWebappURI(request);
+                URLInformation info = new URLInformation(webappUrl);
+                pubId = info.getPublicationId();
+                areaName = info.getArea();
+                relativePath = absolutePath;
+            } else {
+                throw new MalformedURLException("The path [" + absolutePath
+                        + "] must start with at least one slash.");
+            }
+
+            DocumentFactory factory = DocumentUtil.getDocumentFactory(this.manager, request);
+            Publication pub = factory.getPublication(pubId);
+            SiteStructure site = pub.getArea(areaName).getSite();
+
+            String[] steps = relativePath.substring(1).split("/");
+
+            String language = steps[0];
+            String prefix = "/" + language;
+            String path = relativePath.substring(prefix.length());
+
+            if (site.contains(path, language)) {
+                Document doc = site.getNode(path).getLink(language).getDocument();
+                String docUri = "lenya-document:" + doc.getUUID() + ",lang=" + doc.getLanguage()
+                        + ",area=" + doc.getArea() + ",pub=" + doc.getPublication().getId();
+
+                if (locationSteps.hasMoreTokens()) {
+                    String queryString = locationSteps.nextToken();
+                    docUri = docUri + "?" + queryString;
+                }
+                return this.resolver.resolveURI(docUri);
+            } else {
+                throw new SourceNotFoundException("The source [" + location + "] doesn't exist.");
+            }
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
     /**
      * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
      */
     public void release(Source source) {
-        // Source will be released by delegated source factory.
+        this.resolver.release(source);
     }
 
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.