在tomcat 6上部署了一个Web服务,并通过apache-cxf 2.3.3公开。使用wsdl2java生成的源存根可以调用此服务。
在我发出大要求(~1Mb)之前,事情似乎很好。此请求未得到处理,并且异常失败:
Interceptor for {http://localhost/}ResourceAllocationServiceSoapService has thrown
exception, unwinding now org.apache.cxf.binding.soap.SoapFault:
Error reading XMLStreamReader.
...
com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]
这里有某种最大请求长度,我完全坚持了它。
答案 0 :(得分:3)
Vladimir's suggestion工作了。下面的代码将帮助其他人了解放置1000000的位置。
public void handleMessage(SoapMessage message) throws Fault {
// Get message content for dirty editing...
InputStream inputStream = message.getContent(InputStream.class);
if (inputStream != null)
{
String processedSoapEnv = "";
// Cache InputStream so it can be read independently
CachedOutputStream cachedInputStream = new CachedOutputStream(1000000);
try {
IOUtils.copy(inputStream,cachedInputStream);
inputStream.close();
cachedInputStream.close();
InputStream tmpInputStream = cachedInputStream.getInputStream();
try{
String inputBuffer = "";
int data;
while((data = tmpInputStream.read()) != -1){
byte x = (byte)data;
inputBuffer += (char)x;
}
/**
* At this point you can choose to reformat the SOAP
* envelope or simply view it just make sure you put
* an InputStream back when you done (see below)
* otherwise CXF will complain.
*/
processedSoapEnv = fixSoapEnvelope(inputBuffer);
}
catch(IOException e){
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Re-set the SOAP InputStream with the new envelope
message.setContent(InputStream.class,new ByteArrayInputStream( processedSoapEnv.getBytes()));
/**
* If you just want to read the InputStream and not
* modify it then you just need to put it back where
* it was using the CXF cached inputstream
*
* message.setContent(InputStream.class,cachedInputStream.getInputStream());
*/
}
}
答案 1 :(得分:2)
我弄清楚出了什么问题。实际上它是拦截器代码中的错误:
CachedOutputStream requestStream = new CachedOutputStream()
当我用
替换它时 CachedOutputStream requestStream = new CachedOutputStream(1000000);
事情开始正常。
因此,在复制流时,请求只是被中断了。
答案 2 :(得分:2)
在使用CachedOutputStream类时,我遇到了同样的问题“com.ctc.wstx.exc.WstxEOFException:prolog中的意外EOF”。
查看CachedOutputStream类的来源,阈值用于在“从内存中”存储流的数据到“文件”之间切换。
假设流对超过阈值的数据进行操作,它将存储在一个文件中,因此代码将会中断
IOUtils.copy(inputStream,cachedInputStream);
inputStream.close();
cachedInputStream.close(); //closes the stream, the file on disk gets deleted
InputStream tmpInputStream = cachedInputStream.getInputStream(); //returned tmpInputStream is brand *empty* one
// ... reading tmpInputStream here will produce WstxEOFException
增加'阈值'确实有帮助,因为所有流数据都存储在内存中,并且在这种情况下调用cachedInputStream.close()并不真正关闭底层流实现,因此以后仍然可以从中读取它。
以上是上述代码的“固定”版本(至少它对我来说毫无例外)
IOUtils.copy(inputStream,cachedInputStream);
inputStream.close();
InputStream tmpInputStream = cachedInputStream.getInputStream();
cachedInputStream.close();
// reading from tmpInputStream here works fine
在tmpInputStream上调用close()并且没有更多其他引用时,临时文件被删除,请参阅CachedOutputStream.maybeDeleteTempFile()的源代码