All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.net.http.server.impl.Http1ServerOutputChannel.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.http.server.impl

import com.fireflysource.common.io.BufferUtils
import com.fireflysource.common.sys.Result
import com.fireflysource.net.http.common.model.MetaData
import com.fireflysource.net.http.server.HttpServerOutputChannel
import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicBoolean

class Http1ServerOutputChannel(
    private val http1ServerConnection: Http1ServerConnection,
    private val response: MetaData.Response,
    private val closeConnection: Boolean
) : HttpServerOutputChannel {

    private val committed = AtomicBoolean(false)
    private val closed = AtomicBoolean(false)

    override fun isCommitted(): Boolean = committed.get()

    override fun commit(): CompletableFuture {
        return if (committed.compareAndSet(false, true)) {
            val header = Header(response, CompletableFuture())
            http1ServerConnection.sendResponseMessage(header)
            header.future
        } else {
            Result.DONE
        }
    }

    override fun write(byteBuffers: Array, offset: Int, length: Int): CompletableFuture {
        val future = CompletableFuture()
        val buffers = Http1OutputBuffers(byteBuffers, offset, length, Result.futureToConsumer(future))
        http1ServerConnection.sendResponseMessage(buffers)
        return future
    }

    override fun write(byteBufferList: List, offset: Int, length: Int): CompletableFuture {
        val future = CompletableFuture()
        val buffers = Http1OutputBufferList(byteBufferList, offset, length, Result.futureToConsumer(future))
        http1ServerConnection.sendResponseMessage(buffers)
        return future
    }

    override fun write(string: String): CompletableFuture {
        return write(string, StandardCharsets.UTF_8)
    }

    override fun write(string: String, charset: Charset): CompletableFuture {
        val buffer = BufferUtils.toBuffer(string, charset)
        return write(buffer)
    }

    override fun write(byteBuffer: ByteBuffer): CompletableFuture {
        val future = CompletableFuture()
        val buffer = Http1OutputBuffer(byteBuffer, Result.futureToConsumer(future))
        http1ServerConnection.sendResponseMessage(buffer)
        return future
    }

    override fun closeAsync(): CompletableFuture {
        return if (closed.compareAndSet(false, true)) {
            val message = EndResponse(CompletableFuture(), closeConnection)
            http1ServerConnection.sendResponseMessage(message)
            message.future
        } else {
            Result.DONE
        }
    }

    override fun isOpen(): Boolean = !closed.get()

    override fun close() {
        closeAsync()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy