fix crash if download file has space in it
[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 import java.net.URLDecoder;
25 import java.net.URLEncoder;
26
27 import org.apache.commons.httpclient.Credentials;
28 import org.apache.commons.httpclient.HttpClient;
29 import org.apache.commons.httpclient.UsernamePasswordCredentials;
30 import org.apache.commons.httpclient.auth.AuthScope;
31 import org.apache.commons.httpclient.methods.GetMethod;
32 import org.apache.commons.httpclient.methods.HeadMethod;
33 import org.apache.commons.httpclient.methods.PutMethod;
34 import org.apache.commons.httpclient.params.HttpMethodParams;
35 import org.apache.commons.httpclient.protocol.Protocol;
36 import org.apache.http.HttpStatus;
37 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
38
39 import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory;
40
41 import android.net.Uri;
42 import android.util.Log;
43
44 public class WebdavClient extends HttpClient {
45 private Uri mUri;
46 private Credentials mCredentials;
47 final private static String TAG = "WebdavClient";
48 private static final String USER_AGENT = "Android-ownCloud";
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, getCredentials(username, password));
58 }
59
60 private Credentials getCredentials(String username, String password) {
61 if (mCredentials == null)
62 mCredentials = new UsernamePasswordCredentials(username, password);
63 return mCredentials;
64 }
65
66 public void allowUnsignedCertificates() {
67 // https
68 Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
69 }
70
71 public boolean downloadFile(String filepath, File targetPath) {
72 //HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20"));
73
74 Log.e("ASD", mUri.toString() + URLDecoder.decode(filepath) + "");
75 GetMethod get = new GetMethod(mUri.toString() + URLEncoder.encode(filepath));
76
77 // get.setHeader("Host", mUri.getHost());
78 // get.setHeader("User-Agent", "Android-ownCloud");
79
80 try {
81 Log.e("ASD", get.toString());
82 int status = executeMethod(get);
83 if (status != HttpStatus.SC_OK) {
84 return false;
85 }
86 BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
87 FileOutputStream fos = new FileOutputStream(targetPath);
88
89 byte[] bytes = new byte[512];
90 int readResult;
91 while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult);
92
93 } catch (IOException e) {
94 e.printStackTrace();
95 return false;
96 }
97 return true;
98 }
99
100 public boolean putFile(String localFile,
101 String remoteTarget,
102 String contentType) {
103 boolean result = true;
104
105 try {
106 FileRequestEntity entity = new FileRequestEntity(new File(localFile), contentType);
107 PutMethod put = new PutMethod(mUri.toString() + remoteTarget.substring(1));
108 put.setRequestEntity(entity);
109 int status = executeMethod(put);
110 Log.d(TAG, "PUT method return with status "+status);
111
112 Log.i(TAG, "Uploading, done");
113 } catch (final Exception e) {
114 Log.i(TAG, ""+e.getMessage());
115 result = false;
116 }
117
118 return result;
119 }
120
121 public int tryToLogin() {
122 int r = 0;
123 HeadMethod head = new HeadMethod(mUri.toString());
124 try {
125 r = executeMethod(head);
126 } catch (Exception e) {
127 Log.e(TAG, "Error: " + e.getMessage());
128 }
129 return r;
130 }
131
132 public boolean createDirectory(String path) {
133 try {
134 MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path + "/");
135 int status = executeMethod(mkcol);
136 Log.d(TAG, "Status returned " + status);
137 Log.d(TAG, "uri: " + mkcol.getURI().toString());
138 Log.i(TAG, "Creating dir completed");
139 } catch (final Exception e) {
140 e.printStackTrace();
141 return false;
142 }
143 return true;
144 }
145 }