Java Code Examples for org.xnio.channels.StreamSourceChannel#resumeReads()

The following examples show how to use org.xnio.channels.StreamSourceChannel#resumeReads() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UndertowIOHandler.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;
    }
    try {
        if (hasBody(exchange)) {
            RequestBodyParser reader = new RequestBodyParser(exchange, next);
            StreamSourceChannel channel = exchange.getRequestChannel();
            reader.read(channel);
            if (!reader.complete()) {
                channel.getReadSetter().set(reader);
                channel.resumeReads();
                return;
            }
        }
        exchange.dispatch(next);
    } catch (Throwable e) {
        if (exchange.isResponseChannelAvailable()) {
            exchange.setStatusCode(500);
            exchange.getResponseHeaders().add(new HttpString("Content-Type"), "text/plain");
            exchange.getResponseSender().send(Exceptions.stackTrace(e));
        }
    }
}
 
Example 2
Source File: FormEncodedDataDefinition.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void parse(HttpHandler handler) throws Exception {
    if (exchange.getAttachment(FORM_DATA) != null) {
        handler.handleRequest(exchange);
        return;
    }
    this.handler = handler;
    StreamSourceChannel channel = exchange.getRequestChannel();
    if (channel == null) {
        throw new IOException(UndertowMessages.MESSAGES.requestChannelAlreadyProvided());
    } else {
        doParse(channel);
        if (state != 4) {
            channel.getReadSetter().set(this);
            channel.resumeReads();
        } else {
            exchange.dispatch(SameThreadExecutor.INSTANCE, handler);
        }
    }
}
 
Example 3
Source File: StringReadChannelListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void setup(final StreamSourceChannel channel) {
    PooledByteBuffer resource = bufferPool.allocate();
    ByteBuffer buffer = resource.getBuffer();
    try {
        int r = 0;
        do {
            r = channel.read(buffer);
            if (r == 0) {
                channel.getReadSetter().set(this);
                channel.resumeReads();
            } else if (r == -1) {
                stringDone(string.extract());
                IoUtils.safeClose(channel);
            } else {
                buffer.flip();
                string.write(buffer);
            }
        } while (r > 0);
    } catch (IOException e) {
        error(e);
    } finally {
        resource.close();
    }
}
 
Example 4
Source File: ByteBufferReadChannelListener.java    From light-4j with Apache License 2.0 5 votes vote down vote up
public void setup(StreamSourceChannel channel) {
    PooledByteBuffer resource = this.bufferPool.allocate();
    ByteBuffer buffer = resource.getBuffer();

    try {
        int r;
        do {
            r = channel.read(buffer);
            if (r == 0) {
                channel.getReadSetter().set(this);
                channel.resumeReads();
            } else if (r == -1) {
                this.bufferDone(this.result);
                IoUtils.safeClose(channel);
            } else {
                buffer.flip();
                ByteBuffer[] buffs = new ByteBuffer[]{buffer};
                for(int i = 0; i < buffs.length; ++i) {
                    ByteBuffer buf = buffs[i];
                    while(buf.hasRemaining()) {
                        result.add(buf.get());
                    }
                }
            }
        } while(r > 0);
    } catch (IOException var8) {
        this.error(var8);
    } finally {
        resource.close();
    }

}
 
Example 5
Source File: HTTPIOHandler.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    if (HEALTH_CHECK_PATH.equals(exchange.getRequestPath())) {      // not treat health-check as action
        exchange.endExchange(); // end exchange will send 200 / content-length=0
        return;
    }

    boolean shutdown = shutdownHandler.handle(new Exchange(exchange));
    if (shutdown) return;

    if (hasBody(exchange)) {    // parse body early, not process until body is read (e.g. for chunked), to save one blocking thread during read
        FormDataParser parser = formParserFactory.createParser(exchange);
        if (parser != null) {
            parser.parse(handler);
            return;
        }

        var reader = new RequestBodyReader(exchange, handler);
        StreamSourceChannel channel = exchange.getRequestChannel();
        reader.read(channel);  // channel will be null if getRequestChannel() is already called, but here should not be that case
        if (!reader.complete()) {
            channel.getReadSetter().set(reader);
            channel.resumeReads();
            return;
        }
    }

    exchange.dispatch(handler);
}
 
