1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.util.Enumeration ; 20 import java.util.Properties ; 21 22 import javax.xml.transform.OutputKeys ; 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 ; 37 import org.w3c.dom.Element ; 38 import org.xml.sax.Attributes ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.helpers.AttributesImpl ; 41 42 112 public class DASLTransformer extends AbstractSAXTransformer { 113 114 115 static final String PREFIX = "dasl"; 116 117 static final String QUERY_TAG = "query"; 118 119 static final String DASL_QUERY_NS = 120 "http://cocoon.apache.org/webdav/dasl/1.0"; 121 122 static final String TARGET_URL = "target"; 123 124 static final String WEBDAV_SCHEME = "webdav://"; 125 126 static final String RESULT_ROOT_TAG = "query-result"; 127 128 static final String SUBSTITUTE_TAG = "substitute-value"; 129 130 static final String SUBSTITUTE_TAG_NAME_ATTRIBUTE = "name"; 131 132 protected static final String PATH_NODE_NAME = "path"; 133 protected static final String RESOURCE_NODE_NAME = "resource"; 134 135 136 String targetUrl; 137 138 143 public void startElement( 144 String uri, 145 String name, 146 String raw, 147 Attributes attr) 148 throws SAXException { 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 ("The query element must contain a \"target\" attribute"); 153 } 154 if (targetUrl.startsWith(WEBDAV_SCHEME)) 156 targetUrl = 157 "http://" + targetUrl.substring(WEBDAV_SCHEME.length()); 158 if (!targetUrl.startsWith("http")) 159 throw new SAXException ("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 parName = attr.getValue( DASL_QUERY_NS, SUBSTITUTE_TAG_NAME_ATTRIBUTE ); 162 if ( parName == null ) { 163 throw new IllegalStateException ( "Substitute value elements must have a " + 164 SUBSTITUTE_TAG_NAME_ATTRIBUTE + " attribute" ); 165 } 166 String 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 182 public void endElement(String uri, String name, String raw) 183 throws SAXException { 184 String query; 185 if (name.equals(QUERY_TAG) && uri.equals(DASL_QUERY_NS)) { 186 DocumentFragment frag = this.endRecording(); 187 try { 188 Properties props = XMLUtils.createPropertiesForXML(false); 189 props.put(OutputKeys.ENCODING, "ISO-8859-1"); 190 query = XMLUtils.serializeNode(frag, props); 191 this.performSearchMethod(query); 193 } catch (ProcessingException e) { 194 throw new SAXException ("Unable to fetch the query data:", e); 195 } 196 } else if (name.equals(SUBSTITUTE_TAG) && uri.equals(DASL_QUERY_NS)) { 197 } else { 199 super.endElement(uri, name, raw); 200 } 201 } 202 203 protected void performSearchMethod(String query) throws SAXException { 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 ("The WebDAV resource don't exist"); 217 } 218 optionsMethod.execute(state, conn); 219 if(!optionsMethod.isAllowed("SEARCH")) { 220 throw new SAXException ("The server don't support the SEARCH method"); 221 } 222 searchMethod.execute(state, conn); 223 224 Enumeration 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 path = (String ) enumeration.nextElement(); 231 Enumeration properties = searchMethod.getResponseProperties(path); 232 AttributesImpl attr = new AttributesImpl (); 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 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 e) { 253 throw new SAXException ("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 ("Unable to connect with server: ", e1); 257 } catch (IOException e2) { 258 throw new SAXException ("Unable to connect with server: ", e2); 259 } catch (NullPointerException e) { 260 throw new SAXException ("Unable to fetch the query data:", e); 261 } catch (Exception e) { 262 throw new SAXException ("Generic Error:", e); 263 } 264 } 265 266 } 267 | Popular Tags |