Class SecureTransport

java.lang.Object
org.simpleframework.transport.SecureTransport
All Implemented Interfaces:
Socket, Transport

class SecureTransport extends Object implements Transport
The SecureTransport object provides an implementation of a transport used to send and receive data over SSL. Data read from this transport is decrypted using an SSLEngine. Also, all data is written is encrypted with the same engine. This ensures that data can be send and received in a transparent way.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Certificate
    This is the certificate associated with this SSL connection.
    private boolean
    This is used to determine if the transport was closed.
    private SSLEngine
    This is the SSL engine used to encrypt and decrypt data.
    private boolean
    This is used to determine if the end of stream was reached.
    private ByteBuffer
    This is the internal buffer used to exchange the SSL data.
    private ByteBuffer
    This buffer is used to output the data for the SSL sent.
    private ByteBuffer
    This is the internal buffer used to exchange the SSL data.
    private Trace
    This is the trace that is used to monitor socket activity.
    private Transport
    This is the transport used to send data over the socket.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SecureTransport(Transport transport, Certificate certificate, ByteBuffer input, ByteBuffer swap)
    Constructor for the SecureTransport object.
    SecureTransport(Transport transport, Certificate certificate, ByteBuffer input, ByteBuffer swap, int size)
    Constructor for the SecureTransport object.
  • Method Summary

    Modifier and Type
    Method
    Description
    private int
    append(ByteBuffer buffer, int count)
    This will append bytes within the transport to the given buffer.
    void
    This is used to close the sender and the underlying transport.
    private int
    fill(ByteBuffer buffer)
    This is used to fill the provided buffer with data that has been read from the secure socket channel.
    private int
    fill(ByteBuffer buffer, int count)
    This is used to fill the provided buffer with data that has been read from the secure socket channel.
    void
    This method is used to flush the contents of the buffer to the client.
    This method is used to get the Map of attributes by this pipeline.
    This is used to acquire the SSL certificate used when the server is using a HTTPS connection.
    This method is used to acquire the SocketChannel for the connection.
    This is used to acquire the SSL engine used for HTTPS.
    This is used to acquire the trace object that is associated with the socket.
    private int
    This is used to perform a non-blocking read on the transport.
    int
    read(ByteBuffer buffer)
    This is used to perform a non-blocking read on the transport.
    private void
    This is used to perform a non-blocking read on the transport.
    private void
    send(ByteBuffer buffer)
    This method is used to deliver the provided buffer of bytes to the underlying transport.
    void
    write(ByteBuffer buffer)
    This method is used to deliver the provided buffer of bytes to the underlying transport.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • certificate

      private Certificate certificate
      This is the certificate associated with this SSL connection.
    • transport

      private Transport transport
      This is the transport used to send data over the socket.
    • output

      private ByteBuffer output
      This buffer is used to output the data for the SSL sent.
    • input

      private ByteBuffer input
      This is the internal buffer used to exchange the SSL data.
    • swap

      private ByteBuffer swap
      This is the internal buffer used to exchange the SSL data.
    • engine

      private SSLEngine engine
      This is the SSL engine used to encrypt and decrypt data.
    • trace

      private Trace trace
      This is the trace that is used to monitor socket activity.
    • closed

      private boolean closed
      This is used to determine if the transport was closed.
    • finished

      private boolean finished
      This is used to determine if the end of stream was reached.
  • Constructor Details

    • SecureTransport

      public SecureTransport(Transport transport, Certificate certificate, ByteBuffer input, ByteBuffer swap)
      Constructor for the SecureTransport object. This is used to create a transport for sending and receiving data over SSL. This must be created with a pipeline that has already performed the SSL handshake and is read to used.
      Parameters:
      transport - this is the transport to delegate operations to
      certificate - this is the certificate for the connection
      input - this is the input buffer used to read the data
      swap - this is the swap buffer to be used for reading
    • SecureTransport

      public SecureTransport(Transport transport, Certificate certificate, ByteBuffer input, ByteBuffer swap, int size)
      Constructor for the SecureTransport object. This is used to create a transport for sending and receiving data over SSL. This must be created with a pipeline that has already performed the SSL handshake and is read to used.
      Parameters:
      transport - this is the transport to delegate operations to
      certificate - this is the certificate for the connection
      input - this is the input buffer used to read the data
      swap - this is the swap buffer to be used for reading
      size - this is the size of the buffers to be allocated
  • Method Details

    • getCertificate

      public Certificate getCertificate()
      This is used to acquire the SSL certificate used when the server is using a HTTPS connection. For plain text connections or connections that use a security mechanism other than SSL this will be null. This is only available when the connection makes specific use of an SSL engine to secure the connection.
      Specified by:
      getCertificate in interface Transport
      Returns:
      this returns the associated SSL certificate if any
    • getTrace

      public Trace getTrace()
      This is used to acquire the trace object that is associated with the socket. A trace object is used to collection details on what operations are being performed on the socket. For instance it may contain information relating to I/O events or more application specific events such as errors.
      Specified by:
      getTrace in interface Socket
      Returns:
      this returns the trace associated with this socket
    • getEngine

      public SSLEngine getEngine()
      This is used to acquire the SSL engine used for HTTPS. If the pipeline is connected to an SSL transport this returns an SSL engine which can be used to establish the secure connection and send and receive content over that connection. If this is null then the pipeline represents a normal transport.
      Specified by:
      getEngine in interface Socket
      Returns:
      the SSL engine used to establish a secure transport
    • getAttributes

      public Map getAttributes()
      This method is used to get the Map of attributes by this pipeline. The attributes map is used to maintain details about the connection. Information such as security credentials to client details can be placed within the attribute map.
      Specified by:
      getAttributes in interface Socket
      Returns:
      this returns the map of attributes for this pipeline
    • getChannel

      public SocketChannel getChannel()
      This method is used to acquire the SocketChannel for the connection. This allows the server to acquire the input and output streams with which to communicate. It can also be used to configure the connection and perform various network operations that could otherwise not be performed.
      Specified by:
      getChannel in interface Socket
      Returns:
      this returns the socket used by this HTTP pipeline
    • read

      public int read(ByteBuffer buffer) throws IOException
      This is used to perform a non-blocking read on the transport. If there are no bytes available on the input buffers then this method will return zero and the buffer will remain the same. If there is data and the buffer can be filled then this will return the number of bytes read. Finally if the socket is closed this will return a -1 value.
      Specified by:
      read in interface Transport
      Parameters:
      buffer - this is the buffer to append the bytes to
      Returns:
      this returns the number of bytes that have been read
      Throws:
      IOException
    • process

      private int process(ByteBuffer buffer) throws IOException
      This is used to perform a non-blocking read on the transport. If there are no bytes available on the input buffers then this method will return zero and the buffer will remain the same. If there is data and the buffer can be filled then this will return the number of bytes read.
      Parameters:
      buffer - this is the buffer to append the bytes to
      Returns:
      this returns the number of bytes that have been read
      Throws:
      IOException
    • fill

      private int fill(ByteBuffer buffer) throws IOException
      This is used to fill the provided buffer with data that has been read from the secure socket channel. This enables reading of the decrypted data in chunks that are smaller than the size of the input buffer used to contain the plain text data.
      Parameters:
      buffer - this is the buffer to append the bytes to
      Returns:
      this returns the number of bytes that have been read
      Throws:
      IOException
    • fill

      private int fill(ByteBuffer buffer, int count) throws IOException
      This is used to fill the provided buffer with data that has been read from the secure socket channel. This enables reading of the decrypted data in chunks that are smaller than the size of the input buffer used to contain the plain text data.
      Parameters:
      buffer - this is the buffer to append the bytes to
      count - this is the number of bytes that are to be read
      Returns:
      this returns the number of bytes that have been read
      Throws:
      IOException
    • append

      private int append(ByteBuffer buffer, int count) throws IOException
      This will append bytes within the transport to the given buffer. Once invoked the buffer will contain the transport bytes, which will have been drained from the buffer. This effectively moves the bytes in the buffer to the end of the packet instance.
      Parameters:
      buffer - this is the buffer containing the bytes
      count - this is the number of bytes that should be used
      Returns:
      returns the number of bytes that have been moved
      Throws:
      IOException
    • receive

      private void receive() throws IOException
      This is used to perform a non-blocking read on the transport. If there are no bytes available on the input buffers then this method will return zero and the buffer will remain the same. If there is data and the buffer can be filled then this will return the number of bytes read. Finally if the socket is closed this will return a -1 value.
      Throws:
      IOException
    • write

      public void write(ByteBuffer buffer) throws IOException
      This method is used to deliver the provided buffer of bytes to the underlying transport. Depending on the connection type the array may be encoded for SSL transport or send directly. Any implementation may choose to buffer the bytes for performance.
      Specified by:
      write in interface Transport
      Parameters:
      buffer - this is the array of bytes to send to the client
      Throws:
      IOException
    • send

      private void send(ByteBuffer buffer) throws IOException
      This method is used to deliver the provided buffer of bytes to the underlying transport. Depending on the connection type the array may be encoded for SSL transport or send directly. Any implementation may choose to buffer the bytes for performance.
      Parameters:
      buffer - this is the array of bytes to send to the client
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      This method is used to flush the contents of the buffer to the client. This method will block until such time as all of the data has been sent to the client. If at any point there is an error sending the content an exception is thrown.
      Specified by:
      flush in interface Transport
      Throws:
      IOException
    • close

      public void close() throws IOException
      This is used to close the sender and the underlying transport. If a close is performed on the sender then no more bytes can be read from or written to the transport and the client will received a connection close on their side.
      Specified by:
      close in interface Transport
      Throws:
      IOException