f4fbda451a1429d58d3c70137f251cd0f1b9d8d2
[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.util.Log;
28
29 public class WebdavEntry {
30 private String mName, mPath, mUri, mContentType;
31 private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
32
33 public WebdavEntry(MultiStatusResponse ms, String splitElement) {
34 resetData();
35 if (ms.getStatus().length != 0) {
36 mUri = ms.getHref();
37
38 mPath = mUri.split(splitElement, 2)[1];
39
40 int status = ms.getStatus()[0].getStatusCode();
41 DavPropertySet propSet = ms.getProperties(status);
42 @SuppressWarnings("rawtypes")
43 DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
44 if (prop != null)
45 mName = (String) prop.getName().toString();
46 else {
47 String[] tmp = mPath.split("/");
48 if (tmp.length > 0)
49 mName = tmp[tmp.length - 1];
50 }
51
52 prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
53 if (prop != null) {
54 mContentType = (String) prop.getValue();
55 // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
56 if (mContentType.indexOf(";") >= 0) {
57 mContentType = mContentType.substring(0, mContentType.indexOf(";"));
58 }
59 } else {
60 mContentType = "DIR";
61 /*
62 * prop = propSet.get(DavPropertyName.ISCOLLECTION); if (prop !=
63 * null && Boolean.parseBoolean((String) prop.getValue()))
64 * mContentType = "DIR";
65 */
66 }
67
68 prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
69 if (prop != null)
70 mContentLength = Long.parseLong((String) prop.getValue());
71
72 prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
73 if (prop != null) {
74 Date d = WebdavUtils
75 .parseResponseDate((String) prop.getValue());
76 mModifiedTimestamp = (d != null) ? d.getTime() : 0;
77 }
78
79 prop = propSet.get(DavPropertyName.CREATIONDATE);
80 if (prop != null) {
81 Date d = WebdavUtils
82 .parseResponseDate((String) prop.getValue());
83 mCreateTimestamp = (d != null) ? d.getTime() : 0;
84 }
85
86 } else {
87 Log.e("WebdavEntry",
88 "General fuckup, no status for webdav response");
89 }
90 }
91
92 public String path() {
93 return mPath;
94 }
95
96 public String name() {
97 return mName;
98 }
99
100 public boolean isDirectory() {
101 return mContentType.equals("DIR");
102 }
103
104 public String contentType() {
105 return mContentType;
106 }
107
108 public String uri() {
109 return mUri;
110 }
111
112 public long contentLength() {
113 return mContentLength;
114 }
115
116 public long createTimestamp() {
117 return mCreateTimestamp;
118 }
119
120 public long modifiedTimesamp() {
121 return mModifiedTimestamp;
122 }
123
124 private void resetData() {
125 mName = mUri = mContentType = null;
126 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
127 }
128 }