@Override
 public void filter(
     ContainerRequestContext requestContext, ContainerResponseContext responseContext)
     throws IOException {
   RequestDetails requestDetails =
       new RequestDetails(
           requestContext.getHeaderString(HEADER_NONCE),
           requestContext.getHeaderString(HEADER_SIGNATURE),
           requestContext.getHeaderString(HEADER_IDENTITY),
           requestContext.getSecurityContext().getUserPrincipal(),
           responseContext.getStatus());
   responseContext.getHeaders().add(HEADER_TIMESTAMP, requestDetails.responseTimestamp);
   requestContext.setProperty(TMP_REQDETAILS, requestDetails);
   if (responseContext.getEntity() == null && requestDetails.principal != null) {
     try {
       RESTResponseSigner responseSigner =
           new RESTResponseSigner(
               requestDetails.nonce, requestDetails.signature, requestDetails.statusCode, null);
       responseContext
           .getHeaders()
           .add(
               RESTRequestSigner.HEADER_SIGNATURE,
               signResponse(requestDetails.principal, responseSigner.getDataToSign()));
     } catch (InvalidKeyException e) {
       throw new UnexpectedException(e);
     }
   }
 }
 @Override
 public void aroundWriteTo(WriterInterceptorContext responseCtx)
     throws IOException, WebApplicationException {
   RequestDetails requestDetails = (RequestDetails) responseCtx.getProperty(TMP_REQDETAILS);
   if (requestDetails.principal != null) {
     ByteArrayOutputStream content = new ByteArrayOutputStream();
     OutputStream oldStream = responseCtx.getOutputStream();
     responseCtx.setOutputStream(content);
     responseCtx.proceed();
     byte[] contentData = content.toByteArray();
     RESTResponseSigner responseSigner =
         new RESTResponseSigner(
             requestDetails.nonce,
             requestDetails.signature,
             requestDetails.statusCode,
             contentData);
     try {
       responseCtx
           .getHeaders()
           .add(
               RESTRequestSigner.HEADER_SIGNATURE,
               signResponse(requestDetails.principal, responseSigner.getDataToSign()));
     } catch (InvalidKeyException e) {
       logServerError(
           "Invalid key for identity " + requestDetails.identity + " : " + e.getMessage(),
           e,
           null);
       throw new WebApplicationException(INTERNAL_SERVER_ERROR);
     } catch (BackendAccessException e) {
       logServerError("Unexpected BackendAccessException" + e.getMessage(), e, null);
       throw new WebApplicationException(INTERNAL_SERVER_ERROR);
     }
     oldStream.write(contentData);
   }
 }