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
;
21 import java
.io
.BufferedOutputStream
;
23 import java
.io
.FileOutputStream
;
24 import java
.io
.IOException
;
25 import java
.io
.InputStreamReader
;
26 import java
.io
.OutputStreamWriter
;
28 import org
.apache
.http
.HttpHost
;
29 import org
.apache
.http
.HttpResponse
;
30 import org
.apache
.http
.HttpStatus
;
31 import org
.apache
.http
.HttpVersion
;
32 import org
.apache
.http
.auth
.AuthScope
;
33 import org
.apache
.http
.auth
.UsernamePasswordCredentials
;
34 import org
.apache
.http
.client
.methods
.HttpGet
;
35 import org
.apache
.http
.client
.methods
.HttpPut
;
36 import org
.apache
.http
.conn
.ClientConnectionManager
;
37 import org
.apache
.http
.conn
.params
.ConnManagerPNames
;
38 import org
.apache
.http
.conn
.params
.ConnPerRouteBean
;
39 import org
.apache
.http
.conn
.scheme
.PlainSocketFactory
;
40 import org
.apache
.http
.conn
.scheme
.Scheme
;
41 import org
.apache
.http
.conn
.scheme
.SchemeRegistry
;
42 import org
.apache
.http
.conn
.ssl
.SSLSocketFactory
;
43 import org
.apache
.http
.entity
.FileEntity
;
44 import org
.apache
.http
.entity
.mime
.content
.FileBody
;
45 import org
.apache
.http
.impl
.auth
.BasicScheme
;
46 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
47 import org
.apache
.http
.impl
.conn
.tsccm
.ThreadSafeClientConnManager
;
48 import org
.apache
.http
.params
.BasicHttpParams
;
49 import org
.apache
.http
.params
.HttpParams
;
50 import org
.apache
.http
.params
.HttpProtocolParams
;
51 import org
.apache
.http
.protocol
.BasicHttpContext
;
53 import eu
.alefzero
.owncloud
.authenticator
.EasySSLSocketFactory
;
54 import eu
.alefzero
.webdav
.HttpMkCol
;
56 import android
.net
.Uri
;
57 import android
.util
.Log
;
59 public class WebdavClient
{
60 private DefaultHttpClient mHttpClient
;
61 private BasicHttpContext mHttpContext
;
62 private HttpHost mTargetHost
;
63 private SchemeRegistry mSchemeRegistry
;
65 final private static String TAG
= "WebdavClient";
67 public DefaultHttpClient
getHttpClient() {
70 public HttpHost
getTargetHost() {
74 public WebdavClient(Uri uri
) {
76 mSchemeRegistry
= new SchemeRegistry();
80 public void setCredentials(String username
, String password
) {
81 // determine default port for http or https
82 int targetPort
= mTargetHost
.getPort() == -1 ?
83 ( mUri
.getScheme().equals("https") ?
443 : 80)
86 mHttpClient
.getCredentialsProvider().setCredentials(
87 new AuthScope(mUri
.getHost(), targetPort
),
88 new UsernamePasswordCredentials(username
, password
));
89 BasicScheme basicAuth
= new BasicScheme();
90 mHttpContext
.setAttribute("preemptive-auth", basicAuth
);
93 public void allowUnsignedCertificates() {
95 mSchemeRegistry
.register(new Scheme("https", new EasySSLSocketFactory(), 443));
98 public boolean downloadFile(String filepath
, File targetPath
) {
99 HttpGet get
= new HttpGet(mUri
.toString() + filepath
.replace(" ", "%20"));
100 get
.setHeader("Host", mUri
.getHost());
101 get
.setHeader("User-Agent", "Android-ownCloud");
104 HttpResponse response
= mHttpClient
.execute(mTargetHost
, get
, mHttpContext
);
105 if (response
.getStatusLine().getStatusCode() != HttpStatus
.SC_OK
) {
108 BufferedInputStream bis
= new BufferedInputStream(response
.getEntity().getContent());
109 FileOutputStream fos
= new FileOutputStream(targetPath
);
111 byte[] bytes
= new byte[512];
113 while ((readResult
= bis
.read(bytes
)) != -1) fos
.write(bytes
, 0, readResult
);
115 } catch (IOException e
) {
122 public boolean putFile(String localFile
,
124 String contentType
) {
125 boolean result
= true
;
126 HttpPut method
= new HttpPut(mUri
.toString() + remoteTarget
.replace(" ", "%20"));
127 method
.setHeader("Content-type", contentType
);
128 method
.setHeader("Host", mUri
.getHost());
129 method
.setHeader("User-Agent", "Android-ownCloud");
132 FileBody fb
= new FileBody(new File(localFile
, contentType
));
133 final FileEntity fileEntity
= new FileEntity(new File(localFile
), contentType
);
135 method
.setEntity(fileEntity
);
136 Log
.i(TAG
, "executing:" + method
.getRequestLine().toString());
138 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
139 /*mHandler.post(new Runnable() {
141 Uploader.this.PartialupdateUpload(c.getString(c.getColumnIndex(Media.DATA)),
142 c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
143 mUploadPath + (mUploadPath.equals("/")?"":"/"),
144 fileEntity.getContentType().getValue(),
145 fileEntity.getContentLength()+"");
148 Log.i(TAG, "Uploading, done");
150 Log
.i(TAG
, "Uploading, done");
151 } catch (final Exception e
) {
152 Log
.i(TAG
, ""+e
.getMessage());
159 public boolean createDirectory(String path
) {
160 HttpMkCol method
= new HttpMkCol(mUri
.toString() + path
+ "/");
161 method
.setHeader("User-Agent", "Android-ownCloud");
164 mHttpClient
.execute(mTargetHost
, method
, mHttpContext
);
165 Log
.i(TAG
, "Creating dir completed");
166 } catch (final Exception e
) {
173 private void setupHttpClient() {
175 mSchemeRegistry
.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
176 mSchemeRegistry
.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
178 HttpParams params
= new BasicHttpParams();
179 params
.setParameter(ConnManagerPNames
.MAX_TOTAL_CONNECTIONS
, 30);
180 params
.setParameter(ConnManagerPNames
.MAX_CONNECTIONS_PER_ROUTE
, new ConnPerRouteBean(30));
181 params
.setParameter(HttpProtocolParams
.USE_EXPECT_CONTINUE
, false
);
182 HttpProtocolParams
.setVersion(params
, HttpVersion
.HTTP_1_1
);
184 mHttpContext
= new BasicHttpContext();
185 ClientConnectionManager cm
= new ThreadSafeClientConnManager(params
, mSchemeRegistry
);
187 int port
= mUri
.getPort() == -1 ?
188 mUri
.getScheme().equals("https") ?
443 : 80
191 mTargetHost
= new HttpHost(mUri
.getHost(), port
, mUri
.getScheme());
193 mHttpClient
= new DefaultHttpClient(cm
, params
);