WebdavClient.java 7.1 KB

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