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