3

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.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
NeillR
  • 107
  • 7

2 Answers2

1

This has been fixed yesterday. Will be part of Wicket 1.5.4. But for this use case you should use a resource. See the implementation of ResourceLink.

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • I am using the jExcelAPI library. I need to give an OutputStream as a parameter to Workbook.createWorkbook I am not writing the output to disk - want to stream it directly to the browser. Will your suggestion work for this? – NeillR Nov 17 '11 at 07:42
  • Yes it will - you use a ResourceLink to hold a Resource, e.g. an AbstractResource. – Carl-Eric Menzel Nov 21 '11 at 09:20
1

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);
        }
    }
}
tetsuo
  • 10,726
  • 3
  • 32
  • 35