Java Code Examples for java.net.InetAddress.getCanonicalHostName()

The following are Jave code examples for showing how to use getCanonicalHostName() of the java.net.InetAddress class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: flume-release-1.7.0   File: BucketPath.java   Source Code and License Vote up 6 votes
/**
 * Not intended as a public API
 */
@VisibleForTesting
protected static String replaceStaticString(String key) {
  String replacementString = "";
  try {
    InetAddress addr = InetAddress.getLocalHost();
    switch (key.toLowerCase()) {
      case "localhost":
        replacementString = addr.getHostName();
        break;
      case "ip":
        replacementString = addr.getHostAddress();
        break;
      case "fqdn":
        replacementString = addr.getCanonicalHostName();
        break;
      default:
        throw new RuntimeException("The static escape string '" + key + "'"
                + " was provided but does not match any of (localhost,IP,FQDN)");
    }
  } catch (UnknownHostException e) {
    throw new RuntimeException("Flume wasn't able to parse the static escape "
            + " sequence '" + key + "' due to UnkownHostException.", e);
  }
  return replacementString;
}
 
Example 2
Project: flume-release-1.7.0   File: HostInterceptor.java   Source Code and License Vote up 6 votes
/**
 * Only {@link HostInterceptor.Builder} can build me
 */
private HostInterceptor(boolean preserveExisting,
    boolean useIP, String header) {
  this.preserveExisting = preserveExisting;
  this.header = header;
  InetAddress addr;
  try {
    addr = InetAddress.getLocalHost();
    if (useIP) {
      host = addr.getHostAddress();
    } else {
      host = addr.getCanonicalHostName();
    }
  } catch (UnknownHostException e) {
    logger.warn("Could not get local host address. Exception follows.", e);
  }

}
 
Example 3
Project: monarch   File: ServerLauncher.java   Source Code and License Vote up 6 votes
/**
 * Gets the host, as either hostname or IP address, on which the Server was bound and running. An
 * attempt is made to get the canonical hostname for IP address to which the Server was bound for
 * accepting client requests. If the server bind address is null or localhost is unknown, then a
 * default String value of "localhost/127.0.0.1" is returned.
 * 
 * Note, this information is purely information and should not be used to re-construct state or
 * for other purposes.
 * 
 * @return the hostname or IP address of the host running the Server, based on the bind-address,
 *         or 'localhost/127.0.0.1' if the bind address is null and localhost is unknown.
 * @see java.net.InetAddress
 * @see #getServerBindAddress()
 */
public String getServerBindAddressAsString() {
  try {
    if (getServerBindAddress() != null) {
      return getServerBindAddress().getCanonicalHostName();
    }

    final InetAddress localhost = SocketCreator.getLocalHost();

    return localhost.getCanonicalHostName();
  } catch (UnknownHostException ignore) {
    // TODO determine a better value for the host on which the Server is running to return here...
    // NOTE returning localhost/127.0.0.1 implies the serverBindAddress was null and no IP address
    // for localhost
    // could be found
    return "localhost/127.0.0.1";
  }
}
 
Example 4
Project: monarch   File: LocatorLauncher.java   Source Code and License Vote up 6 votes
/**
 * Gets the host, as either hostname or IP address, on which the Locator was bound and running. An
 * attempt is made to get the canonical hostname for IP address to which the Locator was bound for
 * accepting client requests. If the bind address is null or localhost is unknown, then a default
 * String value of "localhost/127.0.0.1" is returned.
 * 
 * Note, this information is purely information and should not be used to re-construct state or
 * for other purposes.
 * 
 * @return the hostname or IP address of the host running the Locator, based on the bind-address,
 *         or 'localhost/127.0.0.1' if the bind address is null and localhost is unknown.
 * @see java.net.InetAddress
 * @see #getBindAddress()
 */
protected String getBindAddressAsString() {
  try {
    if (getBindAddress() != null) {
      return getBindAddress().getCanonicalHostName();
    }

    InetAddress localhost = SocketCreator.getLocalHost();

    return localhost.getCanonicalHostName();
  } catch (UnknownHostException ignore) {
    // TODO determine a better value for the host on which the Locator is running to return
    // here...
    // NOTE returning localhost/127.0.0.1 implies the bindAddress was null and no IP address for
    // localhost
    // could be found
    return "localhost/127.0.0.1";
  }
}
 
