* along with this program. If not, see <http://www.gnu.org/licenses/>.\r
*\r
*/\r
-package eu.alefzero.webdav;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
+package eu.alefzero.webdav;\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
import java.net.URLDecoder;\r
import java.net.URLEncoder;\r
-
+\r
import org.apache.commons.httpclient.Credentials;\r
import org.apache.commons.httpclient.HttpClient;\r
import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
import org.apache.commons.httpclient.methods.GetMethod;\r
import org.apache.commons.httpclient.methods.HeadMethod;\r
import org.apache.commons.httpclient.methods.PutMethod;\r
+import org.apache.commons.httpclient.methods.RequestEntity;\r
import org.apache.commons.httpclient.params.HttpMethodParams;\r
-import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.protocol.Protocol;\r
import org.apache.http.HttpStatus;\r
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;\r
-
-import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;
-
+\r
+import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;\r
+\r
import android.net.Uri;\r
-import android.util.Log;
-
-public class WebdavClient extends HttpClient {
- private Uri mUri;\r
- private Credentials mCredentials;
- final private static String TAG = "WebdavClient";\r
- private static final String USER_AGENT = "Android-ownCloud";
-
- public WebdavClient(Uri uri) {
- mUri = uri;\r
- getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
- }
-
- public void setCredentials(String username, String password) {\r
- getParams().setAuthenticationPreemptive(true);
- getState().setCredentials(AuthScope.ANY, getCredentials(username, password));
- }
-
- private Credentials getCredentials(String username, String password) {\r
- if (mCredentials == null)\r
- mCredentials = new UsernamePasswordCredentials(username, password); \r
- return mCredentials;\r
- }\r
-\r
- public void allowUnsignedCertificates() {
- // https\r
- Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
- }
-
- public boolean downloadFile(String filepath, File targetPath) {
- //HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20"));\r
- \r
- Log.e("ASD", mUri.toString() + URLDecoder.decode(filepath) + "");\r
- GetMethod get = new GetMethod(mUri.toString() + URLEncoder.encode(filepath));\r
-
-// get.setHeader("Host", mUri.getHost());
-// get.setHeader("User-Agent", "Android-ownCloud");
-
- try {\r
- Log.e("ASD", get.toString());
- int status = executeMethod(get);
- if (status != HttpStatus.SC_OK) {
- return false;
- }
- BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
- FileOutputStream fos = new FileOutputStream(targetPath);
-
- byte[] bytes = new byte[512];
- int readResult;
- while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult);
-
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
-
- public boolean putFile(String localFile,
- String remoteTarget,
- String contentType) {
- boolean result = true;
-
- try {
- FileRequestEntity entity = new FileRequestEntity(new File(localFile), contentType);\r
- PutMethod put = new PutMethod(mUri.toString() + remoteTarget.substring(1));\r
- put.setRequestEntity(entity);\r
- int status = executeMethod(put);\r
- Log.d(TAG, "PUT method return with status "+status);
-
- Log.i(TAG, "Uploading, done");
- } catch (final Exception e) {
- Log.i(TAG, ""+e.getMessage());
- result = false;
- }
-
- return result;
- }
- \r
- public int tryToLogin() {\r
- int r = 0; \r
- HeadMethod head = new HeadMethod(mUri.toString());\r
- try {\r
- r = executeMethod(head);\r
- } catch (Exception e) {\r
- Log.e(TAG, "Error: " + e.getMessage());\r
+import android.util.Log;\r
+\r
+public class WebdavClient extends HttpClient {\r
+ private Uri mUri;\r
+ private Credentials mCredentials;\r
+ final private static String TAG = "WebdavClient";\r
+ private static final String USER_AGENT = "Android-ownCloud";\r
+ private OnUploadProgressListener mUploadProgressListener;\r
+\r
+ public WebdavClient(Uri uri) {\r
+ mUri = uri;\r
+ getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);\r
+ }\r
+\r
+ public void setCredentials(String username, String password) {\r
+ getParams().setAuthenticationPreemptive(true);\r
+ getState().setCredentials(AuthScope.ANY,\r
+ getCredentials(username, password));\r
+ }\r
+\r
+ private Credentials getCredentials(String username, String password) {\r
+ if (mCredentials == null)\r
+ mCredentials = new UsernamePasswordCredentials(username, password);\r
+ return mCredentials;\r
+ }\r
+\r
+ public void allowUnsignedCertificates() {\r
+ // https\r
+ Protocol.registerProtocol("https", new Protocol("https",\r
+ new EasySSLSocketFactory(), 443));\r
+ }\r
+\r
+ public boolean downloadFile(String filepath, File targetPath) {\r
+ // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",\r
+ // "%20"));\r
+ String[] splitted_filepath = filepath.split("/");\r
+ filepath = "";\r
+ for (String s : splitted_filepath) {\r
+ if (s.equals("")) continue;\r
+ filepath += "/" + URLEncoder.encode(s);\r
+ }\r
+\r
+ Log.e("ASD", mUri.toString() + filepath.replace(" ", "%20") + "");\r
+ GetMethod get = new GetMethod(mUri.toString()\r
+ + filepath.replace(" ", "%20"));\r
+\r
+ // get.setHeader("Host", mUri.getHost());\r
+ // get.setHeader("User-Agent", "Android-ownCloud");\r
+\r
+ try {\r
+ int status = executeMethod(get);\r
+ Log.e(TAG, "status return: " + status);\r
+ if (status != HttpStatus.SC_OK) {\r
+ return false;\r
+ }\r
+ BufferedInputStream bis = new BufferedInputStream(\r
+ get.getResponseBodyAsStream());\r
+ FileOutputStream fos = new FileOutputStream(targetPath);\r
+\r
+ byte[] bytes = new byte[512];\r
+ int readResult;\r
+ while ((readResult = bis.read(bytes)) != -1)\r
+ fos.write(bytes, 0, readResult);\r
+\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ public void setUploadListener(OnUploadProgressListener listener) {\r
+ mUploadProgressListener = listener;\r
+ }\r
+ \r
+ public boolean putFile(String localFile, String remoteTarget,\r
+ String contentType) {\r
+ boolean result = true;\r
+\r
+ try {\r
+ Log.e("ASD", contentType + "");\r
+ File f = new File(localFile);\r
+ FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
+ entity.setOnUploadProgressListener(mUploadProgressListener);\r
+ Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
+ PutMethod put = new PutMethod(mUri.toString() + remoteTarget);\r
+ put.setRequestEntity(entity);\r
+ Log.d(TAG, "" + put.getURI().toString());\r
+ int status = executeMethod(put);\r
+ Log.d(TAG, "PUT method return with status " + status);\r
+\r
+ Log.i(TAG, "Uploading, done");\r
+ } catch (final Exception e) {\r
+ Log.i(TAG, "" + e.getMessage());\r
+ result = false;\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ public int tryToLogin() {\r
+ int r = 0;\r
+ HeadMethod head = new HeadMethod(mUri.toString());\r
+ try {\r
+ r = executeMethod(head);\r
+ } catch (Exception e) {\r
+ Log.e(TAG, "Error: " + e.getMessage());\r
+ }\r
+ return r;\r
+ }\r
+\r
+ public boolean createDirectory(String path) {\r
+ try {\r
+ MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path\r
+ + "/");\r
+ int status = executeMethod(mkcol);\r
+ Log.d(TAG, "Status returned " + status);\r
+ Log.d(TAG, "uri: " + mkcol.getURI().toString());\r
+ Log.i(TAG, "Creating dir completed");\r
+ } catch (final Exception e) {\r
+ e.printStackTrace();\r
+ return false;\r
+ }\r
+ return true;\r
}\r
- return r;\r
- }\r
-
- public boolean createDirectory(String path) {
- try {\r
- MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path + "/");
- int status = executeMethod(mkcol);\r
- Log.d(TAG, "Status returned " + status);\r
- Log.d(TAG, "uri: " + mkcol.getURI().toString());
- Log.i(TAG, "Creating dir completed");
- } catch (final Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
-}
+}\r