WebdavClient.java 6.4 KB

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