Soap Header(序言中出现意外的 EOF)

Soap Header (Unexpected EOF in prolog)

我必须将 Soap header 添加到 web-service 响应中。

我尝试了几种方法,首先是:

public SendFileToAlfaOutParms sendFileToAlfa(SendFileToAlfaInParms inParms)
            throws MsgWSException {
        logger.trace(LogMarkers.IN, "sendFileToAlfa(\ninParms={})", inParms);

        List<Header> headers = (List<Header>) wsContext.getMessageContext().get(Header.HEADER_LIST);
        headers.add(new Header(new QName("http://WSBigFilesWEBService.WEB.ws.bf.sbrf.ru", "SendFileToAlfaAntiVirusStatus"), "TEST"));
        wsContext.getMessageContext().put(Header.HEADER_LIST, headers);

        try {
            InputStream fileContent = fileService.download(inParms.getTransferJobGUID(), inParms.getChunkIdx());
            SendFileToAlfaOutParms outParms = new SendFileToAlfaOutParms();
            outParms.setFileContent(new DataHandler(new InputStreamDataSource(fileContent)));
            logger.trace(LogMarkers.OUT, "sendFileToAlfa\n{}", outParms);
            return outParms;
        } catch (Exception e) {
            logger.error("Exception in sendFileToAlfa", e);
            throw new MsgWSException(e.getMessage(), ExceptionHandlingHelper.convertToExceptionInfo(e));
        }
    }

第二个是通过 cxf OutInteceptor:

public void handleMessage(SoapMessage message) throws Fault {
    List<Header> list = message.getHeaders();
    QName q = new QName("http://WSBigFilesWEBService.WEB.ws.bf.sbrf.ru", "SendFileToAlfaAntiVirusStatus");
    SoapHeader header = new SoapHeader(q, "TEST");
    list.add(header);
}

因此,两种方法都会导致异常:

2015-07-15 17:59:24,719 WARN :[APP] [60069702-34c1-4d4b-9e31-a7282860c10a||DefaultMessageListenerContainer-2] o.a.cxf.phase.PhaseInterceptorChain - []Interceptor for {http://WSBigFilesWEBService.WEB.ws.bf.sbrf.ru}WSBigFilesWEBService#{http://WSBigFilesWEBService.WEB.ws.bf.sbrf.ru}SendFileToAlfa has thrown exception, unwinding now
org.apache.cxf.binding.soap.SoapFault: Error reading XMLStreamReader: Unexpected EOF in prolog
 at [row,col {unknown-source}]: [1,0]
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:259) ~[cxf-rt-bindings-soap-3.0.1.jar:3.0.1]
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:62) ~[cxf-rt-bindings-soap-3.0.1.jar:3.0.1]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:798) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1636) [cxf-rt-transports-http-3.0.1.jar:3.0.1]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1525) [cxf-rt-transports-http-3.0.1.jar:3.0.1]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1330) [cxf-rt-transports-http-3.0.1.jar:3.0.1]
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:638) [cxf-rt-transports-http-3.0.1.jar:3.0.1]
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:423) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:326) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:279) [cxf-core-3.0.1.jar:3.0.1]
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96) [cxf-rt-frontend-simple-3.0.1.jar:3.0.1]
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:137) [cxf-rt-frontend-jaxws-3.0.1.jar:3.0.1]
    at com.sun.proxy.$Proxy112.sendFileToAlfa(Unknown Source) [na:na]
    at ru.sbrf.bf.alfa.io.ws.client.FileServiceStub.download(FileServiceStub.java:82) [classes/:na]
    at ru.sbrf.bf.alfa.services.impl.TransferJobServiceImpl.fileTransferFromDMZToAlfa(TransferJobServiceImpl.java:251) [classes/:na]
    at ru.sbrf.bf.alfa.services.impl.TransferJobServiceImpl$$FastClassBySpringCGLIB$7c8f5f.invoke(<generated>) [spring-core-4.1.0.RELEASE.jar:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) [spring-aop-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:98) [spring-tx-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266) [spring-tx-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) [spring-tx-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) [spring-aop-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at ru.sbrf.bf.alfa.services.impl.TransferJobServiceImpl$$EnhancerBySpringCGLIB$$ea1aa57c.fileTransferFromDMZToAlfa(<generated>) [spring-core-4.1.0.RELEASE.jar:na]
    at ru.sbrf.bf.alfa.events.jms.listeners.TransferDMZToAlphaListener.handleMessage(TransferDMZToAlphaListener.java:50) [classes/:na]
    at ru.sbrf.bf.alfa.events.jms.listeners.TransferDMZToAlphaListener.handleMessage(TransferDMZToAlphaListener.java:28) [classes/:na]
    at ru.sbrf.bf.alfa.io.jms.handler.impl.JmsMessageHandler.handleMessage(JmsMessageHandler.java:27) [big-files-app-jms-0.2.3.jar:na]
    at ru.sbrf.bf.alfa.io.jms.listener.impl.JmsCommonMessageListener.onMessage(JmsCommonMessageListener.java:42) [big-files-app-jms-0.2.3.jar:na]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:685) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:623) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:591) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:308) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:246) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1137) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1129) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1031) [spring-jms-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_71]
Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
 at [row,col {unknown-source}]: [1,0]
    at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:685) ~[woodstox-core-asl-4.4.0.jar:4.4.0]
    at com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2141) ~[woodstox-core-asl-4.4.0.jar:4.4.0]
    at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2047) ~[woodstox-core-asl-4.4.0.jar:4.4.0]
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131) ~[woodstox-core-asl-4.4.0.jar:4.4.0]
    at com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamReader.java:1154) ~[woodstox-core-asl-4.4.0.jar:4.4.0]
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:158) ~[cxf-rt-bindings-soap-3.0.1.jar:3.0.1]
    ... 42 common frames omitted

为什么添加 header 会导致序言中出现一些错误?

我找到了解决方案:

http://cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest/response?

原因是缺少 DataBinding。

List<Header> headers = new ArrayList<Header>();
Header dummyHeader = new Header(new QName("antiVirusStatus"), "clean", new JAXBDataBinding(String.class));
headers.add(dummyHeader);
wsContext.getMessageContext().put(Header.HEADER_LIST, headers);