Example 5
Project: ace   File: TestInteAddress.java   Source Code and License Vote up 5 votes
public static void main(String[] args) throws UnknownHostException {
    List<Long> list=new ArrayList<>();
    list.add(66L);
    list.add(77L);
    String str=String.format("abc %s" ,list);
    System.out.println(str);

    InetAddress inetAddress = InetAddress.getLocalHost();
    String canonical = inetAddress.getCanonicalHostName();
    String host = inetAddress.getHostName();

    System.out.println(canonical);
    System.out.println(host);

}
 
Example 6
Project: joai-project   File: WebLogReader.java   Source Code and License Vote up 5 votes
/**
 *  Gets the fully qualified domain name the requesting IP. Best effort method, meaning we may not be able to
 *  return the FQDN depending on the underlying system configuration. If not avaialable, the the IP address
 *  is returned instead.
 *
 * @return    The remote host domain name, for example mysite.org, or IP address if not available
 */
public String getRemoteHostName() {
	String t = doc.get("remotehost");

	if (t == null || t.trim().length() == 0)
		return null;
	try {
		InetAddress address = InetAddress.getByName(t);
		return address.getCanonicalHostName();
	} catch (Throwable e) {
		return t;
	}
}
 
Example 7
Project: monarch   File: HostStatHelper.java   Source Code and License Vote up 5 votes
/**
 * @return this machine's fully qualified hostname or "unknownHostName" if one cannot be found.
 */
private static String getHostSystemName() {
  String hostname = "unknownHostName";
  try {
    InetAddress addr = SocketCreator.getLocalHost();
    hostname = addr.getCanonicalHostName();
  } catch (UnknownHostException uhe) {
  }
  return hostname;
}
 
Example 8
Project: hadoop   File: TestContainerManager.java   Source Code and License Vote up 5 votes
@Test
public void testContainerManagerInitialization() throws IOException {

  containerManager.start();

  InetAddress localAddr = InetAddress.getLocalHost();
  String fqdn = localAddr.getCanonicalHostName();
  if (!localAddr.getHostAddress().equals(fqdn)) {
    // only check if fqdn is not same as ip
    // api returns ip in case of resolution failure
    Assert.assertEquals(fqdn, context.getNodeId().getHost());
  }

  // Just do a query for a non-existing container.
  boolean throwsException = false;
  try {
    List<ContainerId> containerIds = new ArrayList<ContainerId>();
    ContainerId id =createContainerId(0);
    containerIds.add(id);
    GetContainerStatusesRequest request =
        GetContainerStatusesRequest.newInstance(containerIds);
    GetContainerStatusesResponse response =
        containerManager.getContainerStatuses(request);
    if(response.getFailedRequests().containsKey(id)){
      throw response.getFailedRequests().get(id).deSerialize();
    }
  } catch (Throwable e) {
    throwsException = true;
  }
  Assert.assertTrue(throwsException);
}
 
Example 9
Project: hadoop   File: StreamUtil.java   Source Code and License Vote up 5 votes
static URL qualifyHost(URL url) {
  try {
    InetAddress a = InetAddress.getByName(url.getHost());
    String qualHost = a.getCanonicalHostName();
    URL q = new URL(url.getProtocol(), qualHost, url.getPort(), url.getFile());
    return q;
  } catch (IOException io) {
    return url;
  }
}
 
Example 10
Project: q-mail   File: SmtpTransport.java   Source Code and License Vote up 4 votes
@VisibleForTesting
protected String getCanonicalHostName(InetAddress localAddress) {
    return localAddress.getCanonicalHostName();
}
 
Example 11
Project: directory-ldap-api   File: Network.java   Source Code and License Vote up 4 votes
private static String getLoopbackHostName()
{
    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    return loopbackAddress.getCanonicalHostName();
}
 
Example 12
Project: hawkular-java-toolbox   File: HawkularFactoryTest.java   Source Code and License Vote up 4 votes
@BeforeClass
public static void setupClass() throws UnknownHostException {
    InetAddress address = InetAddress.getLocalHost();
    HOST = address.getCanonicalHostName();
}
 
Example 13
Project: athena   File: Main.java   Source Code and License Vote up 4 votes
@Override
public void getValidationResultOnlineResult(HashMap<String, Object> event, double result) {
    Integer sip = (Integer) event.get(AthenaIndexField.MATCH_IPV4_SRC);
    Integer dip = (Integer) event.get(AthenaIndexField.MATCH_IPV4_DST);
    byte[] sbytes = BigInteger.valueOf(sip).toByteArray();
    byte[] dbytes = BigInteger.valueOf(dip).toByteArray();
    InetAddress saddress = null;
    InetAddress daddress = null;
    try {
        saddress = InetAddress.getByAddress(sbytes);
        daddress = InetAddress.getByAddress(dbytes);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    String[] array;
    array = saddress.getHostAddress().split("\\.");

    int endHost = Integer.parseInt(array[3]);
    List<Integer> hostValue = clusterHistory.get(endHost);
    Integer hostTargetValue = hostValue.get((int) result);
    hostTargetValue += 1;
    hostValue.set((int) result, hostTargetValue);
    clusterHistory.set(endHost, hostValue);

    String outputs = "[Instance in " + localIP.getHostAddress() + "]" + "sIP=" + saddress.getHostAddress() + ";dIP=" + daddress.getCanonicalHostName() + ";loc=" + set.get(saddress.getHostAddress()) + ";"
            + "detection results = " + result;

    System.out.print("host." + endHost + "=>");
    for (int i = 0; i < 10; i++) {
        System.out.print(i + "(" + hostValue.get(i) + ")" + " ");
        if (i == 0 || i == 1 || i == 3) {
            if (hostValue.get(i) > 100) {
                System.out.println("Host " +saddress.getHostAddress() + " has been blocked (Malicious host!)");
                issueBlock(saddress.getHostAddress(), daddress.getHostAddress());
            }
        }
    }
    System.out.print("\n");

    log.info(outputs);
    System.out.println(outputs);
    //Detection results in here
}
 
Example 14
Project: openjdk-jdk10   File: InternalNameServiceTest.java   Source Code and License Vote up 4 votes
private static void testIpAddressToHostNameMappings(String hostsFileName)
        throws Exception {
    System.out.println(" TEST IP ADDRESS TO HOST MAPPINGS ");
    InetAddress testAddress;
    String retrievedHost;
    String expectedHost = "testHost.testDomain";

    byte[] testHostIpAddr = { 10, 2, 3, 4 };
    byte[] testHostIpAddr2 = { 10, 5, 6, 7 };
    byte[] testHostIpAddr3 = { 10, 8, 9, 10 };
    byte[] testHostIpAddr4 = { 10, 8, 9, 11 };

    // add comment to hosts file
    addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,
            false);
    addMappingToHostsFile("testHost.testDomain", "10.2.3.4", hostsFileName,
            true);

    testAddress = InetAddress.getByAddress(testHostIpAddr);
    System.out.println("*******   testAddress == " + testAddress);
    retrievedHost = testAddress.getHostName();
    if (!expectedHost.equals(retrievedHost)) {
        throw new RuntimeException(
                "retrieved host name not equal to expected host name");
    }

    addMappingToHostsFile("testHost.testDomain", "10.5.6.7", hostsFileName,
            true);

    testAddress = InetAddress.getByAddress(testHostIpAddr2);
    System.out.println("*******   testAddress == " + testAddress);
    retrievedHost = testAddress.getHostName();
    System.out.println("*******   retrievedHost == " + retrievedHost);
    if (!expectedHost.equals(retrievedHost)) {
        throw new RuntimeException("retrieved host name " + retrievedHost
                + " not equal to expected host name" + expectedHost);
    }

    testAddress = InetAddress.getByAddress(testHostIpAddr4);
    System.out.println("*******   testAddress == " + testAddress);
    if ("10.8.9.11".equalsIgnoreCase(testAddress.getCanonicalHostName())) {
        System.out.println("addr = " + addrToString(testHostIpAddr4)
                + "  resolve to a host address as expected");
    } else {
        System.out.println("addr = " + addrToString(testHostIpAddr4)
                + " does not resolve as expected, testAddress == " + testAddress.getCanonicalHostName());
        throw new RuntimeException("problem with resolving "
                + addrToString(testHostIpAddr4));
    }

    try {
        addMappingToHostsFile("", "10.8.9.10", hostsFileName, true);
        testAddress = InetAddress.getByAddress(testHostIpAddr3);
        System.out.println("*******   testAddress == " + testAddress);
        retrievedHost = testAddress.getCanonicalHostName();
    } catch (Throwable t) {
        throw new RuntimeException("problem with resolving "
                + addrToString(testHostIpAddr3));
    }

}