1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.jdkim.impl;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.james.jdkim.api.PublicKeyRecordRetriever;
27 import org.apache.james.jdkim.exceptions.PermFailException;
28 import org.apache.james.jdkim.exceptions.TempFailException;
29 import org.xbill.DNS.Lookup;
30 import org.xbill.DNS.Record;
31 import org.xbill.DNS.Resolver;
32 import org.xbill.DNS.TXTRecord;
33 import org.xbill.DNS.TextParseException;
34 import org.xbill.DNS.Type;
35
36 public class DNSPublicKeyRecordRetriever implements PublicKeyRecordRetriever {
37
38
39 protected Resolver resolver;
40
41 public DNSPublicKeyRecordRetriever() {
42 this(Lookup.getDefaultResolver());
43 }
44
45 public DNSPublicKeyRecordRetriever(Resolver resolver) {
46 this.resolver = resolver;
47 }
48
49
50
51
52 public List
53 CharSequence selector, CharSequence token)
54 throws TempFailException, PermFailException {
55 if (!"dns/txt".equals(methodAndOptions))
56 throw new PermFailException("Only dns/txt is supported: "
57 + methodAndOptions + " options unsupported.");
58 try {
59 Lookup query = new Lookup(selector + "._domainkey." + token,
60 Type.TXT);
61 query.setResolver(resolver);
62
63 Record[] rr = query.run();
64 int queryResult = query.getResult();
65
66 if (queryResult == Lookup.TRY_AGAIN) {
67 throw new TempFailException(query.getErrorString());
68 }
69
70 List
71 return records;
72 } catch (TextParseException e) {
73
74 return null;
75 }
76 }
77
78
79
80
81
82
83
84
85 public static List
86 List
87 if (rr != null && rr.length > 0) {
88 records = new ArrayList
89 for (int i = 0; i < rr.length; i++) {
90 switch (rr[i].getType()) {
91 case Type.TXT:
92 TXTRecord txt = (TXTRecord) rr[i];
93 if (txt.getStrings().size() == 1) {
94
95
96
97
98
99 records.add(((String) txt.getStrings().get(0)));
100 } else {
101 StringBuffer sb = new StringBuffer();
102 for (Iterator
103 .iterator(); it.hasNext();) {
104 String k = (String) it.next();
105
106
107
108
109 sb.append(k);
110 }
111 records.add(sb.toString());
112 }
113 break;
114 default:
115 return null;
116 }
117 }
118 } else {
119 records = null;
120 }
121 return records;
122 }
123
124 }