X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/91bc3fdb1eb923053b469b94e5b200482f522010..2e5b40e357a5ab18b3a04f823b7cd27d5c94dc90:/src/eu/alefzero/webdav/WebdavClient.java?ds=inline diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index ccb08cac..7577152d 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -1,9 +1,10 @@ /* ownCloud Android client application * Copyright (C) 2011 Bartek Przybylski + * Copyright (C) 2012-2013 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -22,14 +23,20 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthPolicy; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.HeadMethod; @@ -39,7 +46,9 @@ import org.apache.http.HttpStatus; import org.apache.http.params.CoreProtocolPNames; import org.apache.jackrabbit.webdav.client.methods.DavMethod; import org.apache.jackrabbit.webdav.client.methods.DeleteMethod; -import org.apache.jackrabbit.webdav.client.methods.MkColMethod; + +import com.owncloud.android.network.BearerAuthScheme; +import com.owncloud.android.network.BearerCredentials; import android.net.Uri; import android.util.Log; @@ -63,18 +72,28 @@ public class WebdavClient extends HttpClient { getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); } - public void setCredentials(String username, String password) { - getParams().setAuthenticationPreemptive(true); - getState().setCredentials(AuthScope.ANY, - getCredentials(username, password)); + public void setBearerCredentials(String accessToken) { + AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class); + + List authPrefs = new ArrayList(1); + authPrefs.add(BearerAuthScheme.AUTH_POLICY); + getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); + + mCredentials = new BearerCredentials(accessToken); + getState().setCredentials(AuthScope.ANY, mCredentials); } - private Credentials getCredentials(String username, String password) { - if (mCredentials == null) - mCredentials = new UsernamePasswordCredentials(username, password); - return mCredentials; + public void setBasicCredentials(String username, String password) { + List authPrefs = new ArrayList(1); + authPrefs.add(AuthPolicy.BASIC); + getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); + + getParams().setAuthenticationPreemptive(true); + mCredentials = new UsernamePasswordCredentials(username, password); + getState().setCredentials(AuthScope.ANY, mCredentials); } + /** * Downloads a file in remoteFilepath to the local targetPath. * @@ -98,15 +117,15 @@ public class WebdavClient extends HttpClient { int readResult; while ((readResult = bis.read(bytes)) != -1) { if (mDataTransferListener != null) - mDataTransferListener.transferProgress(readResult); + mDataTransferListener.onTransferProgress(readResult); fos.write(bytes, 0, readResult); } + fos.close(); ret = true; } else { exhaustResponse(get.getResponseBodyAsStream()); } Log.e(TAG, "Download of " + remoteFilePath + " to " + targetFile + " finished with HTTP status " + status + (!ret?"(FAIL)":"")); - } catch (Exception e) { logException(e, "dowloading " + remoteFilePath); @@ -151,39 +170,32 @@ public class WebdavClient extends HttpClient { /** * Creates or update a file in the remote server with the contents of a local file. * - * * @param localFile Path to the local file to upload. * @param remoteTarget Remote path to the file to create or update, URL DECODED * @param contentType MIME type of the file. - * @return 'True' then the upload was successfully completed + * @return Status HTTP code returned by the server. + * @throws IOException When a transport error that could not be recovered occurred while uploading the file to the server. + * @throws HttpException When a violation of the HTTP protocol occurred. */ - public boolean putFile(String localFile, String remoteTarget, String contentType) { - boolean result = false; + public int putFile(String localFile, String remoteTarget, String contentType) throws HttpException, IOException { int status = -1; PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget)); try { File f = new File(localFile); FileRequestEntity entity = new FileRequestEntity(f, contentType); - entity.setOnDatatransferProgressListener(mDataTransferListener); + entity.addOnDatatransferProgressListener(mDataTransferListener); put.setRequestEntity(entity); status = executeMethod(put); - result = (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT); - - Log.d(TAG, "PUT to " + remoteTarget + " finished with HTTP status " + status + (!result?"(FAIL)":"")); - exhaustResponse(put.getResponseBodyAsStream()); - } catch (Exception e) { - logException(e, "uploading " + localFile + " to " + remoteTarget); - } finally { put.releaseConnection(); // let the connection available for other methods } - return result; + return status; } - + /** * Tries to log in to the current URI, with the current credentials * @@ -207,41 +219,14 @@ public class WebdavClient extends HttpClient { return status; } - /** - * Creates a remote directory with the received path. - * - * @param path Path of the directory to create, URL DECODED - * @return 'True' when the directory is successfully created - */ - public boolean createDirectory(String path) { - boolean result = false; - int status = -1; - MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path)); - try { - Log.d(TAG, "Creating directory " + path); - status = executeMethod(mkcol); - Log.d(TAG, "Status returned: " + status); - result = mkcol.succeeded(); - - Log.d(TAG, "MKCOL to " + path + " finished with HTTP status " + status + (!result?"(FAIL)":"")); - exhaustResponse(mkcol.getResponseBodyAsStream()); - - } catch (Exception e) { - logException(e, "creating directory " + path); - - } finally { - mkcol.releaseConnection(); // let the connection available for other methods - } - return result; - } - /** * Check if a file exists in the OC server * - * @return 'Boolean.TRUE' if the file exists; 'Boolean.FALSE' it doesn't exist; NULL if couldn't be checked + * @return 'true' if the file exists; 'false' it doesn't exist + * @throws Exception When the existence could not be determined */ - public Boolean existsFile(String path) { + public boolean existsFile(String path) throws IOException, HttpException { HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path)); try { int status = executeMethod(head); @@ -249,10 +234,6 @@ public class WebdavClient extends HttpClient { exhaustResponse(head.getResponseBodyAsStream()); return (status == HttpStatus.SC_OK); - } catch (Exception e) { - logException(e, "checking existence of " + path); - return null; - } finally { head.releaseConnection(); // let the connection available for other methods } @@ -295,7 +276,7 @@ public class WebdavClient extends HttpClient { * * @param responseBodyAsStream InputStream with the HTTP response to exhaust. */ - private static void exhaustResponse(InputStream responseBodyAsStream) { + public void exhaustResponse(InputStream responseBodyAsStream) { if (responseBodyAsStream != null) { try { while (responseBodyAsStream.read(sExhaustBuffer) >= 0); @@ -342,5 +323,24 @@ public class WebdavClient extends HttpClient { public void setBaseUri(Uri uri) { mUri = uri; } + + public Uri getBaseUri() { + return mUri; + } + + + @Override + public int executeMethod(HostConfiguration hostconfig, final HttpMethod method, final HttpState state) throws IOException, HttpException { + if (mCredentials instanceof BearerCredentials) { + method.getHostAuthState().setAuthScheme(AuthPolicy.getAuthScheme(BearerAuthScheme.AUTH_POLICY)); + method.getHostAuthState().setAuthAttempted(true); + } + return super.executeMethod(hostconfig, method, state); + } + + public final Credentials getCredentials() { + return mCredentials; + } + }