cxf过滤器,XML太长了烂机器就会拦截失败

sunyongxing001 发布于 2014/07/03 09:44
阅读 552
收藏 0
package XXX.web.webservice.interceptor;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.log4j.Logger;

public class ArtifactOutInterceptor extends AbstractPhaseInterceptor<Message>{
	private static final Logger log = Logger.getLogger(ArtifactOutInterceptor.class);

	public ArtifactOutInterceptor() {
		//这儿使用pre_stream,意思为在流关闭之前
		super(Phase.PRE_STREAM);
	}

	public void handleMessage(Message message) {

		try {

			OutputStream os = message.getContent(OutputStream.class);

			CachedStream cs = new CachedStream();

			message.setContent(OutputStream.class, cs);

			message.getInterceptorChain().doIntercept(message);

			CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
			InputStream in = csnew.getInputStream();
			
			String xml = IOUtils.toString(in);
			
			//这里对xml做处理,处理完后同理,写回流中
			IOUtils.copy(new ByteArrayInputStream(xml.getBytes()), os);
			
			cs.close();
			os.flush();

			message.setContent(OutputStream.class, os);


		} catch (Exception e) {
			log.error("Error when split original inputStream. CausedBy : " + "\n" + e);
		}
	}

	private class CachedStream extends CachedOutputStream {

		public CachedStream() {

			super();

		}

		protected void doFlush() throws IOException {

			currentStream.flush();

		}

		protected void doClose() throws IOException {

		}

		protected void onWrite() throws IOException {

		}

	}

}

源代码就是这样的。只要XML太长太大了就或失败!请大师指点!

加载中
0
防腐基
楼主,请问你解决了吗,在下最近工作中也遇到了相同的问题。求指教/(ㄒoㄒ)
0
亚Asia
楼主,请问你解决了吗?跪求指教
返回顶部
顶部