1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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.
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/>.
19 package eu
.alefzero
.webdav
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
26 import org
.apache
.commons
.httpclient
.Credentials
;
27 import org
.apache
.commons
.httpclient
.Header
;
28 import org
.apache
.commons
.httpclient
.HttpClient
;
29 import org
.apache
.commons
.httpclient
.HttpConnectionManager
;
30 import org
.apache
.commons
.httpclient
.HttpException
;
31 import org
.apache
.commons
.httpclient
.HttpMethod
;
32 import org
.apache
.commons
.httpclient
.HttpMethodBase
;
33 import org
.apache
.commons
.httpclient
.HttpVersion
;
34 import org
.apache
.commons
.httpclient
.URI
;
35 import org
.apache
.commons
.httpclient
.UsernamePasswordCredentials
;
36 import org
.apache
.commons
.httpclient
.auth
.AuthPolicy
;
37 import org
.apache
.commons
.httpclient
.auth
.AuthScope
;
38 import org
.apache
.commons
.httpclient
.cookie
.CookiePolicy
;
39 import org
.apache
.commons
.httpclient
.methods
.HeadMethod
;
40 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
41 import org
.apache
.http
.HttpStatus
;
42 import org
.apache
.http
.params
.CoreProtocolPNames
;
44 import com
.owncloud
.android
.Log_OC
;
46 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
47 import com
.owncloud
.android
.network
.BearerAuthScheme
;
48 import com
.owncloud
.android
.network
.BearerCredentials
;
50 import android
.net
.Uri
;
52 public class WebdavClient
extends HttpClient
{
53 private static final int MAX_REDIRECTIONS_COUNT
= 3;
56 private Credentials mCredentials
;
57 private boolean mFollowRedirects
;
58 private String mSsoSessionCookie
;
59 private String mAuthTokenType
;
60 final private static String TAG
= "WebdavClient";
61 public static final String USER_AGENT
= "Android-ownCloud";
63 static private byte[] sExhaustBuffer
= new byte[1024];
68 public WebdavClient(HttpConnectionManager connectionMgr
) {
70 Log_OC
.d(TAG
, "Creating WebdavClient");
71 getParams().setParameter(HttpMethodParams
.USER_AGENT
, USER_AGENT
);
72 getParams().setParameter(CoreProtocolPNames
.PROTOCOL_VERSION
, HttpVersion
.HTTP_1_1
);
73 mFollowRedirects
= true
;
74 mSsoSessionCookie
= null
;
75 mAuthTokenType
= AccountAuthenticator
.AUTH_TOKEN_TYPE_PASSWORD
;
78 public void setBearerCredentials(String accessToken
) {
79 AuthPolicy
.registerAuthScheme(BearerAuthScheme
.AUTH_POLICY
, BearerAuthScheme
.class);
81 List
<String
> authPrefs
= new ArrayList
<String
>(1);
82 authPrefs
.add(BearerAuthScheme
.AUTH_POLICY
);
83 getParams().setParameter(AuthPolicy
.AUTH_SCHEME_PRIORITY
, authPrefs
);
85 mCredentials
= new BearerCredentials(accessToken
);
86 getState().setCredentials(AuthScope
.ANY
, mCredentials
);
87 mSsoSessionCookie
= null
;
88 mAuthTokenType
= AccountAuthenticator
.AUTH_TOKEN_TYPE_ACCESS_TOKEN
;
91 public void setBasicCredentials(String username
, String password
) {
92 List
<String
> authPrefs
= new ArrayList
<String
>(1);
93 authPrefs
.add(AuthPolicy
.BASIC
);
94 getParams().setParameter(AuthPolicy
.AUTH_SCHEME_PRIORITY
, authPrefs
);
96 getParams().setAuthenticationPreemptive(true
);
97 mCredentials
= new UsernamePasswordCredentials(username
, password
);
98 getState().setCredentials(AuthScope
.ANY
, mCredentials
);
99 mSsoSessionCookie
= null
;
100 mAuthTokenType
= AccountAuthenticator
.AUTH_TOKEN_TYPE_PASSWORD
;
103 public void setSsoSessionCookie(String accessToken
) {
104 getParams().setAuthenticationPreemptive(false
);
105 getParams().setCookiePolicy(CookiePolicy
.IGNORE_COOKIES
);
106 mSsoSessionCookie
= accessToken
;
108 mAuthTokenType
= AccountAuthenticator
.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE
;
113 * Check if a file exists in the OC server
115 * TODO replace with ExistenceOperation
117 * @return 'true' if the file exists; 'false' it doesn't exist
118 * @throws Exception When the existence could not be determined
120 public boolean existsFile(String path
) throws IOException
, HttpException
{
121 HeadMethod head
= new HeadMethod(mUri
.toString() + WebdavUtils
.encodePath(path
));
123 int status
= executeMethod(head
);
124 Log_OC
.d(TAG
, "HEAD to " + path
+ " finished with HTTP status " + status
+ ((status
!= HttpStatus
.SC_OK
)?
"(FAIL)":""));
125 exhaustResponse(head
.getResponseBodyAsStream());
126 return (status
== HttpStatus
.SC_OK
);
129 head
.releaseConnection(); // let the connection available for other methods
134 * Requests the received method with the received timeout (milliseconds).
136 * Executes the method through the inherited HttpClient.executedMethod(method).
138 * Sets the socket and connection timeouts only for the method received.
140 * The timeouts are both in milliseconds; 0 means 'infinite'; < 0 means 'do not change the default'
142 * @param method HTTP method request.
143 * @param readTimeout Timeout to set for data reception
144 * @param conntionTimout Timeout to set for connection establishment
146 public int executeMethod(HttpMethodBase method
, int readTimeout
, int connectionTimeout
) throws HttpException
, IOException
{
147 int oldSoTimeout
= getParams().getSoTimeout();
148 int oldConnectionTimeout
= getHttpConnectionManager().getParams().getConnectionTimeout();
150 if (readTimeout
>= 0) {
151 method
.getParams().setSoTimeout(readTimeout
); // this should be enough...
152 getParams().setSoTimeout(readTimeout
); // ... but this looks like necessary for HTTPS
154 if (connectionTimeout
>= 0) {
155 getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout
);
157 return executeMethod(method
);
159 getParams().setSoTimeout(oldSoTimeout
);
160 getHttpConnectionManager().getParams().setConnectionTimeout(oldConnectionTimeout
);
166 public int executeMethod(HttpMethod method
) throws IOException
, HttpException
{
167 boolean customRedirectionNeeded
= false
;
169 method
.setFollowRedirects(mFollowRedirects
);
170 } catch (Exception e
) {
171 if (mFollowRedirects
) Log_OC
.d(TAG
, "setFollowRedirects failed for " + method
.getName() + " method, custom redirection will be used");
172 customRedirectionNeeded
= mFollowRedirects
;
174 if (mSsoSessionCookie
!= null
&& mSsoSessionCookie
.length() > 0) {
175 method
.setRequestHeader("Cookie", mSsoSessionCookie
);
177 int status
= super.executeMethod(method
);
178 int redirectionsCount
= 0;
179 while (customRedirectionNeeded
&&
180 redirectionsCount
< MAX_REDIRECTIONS_COUNT
&&
181 ( status
== HttpStatus
.SC_MOVED_PERMANENTLY
||
182 status
== HttpStatus
.SC_MOVED_TEMPORARILY
||
183 status
== HttpStatus
.SC_TEMPORARY_REDIRECT
)
186 Header location
= method
.getResponseHeader("Location");
187 if (location
!= null
) {
188 Log_OC
.d(TAG
, "Location to redirect: " + location
.getValue());
189 method
.setURI(new URI(location
.getValue(), true
));
190 status
= super.executeMethod(method
);
194 Log_OC
.d(TAG
, "No location to redirect!");
195 status
= HttpStatus
.SC_NOT_FOUND
;
204 * Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
206 * @param responseBodyAsStream InputStream with the HTTP response to exhaust.
208 public void exhaustResponse(InputStream responseBodyAsStream
) {
209 if (responseBodyAsStream
!= null
) {
211 while (responseBodyAsStream
.read(sExhaustBuffer
) >= 0);
212 responseBodyAsStream
.close();
214 } catch (IOException io
) {
215 Log_OC
.e(TAG
, "Unexpected exception while exhausting not interesting HTTP response; will be IGNORED", io
);
221 * Sets the connection and wait-for-data timeouts to be applied by default to the methods performed by this client.
223 public void setDefaultTimeouts(int defaultDataTimeout
, int defaultConnectionTimeout
) {
224 getParams().setSoTimeout(defaultDataTimeout
);
225 getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout
);
229 * Sets the base URI for the helper methods that receive paths as parameters, instead of full URLs
232 public void setBaseUri(Uri uri
) {
236 public Uri
getBaseUri() {
240 public final Credentials
getCredentials() {
244 public final String
getSsoSessionCookie() {
245 return mSsoSessionCookie
;
248 public void setFollowRedirects(boolean followRedirects
) {
249 mFollowRedirects
= followRedirects
;
252 public String
getAuthTokenType() {
253 return mAuthTokenType
;