在 java 中调用 WebDAV propFindMethod 时出现异常

Getting Exception while calling WebDAV propFindMethod in java

低于异常::

WARN main [DavDocumentBuilderFactory.createFactory] - Secure XML processing is not supported
java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
    at org.apache.jackrabbit.webdav.xml.DavDocumentBuilderFactory.createFactory(DavDocumentBuilderFactory.java:50)
    at org.apache.jackrabbit.webdav.xml.DavDocumentBuilderFactory.<init>(DavDocumentBuilderFactory.java:39)
    at org.apache.jackrabbit.webdav.xml.DomUtil.<clinit>(DomUtil.java:60)
    at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:203)
    at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:72)
    at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:61)
    at com.spero.poc.WebDavClient.listOfFilesFromWebDav(WebDavClient.java:108)
    at com.spero.poc.WebDavClient.main(WebDavClient.java:92)

Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:377)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:131)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:684)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:728)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:343)
    at org.apache.jackrabbit.webdav.xml.DomUtil.transformDocument(DomUtil.java:805)
    at org.apache.jackrabbit.webdav.client.methods.XmlRequestEntity.<init>(XmlRequestEntity.java:43)
    at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:193)
    at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:205)
    at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:72)
    at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:61)
    at com.spero.poc.WebDavClient.listOfFilesFromWebDav(WebDavClient.java:109)
    at com.spero.poc.WebDavClient.main(WebDavClient.java:92)

这是我的代码::

package com.spero.poc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;

public class WebDavClient {
    
    static HttpClient client = null;

    public static void main(String[] args) {
//      String uri = "https://www.webdavserver.com/Userab71566/Library/Content.txt";
        String uri = "https://www.webdavserver.com/Userab71566/Library/";
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setHost("https://www.webdavserver.com");
        
        HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        int maxHostConnections = 20;
        params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
        params.setConnectionTimeout(60000);
        connectionManager.setParams(params);
        HttpClientParams cParams = new HttpClientParams();
        cParams.setSoTimeout(60000);
        
//      Create the HttpClient object and eventually pass the Credentials if required:
        client = new HttpClient(cParams,connectionManager);
        client.setHostConfiguration(hostConfig);
//        Credentials creds = new UsernamePasswordCredentials("userId", "pw");
//        client.getState().setCredentials(AuthScope.ANY, creds);
        
        try {
            WebDavClient test = new WebDavClient();
            test.listOfFilesFromWebDav(uri);
              
         
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            client = null;
        }
        if(client == null)
            System.out.println("Client is in null state");
        
    }
    
    private List<String> listOfFilesFromWebDav(String folderUrl) {
        try {
            PropFindMethod pFind = new PropFindMethod(folderUrl, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1); 
            pFind.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            client.executeMethod(pFind); 
            List<String> files = new ArrayList<>();
            MultiStatus multiStatus = pFind.getResponseBodyAsMultiStatus(); 
            MultiStatusResponse[] responses = multiStatus.getResponses(); 
 
            for (MultiStatusResponse response : responses) { 
                if (response.getHref().endsWith(".txt")) { 
                    System.out.println("Data::"+response.getHref());
                } 
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DavException e) {
            e.printStackTrace();
        } 
        return null;
    }

}

Jackrabbit 是什么版本?

查看源代码我看到异常实际上被捕获并记录了,系统应该继续。

https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DavDocumentBuilderFactory.java#L43

你真的在代码中遇到异常吗?

感谢您的建议,我已经查看了日志并找出了不匹配的 class 和 jars。我已将以下系统设置添加到我的代码和 Jar 中。

我们不得不求助于来自您的代码的 System.setProperty(...) 调用,例如:

//this would enforce the DOM implementation to be Xerces
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
//this would enforce the SAX implementation to be Xerces
System.setProperty("javax.xml.parsers.SAXParserFactory","org.apache.xerces.jaxp.SAXParserFactoryImpl");
//this would enforce the XSLT implementation to be Xalan
System.setProperty("javax.xml.transform.TransformerFactory","org.apache.xalan.processor.TransformerFactoryImpl");

相应的 JRE 实现 class 是:

com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl