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

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

Introduction

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

Prototype

boolean isCompleted();

Source Link

Document

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

Usage

From source file:marytts.server.http.AudioStreamNHttpEntity.java

public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException {
    if (out == null) {
        synchronized (mutex) {
            out = new SharedOutputBuffer(8192, ioctrl, new HeapByteBufferAllocator());
            mutex.notify();//w w w  .java  2 s .c o m
        }
    }
    while (!encoder.isCompleted())
        out.produceContent(encoder);
}

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

/**
 * Consume the content through the Pipe and write them to the wire
 * @param conn connection/*from w  ww  .  java2  s.co m*/
 * @param encoder encoder
 * @throws java.io.IOException if an error occurs
 * @return number of bytes written
 */
public int write(NHttpServerConnection conn, ContentEncoder encoder) throws IOException {
    int bytes = 0;
    if (pipe != null) {
        bytes = pipe.consume(encoder);
    } else {
        encoder.complete();
    }
    // Update connection state
    if (encoder.isCompleted()) {
        SourceContext.updateState(conn, ProtocolState.RESPONSE_DONE);

        sourceConfiguration.getMetrics().notifySentMessageSize(conn.getMetrics().getSentBytesCount());

        if (response != null && !this.connStrategy.keepAlive(response, conn.getContext())) {
            SourceContext.updateState(conn, ProtocolState.CLOSING);

            sourceConfiguration.getSourceConnections().closeConnection(conn);
        } else if (SourceContext.get(conn).isShutDown()) {
            // we need to shut down if the shutdown flag is set
            SourceContext.updateState(conn, ProtocolState.CLOSING);

            sourceConfiguration.getSourceConnections().closeConnection(conn);
        } else {
            // Reset connection state
            sourceConfiguration.getSourceConnections().releaseConnection(conn);
            // Ready to deal with a new request                
            conn.requestInput();
        }
    }
    return bytes;
}

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

/**
 * Consume the data from the pipe and write it to the wire.
 *
 * @param conn the connection to the target
 * @param encoder encoder for writing the message through
 * @throws java.io.IOException if an error occurs
 * @return number of bytes written/*from w  w w .j  a  va 2  s. c o m*/
 */
public int write(NHttpClientConnection conn, ContentEncoder encoder) throws IOException {
    int bytes = 0;
    if (pipe != null) {
        bytes = pipe.consume(encoder);
    }

    if (encoder.isCompleted()) {
        conn.getContext().setAttribute(PassThroughConstants.REQ_DEPARTURE_TIME, System.currentTimeMillis());
        conn.getContext().setAttribute(PassThroughConstants.REQ_TO_BACKEND_WRITE_END_TIME,
                System.currentTimeMillis());
        targetConfiguration.getMetrics().notifySentMessageSize(conn.getMetrics().getSentBytesCount());

        TargetContext.updateState(conn, ProtocolState.REQUEST_DONE);
    }

    return bytes;

}

From source file:org.apache.synapse.transport.nhttp.NhttpSharedOutputBuffer.java

public int produceContent(final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
    if (this.shutdown) {
        return -1;
    }/*from   w  w w .j  av  a  2  s  . c  o  m*/
    this.lock.lock();
    try {
        if (ioctrl != null) {
            this.ioctrl = ioctrl;
        }
        setOutputMode();
        int bytesWritten = 0;
        if (super.hasData()) {
            bytesWritten = encoder.write(this.buffer);
            if (encoder.isCompleted()) {
                this.endOfStream = true;
            }
        }
        if (!super.hasData()) {
            // No more buffered content
            // If at the end of the stream, terminate
            if (this.endOfStream && !encoder.isCompleted()) {
                encoder.complete();
            }
            if (!this.endOfStream) {
                // suspend output events
                if (this.ioctrl != null) {
                    this.ioctrl.suspendOutput();
                }
            }
        }
        this.condition.signalAll();
        return bytesWritten;
    } finally {
        this.lock.unlock();
    }
}

From source file:org.apache.cxf.transport.http.asyncclient.SharedOutputBuffer.java

