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