X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/2a04c6ae5da009baa78c243606297b5a039925c2..a032bdeebc51a6e81d1bd5c558944f96fc55eacb:/src/eu/alefzero/webdav/WebdavClient.java diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index a5398000..503685a1 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,21 +83,25 @@ 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(); @@ -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 @@ -118,24 +137,25 @@ public class WebdavClient extends HttpClient { * Downloads a file in remoteFilepath to the local targetPath. * * @param remoteFilepath Path to the file in the remote server, URL DECODED. - * @param targetPath Local path to save the downloaded file. + * @param targetFile Local path to save the downloaded file. * @return 'True' when the file is successfully downloaded. */ - public boolean downloadFile(String remoteFilepath, File targetPath) { + public boolean downloadFile(String remoteFilepath, File targetFile) { boolean ret = false; + boolean caughtException = false; GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath)); // get.setHeader("Host", mUri.getHost()); // get.setHeader("User-Agent", "Android-ownCloud"); + int status = -1; try { - int status = executeMethod(get, 0); - Log.e(TAG, "status return: " + status); + status = executeMethod(get); if (status == HttpStatus.SC_OK) { - targetPath.createNewFile(); + targetFile.createNewFile(); BufferedInputStream bis = new BufferedInputStream( get.getResponseBodyAsStream()); - FileOutputStream fos = new FileOutputStream(targetPath); + FileOutputStream fos = new FileOutputStream(targetFile); byte[] bytes = new byte[4096]; int readResult; @@ -147,11 +167,28 @@ public class WebdavClient extends HttpClient { ret = true; } - } catch (Throwable e) { - e.printStackTrace(); - targetPath.delete(); + } catch (HttpException e) { + Log.e(TAG, "HTTP exception downloading " + remoteFilepath, e); + caughtException = true; + + } catch (IOException e) { + Log.e(TAG, "I/O exception downloading " + remoteFilepath, e); + caughtException = true; + + } catch (Exception e) { + Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e); + caughtException = true; + + } finally { + if (!ret) { + if (!caughtException) { + Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status); + } + if (targetFile.exists()) { + targetFile.delete(); + } + } } - return ret; } @@ -186,7 +223,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 +237,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; @@ -223,8 +264,12 @@ public class WebdavClient extends HttpClient { HeadMethod head = new HeadMethod(uri.toString()); try { returnCode = client.executeMethod(head); + } catch (HttpException e) { + Log.e(TAG, "HTTP exception trying to login at " + uri.getEncodedPath(), e); + } catch (IOException e) { + Log.e(TAG, "I/O exception trying to login at " + uri.getEncodedPath(), e); } catch (Exception e) { - Log.e(TAG, "Error: " + e.getMessage()); + Log.e(TAG, "Unexpected exception trying to login at " + uri.getEncodedPath(), e); } return returnCode; } @@ -248,6 +293,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; + } + } /**