c4e0a9beb88f7bf09bb4e7d0ed2cfb6246d028fc
[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 import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;
42
43 import android.net.Uri;
44 import android.util.Log;
45
46 public class WebdavClient extends HttpClient {
47 private Uri mUri;
48 private Credentials mCredentials;
49 final private static String TAG = "WebdavClient";
50 private static final String USER_AGENT = "Android-ownCloud";
51 private OnUploadProgressListener mUploadProgressListener;
52 private OnDatatransferProgressListener mDataTransferListener;
53
54 public WebdavClient(Uri uri) {
55 mUri = uri;
56 getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
57 }
58
59 public void setCredentials(String username, String password) {
60 getParams().setAuthenticationPreemptive(true);
61 getState().setCredentials(AuthScope.ANY,
62 getCredentials(username, password));
63 }
64
65 private Credentials getCredentials(String username, String password) {
66 if (mCredentials == null)
67 mCredentials = new UsernamePasswordCredentials(username, password);
68 return mCredentials;
69 }
70
71 public void allowSelfsignedCertificates() {
72 // https
73 Protocol.registerProtocol("https", new Protocol("https",
74 new EasySSLSocketFactory(), 443));
75 }
76
77 public boolean downloadFile(String filepath, File targetPath) {
78 // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",
79 // "%20"));
80 String[] splitted_filepath = filepath.split("/");
81 filepath = "";
82 for (String s : splitted_filepath) {
83 if (s.equals("")) continue;
84 filepath += "/" + URLEncoder.encode(s);
85 }
86
87 Log.e("ASD", mUri.toString() + filepath.replace(" ", "%20") + "");
88 GetMethod get = new GetMethod(mUri.toString()
89 + filepath.replace(" ", "%20"));
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 setUploadListener(OnUploadProgressListener listener) {
120 mUploadProgressListener = listener;
121 }
122
123 public void setDataTransferProgressListener(OnDatatransferProgressListener listener) {
124 mDataTransferListener = listener;
125 }
126
127 public boolean putFile(String localFile, String remoteTarget,
128 String contentType) {
129 boolean result = true;
130
131 try {
132 Log.e("ASD", contentType + "");
133 File f = new File(localFile);
134 FileRequestEntity entity = new FileRequestEntity(f, contentType);
135 entity.setOnUploadProgressListener(mUploadProgressListener);
136 Log.e("ASD", f.exists() + " " + entity.getContentLength());
137 PutMethod put = new PutMethod(mUri.toString() + remoteTarget);
138 put.setRequestEntity(entity);
139 Log.d(TAG, "" + put.getURI().toString());
140 int status = executeMethod(put);
141 Log.d(TAG, "PUT method return with status " + status);
142
143 Log.i(TAG, "Uploading, done");
144 } catch (final Exception e) {
145 Log.i(TAG, "" + e.getMessage());
146 result = false;
147 }
148
149 return result;
150 }
151
152 public int tryToLogin() {
153 int r = 0;
154 HeadMethod head = new HeadMethod(mUri.toString());
155 try {
156 r = executeMethod(head);
157 } catch (Exception e) {
158 Log.e(TAG, "Error: " + e.getMessage());
159 }
160 return r;
161 }
162
163 public boolean createDirectory(String path) {
164 try {
165 MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path
166 + "/");
167 int status = executeMethod(mkcol);
168 Log.d(TAG, "Status returned " + status);
169 Log.d(TAG, "uri: " + mkcol.getURI().toString());
170 Log.i(TAG, "Creating dir completed");
171 } catch (final Exception e) {
172 e.printStackTrace();
173 return false;
174 }
175 return true;
176 }
177 }