Extra logs for debugging connections
[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 com.owncloud.android.AccountUtils;
45 import com.owncloud.android.authenticator.AccountAuthenticator;
46 import com.owncloud.android.authenticator.EasySSLSocketFactory;
47 import com.owncloud.android.files.interfaces.OnDatatransferProgressListener;
48 import com.owncloud.android.utils.OwnCloudVersion;
49
50 import android.accounts.Account;
51 import android.accounts.AccountManager;
52 import android.content.Context;
53 import android.net.Uri;
54 import android.util.Log;
55
56 public class WebdavClient extends HttpClient {
57 private Uri mUri;
58 private Credentials mCredentials;
59 final private static String TAG = "WebdavClient";
60 private static final String USER_AGENT = "Android-ownCloud";
61
62 /** Default timeout for waiting data from the server: 10 seconds */
63 public static final int DEFAULT_DATA_TIMEOUT = 10000;
64
65 /** Default timeout for establishing a connection: infinite */
66 public static final int DEFAULT_CONNECTION_TIMEOUT = 0;
67
68 private OnDatatransferProgressListener mDataTransferListener;
69 static private MultiThreadedHttpConnectionManager mConnManager = null;
70
71 static public MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {
72 if (mConnManager == null) {
73 mConnManager = new MultiThreadedHttpConnectionManager();
74 mConnManager.setMaxConnectionsPerHost(5);
75 mConnManager.setMaxTotalConnections(5);
76 }
77 return mConnManager;
78 }
79
80 /**
81 * Creates a WebdavClient setup for the current account
82 * @param account The client accout
83 * @param context The application context
84 * @return
85 */
86 public WebdavClient (Account account, Context context) {
87 Log.d(TAG, "Creating WebdavClient associated to " + account.name);
88
89 setDefaultTimeouts();
90
91 OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account,
92 AccountAuthenticator.KEY_OC_VERSION));
93 String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
94 String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);
95 String username = account.name.substring(0, account.name.lastIndexOf('@'));
96 String password = AccountManager.get(context).getPassword(account);
97
98 mUri = Uri.parse(baseUrl + webDavPath);
99 Log.e("ASD", ""+username);
100 setCredentials(username, password);
101 }
102
103 public WebdavClient() {
104 super(getMultiThreadedConnManager());
105 Log.d(TAG, "Creating WebdavClient");
106
107 setDefaultTimeouts();
108
109 getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
110 getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
111 allowSelfsignedCertificates();
112 }
113
114 public void setCredentials(String username, String password) {
115 getParams().setAuthenticationPreemptive(true);
116 getState().setCredentials(AuthScope.ANY,
117 getCredentials(username, password));
118 }
119
120 private Credentials getCredentials(String username, String password) {
121 if (mCredentials == null)
122 mCredentials = new UsernamePasswordCredentials(username, password);
123 return mCredentials;
124 }
125
126 /**
127 * Sets the connection and wait-for-data timeouts to be applied by default.
128 */
129 private void setDefaultTimeouts() {
130 getParams().setSoTimeout(DEFAULT_DATA_TIMEOUT);
131 getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
132 }
133
134 public void allowSelfsignedCertificates() {
135 // https
136 Protocol.registerProtocol("https", new Protocol("https",
137 new EasySSLSocketFactory(), 443));
138 }
139
140 /**
141 * Downloads a file in remoteFilepath to the local targetPath.
142 *
143 * @param remoteFilepath Path to the file in the remote server, URL DECODED.
144 * @param targetFile Local path to save the downloaded file.
145 * @return 'True' when the file is successfully downloaded.
146 */
147 public boolean downloadFile(String remoteFilepath, File targetFile) {
148 boolean ret = false;
149 GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));
150
151 int status = -1;
152 try {
153 status = executeMethod(get);
154 if (status == HttpStatus.SC_OK) {
155 targetFile.createNewFile();
156 BufferedInputStream bis = new BufferedInputStream(
157 get.getResponseBodyAsStream());
158 FileOutputStream fos = new FileOutputStream(targetFile);
159
160 byte[] bytes = new byte[4096];
161 int readResult;
162 while ((readResult = bis.read(bytes)) != -1) {
163 if (mDataTransferListener != null)
164 mDataTransferListener.transferProgress(readResult);
165 fos.write(bytes, 0, readResult);
166 }
167 ret = true;
168 }
169
170 } catch (HttpException e) {
171 Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e);
172
173 } catch (IOException e) {
174 Log.e(TAG, "I/O exception downloading " + remoteFilepath, e);
175
176 } catch (Exception e) {
177 Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e);
178
179 } finally {
180 if (!ret) {
181 if (status >= 0) {
182 Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status);
183 }
184 if (targetFile.exists()) {
185 targetFile.delete();
186 }
187 }
188 }
189 return ret;
190 }
191
192 /**
193 * Deletes a remote file via webdav
194 * @param remoteFilePath Remote file path of the file to delete, in URL DECODED format.
195 * @return
196 */
197 public boolean deleteFile(String remoteFilePath){
198 DavMethod delete = new DeleteMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilePath));
199 try {
200 executeMethod(delete);
201 } catch (Throwable e) {
202 Log.e(TAG, "Deleting failed with error: " + e.getMessage(), e);
203 return false;
204 }
205 return true;
206 }
207
208 public void setDataTransferProgressListener(OnDatatransferProgressListener listener) {
209 mDataTransferListener = listener;
210 }
211
212 /**
213 * Creates or update a file in the remote server with the contents of a local file.
214 *
215 *
216 * @param localFile Path to the local file to upload.
217 * @param remoteTarget Remote path to the file to create or update, URL DECODED
218 * @param contentType MIME type of the file.
219 * @return 'True' then the upload was successfully completed
220 */
221 public boolean putFile(String localFile, String remoteTarget, String contentType) {
222 boolean result = false;
223 int status = -1;
224
225 try {
226 File f = new File(localFile);
227 FileRequestEntity entity = new FileRequestEntity(f, contentType);
228 entity.setOnDatatransferProgressListener(mDataTransferListener);
229 PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));
230 put.setRequestEntity(entity);
231 status = executeMethod(put);
232
233 result = (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT);
234
235 Log.d(TAG, "PUT response for " + remoteTarget + " finished with HTTP status " + status);
236
237 } catch (HttpException e) {
238 Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e);
239
240 } catch (IOException e) {
241 Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e);
242
243 } catch (Exception e) {
244 Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);
245 }
246
247 if (!result && status >= 0) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);
248
249 return result;
250 }
251
252 /**
253 * Tries to log in to the given WedDavURI, with the given credentials
254 * @param uri To test
255 * @param username Username to check
256 * @param password Password to verify
257 * @return A {@link HttpStatus}-Code of the result. SC_OK is good.
258 */
259 public static int tryToLogin(Uri uri, String username, String password) {
260 int returnCode = 0;
261 try {
262 WebdavClient client = new WebdavClient();
263 client.setCredentials(username, password);
264 HeadMethod head = new HeadMethod(uri.toString());
265 returnCode = client.executeMethod(head);
266 } catch (HttpException e) {
267 Log.e(TAG, "HTTP exception trying to login at " + uri.getEncodedPath(), e);
268 } catch (IOException e) {
269 Log.e(TAG, "I/O exception trying to login at " + uri.getEncodedPath(), e);
270 } catch (Exception e) {
271 Log.e(TAG, "Unexpected exception trying to login at " + uri.getEncodedPath(), e);
272 }
273 return returnCode;
274 }
275
276 /**
277 * Creates a remote directory with the received path.
278 *
279 * @param path Path of the directory to create, URL DECODED
280 * @return 'True' when the directory is successfully created
281 */
282 public boolean createDirectory(String path) {
283 boolean result = false;
284 int status = -1;
285 try {
286 MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));
287 Log.d(TAG, "Creating directory " + path);
288 status = executeMethod(mkcol);
289 Log.d(TAG, "Status returned: " + status);
290 result = mkcol.succeeded();
291
292 } catch (HttpException e) {
293 Log.e(TAG, "HTTP exception creating directory " + path, e);
294
295 } catch (IOException e) {
296 Log.e(TAG, "I/O exception creating directory " + path, e);
297
298 } catch (Exception e) {
299 Log.e(TAG, "Unexpected exception creating directory " + path, e);
300
301 }
302 if (!result && status >= 0) {
303 Log.e(TAG, "Creation of directory " + path + " failed with HTTP status " + status);
304 }
305 return result;
306 }
307
308
309 /**
310 * Check if a file exists in the OC server
311 *
312 * @return 'Boolean.TRUE' if the file exists; 'Boolean.FALSE' it doesn't exist; NULL if couldn't be checked
313 */
314 public Boolean existsFile(String path) {
315 try {
316 HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path));
317 int status = executeMethod(head);
318 return (status == HttpStatus.SC_OK);
319 } catch (Exception e) {
320 e.printStackTrace();
321 return null;
322 }
323 }
324
325
326 /**
327 * Requests the received method with the received timeout (milliseconds).
328 *
329 * Executes the method through the inherited HttpClient.executedMethod(method).
330 *
331 * Sets the socket timeout for the HttpMethodBase method received.
332 *
333 * @param method HTTP method request.
334 * @param timeout Timeout to set, in milliseconds; <= 0 means infinite.
335 */
336 public int executeMethod(HttpMethodBase method, int readTimeout) throws HttpException, IOException {
337 int oldSoTimeout = getParams().getSoTimeout();
338 try {
339 if (readTimeout < 0) {
340 readTimeout = 0;
341 }
342 HttpMethodParams params = method.getParams();
343 params.setSoTimeout(readTimeout);
344 method.setParams(params); // this should be enough...
345 getParams().setSoTimeout(readTimeout); // ... but this is necessary for HTTPS
346 return executeMethod(method);
347 } finally {
348 getParams().setSoTimeout(oldSoTimeout);
349 }
350 }
351 }