Class NettyConnection
- java.lang.Object
-
- org.jivesoftware.openfire.net.AbstractConnection
-
- org.jivesoftware.openfire.nio.NettyConnection
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Connection
public class NettyConnection extends AbstractConnection
Implementation ofConnectioninterface specific for Netty connections.- Author:
- Matthew Vivian, Alex Gidman
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jivesoftware.openfire.Connection
Connection.ClientAuth, Connection.CompressionPolicy, Connection.State, Connection.TLSPolicy
-
-
Field Summary
Fields Modifier and Type Field Description static StringSSL_HANDLER_NAME-
Fields inherited from class org.jivesoftware.openfire.net.AbstractConnection
closeListeners, session
-
-
Constructor Summary
Constructors Constructor Description NettyConnection(io.netty.channel.ChannelHandlerContext channelHandlerContext, PacketDeliverer packetDeliverer, ConnectionConfiguration configuration)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddCompression()Adds the compression filter to the connection but only filter incoming traffic.voidclose()Close this session including associated socket connection.voidclose(org.xmpp.packet.StreamError error)Close this session including associated socket connection, optionally citing a stream error.voiddeliver(org.xmpp.packet.Packet packet)Delivers the packet to this connection without checking the recipient.voiddeliverRawText(String text)Delivers raw text to this connection.byte[]getAddress()Returns the raw IP address of thisInetAddressobject.Optional<String>getCipherSuiteName()Returns the TLS cipher suite name used by the connection of the session, if any.ConnectionConfigurationgetConfiguration()Returns a representation of the desired state for this connection.StringgetHostAddress()Returns the IP address string in textual presentation.StringgetHostName()Gets the host name for this IP address.Certificate[]getLocalCertificates()Returns the local underlyingX509Certificatechain for the connection.PacketDeliverergetPacketDeliverer()Returns the packet deliverer to use when delivering a packet over the socket fails.Certificate[]getPeerCertificates()Returns the underlyingX509Certificatefor the connection of the peer.Optional<String>getTLSProtocolName()Returns the TLS protocol name used by the connection of the session, if any.booleanisClosed()Returns true if the connection/session is closed.booleanisCompressed()Returns true if the connection is using compression.booleanisEncrypted()Returns true if this connection is encrypted.booleanisInitialized()Checks if the connection has finished initialization.booleanisSecure()Deprecated.booleanisUsingSelfSignedCertificate()Returns true if the other peer of this session presented a self-signed certificate.voidreinit(LocalSession owner)Reinitializes the connection to switch to a different session.voidsetEncrypted(boolean encrypted)voidsetUsingSelfSignedCertificate(boolean isSelfSigned)Keeps track if the other peer of this session presented a self-signed certificate.voidstartCompression()Start compressing outgoing traffic for this connection.voidstartTLS(boolean clientMode, boolean directTLS)Encrypts the plain connection by negotiating TLS with the other peer.voidsystemShutdown()Notification message indicating that the server is being shutdown.StringtoString()booleanvalidate()Verifies that the connection is still live.-
Methods inherited from class org.jivesoftware.openfire.net.AbstractConnection
getAdditionalNamespaces, getMajorXMPPVersion, getMinorXMPPVersion, getSession, init, notifyCloseListeners, registerCloseListener, removeCloseListener, setAdditionalNamespaces, setXMPPVersion
-
-
-
-
Field Detail
-
SSL_HANDLER_NAME
public static final String SSL_HANDLER_NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
NettyConnection
public NettyConnection(io.netty.channel.ChannelHandlerContext channelHandlerContext, @Nullable PacketDeliverer packetDeliverer, ConnectionConfiguration configuration)
-
-
Method Detail
-
validate
public boolean validate()
Description copied from interface:ConnectionVerifies that the connection is still live. Typically, this is done by sending a whitespace character between packets.- Returns:
- true if the socket remains valid, false otherwise.
-
getAddress
public byte[] getAddress() throws UnknownHostExceptionDescription copied from interface:ConnectionReturns the raw IP address of thisInetAddressobject. The result is in network byte order: the highest order byte of the address is ingetAddress()[0].- Returns:
- the raw IP address of this object.
- Throws:
UnknownHostException- if IP address of host could not be determined.
-
getHostAddress
public String getHostAddress() throws UnknownHostException
Description copied from interface:ConnectionReturns the IP address string in textual presentation.- Returns:
- the raw IP address in a string format.
- Throws:
UnknownHostException- if IP address of host could not be determined.
-
getHostName
public String getHostName() throws UnknownHostException
Description copied from interface:ConnectionGets the host name for this IP address.If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call
getCanonicalHostName.If there is a security manager, its
checkConnectmethod is first called with the hostname and-1as its arguments to see if the operation is allowed. If the operation is not allowed, it will return the textual representation of the IP address.- Returns:
- the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.
- Throws:
UnknownHostException- if IP address of host could not be determined.- See Also:
InetAddress.getCanonicalHostName(),SecurityManager.checkConnect(java.lang.String, int)
-
getLocalCertificates
public Certificate[] getLocalCertificates()
Description copied from interface:ConnectionReturns the local underlyingX509Certificatechain for the connection.- Returns:
- an ordered array of certificates, with the local certificate
first followed by any certificate authorities. If no certificates
is present for the connection, then
nullis returned.
-
getPeerCertificates
public Certificate[] getPeerCertificates()
Description copied from interface:ConnectionReturns the underlyingX509Certificatefor the connection of the peer.- Returns:
- an ordered array of peer certificates, with the peer's own certificate first followed by any certificate authorities.
-
getTLSProtocolName
public Optional<String> getTLSProtocolName()
Description copied from interface:ConnectionReturns the TLS protocol name used by the connection of the session, if any.- Returns:
- a TLS protocol (version) name.
-
getCipherSuiteName
public Optional<String> getCipherSuiteName()
Description copied from interface:ConnectionReturns the TLS cipher suite name used by the connection of the session, if any.- Returns:
- cipher suite name.
-
setUsingSelfSignedCertificate
public void setUsingSelfSignedCertificate(boolean isSelfSigned)
Description copied from interface:ConnectionKeeps track if the other peer of this session presented a self-signed certificate. When using self-signed certificate for server-2-server sessions then SASL EXTERNAL will not be used and instead server-dialback will be preferred for vcerifying the identify of the remote server.- Parameters:
isSelfSigned- true if the other peer presented a self-signed certificate.
-
isUsingSelfSignedCertificate
public boolean isUsingSelfSignedCertificate()
Description copied from interface:ConnectionReturns true if the other peer of this session presented a self-signed certificate. When using self-signed certificate for server-2-server sessions then SASL EXTERNAL will not be used and instead server-dialback will be preferred for vcerifying the identify of the remote server.- Returns:
- true if the other peer of this session presented a self-signed certificate.
-
getPacketDeliverer
@Nullable public PacketDeliverer getPacketDeliverer()
Description copied from interface:ConnectionReturns the packet deliverer to use when delivering a packet over the socket fails. The packet deliverer will retry to send the packet using some other connection or will store the packet offline for later retrieval. When null, packets will just be dropped.- Returns:
- the packet deliverer to use when delivering a packet over the socket fails.
-
close
public void close()
Description copied from interface:ConnectionClose this session including associated socket connection. The order of events for closing the session is:- Set closing flag to prevent redundant shutdowns.
- Call notifyEvent all listeners that the channel is shutting down.
- Close the socket.
Closeable.close()contract (idempotent, try-with-resources, etc.)
-
close
public void close(@Nullable org.xmpp.packet.StreamError error)Description copied from interface:ConnectionClose this session including associated socket connection, optionally citing a stream error. The events for closing the session are:- Set closing flag to prevent redundant shutdowns.
- Close the socket.
- Notify all listeners that the channel is shut down.
- Parameters:
error- If non-null, the end-stream tag will be preceded with this error.
-
systemShutdown
public void systemShutdown()
Description copied from interface:ConnectionNotification message indicating that the server is being shutdown. Implementors should send a stream error whose condition is system-shutdown before closing the connection.
-
reinit
public void reinit(LocalSession owner)
Description copied from interface:ConnectionReinitializes the connection to switch to a different session. This allows for XEP-0198 resumption and transport-switching.- Specified by:
reinitin interfaceConnection- Overrides:
reinitin classAbstractConnection- Parameters:
owner- The new session now owning the connection.
-
isInitialized
public boolean isInitialized()
Description copied from interface:ConnectionChecks if the connection has finished initialization.- Returns:
- true if connection has finished initialization.
-
isClosed
public boolean isClosed()
Description copied from interface:ConnectionReturns true if the connection/session is closed.- Returns:
- true if the connection is closed.
-
isSecure
@Deprecated public boolean isSecure()
Deprecated.Description copied from interface:ConnectionReturns true if this connection is secure.- Returns:
- true if the connection is secure (e.g. TLS)
-
isEncrypted
public boolean isEncrypted()
Description copied from interface:ConnectionReturns true if this connection is encrypted.- Returns:
- true if the connection is encrypted (e.g. uses TLS)
-
setEncrypted
public void setEncrypted(boolean encrypted)
-
deliver
public void deliver(org.xmpp.packet.Packet packet) throws UnauthorizedExceptionDescription copied from interface:ConnectionDelivers the packet to this connection without checking the recipient. The method essentially callssocket.send(packet.getWriteBuffer()). Use with caution! This code is unlikely to be called directly. Instead, ensure that data sent to the entities is sent through the appropriate LocalSession object. For clients, this prevents, for example, synchronisation issues with stanza counts related to Stream Management (XEP-0198).- Parameters:
packet- the packet to deliver.- Throws:
UnauthorizedException- if a permission error was detected.
-
deliverRawText
public void deliverRawText(String text)
Description copied from interface:ConnectionDelivers raw text to this connection. This is a very low level way for sending XML stanzas to the client. This method should not be used unless you have very good reasons for not usingConnection.deliver(org.xmpp.packet.Packet).This method avoids having to get the writer of this connection and mess directly with the writer. Therefore, this method ensures a correct delivery of the stanza even if other threads were sending data concurrently.
- Parameters:
text- the XML stanzas represented kept in a String.
-
startTLS
public void startTLS(boolean clientMode, boolean directTLS) throws ExceptionDescription copied from interface:ConnectionEncrypts the plain connection by negotiating TLS with the other peer. In a server-2-server connection the server requesting the TLS negotiation will be the client and the other server will be the server during the TLS negotiation. Therefore, the server requesting the TLS negotiation must passtruein theclientModeparameter and the server receiving the TLS request must passfalsein theclientModeparameter.In the case of client-2-server the XMPP server must pass
falsein theclientModeparameter since it will behave as the server in the TLS negotiation.- Parameters:
clientMode- boolean indicating if this entity is a client or a server in the TLS negotiation.directTLS- boolean indicating if the negotiation is directTLS (true) or startTLS (false).- Throws:
Exception- if an error occurred while encrypting the connection.
-
addCompression
public void addCompression()
Description copied from interface:ConnectionAdds the compression filter to the connection but only filter incoming traffic. Do not filter outgoing traffic since we still need to send an uncompressed stanza to the client indicating that he can start compressing the traffic. After we sent the uncompresses stanza we can start compression outgoing traffic as well.
-
startCompression
public void startCompression()
Description copied from interface:ConnectionStart compressing outgoing traffic for this connection. Compression will only be available after TLS has been negotiated. This means that a connection can never be using compression before TLS. However, it is possible to use compression without TLS.
-
getConfiguration
public ConnectionConfiguration getConfiguration()
Description copied from interface:ConnectionReturns a representation of the desired state for this connection. Note that this is different from the current state of the connection. For example, TLS can be required by configuration, but while the connection has yet to be fully initialized, the current state might not be TLS-encrypted.- Returns:
- The desired configuration for the connection (never null).
-
isCompressed
public boolean isCompressed()
Description copied from interface:ConnectionReturns true if the connection is using compression.- Returns:
- true if the connection is using compression.
-
-