DnsStringLookup.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements. See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache license, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the license for the specific language governing permissions and
  15.  * limitations under the license.
  16.  */
  17. package org.apache.commons.text.lookup;

  18. import java.net.InetAddress;
  19. import java.net.UnknownHostException;

  20. import org.apache.commons.text.StringSubstitutor;

  21. /**
  22.  * Looks up keys related to DNS entries like host name, canonical host name, host address.
  23.  * <p>
  24.  * The lookup keys are:
  25.  * </p>
  26.  * <ul>
  27.  * <li><strong>name|<em>address</em></strong>: for the host name, for example {@code "name|93.184.216.34"} ->
  28.  * {@code "example.com"}.</li>
  29.  * <li><strong>canonical-name|<em>address</em></strong>: for the canonical host name, for example {@code "name|93.184.216.34"} ->
  30.  * {@code "example.com"}.</li>
  31.  * <li><strong>address|<em>hostname</em></strong>: for the host address, for example {@code "address|example.com"} ->
  32.  * {@code "93.184.216.34"}.</li>
  33.  * <li><strong><em>address</em></strong>: same as {@code address|hostname}.</li>
  34.  * </ul>
  35.  *
  36.  * <p>
  37.  * Using a {@link StringLookup} from the {@link StringLookupFactory}:
  38.  * </p>
  39.  *
  40.  * <pre>
  41.  * StringLookupFactory.INSTANCE.dnsStringLookup().lookup("address|apache.org");
  42.  * </pre>
  43.  * <p>
  44.  * Using a {@link StringSubstitutor}:
  45.  * </p>
  46.  *
  47.  * <pre>
  48.  * StringSubstitutor.createInterpolator().replace("... ${dns:address|apache.org} ..."));
  49.  * </pre>
  50.  * <p>
  51.  * The above examples convert {@code "address|apache.org"} to {@code "95.216.24.32} (or {@code "40.79.78.1"}).
  52.  * </p>
  53.  *
  54.  * @since 1.8
  55.  */
  56. final class DnsStringLookup extends AbstractStringLookup {

  57.     /**
  58.      * Defines the singleton for this class.
  59.      */
  60.     static final DnsStringLookup INSTANCE = new DnsStringLookup();

  61.     /**
  62.      * No need to build instances for now.
  63.      */
  64.     private DnsStringLookup() {
  65.         // empty
  66.     }

  67.     /**
  68.      * Looks up the DNS value of the key.
  69.      *
  70.      * @param key the key to be looked up, may be null
  71.      * @return The DNS value.
  72.      */
  73.     @Override
  74.     public String lookup(final String key) {
  75.         if (key == null) {
  76.             return null;
  77.         }
  78.         final String[] keys = key.trim().split("\\|");
  79.         final int keyLen = keys.length;
  80.         final String subKey = keys[0].trim();
  81.         final String subValue = keyLen < 2 ? key : keys[1].trim();
  82.         try {
  83.             final InetAddress inetAddress = InetAddress.getByName(subValue);
  84.             switch (subKey) {
  85.             case InetAddressKeys.KEY_NAME:
  86.                 return inetAddress.getHostName();
  87.             case InetAddressKeys.KEY_CANONICAL_NAME:
  88.                 return inetAddress.getCanonicalHostName();
  89.             case InetAddressKeys.KEY_ADDRESS:
  90.                 return inetAddress.getHostAddress();
  91.             default:
  92.                 return inetAddress.getHostAddress();
  93.             }
  94.         } catch (final UnknownHostException e) {
  95.             return null;
  96.         }
  97.     }

  98. }