Package com.zebra.sdk.comm
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default TLS port for ZPL devices. -
Constructor Summary
ConstructorsConstructorDescriptionTlsConnection(String address, int port, TlsConfig tlsConfig) Initializes a new instance of theTlsConnectionclass.TlsConnection(String address, int port, TlsConfig tlsConfig, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theTlsConnectionclass. -
Method Summary
Modifier and TypeMethodDescriptionReturns the address which was passed into the constructor.getConnectionReestablisher(long thresholdTime) Returns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.Returns the port number which was passed into the constructor.Return the IP address as the description.Returns the TLS configuration associated with this connection.byte[]sendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, String terminator) SendsdataToSendand returns the response data.voidsendAndWaitForResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, String terminator) Sends data fromsourceStreamand writes the response data to destinationStream.byte[]sendAndWaitForValidResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) SendsdataToSendand returns the response data.voidsendAndWaitForValidResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) Sends data fromsourceStreamand writes the response data to destinationStream.toString()ReturnsTLS:[address]:[port number].
Theaddressandport numberare the parameters which were passed into the constructor.Methods inherited from class com.zebra.sdk.comm.ConnectionA
addWriteLogStream, bytesAvailable, close, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, read, read, readChar, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write, write, writeMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.zebra.sdk.comm.Connection
bytesAvailable, close, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, readChar, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, writeMethods inherited from interface com.zebra.sdk.comm.internal.ConnectionI
getManufacturer, getMaxDataToWrite, read, read, setMaxDataToWrite, setReadTimeout
-
Field Details
-
DEFAULT_TLS_PORT
public static final int DEFAULT_TLS_PORTThe default TLS port for ZPL devices.- See Also:
-
-
Constructor Details
-
TlsConnection
Initializes a new instance of theTlsConnectionclass. This constructor will use the default timeouts forConnection.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 theTlsConnectionclass. This constructor will use the specified timeouts forConnection.read(). The timeout is a maximum ofmaxTimeoutForReadmilliseconds for any data to be received. If no more data is available aftertimeToWaitForMoreDatamilliseconds 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
ReturnsTLS:[address]:[port number].
Theaddressandport numberare the parameters which were passed into the constructor.- Specified by:
toStringin interfaceConnection- Overrides:
toStringin classObject- Returns:
- the connection description string.
- See Also:
-
getSimpleConnectionName
Return the IP address as the description.- Specified by:
getSimpleConnectionNamein interfaceConnection- Returns:
- a human-readable description of the connection.
- See Also:
-
getAddress
Description copied from interface:IpAddressableReturns the address which was passed into the constructor.- Specified by:
getAddressin interfaceIpAddressable- Returns:
- the address used to establish this connection. This can be either a DNS Hostname or an IP address.
- See Also:
-
getPortNumber
Description copied from interface:IpAddressableReturns the port number which was passed into the constructor.- Specified by:
getPortNumberin interfaceIpAddressable- Returns:
- the port number associated with the connection.
- See Also:
-
getTlsConfig
Returns the TLS configuration associated with this connection.- Returns:
- the
TlsConfigused to establish the secure connection.
-
getConnectionReestablisher
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException Description copied from interface:ConnectionReturns aConnectionReestablisherwhich 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:
getConnectionReestablisherin interfaceConnection- Overrides:
getConnectionReestablisherin classConnectionA- 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:ConnectionSendsdataToSendand returns the response data. The software returns immediately if the data received containsterminator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForResponsein interfaceConnection- Overrides:
sendAndWaitForResponsein classConnectionA- Parameters:
dataToSend- byte array of data to sendinitialResponseTimeout- 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. Usenullif 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:ConnectionSends data fromsourceStreamand writes the response data to destinationStream. The software returns immediately if the data received containsterminator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForResponsein interfaceConnection- Overrides:
sendAndWaitForResponsein classConnectionA- 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. Usenullif 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:ConnectionSendsdataToSendand returns the response data. The software returns immediately if the data received satisfiesvalidator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForValidResponsein interfaceConnection- Overrides:
sendAndWaitForValidResponsein classConnectionA- Parameters:
dataToSend- byte array of data to sendinitialResponseTimeout- 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:ConnectionSends data fromsourceStreamand writes the response data to destinationStream. The software returns immediately if the data received satisfiesvalidator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForValidResponsein interfaceConnection- Overrides:
sendAndWaitForValidResponsein classConnectionA- 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:
-