This wraps the android.util.logging into Log_OC which , if its enabled
[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 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
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 com.owncloud.android.Log_OC;
28
29 import android.net.Uri;
30 import android.util.Log;
31
32 public class WebdavEntry {
33 private String mName, mPath, mUri, mContentType;
34 private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
35
36 public WebdavEntry(MultiStatusResponse ms, String splitElement) {
37 resetData();
38 if (ms.getStatus().length != 0) {
39 mUri = ms.getHref();
40
41 mPath = mUri.split(splitElement, 2)[1];
42
43 int status = ms.getStatus()[0].getStatusCode();
44 DavPropertySet propSet = ms.getProperties(status);
45 @SuppressWarnings("rawtypes")
46 DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
47 if (prop != null)
48 mName = (String) prop.getName().toString();
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 , or 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 } else {
94 Log_OC.e("WebdavEntry",
95 "General fuckup, no status for webdav response");
96 }
97 }
98
99 public String path() {
100 return mPath;
101 }
102
103 public String decodedPath() {
104 return Uri.decode(mPath);
105 }
106
107 public String name() {
108 return mName;
109 }
110
111 public boolean isDirectory() {
112 return mContentType.equals("DIR");
113 }
114
115 public String contentType() {
116 return mContentType;
117 }
118
119 public String uri() {
120 return mUri;
121 }
122
123 public long contentLength() {
124 return mContentLength;
125 }
126
127 public long createTimestamp() {
128 return mCreateTimestamp;
129 }
130
131 public long modifiedTimestamp() {
132 return mModifiedTimestamp;
133 }
134
135 private void resetData() {
136 mName = mUri = mContentType = null;
137 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
138 }
139 }