OC-1194: Adapt Sync Process using etag
[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 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 eu.alefzero.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 import com.owncloud.android.Log_OC;
27
28 import android.net.Uri;
29
30 public class WebdavEntry {
31 private String mName, mPath, mUri, mContentType, mEtag;
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 mName = mName.substring(1, mName.length()-1);
48 }
49 else {
50 String[] tmp = mPath.split("/");
51 if (tmp.length > 0)
52 mName = tmp[tmp.length - 1];
53 }
54
55 // use unknown mimetype as default behavior
56 mContentType = "application/octet-stream";
57 prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
58 if (prop != null) {
59 mContentType = (String) prop.getValue();
60 // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
61 if (mContentType.indexOf(";") >= 0) {
62 mContentType = mContentType.substring(0, mContentType.indexOf(";"));
63 }
64 }
65
66 // check if it's a folder in the standard way: see RFC2518 12.2 . RFC4918 14.3
67 prop = propSet.get(DavPropertyName.RESOURCETYPE);
68 if (prop!= null) {
69 Object value = prop.getValue();
70 if (value != null) {
71 mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we have no reason to distinguish MIME types for folders
72 }
73 }
74
75 prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
76 if (prop != null)
77 mContentLength = Long.parseLong((String) prop.getValue());
78
79 prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
80 if (prop != null) {
81 Date d = WebdavUtils
82 .parseResponseDate((String) prop.getValue());
83 mModifiedTimestamp = (d != null) ? d.getTime() : 0;
84 }
85
86 prop = propSet.get(DavPropertyName.CREATIONDATE);
87 if (prop != null) {
88 Date d = WebdavUtils
89 .parseResponseDate((String) prop.getValue());
90 mCreateTimestamp = (d != null) ? d.getTime() : 0;
91 }
92
93 prop = propSet.get(DavPropertyName.GETETAG);
94 if (prop != null) {
95 mEtag = (String) prop.getValue();
96 mEtag = mEtag.substring(1, mEtag.length()-1);
97 }
98
99 } else {
100 Log_OC.e("WebdavEntry",
101 "General fuckup, no status for webdav response");
102 }
103 }
104
105 public String path() {
106 return mPath;
107 }
108
109 public String decodedPath() {
110 return Uri.decode(mPath);
111 }
112
113 public String name() {
114 return mName;
115 }
116
117 public boolean isDirectory() {
118 return mContentType.equals("DIR");
119 }
120
121 public String contentType() {
122 return mContentType;
123 }
124
125 public String uri() {
126 return mUri;
127 }
128
129 public long contentLength() {
130 return mContentLength;
131 }
132
133 public long createTimestamp() {
134 return mCreateTimestamp;
135 }
136
137 public long modifiedTimestamp() {
138 return mModifiedTimestamp;
139 }
140
141 public String etag() {
142 return mEtag;
143 }
144
145 private void resetData() {
146 mName = mUri = mContentType = null;
147 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
148 }
149 }