cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject svn commit: r178038 - in /cocoon/trunk/src/java/org/apache/cocoon/components/source/impl: BlocksSource.java BlocksSourceFactory.java SitemapSourceInfo.java
Date Mon, 23 May 2005 21:32:04 GMT
Author: danielf
Date: Mon May 23 14:32:04 2005
New Revision: 178038

URL: http://svn.apache.org/viewcvs?rev=178038&view=rev
Log:
Created a blocks: source for being able to access mounted blocks from the root sitemap. This
source is only added for being able to use the blocks without changing of the Cocoon object.
It should probably be removed later. Also facored out the cocoon-view parsing in SitemapSourceInfo
to be able to use it in the BlocksSource.

Added:
    cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSource.java   (with
props)
    cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSourceFactory.java
  (with props)
Modified:
    cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/SitemapSourceInfo.java

Added: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSource.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSource.java?rev=178038&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSource.java (added)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSource.java Mon May
23 14:32:04 2005
@@ -0,0 +1,163 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
+ * 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.apache.org/licenses/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.apache.cocoon.components.source.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.apache.cocoon.components.blocks.BlocksManager;
+import org.apache.cocoon.environment.Environment;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.environment.wrapper.EnvironmentWrapper;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.impl.AbstractSource;
+
+/**
+ * Implementation of a {@link Source} that gets its content by
+ * invoking the BlocksManager. 
+ *
+ * WARNING: It is created for being able to use the blocks
+ * functionality without needing to change the Cocoon object and other
+ * fundamental classes. This class will probably be removed later.
+ *
+ * @version $Id$ */
+public final class BlocksSource
+    extends AbstractSource {
+
+    /** The current ServiceManager */
+    private final ServiceManager manager;
+
+    /** The environment */
+    private final EnvironmentWrapper environment;
+
+    /**
+     * Construct a new object
+     */
+    public BlocksSource(ServiceManager manager,
+                        String         uri,
+                        Map            parameters,
+                        Logger         logger)
+        throws MalformedURLException {
+
+        Environment env = EnvironmentHelper.getCurrentEnvironment();
+        if ( env == null ) {
+            throw new MalformedURLException("The blocks protocol can not be used outside
an environment.");
+        }
+        this.manager = manager;
+
+        SitemapSourceInfo info = null;
+        try {
+            info = parseBlocksURI(env, uri);
+        } catch (URISyntaxException e) {
+            throw new MalformedURLException("Malformed URI in blocks source " +
+					    e.getMessage());
+        }
+        setScheme(info.protocol);
+        setSystemId(info.systemId);
+
+        // create environment...
+        this.environment = new EnvironmentWrapper(env, info, logger);
+
+        // ...and put information passed from the parent request to the internal request
+        if ( null != parameters ) {
+            this.environment.getObjectModel().put(ObjectModelHelper.PARENT_CONTEXT, parameters);
+        } else {
+            this.environment.getObjectModel().remove(ObjectModelHelper.PARENT_CONTEXT);
+        }
+
+        this.environment.setURI(info.prefix, info.uri);
+    }
+
+    /**
+     * Return an <code>InputStream</code> object to read from the source.
+     */
+    public InputStream getInputStream()
+        throws IOException, SourceException {
+
+        BlocksManager blocks = null;
+        try {
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            this.environment.setOutputStream(os);
+
+            blocks = (BlocksManager)this.manager.lookup(BlocksManager.ROLE);
+            blocks.process(this.environment);
+            
+            return new ByteArrayInputStream(os.toByteArray());
+
+        } catch (ResourceNotFoundException e) {
+            throw new SourceNotFoundException("Exception during processing of " + this.getURI(),
e);
+        } catch (Exception e) {
+            throw new SourceException("Exception during processing of " + this.getURI(),
e);
+        } finally {
+            this.manager.release(blocks);
+            // Unhide wrapped environment output stream
+            this.environment.setOutputStream(null);
+        }
+    }
+
+    /**
+     * Returns true always.
+     * @see org.apache.excalibur.source.Source#exists()
+     */
+    public boolean exists() {
+        return true;
+    }
+
+    /**
+     * Recyclable
+     */
+    public void recycle() {
+    }
+
+    // Parse the blocks protocol.
+    private static SitemapSourceInfo parseBlocksURI(Environment env, String sitemapURI) 
+        throws URISyntaxException {
+        URI uri = new URI(sitemapURI);
+
+        if (!uri.isAbsolute() && uri.isOpaque() && uri.getAuthority() ==
null) {
+            throw new URISyntaxException(sitemapURI,
+					 "The blocks protocol must be an absolute, hierachial URI whithout authority part");
+        }
+
+        SitemapSourceInfo info = new SitemapSourceInfo();
+        // the blocks protocol is for redirecting the call to a
+        // mounted block, so using the request parameters from the
+        // original request is reasonable.
+        info.rawMode = false;
+        info.protocol = uri.getScheme();
+        // The blocks protocol is always from the root context
+        info.prefix = "";
+        info.uri = uri.getPath();
+        info.requestURI = info.prefix + info.uri;
+        info.processFromRoot = true;
+        info.queryString = uri.getQuery();
+        info.view = SitemapSourceInfo.getView(info.queryString, env);
+        info.systemId = uri.toString();
+
+        return info;
+    }
+}

Propchange: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSourceFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSourceFactory.java?rev=178038&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSourceFactory.java
(added)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSourceFactory.java
Mon May 23 14:32:04 2005
@@ -0,0 +1,90 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * 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.apache.org/licenses/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.apache.cocoon.components.source.impl;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.URIAbsolutizer;
+import org.apache.excalibur.source.SourceUtil;
+
+/**
+ * This class implements the blocks: protocol.
+ *
+ * WARNING: It is created for being able to use the blocks
+ * functionality without needing to change the Cocoon object and other
+ * fundamental classes. This class will probably be removed later.
+ *
+ * @version $Id$ */
+public final class BlocksSourceFactory
+    extends AbstractLogEnabled
+    implements SourceFactory, ThreadSafe, Serviceable, URIAbsolutizer
+{
+    
+    /** The <code>ServiceManager</code> */
+    private ServiceManager manager;
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
+     */
+    public Source getSource( String location, Map parameters )
+        throws MalformedURLException, IOException {
+        if( getLogger().isDebugEnabled() ) {
+            getLogger().debug( "Creating source object for " + location );
+        }
+
+        return new BlocksSource( this.manager,
+                                 location,
+                                 parameters,
+                                 getLogger());
+    }
+    
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+     */
+    public void release( Source source ) {
+        if ( null != source ) {
+            if ( this.getLogger().isDebugEnabled() ) {
+                this.getLogger().debug("Releasing source " + source.getURI());
+            }
+            ((BlocksSource)source).recycle();
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.excalibur.source.URIAbsolutizer#absolutize(java.lang.String, java.lang.String)
+     */
+    public String absolutize(String baseURI, String location) {
+        return SourceUtil.absolutize(baseURI, location, true);
+    }
+
+}

Propchange: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/BlocksSourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/SitemapSourceInfo.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/SitemapSourceInfo.java?rev=178038&r1=178037&r2=178038&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/SitemapSourceInfo.java
(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/source/impl/SitemapSourceInfo.java
Mon May 23 14:32:04 2005
@@ -112,27 +112,7 @@
 
         
         // determine if the queryString specifies a cocoon-view
-        info.view = null;
-        if (info.queryString != null) {
-            int index = info.queryString.indexOf(Constants.VIEW_PARAM);
-            if (index != -1 
-                    && (index == 0 || info.queryString.charAt(index-1) == '&')
-                    && info.queryString.length() > index + Constants.VIEW_PARAM.length()

-                    && info.queryString.charAt(index+Constants.VIEW_PARAM.length())
== '=') {
-                
-                String tmp = info.queryString.substring(index+Constants.VIEW_PARAM.length()+1);
-                index = tmp.indexOf('&');
-                if (index != -1) {
-                    info.view = tmp.substring(0,index);
-                } else {
-                    info.view = tmp;
-                }
-            } else {
-                info.view = env.getView();
-            }
-        } else {
-            info.view = env.getView();
-        }
+        info.view = getView(info.queryString, env);
 
         // build the request uri which is relative to the context
         info.requestURI = info.prefix + info.uri;
@@ -147,5 +127,27 @@
 
         return info;
     }
-    
+
+    public static String getView(String query, Environment env) {
+        if (query != null) {
+            int index = query.indexOf(Constants.VIEW_PARAM);
+            if (index != -1 
+                    && (index == 0 || query.charAt(index-1) == '&')
+                    && query.length() > index + Constants.VIEW_PARAM.length()

+                    && query.charAt(index+Constants.VIEW_PARAM.length()) == '=')
{
+                
+                String tmp = query.substring(index+Constants.VIEW_PARAM.length()+1);
+                index = tmp.indexOf('&');
+                if (index != -1) {
+                    return tmp.substring(0,index);
+                } else {
+                    return tmp;
+                }
+            } else {
+                return env.getView();
+            }
+        } else {
+            return env.getView();
+        }
+    }
 }



Mime
View raw message