Carloshf
Carloshf

Reputation: 539

Websphere appending error to response

Here scenario: I am working on a new functionality on an application using Struts2 and I added a new action to generate a CSV file. Everything works fine on my test system with Tomcat, but on the Quality system with a Websphere server, the file is ok but on the end, there is an extra line including

Error 500: java.lang.IllegalStateException: Response already committed.

So the thing is, file is generated and everything is perfect, but Tomcat does not append error, Websphere appends error.

So how can I avoid this? Here the main process (lines that are important):

1- The action extends an org.apache.struts2.s1.Struts1Action which does the forwarding:

ActionForward forward = action.execute(mapping, actionForm, request, response);

2- Methods in the action are executed, with the following lines that generate the response:

response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"report_"
                + Calendar.getInstance().get(Calendar.YEAR) + ".csv\"");
OutputStream outputStream = response.getOutputStream();
outputStream.write(result.toString().getBytes());
outputStream.flush();
outputStream.close();

3- The parent action finishes doing the thing. At some point, I get this exception, but it never worried us (we were already generating other type of excel reports(xls), but never got this problem with websphere appending the error to the response):

java.lang.IllegalStateException:getOutputStream() has already been called for this response

So what really wonders me is how to avoid websphere to append the error to the csv file... But of course the solution should be avoiding the error, but the thing is that this application has been running for 10 years and I shouldn't change things that already work, however any solutions to this error are welcome!!

Thank you very much guys!!

---Edit---

What I actually want to know, is how could I generate a response with a text file and still being able afterwards to do all the forwarding and stuff.

Any ideas?

Upvotes: 1

Views: 122

Answers (2)

Carloshf
Carloshf

Reputation: 539

Well, I actually found a solution to the problem, maybe not a solution by itself, but a workaround.

So the problem was that websphere was appending the error, as the Response content was just a String.

So what I did, was generating the response as a ByteArray, not as a String, this way websphere cannot append anything as simple text and the ByteStream is closed and safe.

        byte[] data = result.toString().getBytes();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(data.length);
        outStream.write(data, 0, data.length);
        response.setContentType("text/csv" + "; charset=UTF-8");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setHeader("Content-Disposition", "attachment; filename=\"report_"
                    + Calendar.getInstance().get(Calendar.YEAR) + ".csv\"");

        response.setContentLength(outStream.size());
        ServletOutputStream responseOutStream = response.getOutputStream();
        outStream.writeTo(responseOutStream);
        outStream.flush();
        outStream.close();
        responseOutStream.flush();
        responseOutStream.close();

Any edits, proposals or explanations are welcome if I did not explain it well enough!!

Upvotes: 0

PKey
PKey

Reputation: 3841

Just trying to give you some ideas... Hope the following makes sense...

First of all, the cause of java.lang.IllegalStateException:getOutputStream() according to this source is

In short, this error comes when you call the include() or forward() method after committing the response or by calling the getOutputStream() on the response object

Here is an example of struts1 download implementation (as I understand it, you are using struts1 plugin for struts2). There we see the execute method ends with return null;

So, may be, in the code that you have omitted from your post, there is some forwarding going on, that causes you the exception.

Upvotes: 1

Related Questions