Search in sources :

Example 6 with RuntimeIOException

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());
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 7 with RuntimeIOException

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);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException)

Example 8 with RuntimeIOException

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);
        }
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) ReadListener(javax.servlet.ReadListener)

Example 9 with RuntimeIOException

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();
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Aggregations

RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)9 IOException (java.io.IOException)8 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ReadListener (javax.servlet.ReadListener)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpFields (org.eclipse.jetty.http.HttpFields)1 MetaData (org.eclipse.jetty.http.MetaData)1 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)1 Session (org.eclipse.jetty.http2.api.Session)1