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
.net
.URLDecoder
; 
  25 import java
.net
.URLEncoder
; 
  27 import org
.apache
.commons
.httpclient
.Credentials
; 
  28 import org
.apache
.commons
.httpclient
.HttpClient
; 
  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
.methods
.RequestEntity
; 
  35 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
; 
  36 import org
.apache
.commons
.httpclient
.protocol
.Protocol
; 
  37 import org
.apache
.http
.HttpStatus
; 
  38 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MkColMethod
; 
  40 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
; 
  42 import android
.net
.Uri
; 
  43 import android
.util
.Log
; 
  45 public class WebdavClient 
extends HttpClient 
{ 
  47     private Credentials mCredentials
; 
  48     final private static String TAG 
= "WebdavClient"; 
  49     private static final String USER_AGENT 
= "Android-ownCloud"; 
  50     private OnUploadProgressListener mUploadProgressListener
; 
  52     public WebdavClient(Uri uri
) { 
  54         getParams().setParameter(HttpMethodParams
.USER_AGENT
, USER_AGENT
); 
  57     public void setCredentials(String username
, String password
) { 
  58         getParams().setAuthenticationPreemptive(true
); 
  59         getState().setCredentials(AuthScope
.ANY
, 
  60                 getCredentials(username
, password
)); 
  63     private Credentials 
getCredentials(String username
, String password
) { 
  64         if (mCredentials 
== null
) 
  65             mCredentials 
= new UsernamePasswordCredentials(username
, password
); 
  69     public void allowUnsignedCertificates() { 
  71         Protocol
.registerProtocol("https", new Protocol("https", 
  72                 new EasySSLSocketFactory(), 443)); 
  75     public boolean downloadFile(String filepath
, File targetPath
) { 
  76         // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", 
  79         Log
.e("ASD", mUri
.toString() + filepath
.replace(" ", "%20") + ""); 
  80         GetMethod get 
= new GetMethod(mUri
.toString() 
  81                 + filepath
.replace(" ", "%20")); 
  83         // get.setHeader("Host", mUri.getHost()); 
  84         // get.setHeader("User-Agent", "Android-ownCloud"); 
  87             int status 
= executeMethod(get
); 
  88             Log
.e(TAG
, "status return: " + status
); 
  89             if (status 
!= HttpStatus
.SC_OK
) { 
  92             BufferedInputStream bis 
= new BufferedInputStream( 
  93                     get
.getResponseBodyAsStream()); 
  94             FileOutputStream fos 
= new FileOutputStream(targetPath
); 
  96             byte[] bytes 
= new byte[512]; 
  98             while ((readResult 
= bis
.read(bytes
)) != -1) 
  99                 fos
.write(bytes
, 0, readResult
); 
 101         } catch (IOException e
) { 
 108     public void setUploadListener(OnUploadProgressListener listener
) { 
 109         mUploadProgressListener 
= listener
; 
 112     public boolean putFile(String localFile
, String remoteTarget
, 
 113             String contentType
) { 
 114         boolean result 
= true
; 
 117             Log
.e("ASD", contentType 
+ ""); 
 118             File f 
= new File(localFile
); 
 119             FileRequestEntity entity 
= new FileRequestEntity(f
, contentType
); 
 120             entity
.setOnUploadProgressListener(mUploadProgressListener
); 
 121             Log
.e("ASD", f
.exists() + " " + entity
.getContentLength()); 
 122             PutMethod put 
= new PutMethod(mUri
.toString() + remoteTarget
); 
 123             put
.setRequestEntity(entity
); 
 124             Log
.d(TAG
, "" + put
.getURI().toString()); 
 125             int status 
= executeMethod(put
); 
 126             Log
.d(TAG
, "PUT method return with status " + status
); 
 128             Log
.i(TAG
, "Uploading, done"); 
 129         } catch (final Exception e
) { 
 130             Log
.i(TAG
, "" + e
.getMessage()); 
 137     public int tryToLogin() { 
 139         HeadMethod head 
= new HeadMethod(mUri
.toString()); 
 141             r 
= executeMethod(head
); 
 142         } catch (Exception e
) { 
 143             Log
.e(TAG
, "Error: " + e
.getMessage()); 
 148     public boolean createDirectory(String path
) { 
 150             MkColMethod mkcol 
= new MkColMethod(mUri
.toString() + "/" + path
 
 152             int status 
= executeMethod(mkcol
); 
 153             Log
.d(TAG
, "Status returned " + status
); 
 154             Log
.d(TAG
, "uri: " + mkcol
.getURI().toString()); 
 155             Log
.i(TAG
, "Creating dir completed"); 
 156         } catch (final Exception e
) {