View Javadoc
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  
19  import java.net.InetAddress;
20  import java.net.UnknownHostException;
21  
22  import org.apache.commons.text.StringSubstitutor;
23  
24  /**
25   * Looks up keys related to DNS entries like host name, canonical host name, host address.
26   * <p>
27   * The lookup keys are:
28   * </p>
29   * <ul>
30   * <li><b>name|<em>address</em></b>: for the host name, for example {@code "name|93.184.216.34"} ->
31   * {@code "example.com"}.</li>
32   * <li><b>canonical-name|<em>address</em></b>: for the canonical host name, for example {@code "name|93.184.216.34"} ->
33   * {@code "example.com"}.</li>
34   * <li><b>address|<em>hostname</em></b>: for the host address, for example {@code "address|example.com"} ->
35   * {@code "93.184.216.34"}.</li>
36   * <li><b><em>address</em></b>: same as {@code address|hostname}.</li>
37   * </ul>
38   *
39   * <p>
40   * Using a {@link StringLookup} from the {@link StringLookupFactory}:
41   * </p>
42   *
43   * <pre>
44   * StringLookupFactory.INSTANCE.dnsStringLookup().lookup("address|apache.org");
45   * </pre>
46   * <p>
47   * Using a {@link StringSubstitutor}:
48   * </p>
49   *
50   * <pre>
51   * StringSubstitutor.createInterpolator().replace("... ${dns:address|apache.org} ..."));
52   * </pre>
53   * <p>
54   * The above examples convert {@code "address|apache.org"} to {@code "95.216.24.32} (or {@code "40.79.78.1"}).
55   * </p>
56   *
57   * @since 1.8
58   */
59  final class DnsStringLookup extends AbstractStringLookup {
60  
61      /**
62       * Defines the singleton for this class.
63       */
64      static final DnsStringLookup INSTANCE = new DnsStringLookup();
65  
66      /**
67       * No need to build instances for now.
68       */
69      private DnsStringLookup() {
70          // empty
71      }
72  
73      /**
74       * Looks up the DNS value of the key.
75       *
76       * @param key the key to be looked up, may be null
77       * @return The DNS value.
78       */
79      @Override
80      public String lookup(final String key) {
81          if (key == null) {
82              return null;
83          }
84          final String[] keys = key.trim().split("\\|");
85          final int keyLen = keys.length;
86          final String subKey = keys[0].trim();
87          final String subValue = keyLen < 2 ? key : keys[1].trim();
88          try {
89              final InetAddress inetAddress = InetAddress.getByName(subValue);
90              switch (subKey) {
91              case InetAddressKeys.KEY_NAME:
92                  return inetAddress.getHostName();
93              case InetAddressKeys.KEY_CANONICAL_NAME:
94                  return inetAddress.getCanonicalHostName();
95              case InetAddressKeys.KEY_ADDRESS:
96                  return inetAddress.getHostAddress();
97              default:
98                  return inetAddress.getHostAddress();
99              }
100         } catch (final UnknownHostException e) {
101             return null;
102         }
103     }
104 
105 }