1 package eu
.alefzero
.owncloud
;
3 import java
.io
.BufferedInputStream
;
4 import java
.io
.BufferedOutputStream
;
6 import java
.io
.FileOutputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStreamReader
;
9 import java
.io
.OutputStreamWriter
;
11 import org
.apache
.http
.HttpHost
;
12 import org
.apache
.http
.HttpResponse
;
13 import org
.apache
.http
.HttpStatus
;
14 import org
.apache
.http
.HttpVersion
;
15 import org
.apache
.http
.auth
.AuthScope
;
16 import org
.apache
.http
.auth
.UsernamePasswordCredentials
;
17 import org
.apache
.http
.client
.methods
.HttpGet
;
18 import org
.apache
.http
.client
.methods
.HttpPut
;
19 import org
.apache
.http
.conn
.ClientConnectionManager
;
20 import org
.apache
.http
.conn
.params
.ConnManagerPNames
;
21 import org
.apache
.http
.conn
.params
.ConnPerRouteBean
;
22 import org
.apache
.http
.conn
.scheme
.PlainSocketFactory
;
23 import org
.apache
.http
.conn
.scheme
.Scheme
;
24 import org
.apache
.http
.conn
.scheme
.SchemeRegistry
;
25 import org
.apache
.http
.conn
.ssl
.SSLSocketFactory
;
26 import org
.apache
.http
.entity
.FileEntity
;
27 import org
.apache
.http
.entity
.mime
.content
.FileBody
;
28 import org
.apache
.http
.impl
.auth
.BasicScheme
;
29 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
30 import org
.apache
.http
.impl
.conn
.tsccm
.ThreadSafeClientConnManager
;
31 import org
.apache
.http
.params
.BasicHttpParams
;
32 import org
.apache
.http
.params
.HttpParams
;
33 import org
.apache
.http
.params
.HttpProtocolParams
;
34 import org
.apache
.http
.protocol
.BasicHttpContext
;
36 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
;
37 import eu
.alefzero
.webdav
.HttpMkCol
;
39 import android
.net
.Uri
;
40 import android
.util
.Log
;
42 public class WebdavClient
{
43 private DefaultHttpClient mHttpClient
;
44 private BasicHttpContext mHttpContext
;
45 private HttpHost mTargetHost
;
46 private SchemeRegistry mSchemeRegistry
;
48 final private static String TAG
= "WebdavClient";
50 WebdavClient(Uri uri
) {
52 mSchemeRegistry
= new SchemeRegistry();
56 void setCredentials(String username
, String password
) {
57 // determine default port for http or https
58 int targetPort
= mTargetHost
.getPort() == -1 ?
59 ( mUri
.getScheme().equals("https") ?
443 : 80)
62 mHttpClient
.getCredentialsProvider().setCredentials(
63 new AuthScope(mUri
.getHost(), targetPort
),
64 new UsernamePasswordCredentials(username
, password
));
65 BasicScheme basicAuth
= new BasicScheme();
66 mHttpContext
.setAttribute("preemptive-auth", basicAuth
);
69 void allowUnsignedCertificates() {
71 mSchemeRegistry
.register(new Scheme("https", new EasySSLSocketFactory(), 443));
74 boolean downloadFile(String filepath
, File targetPath
) {
75 HttpGet get
= new HttpGet(mUri
.toString() + filepath
.replace(" ", "%20"));
76 get
.setHeader("Host", mUri
.getHost());
77 get
.setHeader("User-Agent", "Android-ownCloud");
80 HttpResponse response
= mHttpClient
.execute(mTargetHost
, get
, mHttpContext
);
81 if (response
.getStatusLine().getStatusCode() != HttpStatus
.SC_OK
) {
84 BufferedInputStream bis
= new BufferedInputStream(response
.getEntity().getContent());
85 FileOutputStream fos
= new FileOutputStream(targetPath
);
87 byte[] bytes
= new byte[512];
89 while ((readResult
= bis
.read(bytes
)) != -1) fos
.write(bytes
, 0, readResult
);
91 } catch (IOException e
) {
98 void getFileList(String dirPath
) {
102 boolean putFile(String localFile
,
104 String contentType
) {
105 boolean result
= true
;
106 HttpPut method
= new HttpPut(mUri
.toString() + remoteTarget
.replace(" ", "%20"));
107 method
.setHeader("Content-type", contentType
);
108 method
.setHeader("Host", mUri
.getHost());
109 method
.setHeader("User-Agent", "Android-ownCloud");
112 FileBody fb
= new FileBody(new File(localFile
, contentType
));
113 final FileEntity fileEntity
= new FileEntity(new File(localFile
), contentType
);
115 method
.setEntity(fileEntity
);
116 Log
.i(TAG
, "executing:" + method
.getRequestLine().toString());
118 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
119 /*mHandler.post(new Runnable() {
121 Uploader.this.PartialupdateUpload(c.getString(c.getColumnIndex(Media.DATA)),
122 c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
123 mUploadPath + (mUploadPath.equals("/")?"":"/"),
124 fileEntity.getContentType().getValue(),
125 fileEntity.getContentLength()+"");
128 Log.i(TAG, "Uploading, done");
130 Log
.i(TAG
, "Uploading, done");
131 } catch (final Exception e
) {
132 Log
.i(TAG
, ""+e
.getMessage());
139 public boolean createDirectory(String path
) {
140 HttpMkCol method
= new HttpMkCol(mUri
.toString() + path
+ "/");
141 method
.setHeader("User-Agent", "Android-ownCloud");
144 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
145 Log
.i(TAG
, "Creating dir completed");
146 } catch (final Exception e
) {
153 private void setupHttpClient() {
155 mSchemeRegistry
.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
156 mSchemeRegistry
.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
158 HttpParams params
= new BasicHttpParams();
159 params
.setParameter(ConnManagerPNames
.MAX_TOTAL_CONNECTIONS
, 30);
160 params
.setParameter(ConnManagerPNames
.MAX_CONNECTIONS_PER_ROUTE
, new ConnPerRouteBean(30));
161 params
.setParameter(HttpProtocolParams
.USE_EXPECT_CONTINUE
, false
);
162 HttpProtocolParams
.setVersion(params
, HttpVersion
.HTTP_1_1
);
164 mHttpContext
= new BasicHttpContext();
165 ClientConnectionManager cm
= new ThreadSafeClientConnManager(params
, mSchemeRegistry
);
167 int port
= mUri
.getPort() == -1 ?
168 mUri
.getScheme().equals("https") ?
443 : 80
171 mTargetHost
= new HttpHost(mUri
.getHost(), port
, mUri
.getScheme());
173 mHttpClient
= new DefaultHttpClient(cm
, params
);