lookfirst / sardine

an easy to use webdav client for java
Other
574 stars 184 forks source link

Fail when creating client on Android #336

Closed helgovic closed 2 years ago

helgovic commented 2 years ago

When i try to aquire i client i get this error

Got a deoptimization request on un-deoptimizable method org.apache.http.conn.ssl.SSLConnectionSocketFactory org.apache.http.conn.ssl.SSLConnectionSocketFactory.getSocketFactory()

My code:

package com.softmagical.libwebdav;

import static android.content.ContentValues.TAG;

import android.net.Uri;
import android.util.Log;
import com.github.sardine.DavQuota;
import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;
import com.github.sardine.impl.SardineException;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WebDav {

    private Sardine mClient = null;
    private String mHost = "";
    private String mRoot = "";
    private String mServer = "";

    public class ncQuota {
        public long quotaUsedBytes;
        public long quotaavailableBytes;
    }

    public WebDav(String userName, String passWord, String serverUrl) {

        try {

            mServer = serverUrl;
            mClient = SardineFactory.begin(userName, passWord);

            StringBuilder buildServerUrl = buildServerUrl();
            Uri mRootUri = Uri.parse(buildServerUrl.toString());
            this.mHost = buildServerUrl.substring(0, buildServerUrl.indexOf("/", 8));
            this.mRoot = mRootUri.toString();

        } catch (Exception e) {
            handleException(e);
        }
    }

    protected StringBuilder buildServerUrl() {
        String appendPathSegment = null;
        int port = 0;
        String str;
        StringBuilder stringBuilder = new StringBuilder();
        Uri parse = Uri.parse(mServer);
        String scheme = parse.getScheme();
        if (scheme != null) {
            appendPathSegment = parse.getHost() + parse.getPath();
            port = parse.getPort();
            if (port <= 0) {
                port = 443;
            }
        }
        stringBuilder.append(scheme).append("://").append(appendPathSegment);
        if (!(port == 80 || port == 443 || port == 0)) {
            int indexOf = stringBuilder.indexOf("/", 8);
            if (indexOf < 0) {
                stringBuilder.append(":").append(port);
            } else {
                stringBuilder.insert(indexOf, ":" + port);
            }
        }

        String path = stringBuilder.toString();

        ensureEndsWithSlash(stringBuilder);
        if (!path.isEmpty()) {
            str = Uri.encode(path, "/");
            if (str.charAt(0) == '/') {
                stringBuilder.append(str.substring(1));
            } else {
                stringBuilder.append(str);
            }
        }
        ensureEndsWithSlash(stringBuilder);
        return stringBuilder;
    }

    static void ensureEndsWithSlash(StringBuilder stringBuilder) {
        if (stringBuilder.charAt(stringBuilder.length() - 1) != '/') {
            stringBuilder.append("/");
        }
    }

    private String buildFileUrl(String str, boolean z) {
        StringBuilder stringBuilder = new StringBuilder(this.mRoot);
        stringBuilder.append(Uri.encode(str.substring(1), "/"));
        if (!"/".equals(str) && z) {
            stringBuilder.append("/");
        }
        return stringBuilder.toString();
    }

    private String getUrlForFile(String name, Boolean folder) {
        return buildFileUrl(name, folder);
    }

    public List<NCFileEntry> list(String fileName, FileFilter fileFilter) {
        int i = 0;
        try {
            String decode;
            DavResource davResource;
            String identity = "";
            List<DavResource> list = this.mClient.list(fileName);
            if (list.size() > 0) {
                decode = Uri.decode(identity);
                while (true) {
                    int i2 = i;
                    if (i2 >= list.size()) {
                        break;
                    } else if (isSelf(decode, ((DavResource) list.get(i2)).getHref().getPath())) {
                        davResource = (DavResource) list.remove(i2);
                        break;
                    } else {
                        i = i2 + 1;
                    }
                }
            }

            List<NCFileEntry> arrayList = new ArrayList<NCFileEntry>(list.size());

            for (DavResource davResource2 : list) {
                NCFileEntry tmpEntry = new NCFileEntry();
                tmpEntry.name = davResource2.getName();
                tmpEntry.path = fileName + "/" + tmpEntry.name;
                tmpEntry.modifiedTime = davResource2.getModified().getTime();
                tmpEntry.folder = davResource2.isDirectory();
                tmpEntry.size = davResource2.getContentLength();
                arrayList.add(tmpEntry);
            }
            return arrayList;

        } catch (IOException e) {
            Log.d(TAG, "listImpl: " + e.getStackTrace());
            ;
            return null;
        }
    }

    private boolean isSelf(String str, String str2) {
        if (str2.startsWith("http")) {
            return str.equals(str2);
        }
        return str.substring(this.mHost.length()).equals(str2);
    }

    public InputStream read(String fileName, long j) {
        Map<String, String> hashMap = new HashMap<String, String>();
        if (j > 0) {
            hashMap.put("Range", "bytes=" + j + "-");
        }
        try {
            return this.mClient.get(fileName, hashMap);
        } catch (IOException e) {
            handleException(e);
            return null;
        }
    }

    public boolean write(InputStream inputStream, String fileName, long size) {
        try {
            String urlForFile = getUrlForFile(fileName, false);
            this.mClient.put(urlForFile, inputStream, MimeTypes.getInstance().getType(fileName, false), false, size);
            return true;
        } catch (IOException e) {
            handleException(e);
            return false;
        }
    }

    public boolean mkdir(String fileName) {
        String buildFileUrl = buildFileUrl(fileName, true);
        try {
            this.mClient.createDirectory(buildFileUrl);
            return true;
        } catch (IOException e) {
            handleException(e);
            return false;
        }
    }

    public boolean mkfile(String fileName) {
        String buildFileUrl = buildFileUrl(fileName, false);
        try {
            this.mClient.put(buildFileUrl, new byte[0]);
            return true;
        } catch (IOException e) {
            handleException(e);
            return false;
        }
    }

    public boolean copy(String fileName, String fileName2) {
        String urlForFile = getUrlForFile(fileName2, false);
        try {
            this.mClient.copy(fileName, urlForFile);
            return true;
        } catch (IOException e) {
            handleException(e);
            return false;
        }
    }

    public boolean move(String fileName, String fileName2) {
        String urlForFile = getUrlForFile(fileName2, false);
        try {
            this.mClient.move(fileName, urlForFile);
            return true;
        } catch (IOException e) {
            handleException(e);
            return false;
        }
    }

    public boolean rename(String fileName, String fileName2, boolean folder) {
        String buildFileUrl = buildFileUrl(fileName2, folder);
        try {
            this.mClient.move(fileName, buildFileUrl);
            return true;
        } catch (IOException e) {
            handleException(e);
            return false;
        }
    }

    public void delete(String fileName) {
        try {
            this.mClient.delete(fileName);
        } catch (IOException e) {
            handleException(e);
        }
    }

    public NCFileEntry getFileInstance(String fileName, boolean folder) {
        String buildFileUrl = buildFileUrl(fileName, folder);
        Uri parse = Uri.parse(buildFileUrl);
        try {
            if (this.mClient.exists(buildFileUrl)) {
                for (DavResource davResource : this.mClient.list(buildFileUrl)) {
                    if (davResource.getPath().equals(parse.getPath())) {
                        NCFileEntry tmpEntry = new NCFileEntry();
                        tmpEntry.name = davResource.getName();
                        tmpEntry.path = fileName + "/" + tmpEntry.name;
                        tmpEntry.modifiedTime = davResource.getModified().getTime();
                        tmpEntry.folder = davResource.isDirectory();
                        tmpEntry.size = davResource.getContentLength();
                        return tmpEntry;
                    }
                }
            }
        } catch (IOException e) {
            if (!(e instanceof SardineException)) {
                handleException(e);
                return null;
            }
        }

        return null;

    }

    public ncQuota getQuota() {
        try {
            DavQuota quota = this.mClient.getQuota(buildFileUrl(mHost, true));
            ncQuota tmpQuota = new ncQuota();
            tmpQuota.quotaavailableBytes = quota.getQuotaAvailableBytes();
            tmpQuota.quotaUsedBytes = quota.getQuotaUsedBytes();
            return tmpQuota;
        } catch (IOException e) {
            handleException(e);
            return null;
        }
    }

    public void handleException(Exception exception) {
        Log.d(TAG, Arrays.toString(exception.getStackTrace()));

    }
}

My build.gradle


plugins {
    id 'com.android.library'
}

android {
    compileSdk 29

    defaultConfig {
        minSdk 21
        targetSdk 29
        versionCode 1
        versionName "1.0"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'com.github.lookfirst:sardine:5.10'
//    implementation files('libs\\httpclientandroidlib-1.2.0.jar')
    implementation 'com.nanohttpd:nanohttpd-webserver:2.2.0'
    implementation 'androidx.annotation:annotation:1.2.0'

}

```Any clues?