If your requirement is to get the size of the response message and
return a meaningful soap fault when the message size exceeds some
limit, I think it is not simple to do it in general cases without
wasting too much resource.

If you can live with some IO exception instead of some soap fault, you
could wrap the output stream with your filtered output stream so that
when someone writes to the output stream, you can count the written
bytes while writing to the underlining output stream. When the size
exceeds the limit, you can throw an IOException. In this case,  you
don't waste memory by buffering the message as in your example.

If you need some soap fault, you can extend this approach by buffering
the output bytes in your output stream up to the limit or until the
stream is closed. If the stream is closed before the limit is reached,
you can flush the buffered bytes to the output stream. Otherwise, you
flush the output stream with your predefined soap fault bytes.

If your application/service knows its message size, it would be easier
to thrown an exception there to create some fault.

2012/9/11 shadowlaw <[email protected]>:
> foud this code which respond to my need, but when throwing the exception i
> get the response which doen't make sens. Normally i would get a fault
> message.
>
> public class ResponseContentLengthValidator extends
> AbstractPhaseInterceptor<Message> {
>
>     private final static Logger log =
> Logger.getLogger(ResponseContentLengthValidator.class);
>
>     public static final int MAX_SOAP_MESSAGE_SIZE = 2 * 1024 * 1024;
>
>     final char[] buffer = new char[MAX_SOAP_MESSAGE_SIZE];
>
>     /**
>      * @deprecated
>      */
>     public ResponseContentLengthValidator() {
> //        super(Phase.PRE_STREAM);
>         super(Phase.PRE_STREAM);
>         log.debug("ResponseContentLengthValidator Started");
>         getAfter().add(SAAJOutInterceptor.class.getName());
>     }
>
>     /**
>      * Intercepts a message.
>      * Interceptors should NOT invoke handleMessage or handleFault
>      * on the next interceptor - the interceptor chain will
>      * take care of this.
>      *
>      * @param message
>      */
>     public void handleMessage(Message message) throws Fault {
>
>         log.debug("ResponseContentLengthValidator.handleMessage(..)");
>
>         OutputStream os = message.getContent(OutputStream.class);
>         CacheAndWriteOutputStream cwos = new CacheAndWriteOutputStream(os);
>         message.setContent(OutputStream.class, cwos);
>
>         cwos.registerCallback(new LoggingOutCallBack());
>     }
>
>     class LoggingOutCallBack implements CachedOutputStreamCallback {
>         public void onClose(CachedOutputStream cos) {
>             try {
>                 if (cos != null) {
>                     final int messageSize =
> IOUtils.toString(cos.getInputStream()).getBytes().length;
>                     if (messageSize > MAX_SOAP_MESSAGE_SIZE) {
>                         if (log.isDebugEnabled()) {
>                             log.debug("Message is Under 2M");
>                         }
>                     } else {
> //                        throw new Fault(new ServiceException("Max Size
> Reached"));
>                         throw new RuntimeException("Max Size Reached");
>                     }
>                 }
>
>             } catch (Exception e) {
>                 // TODO Auto-generated catch block
>                 e.printStackTrace();
>             }
>         }
>
>         public void onFlush(CachedOutputStream arg0) {
>         }
>     }
> }
>
>
>
>
>
> --
> View this message in context: 
> http://cxf.547215.n5.nabble.com/Caculate-SOAP-response-size-tp5713783p5713785.html
> Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to