f03b7a97d1d93e92ae0ec349b31d009f968b6ffb
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavClient.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package eu.alefzero.webdav;
19
20 import java.io.BufferedInputStream;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24
25 import org.apache.commons.httpclient.Credentials;
26 import org.apache.commons.httpclient.HttpClient;
27 import org.apache.commons.httpclient.UsernamePasswordCredentials;
28 import org.apache.commons.httpclient.auth.AuthScope;
29 import org.apache.http.HttpHost;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.HttpVersion;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.methods.HttpPut;
35 import org.apache.http.conn.ClientConnectionManager;
36 import org.apache.http.conn.params.ConnManagerPNames;
37 import org.apache.http.conn.params.ConnPerRouteBean;
38 import org.apache.http.conn.scheme.PlainSocketFactory;
39 import org.apache.http.conn.scheme.Scheme;
40 import org.apache.http.conn.scheme.SchemeRegistry;
41 import org.apache.http.conn.ssl.SSLSocketFactory;
42 import org.apache.http.entity.FileEntity;
43 import org.apache.http.impl.auth.BasicScheme;
44 import org.apache.http.impl.client.DefaultHttpClient;
45 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
46 import org.apache.http.params.BasicHttpParams;
47 import org.apache.http.params.HttpParams;
48 import org.apache.http.params.HttpProtocolParams;
49 import org.apache.http.protocol.BasicHttpContext;
50
51 import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;
52 import eu.alefzero.webdav.HttpMkCol;
53
54 import android.net.Uri;
55 import android.util.Log;
56
57 public class WebdavClient extends HttpClient {
58 private DefaultHttpClient mHttpClient;
59 private BasicHttpContext mHttpContext;
60 private HttpHost mTargetHost;
61 private SchemeRegistry mSchemeRegistry;
62 private Uri mUri;
63 private Credentials mCredentials;
64 final private static String TAG = "WebdavClient";
65
66 public DefaultHttpClient getHttpClient() {
67 return mHttpClient;
68 }
69 public HttpHost getTargetHost() {
70 return mTargetHost;
71 }
72
73 public WebdavClient(Uri uri) {
74 mUri = uri;
75 mSchemeRegistry = new SchemeRegistry();
76 setupHttpClient();
77 }
78
79 public void setCredentials(String username, String password) {
80 // determine default port for http or https
81 int targetPort = mTargetHost.getPort() == -1 ?
82 ( mUri.getScheme().equals("https") ? 443 : 80)
83 : mUri.getPort();
84
85 getParams().setAuthenticationPreemptive(true);
86 getState().setCredentials(AuthScope.ANY, getCredentials(username, password));
87 }
88
89 private Credentials getCredentials(String username, String password) {
90 if (mCredentials == null)
91 mCredentials = new UsernamePasswordCredentials(username, password);
92 return mCredentials;
93 }
94 public void allowUnsignedCertificates() {
95 // https
96 mSchemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
97 }
98
99 public boolean downloadFile(String filepath, File targetPath) {
100 HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20"));
101 get.setHeader("Host", mUri.getHost());
102 get.setHeader("User-Agent", "Android-ownCloud");
103
104 try {
105 HttpResponse response = mHttpClient.execute(mTargetHost, get, mHttpContext);
106 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
107 return false;
108 }
109 BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
110 FileOutputStream fos = new FileOutputStream(targetPath);
111
112 byte[] bytes = new byte[512];
113 int readResult;
114 while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult);
115
116 } catch (IOException e) {
117 e.printStackTrace();
118 return false;
119 }
120 return true;
121 }
122
123 public boolean putFile(String localFile,
124 String remoteTarget,
125 String contentType) {
126 boolean result = true;
127 HttpPut method = new HttpPut(mUri.toString() + remoteTarget.replace(" ", "%20"));
128 method.setHeader("Content-type", contentType);
129 method.setHeader("Host", mUri.getHost());
130 method.setHeader("User-Agent", "Android-ownCloud");
131
132 try {
133 final FileEntity fileEntity = new FileEntity(new File(localFile), contentType);
134
135 method.setEntity(fileEntity);
136 Log.i(TAG, "executing:" + method.getRequestLine().toString());
137
138 mHttpClient.execute(mTargetHost, method, mHttpContext);
139 /*mHandler.post(new Runnable() {
140 public void run() {
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()+"");
146 }
147 });
148 Log.i(TAG, "Uploading, done");
149 */
150 Log.i(TAG, "Uploading, done");
151 } catch (final Exception e) {
152 Log.i(TAG, ""+e.getMessage());
153 result = false;
154 }
155
156 return result;
157 }
158
159 public boolean createDirectory(String path) {
160 HttpMkCol method = new HttpMkCol(mUri.toString() + path + "/");
161 method.setHeader("User-Agent", "Android-ownCloud");
162
163 try {
164 mHttpClient.execute(mTargetHost, method, mHttpContext);
165 Log.i(TAG, "Creating dir completed");
166 } catch (final Exception e) {
167 e.printStackTrace();
168 return false;
169 }
170 return true;
171 }
172
173 private void setupHttpClient() {
174 // http scheme
175 mSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
176 mSchemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
177
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);
183
184 mHttpContext = new BasicHttpContext();
185 ClientConnectionManager cm = new ThreadSafeClientConnManager(params, mSchemeRegistry);
186
187 int port = mUri.getPort() == -1 ?
188 mUri.getScheme().equals("https") ? 443 : 80
189 : mUri.getPort();
190
191 mTargetHost = new HttpHost(mUri.getHost(), port, mUri.getScheme());
192
193 mHttpClient = new DefaultHttpClient(cm, params);
194 }
195 }