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    *      https://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><strong>name|<em>address</em></strong>: for the host name, for example {@code "name|93.184.216.34"} ->
31   * {@code "example.com"}.</li>
32   * <li><strong>canonical-name|<em>address</em></strong>: for the canonical host name, for example {@code "name|93.184.216.34"} ->
33   * {@code "example.com"}.</li>
34   * <li><strong>address|<em>hostname</em></strong>: for the host address, for example {@code "address|example.com"} ->
35   * {@code "93.184.216.34"}.</li>
36   * <li><strong><em>address</em></strong>: 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   * <p>
57   * Public access is through {@link StringLookupFactory}.
58   * </p>
59   *
60   * @see StringLookupFactory
61   * @since 1.8
62   */
63  final class DnsStringLookup extends AbstractStringLookup {
64  
65      /**
66       * Defines the singleton for this class.
67       */
68      static final DnsStringLookup INSTANCE = new DnsStringLookup();
69  
70      /**
71       * No need to build instances for now.
72       */
73      private DnsStringLookup() {
74          // empty
75      }
76  
77      /**
78       * Looks up the DNS value of the key.
79       *
80       * @param key the key to be looked up, may be null
81       * @return The DNS value.
82       */
83      @Override
84      public String lookup(final String key) {
85          if (key == null) {
86              return null;
87          }
88          final String[] keys = key.trim().split("\\|");
89          final int keyLen = keys.length;
90          final String subKey = keys[0].trim();
91          final String subValue = keyLen < 2 ? key : keys[1].trim();
92          try {
93              final InetAddress inetAddress = InetAddress.getByName(subValue);
94              switch (subKey) {
95              case InetAddressKeys.KEY_NAME:
96                  return inetAddress.getHostName();
97              case InetAddressKeys.KEY_CANONICAL_NAME:
98                  return inetAddress.getCanonicalHostName();
99              case InetAddressKeys.KEY_ADDRESS:
100                 return inetAddress.getHostAddress();
101             default:
102                 return inetAddress.getHostAddress();
103             }
104         } catch (final UnknownHostException e) {
105             return null;
106         }
107     }
108 
109 }