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
21 package org.apache.james.util;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 /**
29 * Helper class which is used to store ipAddresses and timestamps for pop before
30 * smtp support.
31 */
32 public class POP3BeforeSMTPHelper {
33
34 private POP3BeforeSMTPHelper() {
35 }
36
37 /**
38 * The map in which the ipAddresses and timestamp stored
39 */
40 public static Map ipMap = Collections.synchronizedMap(new HashMap());
41
42 /**
43 * Default expire time in ms (1 hour)
44 */
45 public static final long EXPIRE_TIME = 216000000;
46
47 /**
48 * Return true if the ip is authorized to relay
49 *
50 * @param ipAddress
51 * The ipAddress
52 * @return true if authorized. Else false
53 */
54 public static boolean isAuthorized(String ipAddress) {
55 return ipMap.containsKey(ipAddress);
56 }
57
58 /**
59 * Add the ipAddress to the authorized ipAddresses
60 *
61 * @param ipAddress
62 * The ipAddress
63 */
64 public static void addIPAddress(String ipAddress) {
65 ipMap.put(ipAddress, Long.toString(System.currentTimeMillis()));
66 }
67
68 /**
69 * @see #removeExpiredIP(long)
70 */
71 public static void removeExpiredIP() {
72 removeExpiredIP(EXPIRE_TIME);
73 }
74
75 /**
76 * Remove all ipAddress from the authorized map which are older then the
77 * given time
78 *
79 * @param clearTime
80 * The time in milliseconds after which an ipAddress should be
81 * handled as expired
82 */
83 public static void removeExpiredIP(long clearTime) {
84 synchronized (ipMap) {
85 Iterator storedIP = ipMap.keySet().iterator();
86 long currTime = System.currentTimeMillis();
87
88 while (storedIP.hasNext()) {
89 String key = storedIP.next().toString();
90 long storedTime = Long.parseLong((String) ipMap.get(key));
91
92 // remove the ip from the map when it is expired
93 if ((currTime - clearTime) > storedTime) {
94 // remove the entry from the iterator first to get sure that we not get
95 // a ConcurrentModificationException
96 storedIP.remove();
97
98 ipMap.remove(key);
99 }
100 }
101 }
102 }
103
104 /**
105 * Remove all ipAddresses from the authorized map
106 */
107 public static void clearIP() {
108 ipMap.clear();
109 }
110 }