ghoward 2003/10/22 18:45:34
Modified: src/webapp/samples/sources sitemap.xmap samples.xml
src/webapp/WEB-INF cocoon.xconf
Added: src/webapp/samples/sources/content upload.xml
src/java/org/apache/cocoon/components/source/impl
PartSource.java PartSourceFactory.java
Log:
Add new multipart file upload Part Source contributed by
Paul Crabtree with samples. "Fixes" bug
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=23931
Revision Changes Path
1.1 cocoon-2.1/src/webapp/samples/sources/content/upload.xml
Index: upload.xml
===================================================================
<?xml version="1.0"?>
<!-- CVS $Id: upload.xml,v 1.1 2003/10/23 01:45:34 ghoward Exp $ -->
<page>
<title>Demonstrate file upload as source</title>
<content>
<para>
Hi there! I'm a simple static page read from a file, but that's not the
interesting part of this sample. Select any xml file from your local
drive, and upload it. The resulting page will display that xml
transformed
by a stylesheet (which could also be provided via upload).
</para>
<para>
You must have configured enable-uploads as true in web.xml or built
Cocoon
with the config.enable-uploads=true property set for this sample to
work.
</para>
<para>
<form method="post" enctype="multipart/form-data"
action="xml-upload-post" >
File: <input type="file" name="formFieldOne" size="50" />
<p>
<input type="submit" value="Upload File" />
</p>
</form>
</para>
</content>
</page>
1.1
cocoon-2.1/src/java/org/apache/cocoon/components/source/impl/PartSource.java
Index: PartSource.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.source.impl;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceValidity;
import org.apache.excalibur.source.SourceNotFoundException;
import org.apache.cocoon.servlet.multipart.Part;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.components.CocoonComponentManager;
import java.net.MalformedURLException;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
/**
* Implementation of a [EMAIL PROTECTED] Source} that gets its content
* from a PartOnDisk or PartInMemory held in the Request when
* a file is uploaded.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Paul Crabtree</a>
* @version CVS $Id: PartSource.java,v 1.1 2003/10/23 01:45:34 ghoward Exp $
*/
public class PartSource implements Source
{
/* hold a private ref to the protocol used to call the Source */
private String protocol;
/* hold a private ref to the full uri */
private String uri;
/* hold a private ref to the Part which has been uploaded. */
private Part part;
/**
* Builds a PartSource given an URI.
* @param uri e.g., upload://formField1
* @throws SourceException
* @throws MalformedURLException
*/
public PartSource(String uri) throws MalformedURLException,
SourceException
{
// set the uri for use in getURI()
this.uri = uri;
int position = uri.indexOf(':') + 1;
if (position != 0)
{
// set the protocol for use in getScheme()
this.protocol = uri.substring(0, position-1);
}
else
{
// if the URI is not correctly formatted then throw an
excpetion
throw new MalformedURLException("No protocol found for
part source in " + uri);
}
// get the request parameter name: the bit after ://
String location = uri.substring(position + 2);
// get the object model from the component managers curr env.
Map objectModel =
CocoonComponentManager.getCurrentEnvironment().getObjectModel();
// get the cocoon request from the object model.
Request request = ObjectModelHelper.getRequest(objectModel);
// try and cast the request object to a Part
Object obj = request.get(location);
if (obj instanceof Part)
{
part = (Part) obj;
}
else
{
throw new SourceException("Request object " + location + "
is not an uploaded Part");
}
}
/**
* @see org.apache.excalibur.source.Source#getInputStream()
*/
public InputStream getInputStream() throws IOException,
SourceNotFoundException
{
try
{
return part.getInputStream();
}
catch (Exception ex)
{
throw new SourceNotFoundException("The part source can
not be found.");
}
}
/**
* @see org.apache.excalibur.source.Source#getMimeType()
*/
public String getMimeType()
{
return part.getMimeType();
}
/**
* @return true if the resource exists.
*/
public boolean exists()
{
return part != null;
}
/*
* @see org.apache.excalibur.source.Source#getURI()
*/
public String getURI()
{
return uri;
}
/*
* @see org.apache.excalibur.source.Source#getScheme()
*/
public String getScheme()
{
return this.protocol;
}
/*
* Not used, Parts are not cacheable.
*/
public SourceValidity getValidity()
{
// not sure what happens here.
return null;
}
/**
* @see org.apache.excalibur.source.Source#refresh()
*/
public void refresh()
{
}
/**
* @see org.apache.excalibur.source.Source#getContentLength()
*/
public long getContentLength()
{
return part.getSize();
}
/**
* @see org.apache.excalibur.source.Source#getLastModified()
*/
public long getLastModified()
{
return 0;
}
}
1.1
cocoon-2.1/src/java/org/apache/cocoon/components/source/impl/PartSourceFactory.java
Index: PartSourceFactory.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.source.impl;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Map;
/**
* A factory for [EMAIL PROTECTED] org.apache.cocoon.servlet.multipart.Part}
based sources (see [EMAIL PROTECTED] PartSource}).
*
* @author <a href="mailto:[EMAIL PROTECTED]">Paul Crabtree</a>
*/
public class PartSourceFactory implements SourceFactory
{
/*
* Returns a new [EMAIL PROTECTED] PartSource} based on the uri.
*
* @see
org.apache.excalibur.source.SourceFactory#getSource(java.lang.String,
java.util.Map)
*/
public Source getSource(String uri, Map parameters) throws IOException,
MalformedURLException
{
return new PartSource(uri);
}
/**
* Do nothing, [EMAIL PROTECTED] PartSource}s don't need to be released.
*
* @see
org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
*/
public void release(Source source)
{
// Nothing to do here
}
}
1.3 +18 -0 cocoon-2.1/src/webapp/samples/sources/sitemap.xmap
Index: sitemap.xmap
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/sources/sitemap.xmap,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- sitemap.xmap 29 Jul 2003 03:15:47 -0000 1.2
+++ sitemap.xmap 23 Oct 2003 01:45:34 -0000 1.3
@@ -191,6 +191,24 @@
</map:transform>
<map:serialize/>
</map:match>
+
+ <!-- Uploaded xml as source. Beware: Very cool! -->
+ <map:match pattern="xml-upload">
+ <map:generate src="content/upload.xml"/>
+ <map:transform
src="context://samples/common/style/xsl/html/simple-page2html.xsl">
+ <map:parameter name="servletPath" value="{request:servletPath}"/>
+ <map:parameter name="sitemapURI" value="{request:sitemapURI}"/>
+ <map:parameter name="contextPath" value="{request:contextPath}"/>
+ <map:parameter name="remove" value="xml-upload"/>
+ <map:parameter name="file" value="content/simple.xml"/>
+ </map:transform>
+ <map:serialize/>
+ </map:match>
+ <map:match pattern="xml-upload-post">
+ <map:generate src="upload://formFieldOne"/>
+ <map:transform src="context://stylesheets/system/xml2html.xslt"/>
+ <map:serialize/>
+ </map:match>
</map:pipeline>
</map:pipelines>
1.3 +7 -1 cocoon-2.1/src/webapp/samples/sources/samples.xml
Index: samples.xml
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/sources/samples.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- samples.xml 2 Oct 2003 04:30:06 -0000 1.2
+++ samples.xml 23 Oct 2003 01:45:34 -0000 1.3
@@ -67,4 +67,10 @@
source cocoon:/xsl-dynamic-source.
</sample>
</group>
+
+ <group name="Uploaded XML through Static XSL">
+ <sample name="Uploaded XML" href="xml-upload">
+ Demonstrates use of upload:// pseudo-protocol.
+ </sample>
+ </group>
</samples>
1.32 +1 -0 cocoon-2.1/src/webapp/WEB-INF/cocoon.xconf
Index: cocoon.xconf
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/webapp/WEB-INF/cocoon.xconf,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- cocoon.xconf 9 Oct 2003 20:12:35 -0000 1.31
+++ cocoon.xconf 23 Oct 2003 01:45:34 -0000 1.32
@@ -232,6 +232,7 @@
<!-- The "file:" protocol is modifiable (can be written to) and
traversable (directory structures
can be crawled). -->
<component-instance
class="org.apache.excalibur.source.impl.FileSourceFactory" name="file"/>
+ <component-instance
class="org.apache.cocoon.components.source.impl.PartSourceFactory"
name="upload"/>
<!-- the "*" protocol handles all uri schemes that are not explicitely
specified. This includes all
JDK standard protocols. -->
<component-instance
class="org.apache.excalibur.source.impl.URLSourceFactory" name="*"/>