Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebResponse.getOutputStream() in Wicket 1.5?

This code was working for me in 1.4:

WebResponse response = (org.apache.wicket.request.http.WebResponse) getResponse();
response.setAttachmentHeader("List.xls");
response.setContentType("application/ms-excel");
OutputStream out = response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(out);
.....
.....
workbook.write();
workbook.close();

I see in 1.5 that there is no WebResponse.getOutputStream() - but it was not marked as deprecated?

I have looked in the 1.5 migration guide but I can't see any obvious solution.

Can someone please tell me how I should be doing this in 1.5.

like image 655
NeillR Avatar asked Feb 07 '26 19:02

NeillR


1 Answers

You could wrap Response in an OutputStream:

public final class ResponseOutputStream extends OutputStream {
    private final Response response;
    private final byte[] singleByteBuffer = new byte[1];
    public ResponseOutputStream(Response response) {
        this.response = response;
    }
    @Override
    public void write(int b) throws IOException {
        singleByteBuffer[0] = (byte) b;
        write(singleByteBuffer);
    }
    @Override
    public void write(byte[] b) throws IOException {
        response.write(b);
    }
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (off == 0 && len == b.length) {
            this.write(b);
        } else {
            super.write(b, off, len);
        }
    }
}
like image 132
tetsuo Avatar answered Feb 12 '26 11:02

tetsuo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!