X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/9d3208ea0372294f606df5ddb543b2062d1eacac..6a9eaaf9aa6ce01ed788d057c56face20fa88dc3:/src/eu/alefzero/webdav/WebdavClient.java?ds=sidebyside diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 7194195c..1135dafa 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -137,24 +137,21 @@ 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; 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; @@ -166,11 +163,25 @@ 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); + + } catch (IOException e) { + Log.e(TAG, "I/O exception downloading " + remoteFilepath, e); + + } catch (Exception e) { + Log.e(TAG, "Unexpected exception downloading " + remoteFilepath, e); + + } finally { + if (!ret) { + if (status >= 0) { + Log.e(TAG, "Download of " + remoteFilepath + " to " + targetFile + " failed with HTTP status " + status); + } + if (targetFile.exists()) { + targetFile.delete(); + } + } } - return ret; } @@ -203,32 +214,34 @@ public class WebdavClient extends HttpClient { * @param contentType MIME type of the file. * @return 'True' then the upload was successfully completed */ - public boolean putFile(String localFile, String remoteTarget, - String contentType) { + public boolean putFile(String localFile, String remoteTarget, String contentType) { boolean result = false; + int status = -1; try { - Log.e("ASD", contentType + ""); File f = new File(localFile); FileRequestEntity entity = new FileRequestEntity(f, contentType); entity.setOnDatatransferProgressListener(mDataTransferListener); - Log.e("ASD", f.exists() + " " + entity.getContentLength()); PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget)); put.setRequestEntity(entity); - Log.d(TAG, "" + put.getURI().toString()); - int status = executeMethod(put, 0); - Log.d(TAG, "PUT method return with status " + status); - - if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT) { - result = true; - Log.i(TAG, "Uploading, done"); - } + status = executeMethod(put); - } catch (final Exception e) { - Log.i(TAG, "" + e.getMessage()); - result = false; - } + result = (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT); + + Log.d(TAG, "PUT response for " + remoteTarget + " finished with HTTP status " + status); + + } catch (HttpException e) { + Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e); + } catch (IOException e) { + Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e); + + } catch (Exception e) { + Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e); + } + + if (!result && status >= 0) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status); + return result; } @@ -246,8 +259,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; } @@ -259,17 +276,29 @@ public class WebdavClient extends HttpClient { * @return 'True' when the directory is successfully created */ public boolean createDirectory(String path) { + boolean result = false; + int status = -1; try { MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path)); - int status = executeMethod(mkcol); - Log.d(TAG, "Status returned " + status); - Log.d(TAG, "uri: " + mkcol.getURI().toString()); - Log.i(TAG, "Creating dir completed"); - } catch (final Exception e) { - e.printStackTrace(); - return false; + Log.d(TAG, "Creating directory " + path); + status = executeMethod(mkcol); + Log.d(TAG, "Status returned: " + status); + result = mkcol.succeeded(); + + } catch (HttpException e) { + Log.e(TAG, "HTTP exception creating directory " + path, e); + + } catch (IOException e) { + Log.e(TAG, "I/O exception creating directory " + path, e); + + } catch (Exception e) { + Log.e(TAG, "Unexpected exception creating directory " + path, e); + } - return true; + if (!result && status >= 0) { + Log.e(TAG, "Creation of directory " + path + " failed with HTTP status " + status); + } + return result; }