Fixed folder detection, now WebDAV compliant
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavEntry.java
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 3 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
20 import java.util.Date;
21
22 import org.apache.jackrabbit.webdav.MultiStatusResponse;
23 import org.apache.jackrabbit.webdav.property.DavProperty;
24 import org.apache.jackrabbit.webdav.property.DavPropertyName;
25 import org.apache.jackrabbit.webdav.property.DavPropertySet;
26
27 import android.net.Uri;
28 import android.util.Log;
29
30 public class WebdavEntry {
31 private String mName, mPath, mUri, mContentType;
32 private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
33
34 public WebdavEntry(MultiStatusResponse ms, String splitElement) {
35 resetData();
36 if (ms.getStatus().length != 0) {
37 mUri = ms.getHref();
38
39 mPath = mUri.split(splitElement, 2)[1];
40
41 int status = ms.getStatus()[0].getStatusCode();
42 DavPropertySet propSet = ms.getProperties(status);
43 @SuppressWarnings("rawtypes")
44 DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
45 if (prop != null)
46 mName = (String) prop.getName().toString();
47 else {
48 String[] tmp = mPath.split("/");
49 if (tmp.length > 0)
50 mName = tmp[tmp.length - 1];
51 }
52
53 prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
54 if (prop != null) {
55 mContentType = (String) prop.getValue();
56 // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
57 if (mContentType.indexOf(";") >= 0) {
58 mContentType = mContentType.substring(0, mContentType.indexOf(";"));
59 }
60 }
61
62 // check if it's a folder in the standard way: see RFC2518 12.2 , or RFC4918 14.3
63 prop = propSet.get(DavPropertyName.RESOURCETYPE);
64 if (prop!= null) {
65 Object value = prop.getValue();
66 if (value != null) {
67 mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we have no reason to distinguish MIME types for folders
68 }
69 }
70
71 prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
72 if (prop != null)
73 mContentLength = Long.parseLong((String) prop.getValue());
74
75 prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
76 if (prop != null) {
77 Date d = WebdavUtils
78 .parseResponseDate((String) prop.getValue());
79 mModifiedTimestamp = (d != null) ? d.getTime() : 0;
80 }
81
82 prop = propSet.get(DavPropertyName.CREATIONDATE);
83 if (prop != null) {
84 Date d = WebdavUtils
85 .parseResponseDate((String) prop.getValue());
86 mCreateTimestamp = (d != null) ? d.getTime() : 0;
87 }
88
89 } else {
90 Log.e("WebdavEntry",
91 "General fuckup, no status for webdav response");
92 }
93 }
94
95 public String path() {
96 return mPath;
97 }
98
99 public String decodedPath() {
100 return Uri.decode(mPath);
101 }
102
103 public String name() {
104 return mName;
105 }
106
107 public boolean isDirectory() {
108 return mContentType.equals("DIR");
109 }
110
111 public String contentType() {
112 return mContentType;
113 }
114
115 public String uri() {
116 return mUri;
117 }
118
119 public long contentLength() {
120 return mContentLength;
121 }
122
123 public long createTimestamp() {
124 return mCreateTimestamp;
125 }
126
127 public long modifiedTimesamp() {
128 return mModifiedTimestamp;
129 }
130
131 private void resetData() {
132 mName = mUri = mContentType = null;
133 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
134 }
135 }