4b719fb607e1d225f67b6f4c074dc54f78f292c6
[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
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, 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 allowUnsignedCertificates() {
68 // https
69 Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
70 }
71
72 public boolean downloadFile(String filepath, File targetPath) {
73 //HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20"));
74
75 Log.e("ASD", mUri.toString() + URLDecoder.decode(filepath) + "");
76 GetMethod get = new GetMethod(mUri.toString() + URLEncoder.encode(filepath));
77
78 // get.setHeader("Host", mUri.getHost());
79 // get.setHeader("User-Agent", "Android-ownCloud");
80
81 try {
82 Log.e("ASD", get.toString());
83 int status = executeMethod(get);
84 if (status != HttpStatus.SC_OK) {
85 return false;
86 }
87 BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
88 FileOutputStream fos = new FileOutputStream(targetPath);
89
90 byte[] bytes = new byte[512];
91 int readResult;
92 while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult);
93
94 } catch (IOException e) {
95 e.printStackTrace();
96 return false;
97 }
98 return true;
99 }
100
101 public boolean putFile(String localFile,
102 String remoteTarget,
103 String contentType) {
104 boolean result = true;
105
106 try {
107 Log.e("ASD", contentType+"");
108 File f = new File(localFile);
109 RequestEntity entity = new FileRequestEntity(f, contentType);
110 Log.e("ASD", f.exists()+" " + entity.getContentLength());
111 PutMethod put = new PutMethod(mUri.toString() + remoteTarget);
112 put.setRequestEntity(entity);
113 Log.d(TAG, "" + put.getURI().toString());
114 int status = executeMethod(put);
115 Log.d(TAG, "PUT method return with status "+status);
116
117 Log.i(TAG, "Uploading, done");
118 } catch (final Exception e) {
119 Log.i(TAG, ""+e.getMessage());
120 result = false;
121 }
122
123 return result;
124 }
125
126 public int tryToLogin() {
127 int r = 0;
128 HeadMethod head = new HeadMethod(mUri.toString());
129 try {
130 r = executeMethod(head);
131 } catch (Exception e) {
132 Log.e(TAG, "Error: " + e.getMessage());
133 }
134 return r;
135 }
136
137 public boolean createDirectory(String path) {
138 try {
139 MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path + "/");
140 int status = executeMethod(mkcol);
141 Log.d(TAG, "Status returned " + status);
142 Log.d(TAG, "uri: " + mkcol.getURI().toString());
143 Log.i(TAG, "Creating dir completed");
144 } catch (final Exception e) {
145 e.printStackTrace();
146 return false;
147 }
148 return true;
149 }
150 }