2013-03-18 09:53:11 +01:00
|
|
|
/**
|
2014-02-17 18:57:38 +01:00
|
|
|
*
|
2014-03-18 17:29:38 +01:00
|
|
|
* Copyright 2013-2014 Florian Schmaus
|
2013-03-18 09:53:11 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-03-18 09:53:11 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-15 15:04:46 +02:00
|
|
|
package org.jivesoftware.smack.util.dns.dnsjava;
|
2013-03-18 09:53:11 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-12-18 12:57:21 +01:00
|
|
|
import org.jivesoftware.smack.initializer.SmackInitializer;
|
2014-08-11 19:21:43 +02:00
|
|
|
import org.jivesoftware.smack.util.DNSUtil;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.util.dns.DNSResolver;
|
|
|
|
import org.jivesoftware.smack.util.dns.SRVRecord;
|
2013-03-18 09:53:11 +01:00
|
|
|
import org.xbill.DNS.Lookup;
|
|
|
|
import org.xbill.DNS.Record;
|
2014-03-18 17:29:38 +01:00
|
|
|
import org.xbill.DNS.TextParseException;
|
2013-03-18 09:53:11 +01:00
|
|
|
import org.xbill.DNS.Type;
|
|
|
|
|
2013-04-01 15:40:02 +02:00
|
|
|
/**
|
|
|
|
* This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses.
|
|
|
|
*
|
|
|
|
*/
|
2014-12-18 12:57:21 +01:00
|
|
|
public class DNSJavaResolver implements SmackInitializer, DNSResolver {
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2013-04-01 15:40:02 +02:00
|
|
|
private static DNSJavaResolver instance = new DNSJavaResolver();
|
2014-08-11 19:21:43 +02:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
public static DNSResolver getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-18 17:29:38 +01:00
|
|
|
public List<SRVRecord> lookupSRVRecords(String name) throws TextParseException {
|
2013-03-18 09:53:11 +01:00
|
|
|
List<SRVRecord> res = new ArrayList<SRVRecord>();
|
|
|
|
|
2014-03-18 17:29:38 +01:00
|
|
|
Lookup lookup = new Lookup(name, Type.SRV);
|
2014-08-15 23:16:18 +02:00
|
|
|
Record[] recs = lookup.run();
|
2014-03-18 17:29:38 +01:00
|
|
|
if (recs == null)
|
|
|
|
return res;
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2014-03-18 17:29:38 +01:00
|
|
|
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();
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2014-03-18 17:29:38 +01:00
|
|
|
SRVRecord r = new SRVRecord(host, port, priority, weight);
|
|
|
|
res.add(r);
|
2013-03-18 09:53:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-18 17:29:38 +01:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
return res;
|
|
|
|
}
|
2014-08-11 19:21:43 +02:00
|
|
|
|
|
|
|
public static void setup() {
|
|
|
|
DNSUtil.setDNSResolver(getInstance());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Exception> initialize() {
|
|
|
|
setup();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
}
|