Example usage for org.apache.http.nio ContentDecoder isCompleted

List of usage examples for org.apache.http.nio ContentDecoder isCompleted

Introduction

In this page you can find the example usage for org.apache.http.nio ContentDecoder isCompleted.

Prototype

boolean isCompleted();

Source Link

Document

Returns true if the entity has been received in its entirety.

Usage

From source file:org.apache.synapse.transport.passthru.Pipe.java

/**
 * Produce data in to the buffer./*ww w  .j  av a2 s.  co m*/
 *
 * @param decoder decoder to read bytes from the underlying stream
 * @return bytes read (consumed)
 * @throws IOException if an error occurs while reading data
 */
public int produce(final ContentDecoder decoder) throws IOException {
    if (producerIoControl == null) {
        throw new IllegalStateException("Producer cannot be null when calling produce");
    }

    lock.lock();
    try {
        setInputMode(buffer);
        int bytesRead = 0;
        try {
            bytesRead = decoder.read(buffer.getByteBuffer());
        } catch (MalformedChunkCodingException ignore) {
            // we assume that this is a truncated chunk, hence simply ignore the exception
            // https://issues.apache.org/jira/browse/HTTPCORE-195
            // we should add the EoF character
            buffer.putInt(-1);
            // now the buffer's position should give us the bytes read.
            bytesRead = buffer.position();

        }

        // if consumer is at error we have to let the producer complete
        if (consumerError) {
            buffer.clear();
        }

        if (!buffer.hasRemaining()) {
            // Input buffer is full. Suspend client input
            // until the origin handler frees up some space in the buffer
            producerIoControl.suspendInput();
        }

        // If there is some content in the input buffer make sure consumer output is active
        if (buffer.position() > 0 || decoder.isCompleted()) {
            if (consumerIoControl != null) {
                consumerIoControl.requestOutput();
            }
            readCondition.signalAll();
        }

        if (decoder.isCompleted()) {
            producerCompleted = true;
        }
        return bytesRead;
    } finally {
        lock.unlock();
    }
}

From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java

public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
    System.out.println(conn + " [proxy<-origin] input ready");

    HttpContext context = conn.getContext();
    ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB);

    synchronized (proxyTask) {
        ConnState connState = proxyTask.getOriginState();
        if (connState != ConnState.RESPONSE_RECEIVED && connState != ConnState.RESPONSE_BODY_STREAM) {
            throw new IllegalStateException("Illegal target connection state: " + connState);
        }/*from ww w.  ja  v a  2s  . co m*/

        final Response response = proxyTask.getResponse();
        try {

            ByteBuffer dst = proxyTask.getOutBuffer();
            int bytesRead = decoder.read(dst);
            if (bytesRead > 0) {
                dst.flip();
                final ByteChunk chunk = new ByteChunk(bytesRead);
                final byte[] buf = new byte[bytesRead];
                dst.get(buf);
                chunk.setBytes(buf, 0, bytesRead);
                dst.compact();
                try {
                    response.doWrite(chunk);
                } catch (ClassCastException e) {
                    System.err.println("gone bad: " + e.getMessage());
                    e.printStackTrace(System.err);
                }
                response.flush();
                System.out.println(conn + " [proxy<-origin] " + bytesRead + " bytes read");
                System.out.println(conn + " [proxy<-origin] " + decoder);
            }
            if (!dst.hasRemaining()) {
                // Output buffer is full. Suspend origin input until
                // the client handler frees up some space in the buffer
                conn.suspendInput();
            }
            /*
                    // If there is some content in the buffer make sure client output
                    // is active
                    if (dst.position() > 0) {
                      proxyTask.getClientIOControl().requestOutput();
                    }
            */

            if (decoder.isCompleted()) {
                System.out.println(conn + " [proxy<-origin] response body received");
                proxyTask.setOriginState(ConnState.RESPONSE_BODY_DONE);
                if (!this.connStrategy.keepAlive(conn.getHttpResponse(), context)) {
                    System.out.println(conn + " [proxy<-origin] close connection");
                    proxyTask.setOriginState(ConnState.CLOSING);
                    conn.close();
                }
                proxyTask.getCompletion().run();
            } else {
                proxyTask.setOriginState(ConnState.RESPONSE_BODY_STREAM);
            }

        } catch (IOException ex) {
            shutdownConnection(conn);
        }
    }
}

From source file:org.apache.axis2.transport.nhttp.ClientHandler.java

/**
 * Process ready input (i.e. response from remote server)
 * @param conn connection being processed
 * @param decoder the content decoder in use
 *///  w w w  .  j a v a2s  .  co  m
public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(RESPONSE_SINK_CHANNEL);
    ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER);

    try {
        while (decoder.read(inbuf) > 0) {
            inbuf.flip();
            sink.write(inbuf);
            inbuf.compact();
        }

        if (decoder.isCompleted()) {
            sink.close();
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else {
                ConnectionPool.release(conn);
            }
        }

    } catch (IOException e) {
        handleException("I/O Error : " + e.getMessage(), e, conn);
    }
}

From source file:org.apache.axis2.transport.nhttp.ServerHandler.java

/**
 * Process ready input by writing it into the Pipe
 * @param conn the connection being processed
 * @param decoder the content decoder in use
 *//*from w ww . ja  va  2s  .co m*/
public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {

    HttpContext context = conn.getContext();
    Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(REQUEST_SINK_CHANNEL);
    ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER);

    try {
        while (decoder.read(inbuf) > 0) {
            inbuf.flip();
            sink.write(inbuf);
            inbuf.compact();
        }

        if (decoder.isCompleted()) {
            sink.close();
        }

    } catch (IOException e) {
        handleException("I/O Error : " + e.getMessage(), e, conn);
    }
}

From source file:org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.java

@Override
public void consumeContent(final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
    this.exec.consumeContent(this.state, decoder, ioctrl);
    if (!decoder.isCompleted() && this.responseConsumer.isDone()) {
        markConnectionNonReusable();//w w w . j  av a2s .c o m
        try {
            markCompleted();
            releaseConnection();
            this.resultFuture.cancel();
        } finally {
            close();
        }
    }
}

From source file:org.apache.http.impl.nio.client.MinimalClientExchangeHandlerImpl.java

@Override
public void consumeContent(final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + getId() + "] Consume content");
    }//from   ww  w  .j  a  va2s  . co m
    this.responseConsumer.consumeContent(decoder, ioctrl);
    if (!decoder.isCompleted() && this.responseConsumer.isDone()) {
        markConnectionNonReusable();
        try {
            markCompleted();
            releaseConnection();
            this.resultFuture.cancel();
        } finally {
            close();
        }
    }
}

From source file:org.apache.http.impl.nio.client.NHttpClientProtocolHandler.java

public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
    HttpContext context = conn.getContext();
    HttpExchange httpexchange = getHttpExchange(context);
    HttpAsyncExchangeHandler<?> handler = getHandler(context);
    if (this.log.isDebugEnabled()) {
        this.log.debug("Input ready " + formatState(conn, httpexchange));
    }/*from   w  w  w.java2s  .  co  m*/
    try {
        handler.consumeContent(decoder, conn);
        if (decoder.isCompleted()) {
            processResponse(conn, httpexchange, handler);
        }
    } catch (IOException ex) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("I/O error: " + ex.getMessage(), ex);
        }
        shutdownConnection(conn);
        handler.failed(ex);
    }
}