From 7390be0aa1b086c68aa64f9c47d697cf27e1a560 Mon Sep 17 00:00:00 2001 From: Gideon le Grange Date: Fri, 1 Jan 2016 10:03:04 +0200 Subject: [PATCH] Added ConnectTLS example --- src/main/java/examples/ConnectTLS.java | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/examples/ConnectTLS.java diff --git a/src/main/java/examples/ConnectTLS.java b/src/main/java/examples/ConnectTLS.java new file mode 100644 index 0000000..29f338a --- /dev/null +++ b/src/main/java/examples/ConnectTLS.java @@ -0,0 +1,54 @@ +/* + * Copyright 2015 gideon. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package examples; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLSocketFactory; +import me.legrange.mikrotik.ApiConnection; +import me.legrange.mikrotik.MikrotikApiException; + +/** + * Example: Open a TLS connection + * + * @author gideon + */ +public class ConnectTLS { + + public static void main(String... args) throws Exception { + ConnectTLS ex = new ConnectTLS(); + ex.connect(); + ex.test(); + ex.disconnect(); + } + + private void test() throws MikrotikApiException { + con.execute("/system/reboot"); + } + + protected void connect() throws Exception { + SocketFactory fact = SSLSocketFactory.getDefault(); + + con = ApiConnection.connect(fact, Config.HOST, ApiConnection.DEFAULT_TLS_PORT, 2000); + con.login(Config.USERNAME, Config.PASSWORD); + } + + protected void disconnect() throws Exception { + con.disconnect(); + } + + private ApiConnection con; +}