use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.
the class ResponseTest method testWriteRuntimeIOException.
@Test
public void testWriteRuntimeIOException() throws Exception {
Response response = getResponse();
PrintWriter writer = response.getWriter();
writer.println("test");
writer.flush();
Assert.assertFalse(writer.checkError());
Throwable cause = new IOException("problem at mill");
_channel.abort(cause);
writer.println("test");
Assert.assertTrue(writer.checkError());
try {
writer.println("test");
Assert.fail();
} catch (RuntimeIOException e) {
Assert.assertEquals(cause, e.getCause());
}
}
use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.
the class Response method setContentLength.
@Override
public void setContentLength(int len) {
// if the getHandling committed the response!
if (isCommitted() || isIncluding())
return;
if (len > 0) {
long written = _out.getWritten();
if (written > len)
throw new IllegalArgumentException("setContentLength(" + len + ") when already written " + written);
_contentLength = len;
_fields.putLongField(HttpHeader.CONTENT_LENGTH, len);
if (isAllContentWritten(written)) {
try {
closeOutput();
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
} else if (len == 0) {
long written = _out.getWritten();
if (written > 0)
throw new IllegalArgumentException("setContentLength(0) when already written " + written);
_contentLength = len;
_fields.put(HttpHeader.CONTENT_LENGTH, "0");
} else {
_contentLength = len;
_fields.remove(HttpHeader.CONTENT_LENGTH);
}
}
use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.
the class HttpInput method run.
/*
* <p> While this class is-a Runnable, it should never be dispatched in it's own thread. It is a runnable only so that the calling thread can use {@link
* ContextHandler#handle(Runnable)} to setup classloaders etc. </p>
*/
@Override
public void run() {
final ReadListener listener;
Throwable error;
boolean aeof = false;
synchronized (_inputQ) {
listener = _listener;
if (_state == EOF)
return;
if (_state == AEOF) {
_state = EOF;
aeof = true;
}
error = _state.getError();
if (!aeof && error == null) {
Content content = nextInterceptedContent();
if (content == null)
return;
// So -1 will never be read and only onAddDataRread or onError will be called
if (content instanceof EofContent) {
consume(content);
if (_state == EARLY_EOF)
error = _state.getError();
else if (_state == AEOF) {
aeof = true;
_state = EOF;
}
}
}
}
try {
if (error != null) {
// TODO is this necessary to add here?
_channelState.getHttpChannel().getResponse().getHttpFields().add(HttpConnection.CONNECTION_CLOSE);
listener.onError(error);
} else if (aeof) {
listener.onAllDataRead();
} else {
listener.onDataAvailable();
// If -1 was read, then HttpChannelState#onEOF will have been called and a subsequent
// unhandle will call run again so onAllDataRead() can be called.
}
} catch (Throwable e) {
LOG.warn(e.toString());
LOG.debug(e);
try {
if (aeof || error == null) {
_channelState.getHttpChannel().getResponse().getHttpFields().add(HttpConnection.CONNECTION_CLOSE);
listener.onError(e);
}
} catch (Throwable e2) {
LOG.warn(e2.toString());
LOG.debug(e2);
throw new RuntimeIOException(e2);
}
}
}
use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.
the class HttpInput method setReadListener.
@Override
public void setReadListener(ReadListener readListener) {
readListener = Objects.requireNonNull(readListener);
boolean woken = false;
try {
synchronized (_inputQ) {
if (_listener != null)
throw new IllegalStateException("ReadListener already set");
_listener = readListener;
Content content = produceNextContext();
if (content != null) {
_state = ASYNC;
woken = _channelState.onReadReady();
} else if (_state == EOF) {
_state = AEOF;
woken = _channelState.onReadEof();
} else {
_state = ASYNC;
_channelState.onReadUnready();
}
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
if (woken)
wake();
}
Aggregations