9d0d61fe9a28952c14600dbc0bf51cc37c39de02
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / network / webdav / WebdavEntry.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.network.webdav;
26
27 import java.util.Date;
28
29 import org.apache.jackrabbit.webdav.MultiStatusResponse;
30 import org.apache.jackrabbit.webdav.property.DavProperty;
31 import org.apache.jackrabbit.webdav.property.DavPropertyName;
32 import org.apache.jackrabbit.webdav.property.DavPropertySet;
33
34
35
36 import android.net.Uri;
37 import android.util.Log;
38
39 public class WebdavEntry {
40 private String mName, mPath, mUri, mContentType, mEtag;
41 private long mContentLength, mCreateTimestamp, mModifiedTimestamp;
42
43 public WebdavEntry(MultiStatusResponse ms, String splitElement) {
44 resetData();
45 if (ms.getStatus().length != 0) {
46 mUri = ms.getHref();
47
48 mPath = mUri.split(splitElement, 2)[1];
49
50 int status = ms.getStatus()[0].getStatusCode();
51 DavPropertySet propSet = ms.getProperties(status);
52 @SuppressWarnings("rawtypes")
53 DavProperty prop = propSet.get(DavPropertyName.DISPLAYNAME);
54 if (prop != null) {
55 mName = (String) prop.getName().toString();
56 mName = mName.substring(1, mName.length()-1);
57 }
58 else {
59 String[] tmp = mPath.split("/");
60 if (tmp.length > 0)
61 mName = tmp[tmp.length - 1];
62 }
63
64 // use unknown mimetype as default behavior
65 mContentType = "application/octet-stream";
66 prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
67 if (prop != null) {
68 mContentType = (String) prop.getValue();
69 // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
70 if (mContentType.indexOf(";") >= 0) {
71 mContentType = mContentType.substring(0, mContentType.indexOf(";"));
72 }
73 }
74
75 // check if it's a folder in the standard way: see RFC2518 12.2 . RFC4918 14.3
76 prop = propSet.get(DavPropertyName.RESOURCETYPE);
77 if (prop!= null) {
78 Object value = prop.getValue();
79 if (value != null) {
80 mContentType = "DIR"; // a specific attribute would be better, but this is enough; unless while we have no reason to distinguish MIME types for folders
81 }
82 }
83
84 prop = propSet.get(DavPropertyName.GETCONTENTLENGTH);
85 if (prop != null)
86 mContentLength = Long.parseLong((String) prop.getValue());
87
88 prop = propSet.get(DavPropertyName.GETLASTMODIFIED);
89 if (prop != null) {
90 Date d = WebdavUtils
91 .parseResponseDate((String) prop.getValue());
92 mModifiedTimestamp = (d != null) ? d.getTime() : 0;
93 }
94
95 prop = propSet.get(DavPropertyName.CREATIONDATE);
96 if (prop != null) {
97 Date d = WebdavUtils
98 .parseResponseDate((String) prop.getValue());
99 mCreateTimestamp = (d != null) ? d.getTime() : 0;
100 }
101
102 prop = propSet.get(DavPropertyName.GETETAG);
103 if (prop != null) {
104 mEtag = (String) prop.getValue();
105 mEtag = mEtag.substring(1, mEtag.length()-1);
106 }
107
108 } else {
109 Log.e("WebdavEntry",
110 "General fuckup, no status for webdav response");
111 }
112 }
113
114 public String path() {
115 return mPath;
116 }
117
118 public String decodedPath() {
119 return Uri.decode(mPath);
120 }
121
122 public String name() {
123 return mName;
124 }
125
126 public boolean isDirectory() {
127 return mContentType.equals("DIR");
128 }
129
130 public String contentType() {
131 return mContentType;
132 }
133
134 public String uri() {
135 return mUri;
136 }
137
138 public long contentLength() {
139 return mContentLength;
140 }
141
142 public long createTimestamp() {
143 return mCreateTimestamp;
144 }
145
146 public long modifiedTimestamp() {
147 return mModifiedTimestamp;
148 }
149
150 public String etag() {
151 return mEtag;
152 }
153
154 private void resetData() {
155 mName = mUri = mContentType = null;
156 mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
157 }
158 }