WebdavClient.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 java.net.URLDecoder;
  24. import java.net.URLEncoder;
  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.commons.httpclient.methods.GetMethod;
  30. import org.apache.commons.httpclient.methods.HeadMethod;
  31. import org.apache.commons.httpclient.methods.PutMethod;
  32. import org.apache.commons.httpclient.methods.RequestEntity;
  33. import org.apache.commons.httpclient.params.HttpMethodParams;
  34. import org.apache.commons.httpclient.protocol.Protocol;
  35. import org.apache.http.HttpStatus;
  36. import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
  37. import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;
  38. import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;
  39. import android.net.Uri;
  40. import android.util.Log;
  41. public class WebdavClient extends HttpClient {
  42. private Uri mUri;
  43. private Credentials mCredentials;
  44. final private static String TAG = "WebdavClient";
  45. private static final String USER_AGENT = "Android-ownCloud";
  46. private OnUploadProgressListener mUploadProgressListener;
  47. private OnDatatransferProgressListener mDataTransferListener;
  48. public WebdavClient(Uri uri) {
  49. mUri = uri;
  50. getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
  51. }
  52. public void setCredentials(String username, String password) {
  53. getParams().setAuthenticationPreemptive(true);
  54. getState().setCredentials(AuthScope.ANY,
  55. getCredentials(username, password));
  56. }
  57. private Credentials getCredentials(String username, String password) {
  58. if (mCredentials == null)
  59. mCredentials = new UsernamePasswordCredentials(username, password);
  60. return mCredentials;
  61. }
  62. public void allowSelfsignedCertificates() {
  63. // https
  64. Protocol.registerProtocol("https", new Protocol("https",
  65. new EasySSLSocketFactory(), 443));
  66. }
  67. public boolean downloadFile(String filepath, File targetPath) {
  68. // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",
  69. // "%20"));
  70. String[] splitted_filepath = filepath.split("/");
  71. filepath = "";
  72. for (String s : splitted_filepath) {
  73. if (s.equals("")) continue;
  74. filepath += "/" + URLEncoder.encode(s);
  75. }
  76. Log.e("ASD", mUri.toString() + filepath.replace(" ", "%20") + "");
  77. GetMethod get = new GetMethod(mUri.toString()
  78. + filepath.replace(" ", "%20"));
  79. // get.setHeader("Host", mUri.getHost());
  80. // get.setHeader("User-Agent", "Android-ownCloud");
  81. try {
  82. int status = executeMethod(get);
  83. Log.e(TAG, "status return: " + status);
  84. if (status != HttpStatus.SC_OK) {
  85. return false;
  86. }
  87. BufferedInputStream bis = new BufferedInputStream(
  88. get.getResponseBodyAsStream());
  89. FileOutputStream fos = new FileOutputStream(targetPath);
  90. byte[] bytes = new byte[4096];
  91. int readResult;
  92. while ((readResult = bis.read(bytes)) != -1) {
  93. if (mDataTransferListener != null)
  94. mDataTransferListener.transferProgress(readResult);
  95. fos.write(bytes, 0, readResult);
  96. }
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. return false;
  100. }
  101. return true;
  102. }
  103. public void setUploadListener(OnUploadProgressListener listener) {
  104. mUploadProgressListener = listener;
  105. }
  106. public void setDataTransferProgressListener(OnDatatransferProgressListener listener) {
  107. mDataTransferListener = listener;
  108. }
  109. public boolean putFile(String localFile, String remoteTarget,
  110. String contentType) {
  111. boolean result = true;
  112. try {
  113. Log.e("ASD", contentType + "");
  114. File f = new File(localFile);
  115. FileRequestEntity entity = new FileRequestEntity(f, contentType);
  116. entity.setOnUploadProgressListener(mUploadProgressListener);
  117. Log.e("ASD", f.exists() + " " + entity.getContentLength());
  118. PutMethod put = new PutMethod(mUri.toString() + remoteTarget);
  119. put.setRequestEntity(entity);
  120. Log.d(TAG, "" + put.getURI().toString());
  121. int status = executeMethod(put);
  122. Log.d(TAG, "PUT method return with status " + status);
  123. Log.i(TAG, "Uploading, done");
  124. } catch (final Exception e) {
  125. Log.i(TAG, "" + e.getMessage());
  126. result = false;
  127. }
  128. return result;
  129. }
  130. public int tryToLogin() {
  131. int r = 0;
  132. HeadMethod head = new HeadMethod(mUri.toString());
  133. try {
  134. r = executeMethod(head);
  135. } catch (Exception e) {
  136. Log.e(TAG, "Error: " + e.getMessage());
  137. }
  138. return r;
  139. }
  140. public boolean createDirectory(String path) {
  141. try {
  142. MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path
  143. + "/");
  144. int status = executeMethod(mkcol);
  145. Log.d(TAG, "Status returned " + status);
  146. Log.d(TAG, "uri: " + mkcol.getURI().toString());
  147. Log.i(TAG, "Creating dir completed");
  148. } catch (final Exception e) {
  149. e.printStackTrace();
  150. return false;
  151. }
  152. return true;
  153. }
  154. }