880995da984c241b7ae6e19eda5d696133e4c547
[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
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
40 import android.net.Uri;
41 import android.util.Log;
42
43 public class WebdavClient extends HttpClient {
44 private Uri mUri;
45 private Credentials mCredentials;
46 final private static String TAG = "WebdavClient";
47 private static final String USER_AGENT = "Android-ownCloud";
48
49 public WebdavClient(Uri uri) {
50 mUri = uri;
51 getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
52 }
53
54 public void setCredentials(String username, String password) {
55 getParams().setAuthenticationPreemptive(true);
56 getState().setCredentials(AuthScope.ANY, getCredentials(username, password));
57 }
58
59 private Credentials getCredentials(String username, String password) {
60 if (mCredentials == null)
61 mCredentials = new UsernamePasswordCredentials(username, password);
62 return mCredentials;
63 }
64
65 public void allowUnsignedCertificates() {
66 // https
67 Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
68 }
69
70 public boolean downloadFile(String filepath, File targetPath) {
71 //HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20"));
72
73 Log.e("ASD", mUri.toString() + URLDecoder.decode(filepath) + "");
74 GetMethod get = new GetMethod(mUri.toString() + URLDecoder.decode(filepath));
75
76 // get.setHeader("Host", mUri.getHost());
77 // get.setHeader("User-Agent", "Android-ownCloud");
78
79 try {
80 int status = executeMethod(get);
81 if (status != HttpStatus.SC_OK) {
82 return false;
83 }
84 BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
85 FileOutputStream fos = new FileOutputStream(targetPath);
86
87 byte[] bytes = new byte[512];
88 int readResult;
89 while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult);
90
91 } catch (IOException e) {
92 e.printStackTrace();
93 return false;
94 }
95 return true;
96 }
97
98 public boolean putFile(String localFile,
99 String remoteTarget,
100 String contentType) {
101 boolean result = true;
102
103 try {
104 FileRequestEntity entity = new FileRequestEntity(new File(localFile), contentType);
105 PutMethod put = new PutMethod(mUri.toString() + remoteTarget.substring(1));
106 put.setRequestEntity(entity);
107 int status = executeMethod(put);
108 Log.d(TAG, "PUT method return with status "+status);
109
110 Log.i(TAG, "Uploading, done");
111 } catch (final Exception e) {
112 Log.i(TAG, ""+e.getMessage());
113 result = false;
114 }
115
116 return result;
117 }
118
119 public int tryToLogin() {
120 int r = 0;
121 HeadMethod head = new HeadMethod(mUri.toString());
122 try {
123 r = executeMethod(head);
124 } catch (Exception e) {
125 Log.e(TAG, "Error: " + e.getMessage());
126 }
127 return r;
128 }
129
130 public boolean createDirectory(String path) {
131 try {
132 MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path + "/");
133 int status = executeMethod(mkcol);
134 Log.d(TAG, "Status returned " + status);
135 Log.d(TAG, "uri: " + mkcol.getURI().toString());
136 Log.i(TAG, "Creating dir completed");
137 } catch (final Exception e) {
138 e.printStackTrace();
139 return false;
140 }
141 return true;
142 }
143 }