use of org.xbill.DNS.TextParseException in project Smack by igniterealtime.
the class DNSJavaResolver method lookupSRVRecords0.
@Override
protected List<SRVRecord> lookupSRVRecords0(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
List<SRVRecord> res = new ArrayList<SRVRecord>();
Lookup lookup;
try {
lookup = new Lookup(name, Type.SRV);
} catch (TextParseException e) {
throw new IllegalStateException(e);
}
Record[] recs = lookup.run();
if (recs == null)
return res;
for (Record record : recs) {
org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
if (srvRecord != null && srvRecord.getTarget() != null) {
String host = srvRecord.getTarget().toString();
int port = srvRecord.getPort();
int priority = srvRecord.getPriority();
int weight = srvRecord.getWeight();
List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
if (hostAddresses == null) {
continue;
}
SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses);
res.add(r);
}
}
return res;
}
use of org.xbill.DNS.TextParseException in project GNS by MobilityFirst.
the class NameResolution method getARecordsFromAField.
/**
* retrieve all A records from A field of a JSON object
*
* @return
*/
private static JSONArray getARecordsFromAField(JSONObject fieldResponseJson, String nameToResolve) {
JSONArray aList = new JSONArray();
/**
* Format of A record in GNS:
* {
* "A":
* {
* "record": String[ip1, ip2, ...],
* "ttl": int
* }
* }
*/
if (fieldResponseJson.has("A")) {
JSONArray records = null;
int ttl = 60;
try {
JSONObject recordObj = fieldResponseJson.getJSONObject("A");
records = recordObj.getJSONArray(ManagedDNSServiceProxy.RECORD_FIELD);
ttl = recordObj.getInt(ManagedDNSServiceProxy.TTL_FIELD);
} catch (JSONException e) {
// something is wrong with the JSON object, return null
e.printStackTrace();
return null;
}
// The records may contain multiple ip addresses
for (int i = 0; i < records.length(); i++) {
try {
String ip = records.getString(i);
ARecord gnsARecord = new ARecord(new Name(nameToResolve), DClass.IN, ttl, InetAddress.getByName(ip));
aList.put(gnsARecord);
} catch (JSONException | TextParseException | UnknownHostException e) {
// do nothing, just trash this record
e.printStackTrace();
}
}
} else {
// there is no A record in the original GNS record, so return null
return null;
}
return aList;
}
use of org.xbill.DNS.TextParseException in project GNS by MobilityFirst.
the class NameResolution method getMXRecordsFromMXField.
/**
* retrieve MX record from MX field of a JSON object
* the key "MX" contains a list of all MX records
* the key "A" contains a list of all A records, which must be put into ADDITIONAL section
*
* @return
*/
private static JSONObject getMXRecordsFromMXField(JSONObject fieldResponseJson, String nameToResolve) {
JSONObject obj = new JSONObject();
JSONArray mxList = new JSONArray();
JSONArray aList = new JSONArray();
if (fieldResponseJson.has("MX")) {
JSONArray records = null;
int ttl = 3600;
try {
JSONObject mxname = fieldResponseJson.getJSONObject("MX");
records = mxname.getJSONArray(ManagedDNSServiceProxy.RECORD_FIELD);
ttl = mxname.getInt(ManagedDNSServiceProxy.TTL_FIELD);
} catch (JSONException e) {
// something is wrong with the JSON object, return null
e.printStackTrace();
return null;
}
NameResolution.getLogger().log(Level.FINE, "Get MX records list: {0}", records.toString());
// The records may contain multiple NS records
for (int i = 0; i < records.length(); i++) {
try {
JSONArray record = records.getJSONArray(i);
String pString = record.getString(0);
int priority = Integer.parseInt(pString);
String host = record.getString(1);
// the host must be an absolute name, i.e., ended with a dot
if (!host.endsWith(".")) {
host = host + ".";
}
MXRecord mxRecord = new MXRecord(new Name(nameToResolve), DClass.IN, ttl, priority, new Name(host));
mxList.put(mxRecord);
if (record.length() == 3) {
String address = record.getString(2);
ARecord mxARecord = new ARecord(new Name(host), DClass.IN, ttl, InetAddress.getByName(address));
aList.put(mxARecord);
} else {
// no IP address in the record for the mail server
}
} catch (JSONException | TextParseException | UnknownHostException e) {
// trash this record
e.printStackTrace();
}
}
}
try {
obj.put("MX", mxList);
obj.put("A", aList);
} catch (JSONException e) {
// return a null if JSON operation fails
return null;
}
return obj;
}
use of org.xbill.DNS.TextParseException in project GNS by MobilityFirst.
the class NameResolution method getNSRecordsFromNSField.
/**
* retrieve all NS records and the corresponding A records from NS field of a JSON object.
* the key "NS" contains a list of all NS records
* the key "A" contains a list of all A records, which must be put into ADDITIONAL section
*
*/
private static JSONObject getNSRecordsFromNSField(JSONObject fieldResponseJson, String nameToResolve) {
JSONObject obj = new JSONObject();
JSONArray aList = new JSONArray();
JSONArray nsList = new JSONArray();
/**
* Format of NS record in GNS:
* {
* "NS":
* {
* "record":[(ns1, addr1), (ns2, addr2), ...],
* "ttl":int
* }
* }
*
*/
if (fieldResponseJson.has("NS")) {
JSONArray records = null;
int ttl = 3600;
try {
JSONObject recordObj = fieldResponseJson.getJSONObject("NS");
records = recordObj.getJSONArray(ManagedDNSServiceProxy.RECORD_FIELD);
ttl = recordObj.getInt(ManagedDNSServiceProxy.TTL_FIELD);
} catch (JSONException e) {
// something is wrong with the JSON object, return null
e.printStackTrace();
return null;
}
// The records may contain multiple NS records
for (int i = 0; i < records.length(); i++) {
try {
JSONArray record = records.getJSONArray(i);
String ns = record.getString(0);
// It must be an absolute name, i.e., the string must be ended with a dot, e.g., example.com.
if (!ns.endsWith(".")) {
ns = ns + ".";
}
NSRecord nsRecord = new NSRecord(new Name(nameToResolve), DClass.IN, ttl, new Name(ns));
nsList.put(nsRecord);
// address can be null as the domain name might use other service as its name server
if (record.length() == 2) {
String address = record.getString(1);
ARecord nsARecord = new ARecord(new Name(ns), DClass.IN, ttl, InetAddress.getByName(address));
aList.put(nsARecord);
} else {
// no IP address in the record for the name server
}
} catch (JSONException | TextParseException | UnknownHostException e) {
// do nothing and trash this record
e.printStackTrace();
}
}
} else {
// No NS record, return null
}
try {
obj.put("NS", nsList);
obj.put("A", aList);
} catch (JSONException e) {
// return a null if JSON operation fails
return null;
}
return obj;
}
use of org.xbill.DNS.TextParseException in project ORCID-Source by ORCID.
the class OrcidDomainValidator method isValid.
public boolean isValid(String hostLocation) {
try {
Lookup lookup = new Lookup(hostLocation);
lookup.run();
int result = lookup.getResult();
if (Lookup.SUCCESSFUL == result) {
return true;
}
if (Lookup.HOST_NOT_FOUND == result || Lookup.TYPE_NOT_FOUND == result) {
return false;
}
LOGGER.warn("DNS is not OK, so validating in offline mode");
return standardDomainValidator.isValid(hostLocation);
} catch (TextParseException e) {
return false;
}
}
Aggregations