Skip to content

Commit

Permalink
Port fix for #681
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Oct 11, 2012
1 parent 49edf6c commit e801d3c
Showing 1 changed file with 109 additions and 24 deletions.
133 changes: 109 additions & 24 deletions modules/cpr/src/main/java/org/atmosphere/cpr/AtmosphereResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
Expand Down Expand Up @@ -452,7 +453,12 @@ public void setCharacterEncoding(String charSet) {
*/
@Override
public void flushBuffer() throws IOException {
response.flushBuffer();
try {
response.flushBuffer();
} catch (IOException ex) {
handleException(ex);
throw ex;
}
}

/**
Expand Down Expand Up @@ -502,7 +508,12 @@ public void write(int i) throws java.io.IOException {
// Prevent StackOverflow
boolean b = forceAsyncIOWriter;
forceAsyncIOWriter = false;
asyncIOWriter.write(AtmosphereResponse.this, new byte[]{(byte) i});
try {
asyncIOWriter.write(AtmosphereResponse.this, new byte[]{(byte) i});
} catch (IOException e) {
handleException(e);
throw e;
}
forceAsyncIOWriter = b;
}

Expand All @@ -513,7 +524,12 @@ public void write(byte[] bytes) throws java.io.IOException {
// Prevent StackOverflow
boolean b = forceAsyncIOWriter;
forceAsyncIOWriter = false;
asyncIOWriter.write(AtmosphereResponse.this, bytes);
try {
asyncIOWriter.write(AtmosphereResponse.this, bytes);
} catch (IOException e) {
handleException(e);
throw e;
}
forceAsyncIOWriter = b;
}

Expand All @@ -524,7 +540,12 @@ public void write(byte[] bytes, int start, int offset) throws java.io.IOExceptio
// Prevent StackOverflow
boolean b = forceAsyncIOWriter;
forceAsyncIOWriter = false;
asyncIOWriter.write(AtmosphereResponse.this, bytes, start, offset);
try {
asyncIOWriter.write(AtmosphereResponse.this, bytes, start, offset);
} catch (IOException e) {
handleException(e);
throw e;
}
forceAsyncIOWriter = b;
}

Expand All @@ -535,7 +556,12 @@ public void flush() throws IOException {
// Prevent StackOverflow
boolean b = forceAsyncIOWriter;
forceAsyncIOWriter = false;
asyncIOWriter.flush(AtmosphereResponse.this);
try {
asyncIOWriter.flush(AtmosphereResponse.this);
} catch (IOException e) {
handleException(e);
throw e;
}
forceAsyncIOWriter = b;

}
Expand All @@ -546,7 +572,12 @@ public void close() throws java.io.IOException {
// Prevent StackOverflow
boolean b = forceAsyncIOWriter;
forceAsyncIOWriter = false;
asyncIOWriter.close(AtmosphereResponse.this);
try {
asyncIOWriter.close(AtmosphereResponse.this);
} catch (IOException e) {
handleException(e);
throw e;
}
forceAsyncIOWriter = b;
}
};
Expand Down Expand Up @@ -601,7 +632,7 @@ public void write(char[] chars, int offset, int lenght) {
forceAsyncIOWriter = b;

} catch (IOException e) {
throw new RuntimeException(e);
handleException(e);
}
}

Expand All @@ -615,7 +646,7 @@ public void write(char[] chars) {
forceAsyncIOWriter = b;

} catch (IOException e) {
throw new RuntimeException(e);
handleException(e);
}
}

Expand All @@ -629,7 +660,7 @@ public void write(String s, int offset, int lenght) {
forceAsyncIOWriter = b;

} catch (IOException e) {
throw new RuntimeException(e);
handleException(e);
}
}

Expand All @@ -643,7 +674,7 @@ public void write(java.lang.String s) {
forceAsyncIOWriter = b;

} catch (IOException e) {
throw new RuntimeException(e);
handleException(e);
}
}
};
Expand Down Expand Up @@ -800,7 +831,7 @@ public void closeStreamOrWriter() {
try {
getOutputStream().close();
} catch (java.lang.IllegalStateException ex) {
logger.trace("",ex);
logger.trace("", ex);
}
} else {
getWriter().close();
Expand All @@ -817,19 +848,42 @@ public void closeStreamOrWriter() {
* @param data the String to write
*/
public AtmosphereResponse write(String data) {
return write(data, false);
}

private void handleException(Exception ex) {
AtmosphereResource r = resource();
if (r != null) {
AtmosphereResourceImpl.class.cast(r).notifyListeners(
new AtmosphereResourceEventImpl(AtmosphereResourceImpl.class.cast(r), true, false));
}
logger.trace("", ex);
}

/**
* Write the String by either using the {@link PrintWriter} or {@link java.io.OutputStream}. The decision is
* based on the request attribute {@link ApplicationConfig#PROPERTY_USE_STREAM} If writeUsingOriginalResponse if set to true,
* execute the write without invoking the defined {@link AsyncIOWriter}
*
* @param data the String to write
* @param writeUsingOriginalResponse if true, execute the write without invoking the {@link AsyncIOWriter}
*/
public AtmosphereResponse write(String data, boolean writeUsingOriginalResponse) {
boolean isUsingStream = (Boolean) request().getAttribute(PROPERTY_USE_STREAM);
try {
if (isUsingStream) {
try {
getOutputStream().write(data.getBytes(getCharacterEncoding()));
OutputStream o = writeUsingOriginalResponse ? _r().getOutputStream() : getOutputStream();
o.write(data.getBytes(getCharacterEncoding()));
} catch (java.lang.IllegalStateException ex) {
logger.trace("",ex);
logger.trace("", ex);
}
} else {
getWriter().write(data);
PrintWriter w = writeUsingOriginalResponse ? _r().getWriter() : getWriter();
w.write(data);
}
} catch (IOException e) {
logger.trace("", e);
} catch (Exception ex) {
handleException(ex);
}
return this;
}
Expand All @@ -841,18 +895,32 @@ public AtmosphereResponse write(String data) {
* @param data the bytes to write
*/
public AtmosphereResponse write(byte[] data) {
return write(data, false);
}

/**
* Write the String by either using the {@link PrintWriter} or {@link java.io.OutputStream}. The decision is
* based on the request attribute {@link ApplicationConfig#PROPERTY_USE_STREAM} If writeUsingOriginalResponse if set to true,
* execute the write without invoking the defined {@link AsyncIOWriter}
*
* @param data the bytes to write
* @param writeUsingOriginalResponse if true, execute the write without invoking the {@link AsyncIOWriter}
*/
public AtmosphereResponse write(byte[] data, boolean writeUsingOriginalResponse) {
boolean isUsingStream = (Boolean) request().getAttribute(PROPERTY_USE_STREAM);
try {
if (isUsingStream) {
try {
getOutputStream().write(data);
OutputStream o = writeUsingOriginalResponse ? _r().getOutputStream() : getOutputStream();
o.write(data);
} catch (java.lang.IllegalStateException ex) {
}
} else {
getWriter().write(new String(data, getCharacterEncoding()));
PrintWriter w = writeUsingOriginalResponse ? _r().getWriter() : getWriter();
w.write(new String(data, getCharacterEncoding()));
}
} catch (IOException e) {
logger.trace("", e);
} catch (Exception ex) {
handleException(ex);
}
return this;
}
Expand All @@ -866,18 +934,34 @@ public AtmosphereResponse write(byte[] data) {
* @param length the data length
*/
public AtmosphereResponse write(byte[] data, int offset, int length) {
return write(data, offset, length, false);
}

/**
* Write the String by either using the {@link PrintWriter} or {@link java.io.OutputStream}. The decision is
* based on the request attribute {@link ApplicationConfig#PROPERTY_USE_STREAM} If writeUsingOriginalResponse if set to true,
* execute the write without invoking the defined {@link AsyncIOWriter}
*
* @param data the bytes to write
* @param offset the first byte position to write
* @param length the data length
* @param writeUsingOriginalResponse if true, execute the write without invoking the {@link AsyncIOWriter}
*/
public AtmosphereResponse write(byte[] data, int offset, int length, boolean writeUsingOriginalResponse) {
boolean isUsingStream = (Boolean) request().getAttribute(PROPERTY_USE_STREAM);
try {
if (isUsingStream) {
try {
getOutputStream().write(data, offset, length);
OutputStream o = writeUsingOriginalResponse ? _r().getOutputStream() : getOutputStream();
o.write(data, offset, length);
} catch (java.lang.IllegalStateException ex) {
}
} else {
getWriter().write(new String(data, offset, length, getCharacterEncoding()));
PrintWriter w = writeUsingOriginalResponse ? _r().getWriter() : getWriter();
w.write(new String(data, offset, length, getCharacterEncoding()));
}
} catch (IOException e) {
logger.trace("", e);
} catch (Exception ex) {
handleException(ex);
}
return this;
}
Expand Down Expand Up @@ -1088,6 +1172,7 @@ public void close() throws IOException {

/**
* Create an instance not associated with any response parent.
*
* @return
*/
public final static AtmosphereResponse create() {
Expand Down

0 comments on commit e801d3c

Please sign in to comment.