* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
+
package eu.alefzero.webdav;
import java.io.BufferedInputStream;
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;
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.Log_OC;
+import com.owncloud.android.network.BearerAuthScheme;
+import com.owncloud.android.network.BearerCredentials;
+
import android.net.Uri;
-import android.util.Log;
public class WebdavClient extends HttpClient {
private Uri mUri;
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<String> authPrefs = new ArrayList<String>(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<String> authPrefs = new ArrayList<String>(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);
}
/**
return ret;
}
+
/**
* Deletes a remote file via webdav
* @param remoteFilePath Remote file path of the file to delete, in URL DECODED format.
}
/**
- * 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_OC.d(TAG, "Creating directory " + path);
- status = executeMethod(mkcol);
- Log_OC.d(TAG, "Status returned: " + status);
- result = mkcol.succeeded();
-
- Log_OC.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 'true' if the file exists; 'false' it doesn't exist
head.releaseConnection(); // let the connection available for other methods
}
}
-
+
/**
* Requests the received method with the received timeout (milliseconds).
*
return mUri;
}
+ @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
}