/** * Contact the remote WebDAV server and do a PROPFIND lookup, returning a {@link List} of all * scavenged resources. */ private List propfind(Location location) throws IOException { /* Create the new HttpClient instance associated with the location */ final HttpClient client = new HttpClient(location); client.addRequestHeader("Depth", "1"); client.setAcceptableStatus(207).connect("PROPFIND", true); /* Get the XML SAX Parser and parse the output of the PROPFIND */ try { final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); final SAXParser parser = factory.newSAXParser(); final String systemId = location.toString(); final InputSource source = new InputSource(systemId); final Handler handler = new Handler(location); source.setByteStream(client.getResponseStream()); parser.parse(source, handler); return handler.list; } catch (ParserConfigurationException exception) { Exception throwable = new IOException("Error creating XML parser"); throw (IOException) throwable.initCause(exception); } catch (SAXException exception) { Exception throwable = new IOException("Error creating XML parser"); throw (IOException) throwable.initCause(exception); } finally { client.disconnect(); } }
/** Contact the remote WebDAV server and do an OPTIONS lookup. */ private Location options(Location location) throws IOException { /* Create the new HttpClient instance associated with the location */ final HttpClient client = new HttpClient(location); client.setAcceptableStatus(200).connect("OPTIONS", true).disconnect(); /* Check that the remote server returned the "Dav" header */ final List davHeader = client.getResponseHeaderValues("dav"); if (davHeader == null) { throw new IOException("Server did not respond with a DAV header"); } /* Check if the OPTIONS request contained the DAV header */ final Iterator iterator = davHeader.iterator(); boolean foundLevel1 = false; while (iterator.hasNext() && (!foundLevel1)) { String value = (String) iterator.next(); StringTokenizer tokenizer = new StringTokenizer(value, ","); while (tokenizer.hasMoreTokens()) { if (!"1".equals(tokenizer.nextToken().trim())) continue; foundLevel1 = true; break; } } /* Return the (possibly redirected) location or fail miserably */ if (foundLevel1) return client.getLocation(); throw new IOException("Server doesn't support DAV Level 1"); }
/** * Create a new collection as a child of the collection represented by this {@link WebDavClient} * instance. * * <p>In comparison to {@link #put(String)} and {@link #put(String, long)} this method will fail * if the specified child already exist. * * @see #hasChild(String) * @return this {@link WebDavClient} instance. * @throws IOException if an I/O or network error occurred, or if the child specified already * exist. * @throws NullPointerException if the child was <b>null</b>. */ public WebDavClient mkcol(String child) throws NullPointerException, IOException { if (child == null) throw new NullPointerException("Null child"); if (this.hasChild(child)) throw new IOException("Child \"" + child + "\" already exists"); final Location location = this.resource.location.resolve(child); final HttpClient client = new HttpClient(location); client.setAcceptableStatus(201).connect("MKCOL").disconnect(); return this.refresh(); }
/** * Fetch the contents of the specified child resource of the collection represented by this {@link * WebDavClient} instance. * * @see #isCollection(String) * @return a <b>non-null</b> {@link InputStream}. * @throws IOException if an I/O or network error occurred, or if the child specified represents a * collection. * @throws NullPointerException if the child was <b>null</b>. */ public InputStream get(String child) throws NullPointerException, IOException { if (child == null) throw new NullPointerException("Null child"); if (!this.isCollection(child)) { final Location location = this.getLocation(child); final HttpClient client = new HttpClient(location); client.setAcceptableStatus(200).connect("GET"); return client.getResponseStream(); } throw new IOException("Child \"" + child + "\" is a collection"); }
/** * Delete the child resource (or collection) of the collection represented by this {@link * WebDavClient} instance. * * @return this {@link WebDavClient} instance. * @throws IOException if an I/O or network error occurred, or if the child specified represents a * collection. * @throws NullPointerException if the child was <b>null</b>. */ public WebDavClient delete(String child) throws NullPointerException, IOException { if (child == null) throw new NullPointerException("Null child"); final HttpClient client = new HttpClient(this.getLocation(child)); client.setAcceptableStatus(204).connect("DELETE").disconnect(); return this.refresh(); }