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