1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.jdkim;
21
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.james.jdkim.api.PublicKeyRecordRetriever;
28 import org.apache.james.jdkim.exceptions.PermFailException;
29 import org.apache.james.jdkim.exceptions.TempFailException;
30
31 /**
32 * This is a mock public key record retriever that store the "registry" in a
33 * local map.
34 */
35 public class MockPublicKeyRecordRetriever implements PublicKeyRecordRetriever {
36 private static final String _DOMAINKEY = "._domainkey.";
37 private Map/* String, List<String> */records = new HashMap();
38
39 public void addRecord(String selector, String token, String record) {
40 String key = selector + _DOMAINKEY + token;
41 List l = (List) records.get(key);
42 if (l == null) {
43 l = new LinkedList();
44 records.put(key, l);
45 }
46 if (record != null) {
47 l.add(record);
48 }
49 }
50
51 public MockPublicKeyRecordRetriever() {
52
53 }
54
55 public MockPublicKeyRecordRetriever(String record, CharSequence selector,
56 CharSequence token) {
57 addRecord(selector.toString(), token.toString(), record);
58 }
59
60 public List getRecords(CharSequence methodAndOptions,
61 CharSequence selector, CharSequence token)
62 throws TempFailException, PermFailException {
63 if ("dns/txt".equals(methodAndOptions)) {
64 String search = selector + _DOMAINKEY + token;
65 List res = (List) records.get(search);
66 if (res == null || res.size() > 0)
67 return res;
68 else
69 throw new TempFailException("Timout or servfail");
70 } else
71 throw new PermFailException("Unsupported method");
72 }
73 }