diff --git a/pom.xml b/pom.xml index ef1a771..ab06b6c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ me.legrange mikrotik - 1.1-SNAPSHOT + 1.2 jar Mikrotik API Java Client Library diff --git a/src/main/java/examples/Example.java b/src/main/java/examples/Example.java index 7c55df4..33dea60 100644 --- a/src/main/java/examples/Example.java +++ b/src/main/java/examples/Example.java @@ -9,7 +9,7 @@ import me.legrange.mikrotik.ApiConnection; abstract class Example { protected void connect() throws Exception { - con = ApiConnection.connectTLS(Config.HOST); + con = ApiConnection.connect(Config.HOST); con.login(Config.USERNAME, Config.PASSWORD); } diff --git a/src/main/java/examples/Example7.java b/src/main/java/examples/Example7.java new file mode 100644 index 0000000..42be235 --- /dev/null +++ b/src/main/java/examples/Example7.java @@ -0,0 +1,29 @@ +package examples; + +import java.util.List; +import java.util.Map; +import me.legrange.mikrotik.MikrotikApiException; + +/** + * Example 7: Dump your complete config + * + * @author gideon + */ +public class Example7 extends Example { + + public static void main(String... args) throws Exception { + Example7 ex = new Example7(); + ex.connect(); + ex.test(); + ex.disconnect(); + } + + private void test() throws MikrotikApiException, InterruptedException { + con.execute("/export file=conf"); + List> res = con.execute("/file/print detail where name=conf.rsc"); + String text = res.get(0).get("contents"); + for (String line : text.split("\r")) { + System.out.println(line); + } + } +} diff --git a/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java b/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java index 435c236..fc48278 100644 --- a/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java +++ b/src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java @@ -8,8 +8,6 @@ import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; -import java.security.Provider; -import java.security.Security; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; @@ -33,7 +31,9 @@ public final class ApiConnectionImpl extends ApiConnection { * * @param host The host to which to connect. * @param port The TCP port to use. + * @param secure Is TLS required * @return The ApiConnection + * @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a problem connecting */ public static ApiConnection connect(String host, int port, boolean secure) throws ApiConnectionException { ApiConnectionImpl con = new ApiConnectionImpl(); @@ -46,13 +46,16 @@ public final class ApiConnectionImpl extends ApiConnection { * * @return if connection is established to router it returns true. */ + @Override public boolean isConnected() { return connected; } /** * Disconnect from the remote API + * @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a problem disconnecting */ + @Override public void disconnect() throws ApiConnectionException { if (!connected) { throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik")); @@ -341,7 +344,12 @@ public final class ApiConnectionImpl extends ApiConnection { if (line.startsWith(("="))) { String parts[] = line.split("=", 3); if (parts.length == 3) { - res.put(parts[1], parts[2]); + if (!parts[2].endsWith("\r")) { + res.put(parts[1], parts[2]); + } + else { + res.put(parts[1], unpackMultiLine(parts[2])); + } } else { throw new ApiDataException(String.format("Malformed line '%s'", line)); } @@ -363,6 +371,15 @@ public final class ApiConnectionImpl extends ApiConnection { return res; } + private String unpackMultiLine(String head) throws ApiConnectionException, ApiDataException { + StringBuilder buf = new StringBuilder(head); + do { + nextLine(); + buf.append(line); + } while (line.endsWith("\r")); + return buf.toString(); + } + private Done unpackDone() throws MikrotikApiException { Done done = new Done(null); if (hasNextLine()) {