Example 6
Source File: ChunkyByteBuffer.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private void waitForEndOfStream(final StreamSourceChannel channel) {
    // So we have to supply a buffer to detect EOF.
    // This is a corner case, and some alternatives are inappropriate:
    //  - A singleton 1-byte buffer will lead to contention if multiple clients are in this state.
    //  - A zero-byte buffer can't be used because the semantics for reading from it aren't properly
    //    defined.
    final ByteBuffer tinyBuffer = ByteBuffer.allocate(1);
    try {
        final int numRead = channel.read(tinyBuffer);
        switch (numRead) {
            case -1:
                // End-of-stream. Phew.
                endOfFile(channel);
                break;
            case 0:
                // Not yet sure; keep waiting.
                channel.resumeReads();
                break;
            default:
                // Overflow. Doh.
                channel.getReadSetter().set(null);
                completionHandler.overflow();
        }
    } catch (final IOException e) {
        onFailure(channel, e);
    }
}
 
Example 7
Source File: UndertowXhrTransport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void setup(StreamSourceChannel channel) {
	channel.suspendReads();
	channel.getReadSetter().set(this);
	channel.resumeReads();
}
 
Example 8
Source File: UndertowXhrTransport.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void setup(StreamSourceChannel channel) {
	channel.suspendReads();
	channel.getReadSetter().set(this);
	channel.resumeReads();
}
 
Example 9
Source File: ServletInputStreamImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void handleEvent(final StreamSourceChannel channel) {
    try {
        if (asyncContext.isDispatched()) {
            //this is no longer an async request
            //we just return
            //TODO: what do we do here? Revert back to blocking mode?
            channel.suspendReads();
            return;
        }
        if (anyAreSet(state, FLAG_FINISHED)) {
            channel.suspendReads();
            return;
        }
        readIntoBufferNonBlocking();
        if (pooled != null) {
            channel.suspendReads();
            setFlags(FLAG_READY);
            if (!anyAreSet(state, FLAG_FINISHED)) {
                setFlags(FLAG_BEING_INVOKED_IN_IO_THREAD);
                try {
                    request.getServletContext().invokeOnDataAvailable(request.getExchange(), listener);
                } finally {
                    clearFlags(FLAG_BEING_INVOKED_IN_IO_THREAD);
                }
                if(anyAreSet(state, FLAG_CALL_ON_ALL_DATA_READ) && allAreClear(state, FLAG_ON_DATA_READ_CALLED)) {
                    setFlags(FLAG_ON_DATA_READ_CALLED);
                    request.getServletContext().invokeOnAllDataRead(request.getExchange(), listener);
                }
            }
        } else if(anyAreSet(state, FLAG_FINISHED)) {
            if (allAreClear(state, FLAG_ON_DATA_READ_CALLED)) {
                setFlags(FLAG_ON_DATA_READ_CALLED);
                request.getServletContext().invokeOnAllDataRead(request.getExchange(), listener);
            }
        } else {
            channel.resumeReads();
        }
    } catch (final Throwable e) {
        try {
            request.getServletContext().invokeRunnable(request.getExchange(), new Runnable() {
                @Override
                public void run() {
                    listener.onError(e);
                }
            });
        } finally {
            if (pooled != null) {
                pooled.close();
                pooled = null;
            }
            IoUtils.safeClose(channel);
        }
    }
}
 
Example 10
Source File: UndertowXhrTransport.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void setup(StreamSourceChannel channel) {
	channel.suspendReads();
	channel.getReadSetter().set(this);
	channel.resumeReads();
}