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";
60 private OnDatatransferProgressListener mDataTransferListener
;
61 static private MultiThreadedHttpConnectionManager mConnManager
= null
;
63 static public MultiThreadedHttpConnectionManager
getMultiThreadedConnManager() {
64 if (mConnManager
== null
) {
65 mConnManager
= new MultiThreadedHttpConnectionManager();
66 mConnManager
.setMaxConnectionsPerHost(5);
67 mConnManager
.setMaxTotalConnections(5);
73 * Creates a WebdavClient setup for the current account
74 * @param account The client accout
75 * @param context The application context
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
);
86 mUri
= Uri
.parse(baseUrl
+ webDavPath
);
88 setCredentials(username
, password
);
91 public WebdavClient() {
92 super(getMultiThreadedConnManager());
94 getParams().setParameter(HttpMethodParams
.USER_AGENT
, USER_AGENT
);
95 getParams().setParameter(CoreProtocolPNames
.PROTOCOL_VERSION
, HttpVersion
.HTTP_1_1
);
96 allowSelfsignedCertificates();
99 public void setCredentials(String username
, String password
) {
100 getParams().setAuthenticationPreemptive(true
);
101 getState().setCredentials(AuthScope
.ANY
,
102 getCredentials(username
, password
));
105 private Credentials
getCredentials(String username
, String password
) {
106 if (mCredentials
== null
)
107 mCredentials
= new UsernamePasswordCredentials(username
, password
);
111 public void allowSelfsignedCertificates() {
113 Protocol
.registerProtocol("https", new Protocol("https",
114 new EasySSLSocketFactory(), 443));
118 * Downloads a file in remoteFilepath to the local targetPath.
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.
124 public boolean downloadFile(String remoteFilepath
, File targetPath
) {
126 GetMethod get
= new GetMethod(mUri
.toString() + WebdavUtils
.encodePath(remoteFilepath
));
128 // get.setHeader("Host", mUri.getHost());
129 // get.setHeader("User-Agent", "Android-ownCloud");
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
);
140 byte[] bytes
= new byte[4096];
142 while ((readResult
= bis
.read(bytes
)) != -1) {
143 if (mDataTransferListener
!= null
)
144 mDataTransferListener
.transferProgress(readResult
);
145 fos
.write(bytes
, 0, readResult
);
150 } catch (Throwable e
) {
159 * Deletes a remote file via webdav
160 * @param remoteFilePath Remote file path of the file to delete, in URL DECODED format.
163 public boolean deleteFile(String remoteFilePath
){
164 DavMethod delete
= new DeleteMethod(mUri
.toString() + WebdavUtils
.encodePath(remoteFilePath
));
166 executeMethod(delete
);
167 } catch (Throwable e
) {
168 Log
.e(TAG
, "Deleting failed with error: " + e
.getMessage(), e
);
174 public void setDataTransferProgressListener(OnDatatransferProgressListener listener
) {
175 mDataTransferListener
= listener
;
179 * Creates or update a file in the remote server with the contents of a local file.
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
187 public boolean putFile(String localFile
, String remoteTarget
,
188 String contentType
) {
189 boolean result
= true
;
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
);
203 Log
.i(TAG
, "Uploading, done");
204 } catch (final Exception e
) {
205 Log
.i(TAG
, "" + e
.getMessage());
213 * Tries to log in to the given WedDavURI, with the given credentials
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.
219 public static int tryToLogin(Uri uri
, String username
, String password
) {
221 WebdavClient client
= new WebdavClient();
222 client
.setCredentials(username
, password
);
223 HeadMethod head
= new HeadMethod(uri
.toString());
225 returnCode
= client
.executeMethod(head
);
226 } catch (Exception e
) {
227 Log
.e(TAG
, "Error: " + e
.getMessage());
233 * Creates a remote directory with the received path.
235 * @param path Path of the directory to create, URL DECODED
236 * @return 'True' when the directory is successfully created
238 public boolean createDirectory(String path
) {
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
) {
254 * Requests the received method with the received timeout (milliseconds).
256 * Executes the method through the inherited HttpClient.executedMethod(method).
258 * Sets the socket timeout for the HttpMethodBase method received.
260 * @param method HTTP method request.
261 * @param timeout Timeout to set, in milliseconds; <= 0 means infinite.
263 public int executeMethod(HttpMethodBase method
, int readTimeout
) throws HttpException
, IOException
{
264 int oldSoTimeout
= getParams().getSoTimeout();
266 if (readTimeout
< 0) {
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
);
275 getParams().setSoTimeout(oldSoTimeout
);