1 package org.simantics.scl.rest;
3 import java.io.FilterOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
7 import javax.ws.rs.WebApplicationException;
8 import javax.ws.rs.ext.WriterInterceptor;
9 import javax.ws.rs.ext.WriterInterceptorContext;
11 import org.simantics.scl.runtime.function.Function1;
12 import org.simantics.scl.runtime.tuple.Tuple0;
14 public class WriteProgressInterceptor implements WriterInterceptor {
16 private Function1<Long, Tuple0> callback;
18 public WriteProgressInterceptor(Function1<Long, Tuple0> callback) {
19 this.callback = callback;
23 public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
25 final OutputStream os = context.getOutputStream();
27 context.setOutputStream(new FilterOutputStream(os) {
30 @Override public void write(byte[] b, int off, int len) throws IOException {
31 out.write(b, off, len);
33 callback.apply(count);
37 public void write(int b) throws IOException {
40 callback.apply(count);