Features
Cocoon 3 consists of 3 layers. Each of this layers is packaged as separate module:
- cocoon-pipeline
- cocoon-sitemap
- cocoon-servlet
Additionally there are more optional modules:
- cocoon-stringtemplate
- cocoon-controller
- cocoon-rest
Cocoon Pipeline
This module doesn't have any third-party dependencies. Currently it provides three pipeline implementations:
- noncaching
- caching
- async-caching
and it provides a basic set of SAX components:
- FileGenerator
- XSLTTransformer
- XMLSerializer
- IncludeTransformer
- CleaningTransformer
- FileReader
- StringGenerator
Here is an example:
Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>(); pipeline.addComponent(new XMLGenerator("<x></x>")); pipeline.addComponent(new XSLTTransformer(this.getClass().getResource("/test1.xslt"))); pipeline.addComponent(new XMLSerializer()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); pipeline.setup(baos); pipeline.execute();
Cocoon Sitemap
A sitemap connects a request (not necessarily a servlet request!) with a pipeline. Here is an example:
<?xml version="1.0" encoding="UTF-8"?> <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap" xmlns:servlet="http://apache.org/cocoon/servlet"> <map:pipelines> <map:pipeline> <map:match pattern="screen/video"> <map:generate src="video.xml" type="stringtemplate" /> <map:serialize type="xml" /> </map:match> </map:pipeline> </map:pipelines> </map:sitemap>
Cocoon Servlet
This module provides a servlet that connects an HTTP request with a sitemap. It works smoothly within the Servlet-Service framework. The Servlet-Service framework enables the inter-sitemap communication and provides polomorphistic features (e.g. one sitemap can extend another one which allows e.g. the implementation of fallback solutions).
Additional Modules
The additional modules cocoon-stringtemplate, cocoon-controller, and cocoon-rest make the development of RESTful webservices simple. Here is the sitemap that invokes a controller:
<?xml version="1.0" encoding="UTF-8"?> <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap" xmlns:servlet="http://apache.org/cocoon/servlet" xmlns:controller="http://apache.org/cocoon/controller"> <map:pipelines> <map:pipeline> <map:match pattern="videos/{id}"> <controller:call controller="rest-controller" select="VideoController"> <map:parameter name="id" value="{map:id}" /> </controller:call> </map:match> </map:pipeline> </map:pipelines> </map:sitemap>
And here the controller:
@RESTController public class VideoController implements Get { @SitemapParameter private String id; @RequestParameter private String details; public RestResponse doGet() throws Exception { Map<String, Object> data = new HashMap<String, Object>(); data.put("video", new Video(this.id)); data.put("details", this.details); return new Page("servlet:/screen/video", data); } }
When this annotated controller class is being executed, the sitemap parameter id and the request parameter details are passed to the controller.
The controller implements the Get interface which makes it reacting on GET requests. Then the controller invokes the pipeline servlet:/screen/video (see the sitemap in the section above) which uses StringTemplate to render the Video object to XML.