简体   繁体   中英

How to abort REST request inside a Jax-RS WriterInterceptor?

I am using a javax.ws.rs.ext.WriterInterceptor on my server as shown below to modify the REST responses generated by the server before they are sent out to the client. This works as intended but is there a way to abort the request from within aroundWriteTo?

@Provider
@ServerInterceptor
public class MyInterceptor implements WriterInterceptor {

  @Override
  public void aroundWriteTo (WriterInterceptorContext context) {

    final ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();
    final OutputStream originalStream = context.getOutputStream();
    context.setOutputStream(bufferStream);
    context.proceed();

    final String originalMsg = bufferStream.toString(Charset.defaultCharset().name());
    try {
      // not important for this example what modify does
      final String modifiedMsg = modify(originalMsg);
    } catch (Exception e) {
      //How to abort request with HTTP status 500 at this point?
    }
    originalStream.write(modifiedMsg.getBytes(Charset.defaultCharset()));
    context.setOutputStream(originalStream);
  }
}

I'm using JBoss EAS 6.4.7 GA, RestEasy 3.0.9.

The request can be aborted with an exception. Here is the example code : ContentMD5Writer.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM