cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Marasti-Georg" <cge...@rjlg.com>
Subject RE: Using the output of a pipeline to call another pipeline?
Date Fri, 30 Sep 2005 13:19:08 GMT
Christoph's solution is probably the easiest one, but here's an example
of a simple action I wrote to simply call a URL and put the response in
the returned map.  Just compile it (need the cocoon jars on the
classpath), jar it up, and stick it in the web-inf/lib folder.  If
anyone notices anything bad, by all means let me know - I have hardly
dipped my toes into programming with cocoon, and this was actually the
first class I created for it.

package com.rjlg.mkb.repo.controller.cocoon;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.acting.AbstractAction;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;

public class RemoteXQLAction extends AbstractAction implements
ThreadSafe, Composable, Disposable
{
	long created = System.currentTimeMillis();
	
	public void compose(ComponentManager manager) throws
ComponentException {}
	
	public Map act( Redirector redirector, SourceResolver resolver,
Map objectModel, String source, Parameters param ) throws Exception
	{
		String remoteURL = param.getParameter("url");
		if (remoteURL == null) {
			getLogger().error("Missing a url param");
			return null;
		}
		
		Map map = new HashMap();
		
		try{
			URL url = new URL(remoteURL);
			HttpURLConnection urlConnection =
(HttpURLConnection)url.openConnection();
			urlConnection.setDoInput(true);
			urlConnection.setDoOutput(false);
			urlConnection.connect();
			
			InputStream is = urlConnection.getInputStream();
			ByteArrayOutputStream os = new
ByteArrayOutputStream();
			byte[] b = new byte[2048];
			int ret = 0;
			while ((ret = is.read(b)) != -1) {
				os.write(b, 0, ret);
			}
			String response = os.toString();
			is.close();
			os.close();
			getLogger().debug("got back: "+response);
			map.put("response", response);
			
			return(map);
		} catch (Exception ex){
			getLogger().error("Error connecting to/reading
from remote server: "+remoteURL);
			return null;
		}
	}
	
	public void dispose() {
	}
}

Chris Marasti-Georg
 

> -----Original Message-----
> From: Jonas Lundberg [mailto:my.name.is.hans@gmail.com] 
> Sent: Friday, September 30, 2005 3:09 AM
> To: users@cocoon.apache.org
> Subject: Re: Using the output of a pipeline to call another pipeline?
> 
> Thank's for the replies,
> Both using an action, and using a flowscript seem promising, 
> but also problematic.
> 
> For the flowscript solution to work, I need to read the 
> xQuery result from a cocoon pipeline. Unfortunately, 
> cocoon.processPipelineTo() does not work as in this example:
> readImage() {
>        // get request uri in here somehow
>        var realfile =
> cocoon.processPipelineTo("flow/locateimage"+cocoon.getRequestUri());
> 
>        //parse response
>        var realFilename = getFilenameFromResponse(realfile);
> 
>        Cocoon.sendPage("flow/readimage/"+realFileName");
> }
> processPipelineTo wants an outputstream to send the response 
> to, rather than storing it in a variable, as I need to do. Is 
> there a way around this?
> 
> The Action solution also seems promising, but I do not know 
> how to create actions, and I cannot find any tutorial about it.
> I would need to know:
> - how to read from a pipeline inside the action, and store 
> the result in a variable.
> - how to store that resulting variable in a sitemap variable 
> (I have found tutorials describing that specific part)
> - how to compile the action, and where to put it for Cocoon 
> to find it.
> 
> I would really appreciate any responses!
> 
> Hans
> 
> 
> 
> On 9/27/05, Chris Marasti-Georg <cgeorg@rjlg.com> wrote:
> > I think you have 2 options - a reader, or an action.
> >
> > Your action could call the xquery, get the result, and put 
> it in the 
> > returned map, making it visible to a subsequent read call 
> (my syntax 
> > may not be perfect, it's been a while since I coded an action):
> >
> > <map:match pattern="*/*/*.jpg">
> >         <map:act type="image-filename-resolver">
> >                 <map:parameter name="virtual-uri"
> > value="{1}/{2}/{3}.jpg"/>
> >                 <map:read
> > src="http://myexistserver/exist/db/jpeg/{filename}"/>
> >         </map:act>
> > </map:match>
> >
> > Or, implement a special reader that performs both steps... 
> Calling the 
> > xquery, and then using the result to retrieve the actual 
> image.  I'm 
> > not sure of the inner workings of readers, with respect to reading 
> > from a source other than the one that is passed in from the 
> sitemap... 
> > My guess would be that the action would be a cleaner implementation.
> >
> > One other thought I just had, but have no idea how 
> reasonable it would 
> > be, is to use a flowscript function like
> >
> > readImage() {
> >         // get request uri in here somehow
> >         var realfile =
> > cocoon.processPipelineTo("flow/locateimage"+cocoon.getRequestUri());
> >
> >         //parse response
> >         var realFilename = getFilenameFromResponse(realfile);
> >
> >         Cocoon.sendPage("flow/readimage/"+realFileName");
> > }
> >
> > External pipeline
> > <map:match pattern="**.jpg">
> >         <map:call name="readImage()"/> </map:match>
> >
> > Internal pipeline:
> > <map:match pattern="flow/locateimage/**">
> >         <map:read src="http://myexistserver/exist/db/jpeg/{1}"/>
> > </map:match>
> > <map:match pattern="flow/readimage/**">
> >         <map:read src="http://myexistserver/exist/db/jpeg/{1}"/>
> > </map:match>
> >
> > Just some ideas, and I take no blame if nothing works... I 
> waited to 
> > see if anyone else would pitch in 2 cents, and they didn't, 
> so you get 
> > my penny instead ;) Good to see another person using eXist...
> > Chris Marasti-Georg
> >
> >
> > > -----Original Message-----
> > > From: Jonas Lundberg [mailto:my.name.is.hans@gmail.com]
> > > Sent: Tuesday, September 27, 2005 11:54 AM
> > > To: users@cocoon.apache.org
> > > Subject: Re: Using the output of a pipeline to call 
> another pipeline?
> > >
> > > Thanks for the replies, I'll explain in more detail then.
> > >
> > > The images are referenced in an HTML page, which means that the 
> > > browser will request them separately after the page is 
> sent. Do you 
> > > propose that I somehow embed the image data in the page?
> > >
> > > All paths in the system are virtual. I use XML files to create 
> > > virtual hierarchies, that gives depth and structure to a 
> site. That 
> > > means that the same contents can be rendered using 
> several different 
> > > site layouts, at the same time.
> > >
> > > Therefore, all images are stored in one big collection in 
> the eXist 
> > > database. To avoid that any two images get the same file 
> name, each 
> > > file is renamed when uploaded, and given a unique name (a 
> number). 
> > > The actual name, and the virtual path where it is to be shown, is 
> > > stored in the meta data file.
> > >
> > > In the html page generation pipeline, I could put the new 
> file name 
> > > directly in the html page, but that would violate the separation 
> > > between storage and presentation. The author of the 
> document should 
> > > decide what file name to present to the user, not the system. A 
> > > strict separation between storage and presentation is a 
> main point 
> > > with my system.
> > >
> > > Also, if I execute an xquery to provide the file name, 
> then I can at 
> > > the same time check whether the metadata file states that 
> the file 
> > > is "live" or "waiting". It should not be possible to retreive an 
> > > image before it is published. And when I am doing that, I 
> can also 
> > > check whether the particular user requesting the file should be 
> > > allowed to see it. If the user is not allowed to see the 
> image, or 
> > > if it is not there, I can simply send a different image.
> > >
> > > So, when the xquery has created an xml file with the image:
> > > <file>
> > > 44500.jpg
> > > </file>
> > > Then I cannot send a HTML file with an <img src...> because the 
> > > browser has requested the actual image file. Also, if I do that, 
> > > then I have to check the user premissions, and the live-status of 
> > > the image again, when the browser requests the actual image.
> > >
> > > If I instead simply cinclude the image then I will get 
> <file> image 
> > > data </file> That would be an image embedded in XML, which is not 
> > > what I want.
> > >
> > > So, what I want to do is very basic:
> > >
> > > First, I want to generate the internal file name using 
> xQuery, given 
> > > the virtual path and the virtual file name. At the same 
> time I check 
> > > whether the image is live ,and whether the particular 
> user should be 
> > > allowed to view it.
> > > Then, I want to call a different (internal) pipeline using the 
> > > resulting file name (embedded in the <file></file> tag) (for 
> > > instance
> > > 44400.jpg) as the request, and return the result of that pipeline.
> > >
> > > Any ideas now of how I can achieve that?
> > >
> > > Hans
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > On 9/27/05, Derek Hohls <dhohls@csir.co.za> wrote:
> > > >
> > > > Its not clear what you mean by the "second pipeline" - do
> > > you mean the
> > > > **.jpg ?  If so, can you not simply call that from the page
> > > where it
> > > > needs to appear?  (And the page itself is generated using
> > > the output
> > > > from the first pipeline; cinclude or simply a map:aggregate
> > > will feed
> > > > the XML into your page prior to a final transform.)
> > > >
> > > > If you need more help, you may have to lay out your problem
> > > a little
> > > > more clearly.
> > > >
> > > > >>> my.name.is.hans@gmail.com 2005/09/27 11:40:16 AM >>>
> > > >
> > > > I've got a very basic question that I got stuck with....
> > > maybe someone
> > > > can help me out?
> > > >
> > > > I have a web publishing system that stores all images in
> > > eXist, using
> > > > a number as the file name, e.g. 44500.jpg
> > > >
> > > > Each image has a metadata file, describing at what URL 
> the image 
> > > > should be shown (and also some other things, not shown 
> here), e.g.
> > > > <metadata>
> > > > <live><at>webdesign/cocoon</at></live>
> > > > <filename>banner.jpg</filename>
> > > > </metadata>
> > > >
> > > > In the html files, the url to the file points at its
> > > virutal location,
> > > > and virtual name, e.g. webdesign/cocoon/banner.jpg Thus, I
> > > could write
> > > > a cocoon sitemap matcher like this:
> > > >
> > > > <map:match pattern="*/*/*.jpg>
> > > > <map:generate src="xq/{1}.xq" type="xquery"> <map:parameter 
> > > > name="file" value="{1}/{2}/{3}"/> </map:generate> ? How to call

> > > > the second pipeline with the result of the query?
> > > > </map:match>
> > > >
> > > > The xquery generates the file name to use in the second
> > > pipeline, like
> > > > this <file> 44500.jpg </file>
> > > >
> > > > The image is then to be fetched from eXist using this pipeline:
> > > > <map:match pattern="*.jpg">
> > > > <map:read type="image"
> > > >
> > > >
> > > src="http://guest:guest@localhost:8080/cocoon/webdav/db/conten
> > > ts/jpeg/{1}.jpg"
> > > > "
> > > > mime-type="text/jpg "/>
> > > > </map:match>
> > > >
> > > > But how do I call the second pipeline, using the file name
> > > created by
> > > > the generator in the first pipeline? This is a very basic 
> > > > question, but I got stuck with it anyway... Any ideas?
> > > >
> > > > Hans
> > > >
> > > >
> > > 
> --------------------------------------------------------------------
> > > -
> > > > To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> > > > For additional commands, e-mail: users-help@cocoon.apache.org
> > > >
> > > >
> > > > --
> > > > This message is subject to the CSIR's copyright, terms and
> > > conditions
> > > > and e-mail legal notice.
> > > > Views expressed herein do not necessarily represent the
> > > views of the CSIR.
> > > >
> > > > CSIR E-mail Legal Notice
> > > >
> > > > CSIR Copyright, Terms and Conditions
> > > >
> > > > For electronic copies of the CSIR Copyright, Terms and
> > > Conditions and
> > > > the CSIR Legal Notice send a blank message with "REQUEST
> > > LEGAL" in the
> > > > subject line to CSIR HelpDesk
> > > >
> > > >
> > > > This message has been scanned for viruses and dangerous 
> content by 
> > > > MailScanner, and is believed to be clean. MailScanner
> > > thanks Transtec
> > > > Computers for their support.
> > >
> > > 
> --------------------------------------------------------------------
> > > - To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> > > For additional commands, e-mail: users-help@cocoon.apache.org
> > >
> > >
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> > For additional commands, e-mail: users-help@cocoon.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message