public int produceContent(final ContentEncoder encoder, final IOControl ioc) throws IOException {
    if (this.shutdown) {
        return -1;
    }/*w  ww.  j  a  v  a  2s  . co m*/
    this.lock.lock();
    try {
        this.ioctrl = ioc;
        setOutputMode();
        int bytesWritten = 0;
        if (largeWrapper != null || super.hasData()) {
            if (!this.buffer.hasRemaining() && largeWrapper != null) {
                bytesWritten = encoder.write(largeWrapper);
            } else {
                bytesWritten = encoder.write(this.buffer);
            }
            if (encoder.isCompleted()) {
                this.endOfStream = true;
            }
        }
        if ((largeWrapper == null || !largeWrapper.hasRemaining()) && !super.hasData()) {
            // No more buffered content
            // If at the end of the stream, terminate
            if (this.endOfStream && !encoder.isCompleted()) {
                encoder.complete();
            }
            if (!this.endOfStream && this.ioctrl != null) {
                // suspend output events
                this.ioctrl.suspendOutput();
            }
        }
        // no need to signal if the large wrapper is present and has data remaining 
        if (largeWrapper == null || !largeWrapper.hasRemaining()) {
            this.condition.signalAll();
        }
        return bytesWritten;
    } finally {
        this.lock.unlock();
    }
}

From source file:marytts.server.http.TestProducingNHttpEntity.java

public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException {
    final SharedOutputBuffer ob = new SharedOutputBuffer(8192, ioctrl, new HeapByteBufferAllocator());
    new Thread() {
        public void run() {
            try {
                FileInputStream fis = new FileInputStream("/Users/marc/Music/enjoytheride_feat.judytzuke_.mp3");
                int nRead;
                byte[] bytes = new byte[4096];
                while ((nRead = fis.read(bytes)) != -1) {
                    synchronized (this) {
                        try {
                            wait(1);// w w w . j  av a  2 s.  co  m
                        } catch (InterruptedException ie) {
                        }
                    }
                    ob.write(bytes, 0, nRead);
                }
                ob.writeCompleted();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
    while (!encoder.isCompleted())
        ob.produceContent(encoder);
}

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

/**
 * Process ready output by writing into the channel
 * @param conn the connection being processed
 * @param encoder the content encoder in use
 *///from w w  w  .ja v  a  2s .com
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {

    HttpContext context = conn.getContext();
    HttpResponse response = conn.getHttpResponse();
    ContentOutputBuffer outBuf = (ContentOutputBuffer) context.getAttribute(RESPONSE_SOURCE_BUFFER);

    if (outBuf == null) {
        // fix for SYNAPSE 584. This is a temporaly fix becuase of HTTPCORE-208
        shutdownConnection(conn, false, null);
        return;
    }

    try {
        int bytesWritten = outBuf.produceContent(encoder);
        if (metrics != null && bytesWritten > 0) {
            metrics.incrementBytesSent(bytesWritten);
        }

        if (encoder.isCompleted()) {
            long currentTime = System.currentTimeMillis();
            context.setAttribute(NhttpConstants.RES_TO_CLIENT_WRITE_END_TIME, currentTime);
            context.setAttribute(NhttpConstants.RES_DEPARTURE_TIME, currentTime);
            updateLatencyView(context);

            context.removeAttribute(NhttpConstants.REQ_ARRIVAL_TIME);
            context.removeAttribute(NhttpConstants.REQ_DEPARTURE_TIME);
            context.removeAttribute(NhttpConstants.RES_ARRIVAL_TIME);

            ((ServerConnectionDebug) conn.getContext().getAttribute(SERVER_CONNECTION_DEBUG))
                    .recordResponseCompletionTime();

            Boolean reqRead = (Boolean) conn.getContext().getAttribute(NhttpConstants.REQUEST_READ);
            Boolean forceConnectionClose = (Boolean) conn.getContext()
                    .getAttribute(NhttpConstants.FORCE_CONNECTION_CLOSE);
            if (reqRead != null && !reqRead) {
                try {
                    // this is a connection we should not re-use
                    conn.close();
                } catch (Exception ignore) {
                }
            } else if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else if (forceConnectionClose != null && forceConnectionClose) {
                conn.close();
            } else {
                conn.requestInput();
            }
        }

    } catch (IOException e) {
        if (metrics != null) {
            metrics.incrementFaultsSending();
        }
        handleException("I/O Error at outputReady : " + e.getMessage(), e, conn);
    }
}