Added support for multi-line results
This commit is contained in:
parent
666c6c4e41
commit
afe511b593
2
pom.xml
2
pom.xml
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
<groupId>me.legrange</groupId>
|
<groupId>me.legrange</groupId>
|
||||||
<artifactId>mikrotik</artifactId>
|
<artifactId>mikrotik</artifactId>
|
||||||
<version>1.1-SNAPSHOT</version>
|
<version>1.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Mikrotik API Java Client Library</name>
|
<name>Mikrotik API Java Client Library</name>
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import me.legrange.mikrotik.ApiConnection;
|
||||||
abstract class Example {
|
abstract class Example {
|
||||||
|
|
||||||
protected void connect() throws Exception {
|
protected void connect() throws Exception {
|
||||||
con = ApiConnection.connectTLS(Config.HOST);
|
con = ApiConnection.connect(Config.HOST);
|
||||||
con.login(Config.USERNAME, Config.PASSWORD);
|
con.login(Config.USERNAME, Config.PASSWORD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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<Map<String, String>> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,8 +8,6 @@ import java.io.UnsupportedEncodingException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.security.Provider;
|
|
||||||
import java.security.Security;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
@ -33,7 +31,9 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
*
|
*
|
||||||
* @param host The host to which to connect.
|
* @param host The host to which to connect.
|
||||||
* @param port The TCP port to use.
|
* @param port The TCP port to use.
|
||||||
|
* @param secure Is TLS required
|
||||||
* @return The ApiConnection
|
* @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 {
|
public static ApiConnection connect(String host, int port, boolean secure) throws ApiConnectionException {
|
||||||
ApiConnectionImpl con = new ApiConnectionImpl();
|
ApiConnectionImpl con = new ApiConnectionImpl();
|
||||||
|
|
@ -46,13 +46,16 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
*
|
*
|
||||||
* @return if connection is established to router it returns true.
|
* @return if connection is established to router it returns true.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return connected;
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect from the remote API
|
* Disconnect from the remote API
|
||||||
|
* @throws me.legrange.mikrotik.ApiConnectionException Thrown if there is a problem disconnecting
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void disconnect() throws ApiConnectionException {
|
public void disconnect() throws ApiConnectionException {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik"));
|
throw new ApiConnectionException(("Not/no longer connected to remote Mikrotik"));
|
||||||
|
|
@ -341,7 +344,12 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
if (line.startsWith(("="))) {
|
if (line.startsWith(("="))) {
|
||||||
String parts[] = line.split("=", 3);
|
String parts[] = line.split("=", 3);
|
||||||
if (parts.length == 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 {
|
} else {
|
||||||
throw new ApiDataException(String.format("Malformed line '%s'", line));
|
throw new ApiDataException(String.format("Malformed line '%s'", line));
|
||||||
}
|
}
|
||||||
|
|
@ -363,6 +371,15 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
return res;
|
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 {
|
private Done unpackDone() throws MikrotikApiException {
|
||||||
Done done = new Done(null);
|
Done done = new Done(null);
|
||||||
if (hasNextLine()) {
|
if (hasNextLine()) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue