Class TlsConnection

Object
com.zebra.sdk.comm.ConnectionA
com.zebra.sdk.comm.TlsConnection
All Implemented Interfaces:
Connection, ConnectionWithWriteLogging, com.zebra.sdk.comm.internal.ConnectionI, IpAddressable
Direct Known Subclasses:
TlsStatusConnection

public class TlsConnection extends ConnectionA implements IpAddressable, com.zebra.sdk.comm.internal.ConnectionI
Establishes a TLS connection to a device


package test.zebra.sdk.comm.examples;
 
 import com.zebra.sdk.comm.Connection;
 import com.zebra.sdk.comm.ConnectionException;
 import com.zebra.sdk.comm.TlsConfig;
 import com.zebra.sdk.comm.TlsConnection;
 import com.zebra.sdk.printer.ZebraPrinter;
 import com.zebra.sdk.printer.ZebraPrinterFactory;
 import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
 
 public class TlsConnectionExample {
 
     public static void main(String[] args) throws Exception {
         new TlsConnectionExample().sendZplOverTls("1.2.3.4");
         new TlsConnectionExample().printConfigLabelUsingDnsName("PrinterName");
     }
 
     private void sendZplOverTls(String theIpAddress) throws ConnectionException {
         // Instantiate connection for ZPL TLS port at given address
         Connection thePrinterConn = new TlsConnection(theIpAddress, TlsConnection.DEFAULT_TLS_PORT, TlsConfig.trustAll());
 
         try {
             // Open the connection - physical connection is established here.
             thePrinterConn.open();
 
             // This example prints "This is a ZPL test." near the top of the label.
             String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
 
             // Send the data to printer as a byte array.
             thePrinterConn.write(zplData.getBytes());
         } catch (ConnectionException e) {
             // Handle communications error here.
             e.printStackTrace();
         } finally {
             // Close the connection to release resources.
             thePrinterConn.close();
         }
     }
 
     private void printConfigLabelUsingDnsName(String dnsName) throws ConnectionException {
         Connection connection = new TlsConnection(dnsName, 9143, TlsConfig.trustAll());
         try {
             connection.open();
             ZebraPrinter p = ZebraPrinterFactory.getInstance(connection);
             p.printConfigurationLabel();
         } catch (ConnectionException e) {
             e.printStackTrace();
         } catch (ZebraPrinterLanguageUnknownException e) {
             e.printStackTrace();
         } finally {
             // Close the connection to release resources.
             connection.close();
         }
 
     }
 
 }
 
  • Field Details

    • DEFAULT_TLS_PORT

      public static final int DEFAULT_TLS_PORT
      The default TLS port for ZPL devices.
      See Also:
  • Constructor Details

    • TlsConnection

      public TlsConnection(String address, int port, TlsConfig tlsConfig)
      Initializes a new instance of the TlsConnection class. This constructor will use the default timeouts for Connection.read(). The default timeout is a maximum of 5 seconds for any data to be received. If no more data is available after 500 milliseconds the read operation is assumed to be complete.
      To specify timeouts other than the defaults, use:
      TlsConnection(String, int, TlsConfig, int, int).
      Parameters:
      address - the IP Address or DNS Hostname.
      port - the port number.
      tlsConfig - TLS configuration including certificates and protocols.
    • TlsConnection

      public TlsConnection(String address, int port, TlsConfig tlsConfig, int maxTimeoutForRead, int timeToWaitForMoreData)
      Initializes a new instance of the TlsConnection class. This constructor will use the specified timeouts for Connection.read(). The timeout is a maximum of maxTimeoutForRead milliseconds for any data to be received. If no more data is available after timeToWaitForMoreData milliseconds the read operation is assumed to be complete.
      Parameters:
      address - the IP Address or DNS Hostname.
      port - the port number.
      tlsConfig - TLS configuration including certificates and protocols.
      maxTimeoutForRead - the maximum time, in milliseconds, to wait for any data to be received.
      timeToWaitForMoreData - the maximum time, in milliseconds, to wait in-between reads after the initial read.
  • Method Details

    • toString

      public String toString()
      Returns TLS:[address]:[port number].
      The address and port number are the parameters which were passed into the constructor.
      Specified by:
      toString in interface Connection
      Overrides:
      toString in class Object
      Returns:
      the connection description string.
      See Also:
    • getSimpleConnectionName

      public String getSimpleConnectionName()
      Return the IP address as the description.
      Specified by:
      getSimpleConnectionName in interface Connection
      Returns:
      a human-readable description of the connection.
      See Also:
    • getAddress

      public String getAddress()
      Description copied from interface: IpAddressable
      Returns the address which was passed into the constructor.
      Specified by:
      getAddress in interface IpAddressable
      Returns:
      the address used to establish this connection. This can be either a DNS Hostname or an IP address.
      See Also:
    • getPortNumber

      public String getPortNumber()
      Description copied from interface: IpAddressable
      Returns the port number which was passed into the constructor.
      Specified by:
      getPortNumber in interface IpAddressable
      Returns:
      the port number associated with the connection.
      See Also:
    • getTlsConfig

      public TlsConfig getTlsConfig()
      Returns the TLS configuration associated with this connection.
      Returns:
      the TlsConfig used to establish the secure connection.
    • getConnectionReestablisher

      public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
      Description copied from interface: Connection
      Returns a ConnectionReestablisher which allows for easy recreation of a connection which may have been closed.
      • This call should be made while there is still an active connection to the printer (prior to issuing a command which will make the printer non-responsive).
      Specified by:
      getConnectionReestablisher in interface Connection
      Overrides:
      getConnectionReestablisher in class ConnectionA
      Parameters:
      thresholdTime - how long the Connection reestablisher will wait before attempting to reconnection to the printer
      Returns:
      ConnectionReestablisher
      Throws:
      ConnectionException - if the ConnectionReestablisher could not be created.
      See Also:
    • sendAndWaitForResponse

      public byte[] sendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException
      Description copied from interface: Connection
      Sends dataToSend and returns the response data. The software returns immediately if the data received contains terminator. The connection must be open before this method is called. If sendAndWaitForResponse is called when a connection is closed, a ConnectionException is thrown.
      Specified by:
      sendAndWaitForResponse in interface Connection
      Overrides:
      sendAndWaitForResponse in class ConnectionA
      Parameters:
      dataToSend - byte array of data to send
      initialResponseTimeout - The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function returns a zero length array.
      responseCompletionTimeout - After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.
      terminator - If the response contains this string, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response is always terminated with a known string. Use null if no terminator is desired.
      Returns:
      received data
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • sendAndWaitForResponse

      public void sendAndWaitForResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException
      Description copied from interface: Connection
      Sends data from sourceStream and writes the response data to destinationStream. The software returns immediately if the data received contains terminator. The connection must be open before this method is called. If sendAndWaitForResponse is called when a connection is closed, a ConnectionException is thrown.
      Specified by:
      sendAndWaitForResponse in interface Connection
      Overrides:
      sendAndWaitForResponse in class ConnectionA
      Parameters:
      destinationStream - Destination for response.
      sourceStream - Source of data to be sent.
      initialResponseTimeout - The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function does not write any data to the destination stream.
      responseCompletionTimeout - After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.
      terminator - If the response contains this string, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response is always terminated with a known string. Use null if no terminator is desired.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • sendAndWaitForValidResponse

      public byte[] sendAndWaitForValidResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException
      Description copied from interface: Connection
      Sends dataToSend and returns the response data. The software returns immediately if the data received satisfies validator. The connection must be open before this method is called. If sendAndWaitForResponse is called when a connection is closed, a ConnectionException is thrown.
      Specified by:
      sendAndWaitForValidResponse in interface Connection
      Overrides:
      sendAndWaitForValidResponse in class ConnectionA
      Parameters:
      dataToSend - byte array of data to send
      initialResponseTimeout - The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function returns a zero length array.
      responseCompletionTimeout - After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.
      validator - If the response satisfies this validator, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response follows a known format.
      Returns:
      received data
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • sendAndWaitForValidResponse

      public void sendAndWaitForValidResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException
      Description copied from interface: Connection
      Sends data from sourceStream and writes the response data to destinationStream. The software returns immediately if the data received satisfies validator. The connection must be open before this method is called. If sendAndWaitForResponse is called when a connection is closed, a ConnectionException is thrown.
      Specified by:
      sendAndWaitForValidResponse in interface Connection
      Overrides:
      sendAndWaitForValidResponse in class ConnectionA
      Parameters:
      destinationStream - Destination for response.
      sourceStream - Source of data to be sent.
      initialResponseTimeout - The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function does not write any data to the destination stream.
      responseCompletionTimeout - After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.
      validator - If the response satisfies this validator, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response follows a known format. If validator is null, no validation is performed. When performing validation, this method will use enough memory to hold the entire response.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also: