private boolean isDirectory(String url) throws IOException, DavException { DavPropertyNameSet nameSet = new DavPropertyNameSet(); nameSet.add(DavPropertyName.create(DavConstants.PROPERTY_RESOURCETYPE)); PropFindMethod method = null; try { method = new PropFindMethod(url, nameSet, DavConstants.DEPTH_0); execute(method); if (method.succeeded()) { MultiStatus multiStatus = method.getResponseBodyAsMultiStatus(); MultiStatusResponse response = multiStatus.getResponses()[0]; DavPropertySet propertySet = response.getProperties(HttpStatus.SC_OK); DavProperty<?> property = propertySet.get(DavConstants.PROPERTY_RESOURCETYPE); if (property != null) { Node node = (Node) property.getValue(); return node.getLocalName().equals(DavConstants.XML_COLLECTION); } } return false; } finally { if (method != null) { method.releaseConnection(); } } }
public WebdavEntry(MultiStatusResponse ms, String splitElement) { resetData(); if (ms.getStatus().length != 0) { mUri = ms.getHref(); mPath = mUri.split(splitElement, 2)[1]; int status = ms.getStatus()[0].getStatusCode(); DavPropertySet propSet = ms.getProperties(status); @SuppressWarnings("rawtypes") DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME); if (prop != null) mName = (String) prop.getName().toString(); else { String[] tmp = mPath.split("/"); if (tmp.length > 0) mName = tmp[tmp.length - 1]; } // use unknown mimetype as default behavior mContentType = "application/octet-stream"; prop = propSet.get(DavPropertyName.GETCONTENTTYPE); if (prop != null) { mContentType = (String) prop.getValue(); // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if // looks fixed, but let's be cautious if (mContentType.indexOf(";") >= 0) { mContentType = mContentType.substring(0, mContentType.indexOf(";")); } } // check if it's a folder in the standard way: see RFC2518 12.2 , or RFC4918 14.3 prop = propSet.get(DavPropertyName.RESOURCETYPE); if (prop != null) { Object value = prop.getValue(); if (value != null) { mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we // have no reason to distinguish MIME types for folders } } prop = propSet.get(DavPropertyName.GETCONTENTLENGTH); if (prop != null) mContentLength = Long.parseLong((String) prop.getValue()); prop = propSet.get(DavPropertyName.GETLASTMODIFIED); if (prop != null) { Date d = WebdavUtils.parseResponseDate((String) prop.getValue()); mModifiedTimestamp = (d != null) ? d.getTime() : 0; } prop = propSet.get(DavPropertyName.CREATIONDATE); if (prop != null) { Date d = WebdavUtils.parseResponseDate((String) prop.getValue()); mCreateTimestamp = (d != null) ? d.getTime() : 0; } } else { Log.e("WebdavEntry", "General fuckup, no status for webdav response"); } }
public List<String> getFileList(String destinationDirectory) throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { String repositoryUrl = repository.getUrl(); String url = repositoryUrl + (repositoryUrl.endsWith("/") ? "" : "/") + destinationDirectory; PropFindMethod method = null; try { if (isDirectory(url)) { DavPropertyNameSet nameSet = new DavPropertyNameSet(); nameSet.add(DavPropertyName.create(DavConstants.PROPERTY_DISPLAYNAME)); method = new PropFindMethod(url, nameSet, DavConstants.DEPTH_1); int status = execute(method); if (method.succeeded()) { ArrayList<String> dirs = new ArrayList<String>(); MultiStatus multiStatus = method.getResponseBodyAsMultiStatus(); for (int i = 0; i < multiStatus.getResponses().length; i++) { MultiStatusResponse response = multiStatus.getResponses()[i]; String entryUrl = response.getHref(); String fileName = PathUtils.filename(URLDecoder.decode(entryUrl)); if (entryUrl.endsWith("/")) { if (i == 0) { // by design jackrabbit webdav sticks parent directory as the first entry // so we need to ignore this entry // http://www.nabble.com/Extra-entry-in-get-file-list-with-jackrabbit-webdav-td21262786.html // http://www.webdav.org/specs/rfc4918.html#rfc.section.9.1 continue; } // extract "dir/" part of "path.to.dir/" fileName = PathUtils.filename(PathUtils.dirname(URLDecoder.decode(entryUrl))) + "/"; } if (!StringUtils.isEmpty(fileName)) { dirs.add(fileName); } } return dirs; } if (status == HttpStatus.SC_NOT_FOUND) { throw new ResourceDoesNotExistException("Destination directory does not exist: " + url); } } } catch (DavException e) { throw new TransferFailedException(e.getMessage(), e); } catch (IOException e) { throw new TransferFailedException(e.getMessage(), e); } finally { if (method != null) { method.releaseConnection(); } } throw new ResourceDoesNotExistException( "Destination path exists but is not a " + "WebDAV collection (directory): " + url); }