Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 85a86e7f authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

fix decompress gzip failed propfind

parent b0ff4872
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -42,6 +42,37 @@ public class GzipedPropfind extends PropFindMethod {
        super(uri);
    }


    public boolean isResponseGzipped(){
        Header contentEncodingHeader= getResponseHeader("Content-Encoding");

        if(contentEncodingHeader != null && contentEncodingHeader.getValue().contains("gzip") ) {
            return true;
        }
        return false;
    }


    public InputStream getDecompressedResponseBodyasStream() throws IOException{
        if(!isResponseGzipped() ) return super.getResponseBodyAsStream();

        //@TOdo: look for potential optimisation like pipe gzipInputStream directly in InputStream in
        StringBuilder s = new StringBuilder();
        String line;
        try{
            GZIPInputStream gzipis = new GZIPInputStream(getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gzipis));

            while ((line = bufferedReader.readLine()) != null) {
                s.append(line);
            }
            bufferedReader.close();
            return new ByteArrayInputStream(s.toString().getBytes());
        }catch(IOException e){
            throw e;
        }
    }

    /*
     * Uncompress the gzipSteam before to parse it with SAX
     * In the case of no "Content-Encoding:gzip" headers is found, the
@@ -49,11 +80,8 @@ public class GzipedPropfind extends PropFindMethod {
     */
    @Override
    public Document getResponseBodyAsDocument() throws IOException {
        Header contentEncodingHeader= getResponseHeader("Content-Encoding");
        if(!isResponseGzipped() ) return super.getResponseBodyAsDocument();

        if(contentEncodingHeader != null && contentEncodingHeader.getValue().indexOf("gzip") == -1 ) {
            return super.getResponseBodyAsDocument();
        }
        //@TOdo: look for potential optimisation like pipe gzipInputStream directly in InputStream in
        StringBuilder s = new StringBuilder();
        String line;
+12 −17
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

@@ -83,33 +84,33 @@ public class LightReadFolderRemoteOperation extends RemoteOperation {
    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result = null;
        PropFindMethod query = null;
        PropFindMethod propfind = null;
        String userAgent ="";
        try {
            // remote request
            if(allowGzip){
                query = new GzipedPropfind(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
                propfind = new GzipedPropfind(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
                    WebdavUtils.getMinimumPropSet(),    // PropFind Properties
                    this.depth);
                query.setRequestHeader("Accept-Encoding", "gzip");
                propfind.setRequestHeader("Accept-Encoding", "gzip");
                userAgent = "gzipUserAgent";
            }else{
                query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
                propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
                        WebdavUtils.getAllPropSet(),    // PropFind Properties
                        this.depth);
            }

            int status = client.executeMethod(query, userAgent);
            int status = client.executeMethod(propfind, userAgent);
            // check and process response
            boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);
            
            if (isSuccess) {

                // get data from remote folder
                MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
                MultiStatus dataInServer = propfind.getResponseBodyAsMultiStatus();
                readData(dataInServer, client);
                // Result of the operation
                result = new RemoteOperationResult(true, query);
                result = new RemoteOperationResult(true, propfind);
                // Add data to the result
                if (result.isSuccess()) {
                    result.setData(mFolderAndFiles);
@@ -117,21 +118,15 @@ public class LightReadFolderRemoteOperation extends RemoteOperation {
            } else {
                // synchronization failed
                //@THis need to be check!
                if(allowGzip) {
                    GZIPInputStream gzipSteam = null;
                    gzipSteam = new GZIPInputStream(query.getResponseBodyAsStream());
                    client.exhaustGZippedResponse(gzipSteam);
                }else{
                    client.exhaustResponse(query.getResponseBodyAsStream());
                }

                result = new RemoteOperationResult(false, query);
                client.exhaustResponse( ( (GzipedPropfind) propfind).getDecompressedResponseBodyasStream() );
                result = new RemoteOperationResult(false, propfind);
            }
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
        } finally {
            if (query != null)
                query.releaseConnection();  // let the connection available for other methods
            if (propfind != null)
                propfind.releaseConnection();  // let the connection available for other methods

            if (result == null) {
                result = new RemoteOperationResult(new Exception("unknown error"));