a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: FQDNUtils.java,v 1.6 2008/08/19 19:08:59 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.common;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.Constants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Enumeration;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This class provides utility methods to check if a host name is valid; and to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * get fully qualified host name from a partial (virtual) host name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class FQDNUtils {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static FQDNUtils instance = new FQDNUtils();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map fqdnMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String defaultHostName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Set validHostNames;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a <code>FQDNUtils</code> object by reading default host name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and <code>FQDN</code> map information from
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>AMConfig.properties</code> file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private FQDNUtils() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster init();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static FQDNUtils getInstance() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return instance;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void init() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster fqdnMap = getFQDNMapFromAMConfig();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster defaultHostName = SystemProperties.get(Constants.AM_SERVER_HOST);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster validHostNames = getLowerCaseValuesFromMap(fqdnMap);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster validHostNames.add(defaultHostName.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a <code>FQDNUtils</code> object with a <code>FQDN</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * map and a default host name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param fqdnMap
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>FQDN</code> map.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param defaultHostName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * default host name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public FQDNUtils(Map fqdnMap, String defaultHostName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.fqdnMap = fqdnMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.defaultHostName = defaultHostName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster validHostNames = getLowerCaseValuesFromMap(fqdnMap);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster validHostNames.add(defaultHostName.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map getFQDNMapFromAMConfig() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map map = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefix = Constants.AM_FQDN_MAP + "[";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int prefixLen = prefix.length();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Enumeration e = (SystemProperties.getAll()).propertyNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (e.hasMoreElements()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String) e.nextElement();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key.startsWith(prefix)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int idx = key.indexOf(']', prefixLen + 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (idx != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String partialName = key.substring(prefixLen, idx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster partialName = partialName.trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (partialName.length() > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster map.put(partialName, SystemProperties.get(key));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Set getLowerCaseValuesFromMap(Map map) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set set = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator iter = map.values().iterator(); iter.hasNext();) {
56ed5bbb263838f338eb8afc978091c01a4f2a2bjeff.schenk String value = ((String) iter.next());
56ed5bbb263838f338eb8afc978091c01a4f2a2bjeff.schenk if ( (value!=null)&&(!value.isEmpty()) )
56ed5bbb263838f338eb8afc978091c01a4f2a2bjeff.schenk { set.add(value.toLowerCase()); }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns true if a host name is valid. <code>hostname</code> is valid if
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * it is contained in the list of fully qualified host names. Or, it is the
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * default host name where OpenAM is installed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param hostname
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * host name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return true if a host name is valid.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean isHostnameValid(String hostname) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (hostname != null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && validHostNames.contains(hostname.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns null if a given host name is valid. Otherwises, this method looks
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * up the fully qualified <code>DN</code> map for matching entry. Default
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * host name is returned if there is no matching entry.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param hostname
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * host name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return fully qualified host name of <code>hostname</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getFullyQualifiedHostName(String hostname) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String fqHostName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!isHostnameValid(hostname)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster fqHostName = (String) fqdnMap.get(hostname);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((fqHostName == null) || (fqHostName.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster fqHostName = defaultHostName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return fqHostName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}