1 package eu
.alefzero
.owncloud
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStreamReader
;
8 import org
.apache
.http
.HttpHost
;
9 import org
.apache
.http
.HttpResponse
;
10 import org
.apache
.http
.HttpStatus
;
11 import org
.apache
.http
.HttpVersion
;
12 import org
.apache
.http
.auth
.AuthScope
;
13 import org
.apache
.http
.auth
.UsernamePasswordCredentials
;
14 import org
.apache
.http
.client
.methods
.HttpGet
;
15 import org
.apache
.http
.client
.methods
.HttpPut
;
16 import org
.apache
.http
.conn
.ClientConnectionManager
;
17 import org
.apache
.http
.conn
.params
.ConnManagerPNames
;
18 import org
.apache
.http
.conn
.params
.ConnPerRouteBean
;
19 import org
.apache
.http
.conn
.scheme
.PlainSocketFactory
;
20 import org
.apache
.http
.conn
.scheme
.Scheme
;
21 import org
.apache
.http
.conn
.scheme
.SchemeRegistry
;
22 import org
.apache
.http
.conn
.ssl
.SSLSocketFactory
;
23 import org
.apache
.http
.entity
.FileEntity
;
24 import org
.apache
.http
.entity
.mime
.content
.FileBody
;
25 import org
.apache
.http
.impl
.auth
.BasicScheme
;
26 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
27 import org
.apache
.http
.impl
.conn
.tsccm
.ThreadSafeClientConnManager
;
28 import org
.apache
.http
.params
.BasicHttpParams
;
29 import org
.apache
.http
.params
.HttpParams
;
30 import org
.apache
.http
.params
.HttpProtocolParams
;
31 import org
.apache
.http
.protocol
.BasicHttpContext
;
33 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
;
34 import eu
.alefzero
.webdav
.HttpMkCol
;
36 import android
.net
.Uri
;
37 import android
.util
.Log
;
39 public class WebdavClient
{
40 private DefaultHttpClient mHttpClient
;
41 private BasicHttpContext mHttpContext
;
42 private HttpHost mTargetHost
;
43 private SchemeRegistry mSchemeRegistry
;
45 final private static String TAG
= "WebdavClient";
47 WebdavClient(Uri uri
) {
49 mSchemeRegistry
= new SchemeRegistry();
53 void setCredentials(String username
, String password
) {
54 // determine default port for http or https
55 int targetPort
= mTargetHost
.getPort() == -1 ?
56 ( mUri
.getScheme().equals("https") ?
443 : 80)
59 mHttpClient
.getCredentialsProvider().setCredentials(
60 new AuthScope(mUri
.getHost(), targetPort
),
61 new UsernamePasswordCredentials(username
, password
));
62 BasicScheme basicAuth
= new BasicScheme();
63 mHttpContext
.setAttribute("preemptive-auth", basicAuth
);
66 void allowUnsignedCertificates() {
68 mSchemeRegistry
.register(new Scheme("https", new EasySSLSocketFactory(), 443));
71 boolean downloadFile(String filepath
, File targetPath
) {
72 HttpGet get
= new HttpGet(mUri
.toString() + filepath
.replace(" ", "%20"));
73 get
.setHeader("Host", mUri
.getHost());
74 get
.setHeader("User-Agent", "Android-ownCloud");
77 HttpResponse response
= mHttpClient
.execute(mTargetHost
, get
, mHttpContext
);
78 if (response
.getStatusLine().getStatusCode() != HttpStatus
.SC_OK
) {
81 InputStreamReader isr
= new InputStreamReader(response
.getEntity().getContent());
82 FileOutputStream fos
= new FileOutputStream(targetPath
);
84 while ((oneByte
= isr
.read()) != -1) fos
.write(oneByte
);
86 } catch (IOException e
) {
93 void getFileList(String dirPath
) {
97 boolean putFile(String localFile
,
100 boolean result
= true
;
101 HttpPut method
= new HttpPut(mUri
.toString() + remoteTarget
.replace(" ", "%20"));
102 method
.setHeader("Content-type", contentType
);
103 method
.setHeader("Host", mUri
.getHost());
104 method
.setHeader("User-Agent", "Android-ownCloud");
107 FileBody fb
= new FileBody(new File(localFile
, contentType
));
108 final FileEntity fileEntity
= new FileEntity(new File(localFile
), contentType
);
110 method
.setEntity(fileEntity
);
111 Log
.i(TAG
, "executing:" + method
.getRequestLine().toString());
113 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
114 /*mHandler.post(new Runnable() {
116 Uploader.this.PartialupdateUpload(c.getString(c.getColumnIndex(Media.DATA)),
117 c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
118 mUploadPath + (mUploadPath.equals("/")?"":"/"),
119 fileEntity.getContentType().getValue(),
120 fileEntity.getContentLength()+"");
123 Log.i(TAG, "Uploading, done");
125 Log
.i(TAG
, "Uploading, done");
126 } catch (final Exception e
) {
127 Log
.i(TAG
, e
.getLocalizedMessage());
134 public boolean createDirectory(String path
) {
135 HttpMkCol method
= new HttpMkCol(mUri
.toString() + path
+ "/");
136 method
.setHeader("User-Agent", "Android-ownCloud");
139 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
140 Log
.i(TAG
, "Creating dir completed");
141 } catch (final Exception e
) {
148 private void setupHttpClient() {
150 mSchemeRegistry
.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
151 mSchemeRegistry
.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
153 HttpParams params
= new BasicHttpParams();
154 params
.setParameter(ConnManagerPNames
.MAX_TOTAL_CONNECTIONS
, 30);
155 params
.setParameter(ConnManagerPNames
.MAX_CONNECTIONS_PER_ROUTE
, new ConnPerRouteBean(30));
156 params
.setParameter(HttpProtocolParams
.USE_EXPECT_CONTINUE
, false
);
157 HttpProtocolParams
.setVersion(params
, HttpVersion
.HTTP_1_1
);
159 mHttpContext
= new BasicHttpContext();
160 ClientConnectionManager cm
= new ThreadSafeClientConnManager(params
, mSchemeRegistry
);
162 int port
= mUri
.getPort() == -1 ?
163 mUri
.getScheme().equals("https") ?
443 : 80
166 mTargetHost
= new HttpHost(mUri
.getHost(), port
, mUri
.getScheme());
168 mHttpClient
= new DefaultHttpClient(cm
, params
);