package org.github.dns;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Security;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Cache;
import org.xbill.DNS.Credibility;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Name;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

public class DnsTest {

	static final String GOOG = "google.com";

	public static final void main( String[] args ) {
	
		System.out.println("Beging DNS test");
		
		/** 
		 * Set the following system properties.
		 * http://docs.oracle.com/javase/1.5.0/docs/guide/net/properties.html
		 */
		System.setProperty("sun.net.spi.nameservice.provider.1", "dns,dnsjava");
		Security.setProperty("networkaddress.cache.ttl", "0"); 
		
		try {
			
			InetAddress address = InetAddress.getByName( GOOG );
			
			System.out.println("ip address: " + address.getHostAddress() );
			System.out.println("INET " + address.toString() + "\n" );
			
			Name gName = new Name(GOOG + ".");
			byte[] yAddr = new byte[]{ 98, (byte) 138, (byte) 253, 109};
			InetAddress yahooAddress = InetAddress.getByAddress( yAddr);
			
			// change the ARecord 
			Record arec = new ARecord( gName, Type.A, 999999999, yahooAddress);
						
			System.out.println("Cache: " + Lookup.getDefaultCache(Type.A) );
			Lookup.getDefaultCache(Type.A).flushName(gName);
			System.out.println("Cache: " + Lookup.getDefaultCache(Type.A) );
			
			Lookup.getDefaultCache(Type.A).addRecord(arec, Credibility.NORMAL, new DnsTest() );
			
			Cache c = Lookup.getDefaultCache(Type.A);
			System.out.println("Cache: " + Lookup.getDefaultCache(Type.A) );
			
			InetAddress address1 = InetAddress.getByName( GOOG );
			System.out.println("ip address: " + address1.getHostAddress() );
			System.out.println("INET " + address1.toString() );
	
			String url = "http://" + GOOG;
			
			HttpClient client = HttpClientBuilder.create().build();
			HttpGet request = new HttpGet(url);
			
			HttpResponse response = client.execute(request);

			System.out.println("Response Code : " 
		                + response.getStatusLine().getStatusCode());

			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (TextParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
}

@@ -1001,16 +1001,50 @@
 java%22);%0A
+%09%09%0A%09%09// set the security settings%0A
 %09%09Securi
@@ -1093,16 +1093,16 @@
  %220%22); %0A
-
 %09%09%0A%09%09try
@@ -1338,16 +1338,140 @@
 + %22.%22);%0A
+%09%09%09%0A%09%09%09/* %0A%09%09%09 * Find the ip address of yahoo.com by nslookup or ping.%0A%09%09%09 * Use that ipaddress in the following line%0A%09%09%09*/%0A
 %09%09%09byte%5B
@@ -1605,26 +1605,77 @@
 // c
-hange the ARecord 
+reate a new ARecord where %22google.com%22 points to yahoo.com ip address
 %0A%09%09%09
@@ -1745,24 +1745,91 @@
 ss);%0A%09%09%09%09%09%09%0A
+%09%09%09// print the default lookup cache for examining its contents%09%09%09%0A
 %09%09%09System.ou
@@ -1884,16 +1884,85 @@
 e.A) );%0A
+%09%09%09%0A%09%09%09// flush the contents of existing cache to remove all entries%0A
 %09%09%09Looku
@@ -2077,16 +2077,58 @@
  );%0A%09%09%09%0A
+%09%09%09// Add the new A-Record into the cache%0A
 %09%09%09Looku
@@ -2255,16 +2255,83 @@
 ype.A);%0A
+%09%09%09// print the default lookup cache for examining its contents%09%09%09%0A
 %09%09%09Syste
@@ -2390,24 +2390,78 @@
 e.A) );%0A%09%09%09%0A
+%09%09%09// TEST 1 : Use InetAddress to find the ip address%0A
 %09%09%09InetAddre
@@ -2628,18 +2628,156 @@
 ng() );%0A
-
 %09%0A
+%09%0A%09%09%09// TEST 2 : Use apache HttpClient to connect to google.com.%0A%09%09%09//          this will actually connect to yahoo and show the results.%0A
 %09%09%09Strin

Update DnsTest.java