Some fixes about downloads and how the detals view and list view is updated when...
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.io.BufferedInputStream;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24
25 import org.apache.commons.httpclient.Credentials;
26 import org.apache.commons.httpclient.HttpClient;
27 import org.apache.commons.httpclient.HttpException;
28 import org.apache.commons.httpclient.HttpMethodBase;
29 import org.apache.commons.httpclient.HttpVersion;
30 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
31 import org.apache.commons.httpclient.UsernamePasswordCredentials;
32 import org.apache.commons.httpclient.auth.AuthScope;
33 import org.apache.commons.httpclient.methods.GetMethod;
34 import org.apache.commons.httpclient.methods.HeadMethod;
35 import org.apache.commons.httpclient.methods.PutMethod;
36 import org.apache.commons.httpclient.params.HttpMethodParams;
37 import org.apache.commons.httpclient.protocol.Protocol;
38 import org.apache.http.HttpStatus;
39 import org.apache.http.params.CoreProtocolPNames;
40 import org.apache.jackrabbit.webdav.client.methods.DavMethod;
41 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
42 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
43
44 import android.accounts.Account;
45 import android.accounts.AccountManager;
46 import android.content.Context;
47 import android.net.Uri;
48 import android.util.Log;
49 import eu.alefzero.owncloud.AccountUtils;
50 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
51 import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;
52 import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;
53 import eu.alefzero.owncloud.utils.OwnCloudVersion;
54
55 public class WebdavClient extends HttpClient {
56 private Uri mUri;
57 private Credentials mCredentials;
58 final private static String TAG = "WebdavClient";
59 private static final String USER_AGENT = "Android-ownCloud";
60 private OnDatatransferProgressListener mDataTransferListener;
61 static private MultiThreadedHttpConnectionManager mConnManager = null;
62
63 static public MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {
64 if (mConnManager == null) {
65 mConnManager = new MultiThreadedHttpConnectionManager();
66 mConnManager.setMaxConnectionsPerHost(5);
67 mConnManager.setMaxTotalConnections(5);
68 }
69 return mConnManager;
70 }
71
72 /**
73 * Creates a WebdavClient setup for the current account
74 * @param account The client accout
75 * @param context The application context
76 * @return
77 */
78 public WebdavClient (Account account, Context context) {
79 OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account,
80 AccountAuthenticator.KEY_OC_VERSION));
81 String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
82 String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);
83 String username = account.name.substring(0, account.name.indexOf('@'));
84 String password = AccountManager.get(context).getPassword(account);
85
86 mUri = Uri.parse(baseUrl + webDavPath);
87
88 setCredentials(username, password);
89 }
90
91 public WebdavClient() {
92 super(getMultiThreadedConnManager());
93
94 getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
95 getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
96 allowSelfsignedCertificates();
97 }
98
99 public void setCredentials(String username, String password) {
100 getParams().setAuthenticationPreemptive(true);
101 getState().setCredentials(AuthScope.ANY,
102 getCredentials(username, password));
103 }
104
105 private Credentials getCredentials(String username, String password) {
106 if (mCredentials == null)
107 mCredentials = new UsernamePasswordCredentials(username, password);
108 return mCredentials;
109 }
110
111 public void allowSelfsignedCertificates() {
112 // https
113 Protocol.registerProtocol("https", new Protocol("https",
114 new EasySSLSocketFactory(), 443));
115 }
116
117 /**
118 * Downloads a file in remoteFilepath to the local targetPath.
119 *
120 * @param remoteFilepath Path to the file in the remote server, URL DECODED.
121 * @param targetPath Local path to save the downloaded file.
122 * @return 'True' when the file is successfully downloaded.
123 */
124 public boolean downloadFile(String remoteFilepath, File targetPath) {
125 boolean ret = false;
126 GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));
127
128 // get.setHeader("Host", mUri.getHost());
129 // get.setHeader("User-Agent", "Android-ownCloud");
130
131 try {
132 int status = executeMethod(get, 0);
133 Log.e(TAG, "status return: " + status);
134 if (status == HttpStatus.SC_OK) {
135 targetPath.createNewFile();
136 BufferedInputStream bis = new BufferedInputStream(
137 get.getResponseBodyAsStream());
138 FileOutputStream fos = new FileOutputStream(targetPath);
139
140 byte[] bytes = new byte[4096];
141 int readResult;
142 while ((readResult = bis.read(bytes)) != -1) {
143 if (mDataTransferListener != null)
144 mDataTransferListener.transferProgress(readResult);
145 fos.write(bytes, 0, readResult);
146 }
147 ret = true;
148 }
149
150 } catch (Throwable e) {
151 e.printStackTrace();
152 targetPath.delete();
153 }
154
155 return ret;
156 }
157
158 /**
159 * Deletes a remote file via webdav
160 * @param remoteFilePath Remote file path of the file to delete, in URL DECODED format.
161 * @return
162 */
163 public boolean deleteFile(String remoteFilePath){
164 DavMethod delete = new DeleteMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilePath));
165 try {
166 executeMethod(delete);
167 } catch (Throwable e) {
168 Log.e(TAG, "Deleting failed with error: " + e.getMessage(), e);
169 return false;
170 }
171 return true;
172 }
173
174 public void setDataTransferProgressListener(OnDatatransferProgressListener listener) {
175 mDataTransferListener = listener;
176 }
177
178 /**
179 * Creates or update a file in the remote server with the contents of a local file.
180 *
181 *
182 * @param localFile Path to the local file to upload.
183 * @param remoteTarget Remote path to the file to create or update, URL DECODED
184 * @param contentType MIME type of the file.
185 * @return 'True' then the upload was successfully completed
186 */
187 public boolean putFile(String localFile, String remoteTarget,
188 String contentType) {
189 boolean result = true;
190
191 try {
192 Log.e("ASD", contentType + "");
193 File f = new File(localFile);
194 FileRequestEntity entity = new FileRequestEntity(f, contentType);
195 entity.setOnDatatransferProgressListener(mDataTransferListener);
196 Log.e("ASD", f.exists() + " " + entity.getContentLength());
197 PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));
198 put.setRequestEntity(entity);
199 Log.d(TAG, "" + put.getURI().toString());
200 int status = executeMethod(put, 0);
201 Log.d(TAG, "PUT method return with status " + status);
202
203 Log.i(TAG, "Uploading, done");
204 } catch (final Exception e) {
205 Log.i(TAG, "" + e.getMessage());
206 result = false;
207 }
208
209 return result;
210 }
211
212 /**
213 * Tries to log in to the given WedDavURI, with the given credentials
214 * @param uri To test
215 * @param username Username to check
216 * @param password Password to verify
217 * @return A {@link HttpStatus}-Code of the result. SC_OK is good.
218 */
219 public static int tryToLogin(Uri uri, String username, String password) {
220 int returnCode = 0;
221 WebdavClient client = new WebdavClient();
222 client.setCredentials(username, password);
223 HeadMethod head = new HeadMethod(uri.toString());
224 try {
225 returnCode = client.executeMethod(head);
226 } catch (Exception e) {
227 Log.e(TAG, "Error: " + e.getMessage());
228 }
229 return returnCode;
230 }
231
232 /**
233 * Creates a remote directory with the received path.
234 *
235 * @param path Path of the directory to create, URL DECODED
236 * @return 'True' when the directory is successfully created
237 */
238 public boolean createDirectory(String path) {
239 try {
240 MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));
241 int status = executeMethod(mkcol);
242 Log.d(TAG, "Status returned " + status);
243 Log.d(TAG, "uri: " + mkcol.getURI().toString());
244 Log.i(TAG, "Creating dir completed");
245 } catch (final Exception e) {
246 e.printStackTrace();
247 return false;
248 }
249 return true;
250 }
251
252
253 /**
254 * Requests the received method with the received timeout (milliseconds).
255 *
256 * Executes the method through the inherited HttpClient.executedMethod(method).
257 *
258 * Sets the socket timeout for the HttpMethodBase method received.
259 *
260 * @param method HTTP method request.
261 * @param timeout Timeout to set, in milliseconds; <= 0 means infinite.
262 */
263 public int executeMethod(HttpMethodBase method, int readTimeout) throws HttpException, IOException {
264 int oldSoTimeout = getParams().getSoTimeout();
265 try {
266 if (readTimeout < 0) {
267 readTimeout = 0;
268 }
269 HttpMethodParams params = method.getParams();
270 params.setSoTimeout(readTimeout);
271 method.setParams(params); // this should be enough...
272 getParams().setSoTimeout(readTimeout); // ... but this is necessary for HTTPS
273 return executeMethod(method);
274 } finally {
275 getParams().setSoTimeout(oldSoTimeout);
276 }
277 }
278 }