WebdavEntry.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 ownCloud
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package eu.alefzero.webdav;
  19. import java.util.Date;
  20. import org.apache.jackrabbit.webdav.MultiStatusResponse;
  21. import org.apache.jackrabbit.webdav.property.DavProperty;
  22. import org.apache.jackrabbit.webdav.property.DavPropertyName;
  23. import org.apache.jackrabbit.webdav.property.DavPropertySet;
  24. import com.owncloud.android.Log_OC;
  25. import android.net.Uri;
  26. import android.util.Log;
  27. public class WebdavEntry {
  28. private String mName, mPath, mUri, mContentType;
  29. private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
  30. public WebdavEntry(MultiStatusResponse ms, String splitElement) {
  31. resetData();
  32. if (ms.getStatus().length != 0) {
  33. mUri = ms.getHref();
  34. mPath = mUri.split(splitElement, 2)[1];
  35. int status = ms.getStatus()[0].getStatusCode();
  36. DavPropertySet propSet = ms.getProperties(status);
  37. @SuppressWarnings("rawtypes")
  38. DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
  39. if (prop != null)
  40. mName = (String) prop.getName().toString();
  41. else {
  42. String[] tmp = mPath.split("/");
  43. if (tmp.length > 0)
  44. mName = tmp[tmp.length - 1];
  45. }
  46. // use unknown mimetype as default behavior
  47. mContentType = "application/octet-stream";
  48. prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
  49. if (prop != null) {
  50. mContentType = (String) prop.getValue();
  51. // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
  52. if (mContentType.indexOf(";") >= 0) {
  53. mContentType = mContentType.substring(0, mContentType.indexOf(";"));
  54. }
  55. }
  56. // check if it's a folder in the standard way: see RFC2518 12.2 , or RFC4918 14.3
  57. prop = propSet.get(DavPropertyName.RESOURCETYPE);
  58. if (prop!= null) {
  59. Object value = prop.getValue();
  60. if (value != null) {
  61. mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we have no reason to distinguish MIME types for folders
  62. }
  63. }
  64. prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
  65. if (prop != null)
  66. mContentLength = Long.parseLong((String) prop.getValue());
  67. prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
  68. if (prop != null) {
  69. Date d = WebdavUtils
  70. .parseResponseDate((String) prop.getValue());
  71. mModifiedTimestamp = (d != null) ? d.getTime() : 0;
  72. }
  73. prop = propSet.get(DavPropertyName.CREATIONDATE);
  74. if (prop != null) {
  75. Date d = WebdavUtils
  76. .parseResponseDate((String) prop.getValue());
  77. mCreateTimestamp = (d != null) ? d.getTime() : 0;
  78. }
  79. } else {
  80. Log_OC.e("WebdavEntry",
  81. "General fuckup, no status for webdav response");
  82. }
  83. }
  84. public String path() {
  85. return mPath;
  86. }
  87. public String decodedPath() {
  88. return Uri.decode(mPath);
  89. }
  90. public String name() {
  91. return mName;
  92. }
  93. public boolean isDirectory() {
  94. return mContentType.equals("DIR");
  95. }
  96. public String contentType() {
  97. return mContentType;
  98. }
  99. public String uri() {
  100. return mUri;
  101. }
  102. public long contentLength() {
  103. return mContentLength;
  104. }
  105. public long createTimestamp() {
  106. return mCreateTimestamp;
  107. }
  108. public long modifiedTimestamp() {
  109. return mModifiedTimestamp;
  110. }
  111. private void resetData() {
  112. mName = mUri = mContentType = null;
  113. mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
  114. }
  115. }