X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/1083eb8683b5c6b486bf8c16c081d7109aced91c..fae44486b9545bfb2a49ded9a0c3fd550fd18549:/src/eu/alefzero/webdav/WebdavClient.java diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 0a5295b1..1f196292 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -22,13 +22,18 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPut; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnManagerPNames; @@ -38,7 +43,6 @@ import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.FileEntity; -import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; @@ -53,12 +57,13 @@ import eu.alefzero.webdav.HttpMkCol; import android.net.Uri; import android.util.Log; -public class WebdavClient { +public class WebdavClient extends HttpClient { private DefaultHttpClient mHttpClient; private BasicHttpContext mHttpContext; private HttpHost mTargetHost; private SchemeRegistry mSchemeRegistry; - private Uri mUri; + private Uri mUri; + private Credentials mCredentials; final private static String TAG = "WebdavClient"; public DefaultHttpClient getHttpClient() { @@ -78,15 +83,17 @@ public class WebdavClient { // determine default port for http or https int targetPort = mTargetHost.getPort() == -1 ? ( mUri.getScheme().equals("https") ? 443 : 80) - : mUri.getPort(); - - mHttpClient.getCredentialsProvider().setCredentials( - new AuthScope(mUri.getHost(), targetPort), - new UsernamePasswordCredentials(username, password)); - BasicScheme basicAuth = new BasicScheme(); - mHttpContext.setAttribute("preemptive-auth", basicAuth); + : mUri.getPort(); + + getParams().setAuthenticationPreemptive(true); + getState().setCredentials(AuthScope.ANY, getCredentials(username, password)); } + private Credentials getCredentials(String username, String password) { + if (mCredentials == null) + mCredentials = new UsernamePasswordCredentials(username, password); + return mCredentials; + } public void allowUnsignedCertificates() { // https mSchemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); @@ -151,6 +158,17 @@ public class WebdavClient { return result; } + + public int tryToLogin() { + int r = 0; + HeadMethod head = new HeadMethod(mUri.toString()); + try { + r = executeMethod(head); + } catch (Exception e) { + Log.e(TAG, "Error: " + e.getMessage()); + } + return r; + } public boolean createDirectory(String path) { HttpMkCol method = new HttpMkCol(mUri.toString() + path + "/");