1 16 package org.apache.cocoon.components.repository.impl; 17 18 import java.io.BufferedInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.MalformedURLException ; 22 import java.util.Properties ; 23 24 import javax.xml.transform.OutputKeys ; 25 26 import org.apache.avalon.excalibur.io.IOUtil; 27 import org.apache.avalon.framework.activity.Disposable; 28 import org.apache.avalon.framework.activity.Initializable; 29 import org.apache.avalon.framework.component.Component; 30 import org.apache.avalon.framework.configuration.Configurable; 31 import org.apache.avalon.framework.configuration.Configuration; 32 import org.apache.avalon.framework.configuration.ConfigurationException; 33 import org.apache.avalon.framework.logger.AbstractLogEnabled; 34 import org.apache.avalon.framework.service.ServiceException; 35 import org.apache.avalon.framework.service.ServiceManager; 36 import org.apache.avalon.framework.service.Serviceable; 37 import org.apache.cocoon.ProcessingException; 38 import org.apache.cocoon.components.LifecycleHelper; 39 import org.apache.cocoon.components.repository.Repository; 40 import org.apache.cocoon.components.repository.helpers.CredentialsToken; 41 import org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper; 42 import org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper; 43 import org.apache.cocoon.components.repository.helpers.RepositoryVersioningHelper; 44 import org.apache.cocoon.components.webdav.WebDAVUtil; 45 import org.apache.cocoon.xml.XMLUtils; 46 import org.apache.commons.httpclient.HttpException; 47 import org.apache.excalibur.source.Source; 48 import org.apache.excalibur.xml.dom.DOMParser; 49 import org.apache.webdav.lib.WebdavResource; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Node ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 55 56 59 public class WebDAVRepository extends AbstractLogEnabled 60 implements Repository, Serviceable, Configurable, Initializable, Disposable, Component { 61 62 63 public static final String REPO_BASE_CONF = "repo-base"; 64 65 66 private ServiceManager manager; 67 68 69 private WebDAVRepositoryPropertyHelper propertyHelper; 70 71 72 private WebDAVRepositoryTransactionHelper transactionHelper; 73 74 75 private WebDAVRepositoryVersioningHelper versioningHelper; 76 77 78 private String repoBaseUrl; 79 80 81 private CredentialsToken credentials; 82 83 86 public void service(ServiceManager manager) throws ServiceException { 87 this.manager = manager; 88 89 } 90 91 94 public void configure(Configuration configuration) throws ConfigurationException { 95 this.repoBaseUrl = configuration.getChild(REPO_BASE_CONF).getValue(); 96 if (this.getLogger().isDebugEnabled()) { 97 this.getLogger().debug("configuring repository location " + this.repoBaseUrl); 98 } 99 } 100 101 104 public void initialize() throws Exception { 105 this.propertyHelper = new WebDAVRepositoryPropertyHelper(this.credentials, this); 106 this.transactionHelper = new WebDAVRepositoryTransactionHelper(this.credentials, this); 107 this.versioningHelper = new WebDAVRepositoryVersioningHelper(this.credentials, this); 108 LifecycleHelper lh = new LifecycleHelper(this.getLogger(), 109 null, 110 this.manager, 111 null, 112 null); 113 lh.setupComponent(this.propertyHelper, true); 114 lh.setupComponent(this.transactionHelper, true); 115 lh.setupComponent(this.versioningHelper, true); 116 } 117 118 121 public void dispose() { 122 this.manager = null; 123 this.propertyHelper.dispose(); 124 this.transactionHelper.dispose(); 125 this.versioningHelper.dispose(); 126 this.propertyHelper = null; 127 this.transactionHelper = null; 128 this.versioningHelper = null; 129 } 130 131 134 public String getContentString(String uri) throws ProcessingException { 135 136 try { 137 return IOUtil.toString(this.getContentStream(uri)); 138 139 } catch (IOException ioe) { 140 throw new ProcessingException ("Error loading resource: " + this.repoBaseUrl + uri, ioe); 141 } 142 } 143 144 147 public InputStream getContentStream(String uri) throws ProcessingException { 148 149 if (this.getLogger().isDebugEnabled()) { 150 this.getLogger().debug("getting content of: " + uri); 151 } 152 153 try { 154 155 WebdavResource resource = WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)); 156 if (!resource.exists()) { 157 throw new HttpException(uri + " does not exist"); 158 } 159 return new BufferedInputStream (resource.getMethodData()); 160 161 } catch (MalformedURLException mue) { 162 throw new ProcessingException ("Bad URL for resource: " + this.repoBaseUrl + uri, mue); 163 } catch (IOException ioe) { 164 throw new ProcessingException ("Error loading resource: " + this.repoBaseUrl + uri, ioe); 165 } 166 } 167 168 171 public Document getContentDOM(String uri) throws ProcessingException { 172 173 DOMParser parser = null; 174 175 try { 176 parser = (DOMParser)this.manager.lookup( DOMParser.ROLE); 177 return parser.parseDocument(new InputSource (this.getContentStream(uri))); 178 179 } catch (SAXException se) { 180 throw new ProcessingException ("Error parsing: " + this.repoBaseUrl + uri, se); 181 } catch (IOException ioe) { 182 throw new ProcessingException ("Error getting: " + this.repoBaseUrl + uri, ioe); 183 } catch (ServiceException se) { 184 throw new ProcessingException ("Error getting DOMParser", se); 185 186 } finally { 187 this.manager.release(parser); 188 } 189 } 190 191 194 public boolean saveContent(String uri, String content) { 195 196 if (this.getLogger().isDebugEnabled()) { 197 this.getLogger().debug("save content to " + uri); 198 } 199 200 try { 201 return WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)).putMethod(content); 202 203 } catch (HttpException he) { 204 this.getLogger().error("Error saving: " + this.repoBaseUrl + uri, he); 205 } catch (IOException ioe) { 206 this.getLogger().error("Error saving: " + this.repoBaseUrl + uri, ioe); 207 } 208 209 return false; 210 } 211 212 215 public boolean saveContent(String uri, Node node) { 216 217 try { 218 Properties format = new Properties (); 219 format.put(OutputKeys.METHOD, "xml"); 220 format.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); 221 return this.saveContent(uri, XMLUtils.serializeNode(node, format)); 222 223 } catch (ProcessingException pe) { 224 this.getLogger().error("Error saving dom to: " + this.repoBaseUrl + uri, pe); 225 } 226 227 return false; 228 } 229 230 233 public boolean saveContent(String uri, Source source) { 234 235 try { 236 return this.saveContent(uri, IOUtil.toString(source.getInputStream())); 237 238 } catch (IOException ioe) { 239 this.getLogger().error("Error saving source: " + source.getURI() + 240 " to "+ this.repoBaseUrl + uri, ioe); 241 } 242 243 return false; 244 } 245 246 249 public boolean createResource(String uri, String content) { 250 251 if (this.getLogger().isDebugEnabled()) { 252 this.getLogger().debug("creating new resource " + uri); 253 } 254 255 try { 256 WebDAVUtil.createResource(this.getAbsoluteURI(uri), content); 257 return true; 258 259 } catch (HttpException he) { 260 this.getLogger().error("Error creating resource: " + this.repoBaseUrl + uri, he); 261 } catch (IOException ioe) { 262 this.getLogger().error("Error creating resource: " + this.repoBaseUrl + uri, ioe); 263 } 264 265 return false; 266 } 267 268 271 public boolean exists(String uri) { 272 273 if (this.getLogger().isDebugEnabled()) { 274 this.getLogger().debug("checking existance of " + uri); 275 } 276 277 try { 278 return WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)).exists(); 279 280 } catch (HttpException he) { 281 this.getLogger().error("HTTP Error occurred while checking for existance of: " + uri, he); 282 } catch (IOException ioe) { 283 this.getLogger().error("IO Error occurred while checking for existance of: " + uri, ioe); 284 } 285 286 return false; 287 } 288 289 292 public boolean copy(String uri, String dest, boolean recurse, boolean overwrite) { 293 294 try { 295 WebDAVUtil.copyResource(this.getAbsoluteURI(uri), this.getAbsoluteURI(dest), recurse, overwrite); 296 return true; 297 298 } catch (HttpException he) { 299 this.getLogger().error("HTTP Error copying: " + this.repoBaseUrl + uri, he); 300 } catch (IOException ioe) { 301 this.getLogger().error("IO Error copying: " + this.repoBaseUrl + uri, ioe); 302 } 303 304 return false; 305 } 306 307 310 public boolean move(String uri, String dest, boolean recurse, boolean overwrite) { 311 312 try { 313 WebDAVUtil.moveResource(this.getAbsoluteURI(uri), this.getAbsoluteURI(dest), recurse, overwrite); 314 return true; 315 } catch (HttpException he) { 316 this.getLogger().error("HTTP Error moving: " + this.repoBaseUrl + uri, he); 317 } catch (IOException ioe) { 318 this.getLogger().error("IO Error moving: " + this.repoBaseUrl + uri, ioe); 319 } 320 321 return false; 322 } 323 324 327 public boolean remove(String uri) { 328 329 try { 330 return WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)).deleteMethod(); 331 332 } catch (HttpException he) { 333 this.getLogger().error("HTTP Error removing: " + this.repoBaseUrl + uri, he); 334 } catch (IOException ioe) { 335 this.getLogger().error("IO Error removing: " + this.repoBaseUrl + uri, ioe); 336 } 337 338 return false; 339 } 340 341 344 public boolean makeCollection(String uri, boolean recursive) { 345 346 try { 347 if (uri.endsWith("/")) uri = uri.substring(0, uri.length()-1); 348 if (recursive) { 349 WebDAVUtil.makePath(this.getAbsoluteURI(uri)); 350 return true; 351 } else { 352 String parent = uri.substring(0, uri.lastIndexOf("/")); 353 String collection = uri.substring(uri.lastIndexOf("/")+1); 354 WebDAVUtil.makeCollection(this.getAbsoluteURI(parent), collection); 355 return true; 356 } 357 358 } catch (HttpException he) { 359 this.getLogger().error("HTTP Error making collection: " + this.repoBaseUrl + uri, he); 360 } catch (IOException ioe) { 361 this.getLogger().error("IO Error making collection: " + this.repoBaseUrl + uri, ioe); 362 } 363 364 return false; 365 } 366 367 372 public RepositoryPropertyHelper getPropertyHelper() { 373 return this.propertyHelper; 374 } 375 376 381 public RepositoryTransactionHelper getTransactionHelper() { 382 return this.transactionHelper; 383 } 384 385 390 public RepositoryVersioningHelper getVersioningHelper() { 391 return this.versioningHelper; 392 } 393 394 400 public String getAbsoluteURI(String uri) { 401 402 return "http://"+this.credentials.getPrincipal().getName() 403 +":"+this.credentials.getCredentials() 404 +"@"+this.repoBaseUrl + uri; 405 } 406 407 410 public CredentialsToken getCredentials() { 411 return this.credentials; 412 } 413 414 417 public void setCredentials(CredentialsToken credentials) { 418 this.credentials = credentials; 419 } 420 421 } | Popular Tags |