Package org.apache.hc.core5.http.impl.io
Class DefaultBHttpServerConnection
- java.lang.Object
-
- org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnection
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,HttpConnection,BHttpConnection,HttpServerConnection,SocketModalCloseable,ModalCloseable
public class DefaultBHttpServerConnection extends java.lang.Object implements HttpServerConnection
Default implementation ofHttpServerConnection.- Since:
- 4.3
-
-
Constructor Summary
Constructors Constructor Description DefaultBHttpServerConnection(java.lang.String scheme, Http1Config http1Config)DefaultBHttpServerConnection(java.lang.String scheme, Http1Config http1Config, java.nio.charset.CharsetDecoder charDecoder, java.nio.charset.CharsetEncoder charEncoder)DefaultBHttpServerConnection(java.lang.String scheme, Http1Config http1Config, java.nio.charset.CharsetDecoder charDecoder, java.nio.charset.CharsetEncoder charEncoder, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy, HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory, HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory)Creates new instance of DefaultBHttpServerConnection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanawaitInput(Timeout timeout)voidbind(java.net.Socket socket)Binds this connection to the givenSocket.voidbind(javax.net.ssl.SSLSocket sslSocket, java.net.Socket baseSocket)protected voidbind(SocketHolder socketHolder)voidclose()Closes this connection gracefully.voidclose(CloseMode closeMode)Closes this process or endpoint and releases any system resources associated with it.protected java.io.InputStreamcreateContentInputStream(long len, SessionInputBuffer buffer, java.io.InputStream inputStream)protected java.io.OutputStreamcreateContentOutputStream(long len, SessionOutputBuffer buffer, java.io.OutputStream outputStream, Supplier<java.util.List<? extends Header>> trailers)protected SocketHolderensureOpen()voidflush()Writes out all pending buffered data over the open connection.EndpointDetailsgetEndpointDetails()Returns this connection's endpoint details.java.net.SocketAddressgetLocalAddress()Returns this connection's local address ornullif it is not bound yet.ProtocolVersiongetProtocolVersion()Returns this connection's protocol version ornullif unknown.java.net.SocketAddressgetRemoteAddress()Returns this connection's remote address ornullif it is not connected yet or unconnected.protected SocketHoldergetSocketHolder()TimeoutgetSocketTimeout()Returns the socket timeout value.javax.net.ssl.SSLSessiongetSSLSession()Returns this connection's SSL session ornullif TLS has not been activated.protected voidincrementRequestCount()protected voidincrementResponseCount()booleanisDataAvailable(Timeout timeout)Checks if input data is available from the connection.booleanisOpen()Checks if this connection is open.booleanisStale()Checks whether this connection has gone down.protected voidonRequestReceived(ClassicHttpRequest request)protected voidonResponseSubmitted(ClassicHttpResponse response)voidreceiveRequestEntity(ClassicHttpRequest request)Receives the next request entity available from this connection and attaches it to an existing request.ClassicHttpRequestreceiveRequestHeader()Receives the request line and all headers available from this connection.voidsendResponseEntity(ClassicHttpResponse response)Sends the response entity of a response over this connection.voidsendResponseHeader(ClassicHttpResponse response)Sends the response line and headers of a response over this connection.voidsetSocketTimeout(Timeout timeout)Sets the socket timeout value.java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.hc.core5.http.io.BHttpConnection
flush, isDataAvailable, isStale
-
Methods inherited from interface org.apache.hc.core5.http.HttpConnection
close, getEndpointDetails, getLocalAddress, getProtocolVersion, getRemoteAddress, getSSLSession, isIdle, isOpen
-
Methods inherited from interface org.apache.hc.core5.io.ModalCloseable
close
-
Methods inherited from interface org.apache.hc.core5.http.SocketModalCloseable
getSocketTimeout, setSocketTimeout
-
-
-
-
Constructor Detail
-
DefaultBHttpServerConnection
public DefaultBHttpServerConnection(java.lang.String scheme, Http1Config http1Config, java.nio.charset.CharsetDecoder charDecoder, java.nio.charset.CharsetEncoder charEncoder, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy, HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory, HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory)Creates new instance of DefaultBHttpServerConnection.- Parameters:
scheme- protocol schemehttp1Config- Message http1Config. IfnullHttp1Config.DEFAULTwill be used.charDecoder- decoder to be used for decoding HTTP protocol elements. Ifnullsimple type cast will be used for byte to char conversion.charEncoder- encoder to be used for encoding HTTP protocol elements. Ifnullsimple type cast will be used for char to byte conversion.incomingContentStrategy- incoming content length strategy. IfnullDefaultContentLengthStrategy.INSTANCEwill be used.outgoingContentStrategy- outgoing content length strategy. IfnullDefaultContentLengthStrategy.INSTANCEwill be used.requestParserFactory- request parser factory. IfnullDefaultHttpRequestParserFactory.INSTANCEwill be used.responseWriterFactory- response writer factory. IfnullDefaultHttpResponseWriterFactory.INSTANCEwill be used.
-
DefaultBHttpServerConnection
public DefaultBHttpServerConnection(java.lang.String scheme, Http1Config http1Config, java.nio.charset.CharsetDecoder charDecoder, java.nio.charset.CharsetEncoder charEncoder)
-
DefaultBHttpServerConnection
public DefaultBHttpServerConnection(java.lang.String scheme, Http1Config http1Config)
-
-
Method Detail
-
onRequestReceived
protected void onRequestReceived(ClassicHttpRequest request)
-
onResponseSubmitted
protected void onResponseSubmitted(ClassicHttpResponse response)
-
bind
public void bind(java.net.Socket socket) throws java.io.IOExceptionBinds this connection to the givenSocket. This socket will be used by the connection to send and receive data.After this method's execution the connection status will be reported as open and the
HttpConnection.isOpen()will returntrue.- Parameters:
socket- the socket.- Throws:
java.io.IOException- in case of an I/O error.
-
bind
public void bind(javax.net.ssl.SSLSocket sslSocket, java.net.Socket baseSocket) throws java.io.IOException- Throws:
java.io.IOException- in case of an I/O error.- Since:
- 5.3
-
receiveRequestHeader
public ClassicHttpRequest receiveRequestHeader() throws HttpException, java.io.IOException
Description copied from interface:HttpServerConnectionReceives the request line and all headers available from this connection. The caller should examine the returned request and decide if to receive a request entity as well.- Specified by:
receiveRequestHeaderin interfaceHttpServerConnection- Returns:
- a new HttpRequest object whose request line and headers are
initialized or
nullif the connection has been closed by the opposite endpoint. - Throws:
HttpException- in case of HTTP protocol violationjava.io.IOException- in case of an I/O error
-
receiveRequestEntity
public void receiveRequestEntity(ClassicHttpRequest request) throws HttpException, java.io.IOException
Description copied from interface:HttpServerConnectionReceives the next request entity available from this connection and attaches it to an existing request.- Specified by:
receiveRequestEntityin interfaceHttpServerConnection- Parameters:
request- the request to attach the entity to.- Throws:
HttpException- in case of HTTP protocol violationjava.io.IOException- in case of an I/O error
-
sendResponseHeader
public void sendResponseHeader(ClassicHttpResponse response) throws HttpException, java.io.IOException
Description copied from interface:HttpServerConnectionSends the response line and headers of a response over this connection.- Specified by:
sendResponseHeaderin interfaceHttpServerConnection- Parameters:
response- the response whose headers to send.- Throws:
HttpException- in case of HTTP protocol violationjava.io.IOException- in case of an I/O error
-
sendResponseEntity
public void sendResponseEntity(ClassicHttpResponse response) throws HttpException, java.io.IOException
Description copied from interface:HttpServerConnectionSends the response entity of a response over this connection.- Specified by:
sendResponseEntityin interfaceHttpServerConnection- Parameters:
response- the response whose entity to send.- Throws:
HttpException- in case of HTTP protocol violationjava.io.IOException- in case of an I/O error
-
ensureOpen
protected SocketHolder ensureOpen() throws java.io.IOException
- Throws:
java.io.IOException
-
bind
protected void bind(SocketHolder socketHolder) throws java.io.IOException
- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()
Description copied from interface:HttpConnectionChecks if this connection is open.- Specified by:
isOpenin interfaceHttpConnection- Returns:
- true if it is open, false if it is closed.
-
getProtocolVersion
public ProtocolVersion getProtocolVersion()
Description copied from interface:HttpConnectionReturns this connection's protocol version ornullif unknown.- Specified by:
getProtocolVersionin interfaceHttpConnection- Returns:
- this connection's protocol version or
nullif unknown. - Since:
- 5.0
-
getSocketHolder
protected SocketHolder getSocketHolder()
-
createContentOutputStream
protected java.io.OutputStream createContentOutputStream(long len, SessionOutputBuffer buffer, java.io.OutputStream outputStream, Supplier<java.util.List<? extends Header>> trailers)
-
createContentInputStream
protected java.io.InputStream createContentInputStream(long len, SessionInputBuffer buffer, java.io.InputStream inputStream)
-
getRemoteAddress
public java.net.SocketAddress getRemoteAddress()
Description copied from interface:HttpConnectionReturns this connection's remote address ornullif it is not connected yet or unconnected.- Specified by:
getRemoteAddressin interfaceHttpConnection- Returns:
- this connection's remote address or
nullif it is not connected yet or unconnected.
-
getLocalAddress
public java.net.SocketAddress getLocalAddress()
Description copied from interface:HttpConnectionReturns this connection's local address ornullif it is not bound yet.- Specified by:
getLocalAddressin interfaceHttpConnection- Returns:
- this connection's local address or
nullif it is not bound yet.
-
setSocketTimeout
public void setSocketTimeout(Timeout timeout)
Description copied from interface:SocketModalCloseableSets the socket timeout value.- Specified by:
setSocketTimeoutin interfaceSocketModalCloseable- Parameters:
timeout- timeout value
-
getSocketTimeout
public Timeout getSocketTimeout()
Description copied from interface:SocketModalCloseableReturns the socket timeout value.- Specified by:
getSocketTimeoutin interfaceSocketModalCloseable- Returns:
- timeout value.
-
close
public void close(CloseMode closeMode)
Description copied from interface:ModalCloseableCloses this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.- Specified by:
closein interfaceModalCloseable- Parameters:
closeMode- How to close the receiver.
-
close
public void close() throws java.io.IOExceptionDescription copied from interface:HttpConnectionCloses this connection gracefully. This method will attempt to flush the internal output buffer prior to closing the underlying socket. This method MUST NOT be called from a different thread to force shutdown of the connection. Useclose(CloseMode.IMMEDIATE)instead.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceHttpConnection- Throws:
java.io.IOException
-
awaitInput
protected boolean awaitInput(Timeout timeout) throws java.io.IOException
- Throws:
java.io.IOException
-
isDataAvailable
public boolean isDataAvailable(Timeout timeout) throws java.io.IOException
Description copied from interface:BHttpConnectionChecks if input data is available from the connection. May wait for the specified time until some data becomes available. Note that some implementations may completely ignore the timeout parameter.- Specified by:
isDataAvailablein interfaceBHttpConnection- Parameters:
timeout- the maximum time to wait for data- Returns:
- true if data is available; false if there was no data available
even after waiting for
timeout. - Throws:
java.io.IOException- if an error happens on the connection
-
isStale
public boolean isStale() throws java.io.IOExceptionDescription copied from interface:BHttpConnectionChecks whether this connection has gone down. Network connections may get closed during some time of inactivity for several reasons. The next time a read is attempted on such a connection it will throw an IOException. This method tries to alleviate this inconvenience by trying to find out if a connection is still usable. Implementations may do that by attempting a read with a very small timeout. Thus this method may block for a small amount of time before returning a result. It is therefore an expensive operation.- Specified by:
isStalein interfaceBHttpConnection- Returns:
trueif attempts to use this connection are likely to fail and this connection should be closed, orfalseif they are likely to succeed- Throws:
java.io.IOException- in case of an I/O error.
-
flush
public void flush() throws java.io.IOExceptionDescription copied from interface:BHttpConnectionWrites out all pending buffered data over the open connection.- Specified by:
flushin interfaceBHttpConnection- Throws:
java.io.IOException- in case of an I/O error
-
incrementRequestCount
protected void incrementRequestCount()
-
incrementResponseCount
protected void incrementResponseCount()
-
getSSLSession
public javax.net.ssl.SSLSession getSSLSession()
Description copied from interface:HttpConnectionReturns this connection's SSL session ornullif TLS has not been activated.- Specified by:
getSSLSessionin interfaceHttpConnection- Returns:
- this connection's SSL session or
nullif TLS has not been activated.
-
getEndpointDetails
public EndpointDetails getEndpointDetails()
Description copied from interface:HttpConnectionReturns this connection's endpoint details.- Specified by:
getEndpointDetailsin interfaceHttpConnection- Returns:
- this connection's endpoint details.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-