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