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
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
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