improved WhoIsParser

This commit is contained in:
jomu
2016-08-26 22:05:51 +00:00
parent 5dc41d120a
commit 5eb53f9eae
5 changed files with 85 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package de.muehlencord.shared.network.whois;
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
import java.util.ArrayList;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -69,9 +70,9 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
// FIXME add CDIR notation support
String startIpAddress = ipAddresses[0];
String endIPAddress = ipAddresses[2];
whoisInformation.getNetwork().addAll(rangeToCidrList(startIpAddress, endIPAddress));
whoisInformation.addNetwork(rangeToCidrList(startIpAddress, endIPAddress));
} else {
whoisInformation.getNetwork().add(inetnum);
whoisInformation.addNetwork(inetnum);
}
LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
@ -88,7 +89,7 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
throw new WhoisException ("Error while reading route information: "+ex.getMessage(), ex);
}
*/
whoisInformation.getRootNetwork().add(route);
whoisInformation.addRootNetwork(route);
LOGGER.info("Root network " + whoisInformation.getRootNetwork().toString());
break;
@ -109,6 +110,7 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
break;
default:
LOGGER.info("Unknown block found");
LOGGER.error(block);
}
}
}

View File

@ -10,11 +10,17 @@ import java.util.List;
*/
public class WhoisInformation {
/** network list in CIDR form the host belongs to */
/**
* network list in CIDR form the host belongs to
*/
private List<String> network;
/** network list in CIDDR form network belongs to */
/**
* network list in CIDDR form network belongs to
*/
private List<String> rootNetwork;
/** network information of host */
/**
* network information of host
*/
private NetworkInformation networkInformation;
public WhoisInformation() {
@ -28,7 +34,11 @@ public class WhoisInformation {
* @return the network
*/
public List<String> getNetwork() {
return new ArrayList<>(network);
return new ArrayList<>(network);
}
public void addNetwork(List<String> network) {
this.network.addAll(new ArrayList<>(network));
}
/**
@ -38,11 +48,20 @@ public class WhoisInformation {
this.network = new ArrayList<>(network);
}
/**
* add a new network to the list of existing networks
*
* @param network the new network to add
*/
public void addNetwork(String network) {
this.network.add(network);
}
/**
* @return the rootNetwork
*/
public List<String> getRootNetwork() {
return new ArrayList<>(rootNetwork);
return new ArrayList<>(rootNetwork);
}
/**
@ -51,6 +70,10 @@ public class WhoisInformation {
public void setRootNetwork(List<String> rootNetwork) {
this.rootNetwork = new ArrayList<>(rootNetwork);
}
public void addRootNetwork (String rootNetwork) {
this.rootNetwork.add (rootNetwork);
}
/**
* @return the networkInformation

View File

@ -2,6 +2,7 @@ package de.muehlencord.shared.network.whois;
import de.muehlencord.shared.network.BaseTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
@ -74,6 +75,19 @@ public class WhoisTest extends BaseTest {
whoIsClient.execute("whois.arin.net", "32.64.68.229");
}
@Test
public void testGeneric() throws WhoisException {
Whois whoIsClient = new Whois();
WhoisInformation info = whoIsClient.execute("whois.ripe.net", "185.125.4.210");
assertNotNull (info);
assertNotNull (info.getNetwork());
assertFalse (info.getNetwork().isEmpty());
System.out.println (info.getNetwork().toString());
System.out.println (info.getRootNetwork().toString());
System.out.println (info.getNetworkInformation().getDescription());
}
@Test
@Ignore

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c] %m%n" />
</layout>
</appender>
<category name="de.muehlencord">
<priority value="DEBUG"/>
</category>
<category name="com.sun">
<priority value="WARN"/>
</category>
<category name="javax.xml">
<priority value="WARN"/>
</category>
<category name="org.apache.commons">
<priority value="WARN"/>
</category>
<root>
<level value="DEBUG" />
<appender-ref ref="consoleAppender" />
</root>
</log4j:configuration>