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
;
24 import java
.util
.HashMap
;
26 import org
.apache
.commons
.httpclient
.Credentials
;
27 import org
.apache
.commons
.httpclient
.HttpClient
;
28 import org
.apache
.commons
.httpclient
.HttpException
;
29 import org
.apache
.commons
.httpclient
.UsernamePasswordCredentials
;
30 import org
.apache
.commons
.httpclient
.auth
.AuthScope
;
31 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
32 import org
.apache
.commons
.httpclient
.methods
.HeadMethod
;
33 import org
.apache
.commons
.httpclient
.methods
.PutMethod
;
34 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
35 import org
.apache
.commons
.httpclient
.protocol
.Protocol
;
36 import org
.apache
.http
.HttpStatus
;
37 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethod
;
38 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DeleteMethod
;
39 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MkColMethod
;
41 import eu
.alefzero
.owncloud
.AccountUtils
;
42 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
43 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
;
44 import eu
.alefzero
.owncloud
.files
.interfaces
.OnDatatransferProgressListener
;
45 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
47 import android
.accounts
.Account
;
48 import android
.accounts
.AccountManager
;
49 import android
.content
.Context
;
50 import android
.net
.Uri
;
51 import android
.util
.Log
;
53 public class WebdavClient
extends HttpClient
{
55 private Credentials mCredentials
;
56 final private static String TAG
= "WebdavClient";
57 private static final String USER_AGENT
= "Android-ownCloud";
58 private OnDatatransferProgressListener mDataTransferListener
;
59 private static HashMap
<String
, WebdavClient
> clients
= new HashMap
<String
, WebdavClient
>();
62 * Gets a WebdavClient setup for the current account
63 * @param account The client accout
64 * @param context The application context
67 public static synchronized WebdavClient
getInstance(Account account
, Context context
){
68 WebdavClient instance
= clients
.get(account
.name
);
69 if(instance
== null
){
70 OwnCloudVersion ownCloudVersion
= new OwnCloudVersion(AccountManager
.get(context
).getUserData(account
,
71 AccountAuthenticator
.KEY_OC_VERSION
));
72 String baseUrl
= AccountManager
.get(context
).getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
73 String webDavPath
= AccountUtils
.getWebdavPath(ownCloudVersion
);
74 WebdavClient client
= new WebdavClient();
76 String username
= account
.name
.substring(0, account
.name
.indexOf('@'));
77 String password
= AccountManager
.get(context
).getPassword(account
);
79 client
.mUri
= Uri
.parse(baseUrl
+ webDavPath
);
80 client
.getParams().setParameter(HttpMethodParams
.USER_AGENT
, USER_AGENT
);
81 client
.setCredentials(username
, password
);
82 clients
.put(account
.name
, client
);
87 public void setCredentials(String username
, String password
) {
88 getParams().setAuthenticationPreemptive(true
);
89 getState().setCredentials(AuthScope
.ANY
,
90 getCredentials(username
, password
));
93 private Credentials
getCredentials(String username
, String password
) {
94 if (mCredentials
== null
)
95 mCredentials
= new UsernamePasswordCredentials(username
, password
);
99 public void allowSelfsignedCertificates() {
101 Protocol
.registerProtocol("https", new Protocol("https",
102 new EasySSLSocketFactory(), 443));
105 public boolean downloadFile(String remoteFilepath
, File targetPath
) {
106 // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",
108 /* dvelasco - this is not necessary anymore; OCFile.mRemotePath (the origin of remoteFielPath) keeps valid URL strings
109 String[] splitted_filepath = remoteFilepath.split("/");
111 for (String s : splitted_filepath) {
112 if (s.equals("")) continue;
113 remoteFilepath += "/" + URLEncoder.encode(s);
116 Log.e("ASD", mUri.toString() + remoteFilepath.replace(" ", "%20") + "");
117 GetMethod get = new GetMethod(mUri.toString()
118 + remoteFilepath.replace(" ", "%20"));
120 GetMethod get
= new GetMethod(mUri
.toString() + remoteFilepath
);
122 // get.setHeader("Host", mUri.getHost());
123 // get.setHeader("User-Agent", "Android-ownCloud");
126 int status
= executeMethod(get
);
127 Log
.e(TAG
, "status return: " + status
);
128 if (status
!= HttpStatus
.SC_OK
) {
131 BufferedInputStream bis
= new BufferedInputStream(
132 get
.getResponseBodyAsStream());
133 FileOutputStream fos
= new FileOutputStream(targetPath
);
135 byte[] bytes
= new byte[4096];
137 while ((readResult
= bis
.read(bytes
)) != -1) {
138 if (mDataTransferListener
!= null
)
139 mDataTransferListener
.transferProgress(readResult
);
140 fos
.write(bytes
, 0, readResult
);
143 } catch (IOException e
) {
151 * Deletes a remote file via webdav
152 * @param remoteFilePath
155 public boolean deleteFile(String remoteFilePath
){
156 DavMethod delete
= new DeleteMethod(mUri
.toString() + remoteFilePath
);
158 executeMethod(delete
);
159 } catch (IOException e
) {
160 Log
.e(TAG
, "Logging failed with error: " + e
.getMessage(), e
);
166 public void setDataTransferProgressListener(OnDatatransferProgressListener listener
) {
167 mDataTransferListener
= listener
;
170 public boolean putFile(String localFile
, String remoteTarget
,
171 String contentType
) {
172 boolean result
= true
;
175 Log
.e("ASD", contentType
+ "");
176 File f
= new File(localFile
);
177 FileRequestEntity entity
= new FileRequestEntity(f
, contentType
);
178 entity
.setOnDatatransferProgressListener(mDataTransferListener
);
179 Log
.e("ASD", f
.exists() + " " + entity
.getContentLength());
180 PutMethod put
= new PutMethod(mUri
.toString() + remoteTarget
);
181 put
.setRequestEntity(entity
);
182 Log
.d(TAG
, "" + put
.getURI().toString());
183 int status
= executeMethod(put
);
184 Log
.d(TAG
, "PUT method return with status " + status
);
186 Log
.i(TAG
, "Uploading, done");
187 } catch (final Exception e
) {
188 Log
.i(TAG
, "" + e
.getMessage());
195 public int tryToLogin() {
197 HeadMethod head
= new HeadMethod(mUri
.toString());
199 r
= executeMethod(head
);
200 } catch (Exception e
) {
201 Log
.e(TAG
, "Error: " + e
.getMessage());
206 public boolean createDirectory(String path
) {
208 MkColMethod mkcol
= new MkColMethod(mUri
.toString() + path
);
209 int status
= executeMethod(mkcol
);
210 Log
.d(TAG
, "Status returned " + status
);
211 Log
.d(TAG
, "uri: " + mkcol
.getURI().toString());
212 Log
.i(TAG
, "Creating dir completed");
213 } catch (final Exception e
) {