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