001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020
021 package org.apache.james.jspf.core;
022
023 import org.apache.james.jspf.core.exceptions.SPFErrorConstants;
024
025
026 /**
027 *
028 * Class that offer static methods to convert SPF Results and contains all
029 * possible results as static Strings.
030 *
031 */
032
033 public class SPF1Utils {
034
035 public static final String DEFAULT_EXPLANATION = "http://www.openspf.org/why.html?sender=%{S}&ip=%{I}";
036 public static final String BEST_GUESS_RECORD = "v=spf1 a/24 mx/24 ptr ?all";
037 public static final String ATTRIBUTE_SPF1_RECORD = "SPF.SPF1Record";
038
039 /**
040 * Convert raw SPF results to SPF names
041 *
042 * @param result The result which should converted
043 * @return coverted result
044 */
045 public static String resultToName(String result) {
046
047 if (result.equals(SPF1Constants.PASS)) {
048 return SPFErrorConstants.PASS_CONV;
049 } else if (result.equals(SPF1Constants.FAIL)) {
050 return SPFErrorConstants.FAIL_CONV;
051 } else if (result.equals(SPF1Constants.NEUTRAL)) {
052 return SPFErrorConstants.NEUTRAL_CONV;
053 } else if (result.equals(SPF1Constants.SOFTFAIL)) {
054 return SPFErrorConstants.SOFTFAIL_CONV;
055 } else {
056 return SPFErrorConstants.NEUTRAL_CONV;
057 }
058
059 }
060
061 /**
062 * Check for valid FQDN
063 *
064 * @param host The hostname to check
065 * @return false or true
066 */
067 public static boolean checkFQDN(String host) {
068 String regex = "(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z]+)$";
069 if (host.matches(regex)) {
070 return true;
071 } else {
072 return false;
073 }
074 }
075 }