在jax-ws中从soap响应中删除无效字符

aemubtdh  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(353)

我正在尝试使用sp webservice获取sharepoint网站上的列表。

Lists listsSevice = new Lists(new URL(spSiteURL + "/_vti_bin/Lists.asmx?wsdl"));
listsSevice.setHandlerResolver(new SPHandlerResolver());
spListsServiceIfx = listsSevice.getListsSoap();
// Calling the List Web Service
GetListItemsResponse.GetListItemsResult result = spListsServiceIfx .getListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, webID);

但是,我得到这个错误是因为soap响应中存在一些无效字符。

com.sun.xml.ws.encoding.soap.DeserializationException: Failed to read a response: javax.xml.bind.UnmarshalException
- with linked exception:
[com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character (code 0x15 at [row,col {unknown-source}]: [1125,122]]

我试图修改soapmessage以从响应中删除无效字符。

public class SOAPMessageHandler implements SOAPHandler<SOAPMessageContext> {

public boolean handleMessage(SOAPMessageContext smc) {
    System.out.println("in handleMessage");
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    System.out.println("outboundProperty: " + outboundProperty);

    try {
        if (outboundProperty.booleanValue()) {
            System.out.println(" SOAP Request ");
        } else {
            System.out.println(" SOAP Response ");
            SOAPMessage message = smc.getMessage();
            message.writeTo(System.out);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            message.writeTo(out);
            String messageAsString = new String(out.toByteArray());
            /*smc.setMessage(new SOAPMessage( 
            stripNonValidXMLCharacters(message.getSOAPPart().toString())));*/
        }

    } catch (SOAPException e) {         
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("in soap msg handler..." + e.getMessage());
        e.printStackTrace();
    }
    System.out.println(""); 

    return true;
}

但我有个例外

SOAPMessage message = smc.getMessage();

堆栈跟踪是:

javax.xml.ws.WebServiceException: javax.xml.soap.SOAPException: org.xml.sax.SAXParseException; lineNumber: 1125; columnNumber: 122; Illegal character entity: expansion character (code 0x15
at [row,col {unknown-source}]: [1125,122]
at com.sun.xml.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:86)
at com.cah.ecm.sharepoint.migrator.util.SOAPMessageHandler.handleMessage(SOAPMessageHandler.java:25)
at com.cah.ecm.sharepoint.migrator.util.SOAPMessageHandler.handleMessage(SOAPMessageHandler.java:1)
at com.sun.xml.ws.handler.HandlerProcessor.callHandleMessageReverse(HandlerProcessor.java:341)
at com.sun.xml.ws.handler.HandlerProcessor.callHandlersResponse(HandlerProcessor.java:214)
at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnResponse(ClientSOAPHandlerTube.java:163)
at com.sun.xml.ws.handler.HandlerTube.processResponse(HandlerTube.java:164)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:651)
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600)
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585)
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482)
at com.sun.xml.ws.client.Stub.process(Stub.java:323)
at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:161)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:113)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:93)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:144)
at com.sun.proxy.$Proxy28.getListItems(Unknown Source)
at com.cah.ecm.sharepoint.migrator.sharepoint.client.SharepointClient.getListItemNodes(SharepointClient.java:292)
at com.cah.ecm.sharepoint.migrator.sharepoint.client.SharepointClient.getListItems(SharepointClient.java:389)
at com.cah.ecm.sp.jde.main.TestIterateAllSPFiles.main(TestIterateAllSPFiles.java:35)

请帮助我在哪里我错了,如果有一个替代的方法来删除无效字符从soap响应。
谢谢!

332nm8kg

332nm8kg1#

一个选择是实现你自己的 SAAJMetaFactory 这将创建自定义 MessageFactory . 这样地:

import java.io.IOException;
import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import com.sun.xml.internal.messaging.saaj.soap.SAAJMetaFactoryImpl;

public class OwnSAAJMetaFactoryImpl extends SAAJMetaFactoryImpl {
    @Override
    protected MessageFactory newMessageFactory(String protocol) throws SOAPException {
        final MessageFactory f = super.newMessageFactory(protocol);
        return new MessageFactory() {

            @Override
            public SOAPMessage createMessage(MimeHeaders headers, InputStream in) throws IOException, SOAPException {
                in = doCleaingStuff(in);
                return createMessage(headers, in);
            }

            @Override
            public SOAPMessage createMessage() throws SOAPException {
                return f.createMessage();
            }
        };
    }

    private InputStream doCleaingStuff(InputStream in) {
        // TODO implement it
        return null;
    }
}

根据saajmetafactory#getinstance()文档,您自己的saajmetafactoryImpl可以通过系统属性公开

System.setProperty("javax.xml.soap.MetaFactory", "own.package.OwnSAAJMetaFactoryImpl");

或者任何一种方式
使用javax.xml.soap.metafactory系统属性。
使用jre目录中的属性文件“lib/jaxm.properties”。此配置文件采用标准java.util.properties格式,包含实现类的完全限定名,键是上面定义的系统属性。
使用服务api(如jar规范中所述)来确定类名(如果可用)。servicesapi将在运行时可用的jars中的文件meta-inf/services/javax.xml.soap.metafactory中查找类名。
默认为com.sun.xml.messaging.saaj.soap.saajmetafactoryimpl。

相关问题