private boolean tryConnection(WebdavClient wc, String urlSt) {
    boolean retval = false;
    GetMethod get = null;
    try {
      get = new GetMethod(urlSt);
      int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
      String response = get.getResponseBodyAsString();
      if (status == HttpStatus.SC_OK) {
        JSONObject json = new JSONObject(response);
        if (!json.getBoolean("installed")) {
          mLatestResult =
              new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
        } else {
          mOCVersion = new OwnCloudVersion(json.getString("version"));
          if (!mOCVersion.isVersionValid()) {
            mLatestResult =
                new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);

          } else {
            mLatestResult =
                new RemoteOperationResult(
                    urlSt.startsWith("https://")
                        ? RemoteOperationResult.ResultCode.OK_SSL
                        : RemoteOperationResult.ResultCode.OK_NO_SSL);

            retval = true;
          }
        }

      } else {
        mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
      }

    } catch (JSONException e) {
      mLatestResult =
          new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);

    } catch (Exception e) {
      mLatestResult = new RemoteOperationResult(e);

    } finally {
      if (get != null) get.releaseConnection();
    }

    if (mLatestResult.isSuccess()) {
      Log_OC.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());

    } else if (mLatestResult.getException() != null) {
      Log_OC.e(
          TAG,
          "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(),
          mLatestResult.getException());

    } else {
      Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
    }

    return retval;
  }
  @Override
  protected RemoteOperationResult run(WebdavClient client) {
    RemoteOperationResult result = null;

    // code before in FileSyncAdapter.fetchData
    PropFindMethod query = null;
    try {
      Log.d(TAG, "Synchronizing " + mAccount.name + ", fetching files in " + mRemotePath);

      // remote request
      query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
      int status = client.executeMethod(query);

      // check and process response   - /// TODO take into account all the possible status per
      // child-resource
      if (isMultiStatus(status)) {
        MultiStatus resp = query.getResponseBodyAsMultiStatus();

        // synchronize properties of the parent folder, if necessary
        if (mParentId == DataStorageManager.ROOT_PARENT_ID) {
          WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath());
          OCFile parent = fillOCFile(we);
          parent.setParentId(mParentId);
          mStorageManager.saveFile(parent);
          mParentId = parent.getFileId();
        }

        // read contents in folder
        List<OCFile> updatedFiles = new Vector<OCFile>(resp.getResponses().length - 1);
        for (int i = 1; i < resp.getResponses().length; ++i) {
          WebdavEntry we = new WebdavEntry(resp.getResponses()[i], client.getBaseUri().getPath());
          OCFile file = fillOCFile(we);
          file.setParentId(mParentId);
          OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
          if (oldFile != null) {
            if (oldFile.keepInSync()
                && file.getModificationTimestamp() > oldFile.getModificationTimestamp()) {
              disableObservance(
                  file); // first disable observer so we won't get file upload right after download
              requestContentDownload(file);
            }
            file.setKeepInSync(oldFile.keepInSync());
          }

          updatedFiles.add(file);
        }

        // save updated contents in local database; all at once, trying to get a best performance in
        // database update (not a big deal, indeed)
        mStorageManager.saveFiles(updatedFiles);

        // removal of obsolete files
        mChildren = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId));
        OCFile file;
        String currentSavePath = FileDownloader.getSavePath(mAccount.name);
        for (int i = 0; i < mChildren.size(); ) {
          file = mChildren.get(i);
          if (file.getLastSyncDate() != mCurrentSyncTime) {
            Log.d(TAG, "removing file: " + file);
            mStorageManager.removeFile(
                file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath)));
            mChildren.remove(i);
          } else {
            i++;
          }
        }

      } else {
        client.exhaustResponse(query.getResponseBodyAsStream());
      }

      // prepare result object
      result = new RemoteOperationResult(isMultiStatus(status), status);
      Log.i(
          TAG,
          "Synchronizing "
              + mAccount.name
              + ", folder "
              + mRemotePath
              + ": "
              + result.getLogMessage());

    } catch (Exception e) {
      result = new RemoteOperationResult(e);
      Log.e(
          TAG,
          "Synchronizing "
              + mAccount.name
              + ", folder "
              + mRemotePath
              + ": "
              + result.getLogMessage(),
          result.getException());

    } finally {
      if (query != null)
        query.releaseConnection(); // let the connection available for other methods
    }

    return result;
  }
Exemplo n.º 3
0
  @Override
  protected RemoteOperationResult run(WebdavClient client) {
    AccountManager accountMngr = AccountManager.get(mContext);
    String statUrl = accountMngr.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
    statUrl += AccountUtils.STATUS_PATH;
    RemoteOperationResult result = null;
    GetMethod get = null;
    try {
      get = new GetMethod(statUrl);
      int status = client.executeMethod(get);
      if (status != HttpStatus.SC_OK) {
        client.exhaustResponse(get.getResponseBodyAsStream());
        result = new RemoteOperationResult(false, status);

      } else {
        String response = get.getResponseBodyAsString();
        if (response != null) {
          JSONObject json = new JSONObject(response);
          if (json != null && json.getString("version") != null) {
            OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version"));
            if (ocver.isVersionValid()) {
              accountMngr.setUserData(
                  mAccount, AccountAuthenticator.KEY_OC_VERSION, ocver.toString());
              Log.d(TAG, "Got new OC version " + ocver.toString());
              result = new RemoteOperationResult(ResultCode.OK);

            } else {
              Log.w(
                  TAG, "Invalid version number received from server: " + json.getString("version"));
              result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
            }
          }
        }
        if (result == null) {
          result =
              new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
        }
      }
      Log.i(
          TAG,
          "Check for update of ownCloud server version at "
              + client.getBaseUri()
              + ": "
              + result.getLogMessage());

    } catch (JSONException e) {
      result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
      Log.e(
          TAG,
          "Check for update of ownCloud server version at "
              + client.getBaseUri()
              + ": "
              + result.getLogMessage(),
          e);

    } catch (Exception e) {
      result = new RemoteOperationResult(e);
      Log.e(
          TAG,
          "Check for update of ownCloud server version at "
              + client.getBaseUri()
              + ": "
              + result.getLogMessage(),
          e);

    } finally {
      if (get != null) get.releaseConnection();
    }
    return result;
  }