View Javadoc

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  
22  
23  package org.apache.james.smtpserver.junkscore;
24  
25  /**
26   * Utility class for providing static method for JunkScore configuration
27   */
28  public class JunkScoreConfigUtil {
29  
30      public static final String JUNKSCORE = "junkscore";
31      public static final String JUNKSCORE_DELIMITER = ":";
32      
33      /**
34       * Return the junkscore which was supplied as raw config String
35       * 
36       * @param raw configuration String
37       * @return junkScore
38       * @throws IllegalArgumentException get thrown on invalid config
39       */
40      public static double getJunkScore(String raw) throws IllegalArgumentException {
41          double score = 0;
42          raw = raw.toLowerCase();
43          if (raw.indexOf(JUNKSCORE_DELIMITER) > 0 && raw.startsWith(JUNKSCORE)) {
44              try {
45                  score = Double.parseDouble(raw.trim().split(JUNKSCORE_DELIMITER)[1]);
46              } catch (NumberFormatException e) {
47                  throw new IllegalArgumentException("Invalid input: " + raw);
48              }
49          } else {
50              throw new IllegalArgumentException("Invalid input: " + raw);
51          }
52          return score;
53      }
54      
55      
56      /**
57       * Return true if the given raw config is a valid JunkScore configuration String
58       * 
59       * @param raw configuration String
60       * @return true of false
61       */
62      public static boolean isValidJunkScoreConfig(String raw) {
63          try {
64              getJunkScore(raw);
65          } catch (IllegalArgumentException e){
66              return false;
67          }
68          return true;
69      }
70  }