X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/cffdf90bf3e0ea45267c133e51d4d834aab8c8de..a238d0635ad16d6adc97d08ef0fa89b8afb18b46:/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 44b6e6b5..7194195c 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -57,6 +57,13 @@ public class WebdavClient extends HttpClient { private Credentials mCredentials; final private static String TAG = "WebdavClient"; private static final String USER_AGENT = "Android-ownCloud"; + + /** Default timeout for waiting data from the server: 10 seconds */ + public static final int DEFAULT_DATA_TIMEOUT = 10000; + + /** Default timeout for establishing a connection: infinite */ + public static final int DEFAULT_CONNECTION_TIMEOUT = 0; + private OnDatatransferProgressListener mDataTransferListener; static private MultiThreadedHttpConnectionManager mConnManager = null; @@ -76,28 +83,32 @@ public class WebdavClient extends HttpClient { * @return */ public WebdavClient (Account account, Context context) { + setDefaultTimeouts(); + OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_VERSION)); String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL); String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion); - String username = account.name.substring(0, account.name.indexOf('@')); + String username = account.name.substring(0, account.name.lastIndexOf('@')); String password = AccountManager.get(context).getPassword(account); mUri = Uri.parse(baseUrl + webDavPath); - + Log.e("ASD", ""+username); setCredentials(username, password); } public WebdavClient() { super(getMultiThreadedConnManager()); + setDefaultTimeouts(); + getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT); getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); allowSelfsignedCertificates(); } public void setCredentials(String username, String password) { - //getParams().setAuthenticationPreemptive(true); + getParams().setAuthenticationPreemptive(true); getState().setCredentials(AuthScope.ANY, getCredentials(username, password)); } @@ -107,6 +118,14 @@ public class WebdavClient extends HttpClient { mCredentials = new UsernamePasswordCredentials(username, password); return mCredentials; } + + /** + * Sets the connection and wait-for-data timeouts to be applied by default. + */ + private void setDefaultTimeouts() { + getParams().setSoTimeout(DEFAULT_DATA_TIMEOUT); + getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT); + } public void allowSelfsignedCertificates() { // https @@ -144,9 +163,9 @@ public class WebdavClient extends HttpClient { mDataTransferListener.transferProgress(readResult); fos.write(bytes, 0, readResult); } - + ret = true; } - ret = true; + } catch (Throwable e) { e.printStackTrace(); targetPath.delete(); @@ -186,7 +205,7 @@ public class WebdavClient extends HttpClient { */ public boolean putFile(String localFile, String remoteTarget, String contentType) { - boolean result = true; + boolean result = false; try { Log.e("ASD", contentType + ""); @@ -200,7 +219,11 @@ public class WebdavClient extends HttpClient { int status = executeMethod(put, 0); Log.d(TAG, "PUT method return with status " + status); - Log.i(TAG, "Uploading, done"); + if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT) { + result = true; + Log.i(TAG, "Uploading, done"); + } + } catch (final Exception e) { Log.i(TAG, "" + e.getMessage()); result = false; @@ -248,6 +271,23 @@ public class WebdavClient extends HttpClient { } return true; } + + + /** + * 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 + */ + public Boolean existsFile(String path) { + try { + HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path)); + int status = executeMethod(head); + return (status == HttpStatus.SC_OK); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } /**