1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
18 package eu
.alefzero
.webdav
;
20 import java
.io
.BufferedInputStream
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
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
;
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
;
55 public class WebdavClient
extends HttpClient
{
57 private Credentials mCredentials
;
58 final private static String TAG
= "WebdavClient";
59 private static final String USER_AGENT
= "Android-ownCloud";
61 /** Default timeout for waiting data from the server: 10 seconds */
62 public static final int DEFAULT_DATA_TIMEOUT
= 10000;
64 /** Default timeout for establishing a connection: infinite */
65 public static final int DEFAULT_CONNECTION_TIMEOUT
= 0;
67 private OnDatatransferProgressListener mDataTransferListener
;
68 static private MultiThreadedHttpConnectionManager mConnManager
= null
;
70 static public MultiThreadedHttpConnectionManager
getMultiThreadedConnManager() {
71 if (mConnManager
== null
) {
72 mConnManager
= new MultiThreadedHttpConnectionManager();
73 mConnManager
.setMaxConnectionsPerHost(5);
74 mConnManager
.setMaxTotalConnections(5);
80 * Creates a WebdavClient setup for the current account
81 * @param account The client accout
82 * @param context The application context
85 public WebdavClient (Account account
, Context context
) {
88 OwnCloudVersion ownCloudVersion
= new OwnCloudVersion(AccountManager
.get(context
).getUserData(account
,
89 AccountAuthenticator
.KEY_OC_VERSION
));
90 String baseUrl
= AccountManager
.get(context
).getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
91 String webDavPath
= AccountUtils
.getWebdavPath(ownCloudVersion
);
92 String username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
93 String password
= AccountManager
.get(context
).getPassword(account
);
95 mUri
= Uri
.parse(baseUrl
+ webDavPath
);
96 Log
.e("ASD", ""+username
);
97 setCredentials(username
, password
);
100 public WebdavClient() {
101 super(getMultiThreadedConnManager());
103 setDefaultTimeouts();
105 getParams().setParameter(HttpMethodParams
.USER_AGENT
, USER_AGENT
);
106 getParams().setParameter(CoreProtocolPNames
.PROTOCOL_VERSION
, HttpVersion
.HTTP_1_1
);
107 allowSelfsignedCertificates();
110 public void setCredentials(String username
, String password
) {
111 getParams().setAuthenticationPreemptive(true
);
112 getState().setCredentials(AuthScope
.ANY
,
113 getCredentials(username
, password
));
116 private Credentials
getCredentials(String username
, String password
) {
117 if (mCredentials
== null
)
118 mCredentials
= new UsernamePasswordCredentials(username
, password
);
123 * Sets the connection and wait-for-data timeouts to be applied by default.
125 private void setDefaultTimeouts() {
126 getParams().setSoTimeout(DEFAULT_DATA_TIMEOUT
);
127 getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT
);
130 public void allowSelfsignedCertificates() {
132 Protocol
.registerProtocol("https", new Protocol("https",
133 new EasySSLSocketFactory(), 443));
137 * Downloads a file in remoteFilepath to the local targetPath.
139 * @param remoteFilepath Path to the file in the remote server, URL DECODED.
140 * @param targetFile Local path to save the downloaded file.
141 * @return 'True' when the file is successfully downloaded.
143 public boolean downloadFile(String remoteFilepath
, File targetFile
) {
145 GetMethod get
= new GetMethod(mUri
.toString() + WebdavUtils
.encodePath(remoteFilepath
));
149 status
= executeMethod(get
);
150 if (status
== HttpStatus
.SC_OK
) {
151 targetFile
.createNewFile();
152 BufferedInputStream bis
= new BufferedInputStream(
153 get
.getResponseBodyAsStream());
154 FileOutputStream fos
= new FileOutputStream(targetFile
);
156 byte[] bytes
= new byte[4096];
158 while ((readResult
= bis
.read(bytes
)) != -1) {
159 if (mDataTransferListener
!= null
)
160 mDataTransferListener
.transferProgress(readResult
);
161 fos
.write(bytes
, 0, readResult
);
166 } catch (HttpException e
) {
167 Log
.e(TAG
, "HTTP exception downloading " + remoteFilepath
, e
);
169 } catch (IOException e
) {
170 Log
.e(TAG
, "I/O exception downloading " + remoteFilepath
, e
);
172 } catch (Exception e
) {
173 Log
.e(TAG
, "Unexpected exception downloading " + remoteFilepath
, e
);
178 Log
.e(TAG
, "Download of " + remoteFilepath
+ " to " + targetFile
+ " failed with HTTP status " + status
);
180 if (targetFile
.exists()) {
189 * Deletes a remote file via webdav
190 * @param remoteFilePath Remote file path of the file to delete, in URL DECODED format.
193 public boolean deleteFile(String remoteFilePath
){
194 DavMethod delete
= new DeleteMethod(mUri
.toString() + WebdavUtils
.encodePath(remoteFilePath
));
196 executeMethod(delete
);
197 } catch (Throwable e
) {
198 Log
.e(TAG
, "Deleting failed with error: " + e
.getMessage(), e
);
204 public void setDataTransferProgressListener(OnDatatransferProgressListener listener
) {
205 mDataTransferListener
= listener
;
209 * Creates or update a file in the remote server with the contents of a local file.
212 * @param localFile Path to the local file to upload.
213 * @param remoteTarget Remote path to the file to create or update, URL DECODED
214 * @param contentType MIME type of the file.
215 * @return 'True' then the upload was successfully completed
217 public boolean putFile(String localFile
, String remoteTarget
, String contentType
) {
218 boolean result
= false
;
222 File f
= new File(localFile
);
223 FileRequestEntity entity
= new FileRequestEntity(f
, contentType
);
224 entity
.setOnDatatransferProgressListener(mDataTransferListener
);
225 PutMethod put
= new PutMethod(mUri
.toString() + WebdavUtils
.encodePath(remoteTarget
));
226 put
.setRequestEntity(entity
);
227 status
= executeMethod(put
);
229 result
= (status
== HttpStatus
.SC_OK
|| status
== HttpStatus
.SC_CREATED
|| status
== HttpStatus
.SC_NO_CONTENT
);
231 Log
.d(TAG
, "PUT response for " + remoteTarget
+ " finished with HTTP status " + status
);
233 } catch (HttpException e
) {
234 Log
.e(TAG
, "HTTP exception uploading " + localFile
+ " to " + remoteTarget
, e
);
236 } catch (IOException e
) {
237 Log
.e(TAG
, "I/O exception uploading " + localFile
+ " to " + remoteTarget
, e
);
239 } catch (Exception e
) {
240 Log
.e(TAG
, "Unexpected exception uploading " + localFile
+ " to " + remoteTarget
, e
);
243 if (!result
&& status
>= 0) Log
.e(TAG
, "Upload of " + localFile
+ " to " + remoteTarget
+ " FAILED with HTTP status " + status
);
249 * Tries to log in to the given WedDavURI, with the given credentials
251 * @param username Username to check
252 * @param password Password to verify
253 * @return A {@link HttpStatus}-Code of the result. SC_OK is good.
255 public static int tryToLogin(Uri uri
, String username
, String password
) {
257 WebdavClient client
= new WebdavClient();
258 client
.setCredentials(username
, password
);
259 HeadMethod head
= new HeadMethod(uri
.toString());
261 returnCode
= client
.executeMethod(head
);
262 } catch (HttpException e
) {
263 Log
.e(TAG
, "HTTP exception trying to login at " + uri
.getEncodedPath(), e
);
264 } catch (IOException e
) {
265 Log
.e(TAG
, "I/O exception trying to login at " + uri
.getEncodedPath(), e
);
266 } catch (Exception e
) {
267 Log
.e(TAG
, "Unexpected exception trying to login at " + uri
.getEncodedPath(), e
);
273 * Creates a remote directory with the received path.
275 * @param path Path of the directory to create, URL DECODED
276 * @return 'True' when the directory is successfully created
278 public boolean createDirectory(String path
) {
279 boolean result
= false
;
282 MkColMethod mkcol
= new MkColMethod(mUri
.toString() + WebdavUtils
.encodePath(path
));
283 Log
.d(TAG
, "Creating directory " + path
);
284 status
= executeMethod(mkcol
);
285 Log
.d(TAG
, "Status returned: " + status
);
286 result
= mkcol
.succeeded();
288 } catch (HttpException e
) {
289 Log
.e(TAG
, "HTTP exception creating directory " + path
, e
);
291 } catch (IOException e
) {
292 Log
.e(TAG
, "I/O exception creating directory " + path
, e
);
294 } catch (Exception e
) {
295 Log
.e(TAG
, "Unexpected exception creating directory " + path
, e
);
298 if (!result
&& status
>= 0) {
299 Log
.e(TAG
, "Creation of directory " + path
+ " failed with HTTP status " + status
);
306 * Check if a file exists in the OC server
308 * @return 'Boolean.TRUE' if the file exists; 'Boolean.FALSE' it doesn't exist; NULL if couldn't be checked
310 public Boolean
existsFile(String path
) {
312 HeadMethod head
= new HeadMethod(mUri
.toString() + WebdavUtils
.encodePath(path
));
313 int status
= executeMethod(head
);
314 return (status
== HttpStatus
.SC_OK
);
315 } catch (Exception e
) {
323 * Requests the received method with the received timeout (milliseconds).
325 * Executes the method through the inherited HttpClient.executedMethod(method).
327 * Sets the socket timeout for the HttpMethodBase method received.
329 * @param method HTTP method request.
330 * @param timeout Timeout to set, in milliseconds; <= 0 means infinite.
332 public int executeMethod(HttpMethodBase method
, int readTimeout
) throws HttpException
, IOException
{
333 int oldSoTimeout
= getParams().getSoTimeout();
335 if (readTimeout
< 0) {
338 HttpMethodParams params
= method
.getParams();
339 params
.setSoTimeout(readTimeout
);
340 method
.setParams(params
); // this should be enough...
341 getParams().setSoTimeout(readTimeout
); // ... but this is necessary for HTTPS
342 return executeMethod(method
);
344 getParams().setSoTimeout(oldSoTimeout
);