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
.http
.HttpHost
; 
  26 import org
.apache
.http
.HttpResponse
; 
  27 import org
.apache
.http
.HttpStatus
; 
  28 import org
.apache
.http
.HttpVersion
; 
  29 import org
.apache
.http
.auth
.AuthScope
; 
  30 import org
.apache
.http
.auth
.UsernamePasswordCredentials
; 
  31 import org
.apache
.http
.client
.methods
.HttpGet
; 
  32 import org
.apache
.http
.client
.methods
.HttpPut
; 
  33 import org
.apache
.http
.conn
.ClientConnectionManager
; 
  34 import org
.apache
.http
.conn
.params
.ConnManagerPNames
; 
  35 import org
.apache
.http
.conn
.params
.ConnPerRouteBean
; 
  36 import org
.apache
.http
.conn
.scheme
.PlainSocketFactory
; 
  37 import org
.apache
.http
.conn
.scheme
.Scheme
; 
  38 import org
.apache
.http
.conn
.scheme
.SchemeRegistry
; 
  39 import org
.apache
.http
.conn
.ssl
.SSLSocketFactory
; 
  40 import org
.apache
.http
.entity
.FileEntity
; 
  41 import org
.apache
.http
.entity
.mime
.content
.FileBody
; 
  42 import org
.apache
.http
.impl
.auth
.BasicScheme
; 
  43 import org
.apache
.http
.impl
.client
.DefaultHttpClient
; 
  44 import org
.apache
.http
.impl
.conn
.tsccm
.ThreadSafeClientConnManager
; 
  45 import org
.apache
.http
.params
.BasicHttpParams
; 
  46 import org
.apache
.http
.params
.HttpParams
; 
  47 import org
.apache
.http
.params
.HttpProtocolParams
; 
  48 import org
.apache
.http
.protocol
.BasicHttpContext
; 
  50 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
; 
  51 import eu
.alefzero
.webdav
.HttpMkCol
; 
  53 import android
.net
.Uri
; 
  54 import android
.util
.Log
; 
  56 public class WebdavClient 
{ 
  57   private DefaultHttpClient mHttpClient
; 
  58   private BasicHttpContext mHttpContext
; 
  59   private HttpHost mTargetHost
; 
  60   private SchemeRegistry mSchemeRegistry
; 
  62   final private static String TAG 
= "WebdavClient"; 
  64   public DefaultHttpClient 
getHttpClient() { 
  67   public HttpHost 
getTargetHost() { 
  71   public WebdavClient(Uri uri
) { 
  73     mSchemeRegistry 
= new SchemeRegistry(); 
  77   public void setCredentials(String username
, String password
) { 
  78     // determine default port for http or https 
  79     int targetPort 
= mTargetHost
.getPort() == -1 ? 
 
  80                         ( mUri
.getScheme().equals("https") ? 
443 : 80) 
  83     mHttpClient
.getCredentialsProvider().setCredentials( 
  84         new AuthScope(mUri
.getHost(), targetPort
),  
  85         new UsernamePasswordCredentials(username
, password
)); 
  86     BasicScheme basicAuth 
= new BasicScheme(); 
  87     mHttpContext
.setAttribute("preemptive-auth", basicAuth
); 
  90   public void allowUnsignedCertificates() { 
  92     mSchemeRegistry
.register(new Scheme("https", new EasySSLSocketFactory(), 443)); 
  95   public boolean downloadFile(String filepath
, File targetPath
) { 
  96     HttpGet get 
= new HttpGet(mUri
.toString() + filepath
.replace(" ", "%20")); 
  97     get
.setHeader("Host", mUri
.getHost()); 
  98     get
.setHeader("User-Agent", "Android-ownCloud"); 
 101       HttpResponse response 
= mHttpClient
.execute(mTargetHost
, get
, mHttpContext
); 
 102       if (response
.getStatusLine().getStatusCode() != HttpStatus
.SC_OK
) { 
 105       BufferedInputStream bis 
= new BufferedInputStream(response
.getEntity().getContent()); 
 106       FileOutputStream fos 
= new FileOutputStream(targetPath
); 
 108       byte[] bytes 
= new byte[512]; 
 110       while ((readResult 
= bis
.read(bytes
)) != -1) fos
.write(bytes
, 0, readResult
); 
 112     } catch (IOException e
) { 
 119   public boolean putFile(String localFile
, 
 121                   String contentType
) { 
 122     boolean result 
= true
; 
 123     HttpPut method 
= new HttpPut(mUri
.toString() + remoteTarget
.replace(" ", "%20")); 
 124     method
.setHeader("Content-type", contentType
); 
 125     method
.setHeader("Host", mUri
.getHost()); 
 126     method
.setHeader("User-Agent", "Android-ownCloud"); 
 129       final FileEntity fileEntity 
= new FileEntity(new File(localFile
), contentType
); 
 131       method
.setEntity(fileEntity
); 
 132       Log
.i(TAG
, "executing:" + method
.getRequestLine().toString()); 
 134       mHttpClient
.execute(mTargetHost
, method
, mHttpContext
); 
 135       /*mHandler.post(new Runnable() { 
 137         Uploader.this.PartialupdateUpload(c.getString(c.getColumnIndex(Media.DATA)), 
 138                                                   c.getString(c.getColumnIndex(Media.DISPLAY_NAME)), 
 139                                                   mUploadPath + (mUploadPath.equals("/")?"":"/"), 
 140                                                   fileEntity.getContentType().getValue(), 
 141                                                   fileEntity.getContentLength()+""); 
 144     Log.i(TAG, "Uploading, done"); 
 146       Log
.i(TAG
, "Uploading, done"); 
 147     } catch (final Exception e
) { 
 148       Log
.i(TAG
, ""+e
.getMessage()); 
 155   public boolean createDirectory(String path
) { 
 156     HttpMkCol method 
= new HttpMkCol(mUri
.toString() + path 
+ "/"); 
 157     method
.setHeader("User-Agent", "Android-ownCloud"); 
 160       mHttpClient
.execute(mTargetHost
, method
, mHttpContext
); 
 161       Log
.i(TAG
, "Creating dir completed"); 
 162     } catch (final Exception e
) { 
 169   private void setupHttpClient() { 
 171     mSchemeRegistry
.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80)); 
 172     mSchemeRegistry
.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443)); 
 174     HttpParams params 
= new BasicHttpParams(); 
 175     params
.setParameter(ConnManagerPNames
.MAX_TOTAL_CONNECTIONS
, 30); 
 176     params
.setParameter(ConnManagerPNames
.MAX_CONNECTIONS_PER_ROUTE
, new ConnPerRouteBean(30)); 
 177     params
.setParameter(HttpProtocolParams
.USE_EXPECT_CONTINUE
, false
); 
 178     HttpProtocolParams
.setVersion(params
, HttpVersion
.HTTP_1_1
); 
 180     mHttpContext 
= new BasicHttpContext(); 
 181     ClientConnectionManager cm 
= new ThreadSafeClientConnManager(params
, mSchemeRegistry
); 
 183     int port 
= mUri
.getPort() == -1 ? 
 
 184                  mUri
.getScheme().equals("https") ? 
443 : 80 
 187     mTargetHost 
= new HttpHost(mUri
.getHost(), port
, mUri
.getScheme()); 
 189     mHttpClient 
= new DefaultHttpClient(cm
, params
);