×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Seiji Sogabe
Added: Aug 4, 2015 1:02 AM
Modified: Aug 4, 2015 1:08 AM
Views: 10
Tags: resteasy
  1. DumpInterceptor interceptor = new DumpInterceptor();
  2. req.getWriterInterceptorList().add(interceptor);
  3. req.getReaderInterceptorList().add(interceptor);
  4.  
  5.  
  6.     @Provider
  7.     @ClientInterceptor
  8.     private static class DumpInterceptor implements MessageBodyWriterInterceptor, MessageBodyReaderInterceptor {
  9.  
  10.         @Override
  11.         public Object read(MessageBodyReaderContext context) throws IOException, WebApplicationException {
  12.             LOG.info("DumpInterceptor#read called!");
  13.             InputStream origin = context.getInputStream();
  14.             try (InputStreamToByteArray stream = new InputStreamToByteArray(origin)) {
  15.                 context.setInputStream(stream);
  16.                 Object proceed = context.proceed();
  17.                 byte[] contents = stream.toByteArray();
  18.                 LOG.info("### RESPONSE: {}", new String(contents, StandardCharsets.UTF_8));
  19.                 return proceed;
  20.             } finally {
  21.                 context.setInputStream(origin);
  22.             }
  23.         }
  24.  
  25.         @Override
  26.         public void write(MessageBodyWriterContext context) throws IOException, WebApplicationException {
  27.             LOG.info("DumpInterceptor#write called!");
  28.             OutputStream origin = context.getOutputStream();
  29.             try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
  30.                 context.setOutputStream(baos);
  31.                 context.proceed();
  32.  
  33.                 byte[] contents = baos.toByteArray();
  34.                 origin.write(contents);
  35.                 LOG.info("### REQUEST: {}", new String(contents, StandardCharsets.UTF_8));
  36.  
  37.             } finally {
  38.                 context.setOutputStream(origin);
  39.             }
  40.  
  41.         }
  42.  
  43.     }