KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > DASLTransformer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.xml.transform.OutputKeys JavaDoc;
23
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.xml.XMLUtils;
26 import org.apache.cocoon.xml.dom.DOMStreamer;
27 import org.apache.commons.httpclient.HttpConnection;
28 import org.apache.commons.httpclient.HttpException;
29 import org.apache.commons.httpclient.HttpState;
30 import org.apache.commons.httpclient.HttpURL;
31 import org.apache.commons.httpclient.UsernamePasswordCredentials;
32 import org.apache.webdav.lib.BaseProperty;
33 import org.apache.webdav.lib.WebdavResource;
34 import org.apache.webdav.lib.methods.OptionsMethod;
35 import org.apache.webdav.lib.methods.SearchMethod;
36 import org.w3c.dom.DocumentFragment JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.xml.sax.Attributes JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.AttributesImpl JavaDoc;
41
42 /**
43  * This transformer performs DASL queries on DASL-enabled WebDAV servers.
44  * It expects a "query" element in the "http://cocoon.apache.org/webdav/dasl/1.0"
45  * namespace containing the DASL query to execute, with a "target" attribute specifiyng
46  * the webdav:// or http:// URL for the target WebDAV server. It will then replace
47  * it with a "query-result" element containing the WebDAV results.
48  *
49  * Each result will be contained in a "result" element with a "path" attribute pointing at it
50  * and all WebDAV properties presented as (namespaced) children elements.
51  *
52  * Sample invocation:
53  * lt;dasl:query xmlns:dasl="http://cocoon.apache.org/webdav/dasl/1.0"
54  * target="webdav://localhost/repos/"gt;
55  * lt;D:searchrequest xmlns:D="DAV:"gt;
56  * lt;D:basicsearchgt;
57  * lt;D:selectgt;
58  * lt;D:allprop/gt;
59  * lt;/D:selectgt;
60  * lt;D:fromgt;
61  * lt;D:scopegt;
62  * lt;D:hrefgt;/repos/lt;/D:hrefgt;
63  * lt;D:depthgt;infinitylt;/D:depthgt;
64  * lt;/D:scopegt;
65  * lt;/D:fromgt;
66  * lt;D:wheregt;
67  * lt;D:eqgt;
68  * lt;D:propgt;lt;D:getcontenttype/gt;lt;/D:propgt;
69  * lt;D:literalgt;text/htmllt;/D:literalgt;
70  * lt;/D:eqgt;
71  * lt;/D:wheregt;
72  * lt;D:orderbygt;
73  * lt;D:ordergt;
74  * lt;D:propgt;
75  * lt;D:getcontentlength/gt;
76  * lt;/D:propgt;
77  * lt;D:ascending/gt;
78  * lt;/D:ordergt;
79  * lt;D:ordergt;
80  * lt;D:propgt;
81  * lt;D:href/gt;
82  * lt;/D:propgt;
83  * lt;D:ascending/gt;
84  * lt;/D:ordergt;
85  * lt;/D:orderbygt;
86  * lt;/D:basicsearchgt;
87  * lt;/D:searchrequestgt;
88  * lt;/dasl:querygt;
89  *
90  * Features
91  * - Substitution of a value: with this feature it's possible to pass value from sitemap
92  * that are substituted into a query.
93  * sitemap example:
94  * lt;map:transformer type="dasl"gt;
95  * lt;parameter name="repos" value="/repos/"gt;
96  * lt;/map:transformergt;
97  * query example:
98  * ....
99  * lt;D:hrefgt;lt;substitute-value name="repos"/gt;lt;/D:hrefgt;
100  * ....
101  * This feature is like substitute-value of SQLTransformer
102  *
103  * TODO: the SWCL Search method doesn't preserve the result order, which makes
104  * order-by clauses useless.
105  *
106  * TODO: *much* better error handling.
107  *
108  * @author <a HREF="mailto: gianugo@apache.org">Gianugo Rabellino</a>
109  * @author <a HREF="mailto:d.madama@pro-netics.com>Daniele Madama</a>
110  * @version $Id: DASLTransformer.java 164808 2005-04-26 16:07:03Z vgritsenko $
111  */

112 public class DASLTransformer extends AbstractSAXTransformer {
113
114     /** The prefix for tag */
115     static final String JavaDoc PREFIX = "dasl";
116     /** The tag name identifying the query */
117     static final String JavaDoc QUERY_TAG = "query";
118     /** The tag namespace */
119     static final String JavaDoc DASL_QUERY_NS =
120         "http://cocoon.apache.org/webdav/dasl/1.0";
121     /** The URL namespace */
122     static final String JavaDoc TARGET_URL = "target";
123     /** The WebDAV scheme*/
124     static final String JavaDoc WEBDAV_SCHEME = "webdav://";
125     /** The tag name of root_tag for result */
126     static final String JavaDoc RESULT_ROOT_TAG = "query-result";
127     /** The tag name for substitution of query parameter */
128     static final String JavaDoc SUBSTITUTE_TAG = "substitute-value";
129     /** The tag name for substitution of query parameter */
130     static final String JavaDoc SUBSTITUTE_TAG_NAME_ATTRIBUTE = "name";
131
132     protected static final String JavaDoc PATH_NODE_NAME = "path";
133     protected static final String JavaDoc RESOURCE_NODE_NAME = "resource";
134
135     /** The target HTTP URL */
136     String JavaDoc targetUrl;
137
138     /**
139      * Intercept the <dasl:query> start tag.
140      *
141      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
142      */

143     public void startElement(
144         String JavaDoc uri,
145         String JavaDoc name,
146         String JavaDoc raw,
147         Attributes JavaDoc attr)
148         throws SAXException JavaDoc {
149         if (name.equals(QUERY_TAG) && uri.equals(DASL_QUERY_NS)) {
150             this.startRecording();
151             if ((targetUrl = attr.getValue(TARGET_URL)) == null) {
152                 throw new IllegalStateException JavaDoc("The query element must contain a \"target\" attribute");
153             }
154             //Sanityze target
155
if (targetUrl.startsWith(WEBDAV_SCHEME))
156                 targetUrl =
157                     "http://" + targetUrl.substring(WEBDAV_SCHEME.length());
158             if (!targetUrl.startsWith("http"))
159                 throw new SAXException JavaDoc("Illegal value for target, must be an http:// or webdav:// URL");
160         } else if (name.equals(SUBSTITUTE_TAG) && uri.equals(DASL_QUERY_NS)) {
161             String JavaDoc parName = attr.getValue( DASL_QUERY_NS, SUBSTITUTE_TAG_NAME_ATTRIBUTE );
162             if ( parName == null ) {
163                 throw new IllegalStateException JavaDoc( "Substitute value elements must have a " +
164                                            SUBSTITUTE_TAG_NAME_ATTRIBUTE + " attribute" );
165             }
166             String JavaDoc substitute = this.parameters.getParameter( parName, null );
167             if (getLogger().isDebugEnabled()) {
168                 getLogger().debug( "SUBSTITUTE VALUE " + substitute );
169             }
170             super.characters(substitute.toCharArray(), 0, substitute.length());
171         } else {
172             super.startElement(uri, name, raw, attr);
173         }
174     }
175
176     /**
177      * Intercept the <dasl:query> end tag, convert buffered input to a String, build and execute the
178      * DASL query.
179      *
180      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
181      */

182     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
183         throws SAXException JavaDoc {
184         String JavaDoc query;
185         if (name.equals(QUERY_TAG) && uri.equals(DASL_QUERY_NS)) {
186             DocumentFragment JavaDoc frag = this.endRecording();
187             try {
188                 Properties JavaDoc props = XMLUtils.createPropertiesForXML(false);
189                 props.put(OutputKeys.ENCODING, "ISO-8859-1");
190                 query = XMLUtils.serializeNode(frag, props);
191                 // Perform the DASL query
192
this.performSearchMethod(query);
193             } catch (ProcessingException e) {
194                 throw new SAXException JavaDoc("Unable to fetch the query data:", e);
195             }
196         } else if (name.equals(SUBSTITUTE_TAG) && uri.equals(DASL_QUERY_NS)) {
197             //Do nothing!!!!
198
} else {
199             super.endElement(uri, name, raw);
200         }
201     }
202
203     protected void performSearchMethod(String JavaDoc query) throws SAXException JavaDoc {
204         try {
205             DOMStreamer propertyStreamer = new DOMStreamer(this.xmlConsumer);
206             OptionsMethod optionsMethod = new OptionsMethod(this.targetUrl);
207             SearchMethod searchMethod = new SearchMethod(this.targetUrl, query);
208             HttpURL url = new HttpURL(this.targetUrl);
209             HttpState state = new HttpState();
210             state.setCredentials(null, new UsernamePasswordCredentials(
211                     url.getUser(),
212                     url.getPassword()));
213             HttpConnection conn = new HttpConnection(url.getHost(), url.getPort());
214             WebdavResource resource = new WebdavResource(new HttpURL(this.targetUrl));
215             if(!resource.exists()) {
216                 throw new SAXException JavaDoc("The WebDAV resource don't exist");
217             }
218             optionsMethod.execute(state, conn);
219             if(!optionsMethod.isAllowed("SEARCH")) {
220                 throw new SAXException JavaDoc("The server don't support the SEARCH method");
221             }
222             searchMethod.execute(state, conn);
223
224             Enumeration JavaDoc enumeration = searchMethod.getAllResponseURLs();
225             this.contentHandler.startElement(DASL_QUERY_NS,
226                                              RESULT_ROOT_TAG,
227                                              PREFIX + ":" + RESULT_ROOT_TAG,
228                                              XMLUtils.EMPTY_ATTRIBUTES);
229             while (enumeration.hasMoreElements()) {
230                 String JavaDoc path = (String JavaDoc) enumeration.nextElement();
231                 Enumeration JavaDoc properties = searchMethod.getResponseProperties(path);
232                 AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
233                 attr.addAttribute(DASL_QUERY_NS, PATH_NODE_NAME, PREFIX + ":" + PATH_NODE_NAME, "CDATA",path);
234
235                 this.contentHandler.startElement(DASL_QUERY_NS,
236                     RESOURCE_NODE_NAME,
237                     PREFIX + ":" + RESOURCE_NODE_NAME,
238                     attr);
239                 while(properties.hasMoreElements()) {
240                     BaseProperty metadata = (BaseProperty) properties.nextElement();
241                     Element JavaDoc propertyElement = metadata.getElement();
242                     propertyStreamer.stream(propertyElement);
243                 }
244
245                 this.contentHandler.endElement(DASL_QUERY_NS,
246                     RESOURCE_NODE_NAME,
247                     PREFIX + ":" + RESOURCE_NODE_NAME);
248             }
249             this.contentHandler.endElement(DASL_QUERY_NS,
250                                            RESULT_ROOT_TAG,
251                                            PREFIX + ":" + RESULT_ROOT_TAG);
252         } catch (SAXException JavaDoc e) {
253             throw new SAXException JavaDoc("Unable to fetch the query data:", e);
254         } catch (HttpException e1) {
255             this.getLogger().error("Unable to contact Webdav server", e1);
256             throw new SAXException JavaDoc("Unable to connect with server: ", e1);
257         } catch (IOException JavaDoc e2) {
258             throw new SAXException JavaDoc("Unable to connect with server: ", e2);
259         } catch (NullPointerException JavaDoc e) {
260             throw new SAXException JavaDoc("Unable to fetch the query data:", e);
261         } catch (Exception JavaDoc e) {
262             throw new SAXException JavaDoc("Generic Error:", e);
263         }
264     }
265
266 }
267
Popular Tags