1 package eu
.alefzero
.owncloud
;
5 import org
.apache
.http
.HttpHost
;
6 import org
.apache
.http
.HttpVersion
;
7 import org
.apache
.http
.auth
.AuthScope
;
8 import org
.apache
.http
.auth
.UsernamePasswordCredentials
;
9 import org
.apache
.http
.client
.methods
.HttpPut
;
10 import org
.apache
.http
.conn
.ClientConnectionManager
;
11 import org
.apache
.http
.conn
.params
.ConnManagerPNames
;
12 import org
.apache
.http
.conn
.params
.ConnPerRouteBean
;
13 import org
.apache
.http
.conn
.scheme
.PlainSocketFactory
;
14 import org
.apache
.http
.conn
.scheme
.Scheme
;
15 import org
.apache
.http
.conn
.scheme
.SchemeRegistry
;
16 import org
.apache
.http
.conn
.ssl
.SSLSocketFactory
;
17 import org
.apache
.http
.entity
.FileEntity
;
18 import org
.apache
.http
.entity
.mime
.content
.FileBody
;
19 import org
.apache
.http
.impl
.auth
.BasicScheme
;
20 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
21 import org
.apache
.http
.impl
.conn
.tsccm
.ThreadSafeClientConnManager
;
22 import org
.apache
.http
.params
.BasicHttpParams
;
23 import org
.apache
.http
.params
.HttpParams
;
24 import org
.apache
.http
.params
.HttpProtocolParams
;
25 import org
.apache
.http
.protocol
.BasicHttpContext
;
27 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
;
28 import eu
.alefzero
.webdav
.HttpMkCol
;
30 import android
.net
.Uri
;
31 import android
.util
.Log
;
33 public class WebdavClient
{
34 private DefaultHttpClient mHttpClient
;
35 private BasicHttpContext mHttpContext
;
36 private HttpHost mTargetHost
;
37 private SchemeRegistry mSchemeRegistry
;
39 final private static String TAG
= "WebdavClient";
41 WebdavClient(Uri uri
) {
43 mSchemeRegistry
= new SchemeRegistry();
47 void setCredentials(String username
, String password
) {
48 // determine default port for http or https
49 int targetPort
= mTargetHost
.getPort() == -1 ?
50 ( mUri
.getScheme().equals("https") ?
443 : 80)
53 mHttpClient
.getCredentialsProvider().setCredentials(
54 new AuthScope(mUri
.getHost(), targetPort
),
55 new UsernamePasswordCredentials(username
, password
));
56 BasicScheme basicAuth
= new BasicScheme();
57 mHttpContext
.setAttribute("preemptive-auth", basicAuth
);
60 void allowUnsignedCertificates() {
62 mSchemeRegistry
.register(new Scheme("https", new EasySSLSocketFactory(), 443));
65 boolean downloadFile(String filepath
) {
69 void getFileList(String dirPath
) {
73 boolean putFile(String localFile
,
76 boolean result
= true
;
77 HttpPut method
= new HttpPut(mUri
.toString() + remoteTarget
.replace(" ", "%20"));
78 method
.setHeader("Content-type", contentType
);
79 method
.setHeader("Host", mUri
.getHost());
80 method
.setHeader("User-Agent", "Android-ownCloud");
83 FileBody fb
= new FileBody(new File(localFile
, contentType
));
84 final FileEntity fileEntity
= new FileEntity(new File(localFile
), contentType
);
86 method
.setEntity(fileEntity
);
87 Log
.i(TAG
, "executing:" + method
.getRequestLine().toString());
89 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
90 /*mHandler.post(new Runnable() {
92 Uploader.this.PartialupdateUpload(c.getString(c.getColumnIndex(Media.DATA)),
93 c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
94 mUploadPath + (mUploadPath.equals("/")?"":"/"),
95 fileEntity.getContentType().getValue(),
96 fileEntity.getContentLength()+"");
99 Log.i(TAG, "Uploading, done");
101 Log
.i(TAG
, "Uploading, done");
102 } catch (final Exception e
) {
103 Log
.i(TAG
, e
.getLocalizedMessage());
110 public boolean createDirectory(String path
) {
111 HttpMkCol method
= new HttpMkCol(mUri
.toString() + path
+ "/");
112 method
.setHeader("User-Agent", "Android-ownCloud");
115 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
116 Log
.i(TAG
, "Creating dir completed");
117 } catch (final Exception e
) {
124 private void setupHttpClient() {
126 mSchemeRegistry
.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
127 mSchemeRegistry
.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
129 HttpParams params
= new BasicHttpParams();
130 params
.setParameter(ConnManagerPNames
.MAX_TOTAL_CONNECTIONS
, 30);
131 params
.setParameter(ConnManagerPNames
.MAX_CONNECTIONS_PER_ROUTE
, new ConnPerRouteBean(30));
132 params
.setParameter(HttpProtocolParams
.USE_EXPECT_CONTINUE
, false
);
133 HttpProtocolParams
.setVersion(params
, HttpVersion
.HTTP_1_1
);
135 mHttpContext
= new BasicHttpContext();
136 ClientConnectionManager cm
= new ThreadSafeClientConnManager(params
, mSchemeRegistry
);
138 int port
= mUri
.getPort() == -1 ?
139 mUri
.getScheme().equals("https") ?
443 : 80
142 mTargetHost
= new HttpHost(mUri
.getHost(), port
, mUri
.getScheme());
144 mHttpClient
= new DefaultHttpClient(cm
, params
);