All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.automationrockstars.bmo.proxy.HttpHostResolver Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2015, 2016 Automation RockStars Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Automation RockStars - initial API and implementation
 *******************************************************************************/
package com.automationrockstars.bmo.proxy;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.automationrockstars.base.ConfigLoader;
import com.google.common.net.InetAddresses;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import net.lightbody.bmp.proxy.dns.AdvancedHostResolver;

/**
 * Created by c952911 on 12/08/2015.
 */
public class HttpHostResolver implements AdvancedHostResolver {
    private static Logger log = LoggerFactory.getLogger(HttpHostResolver.class);
    public static String getIPofDomain(String domainName) throws IOException {
        String ip=null;
        String protocol = "http";
        String host = "www.dns-lg.com/nl01";
        String file = "/"+domainName+"/a";

        // create a URL object
        URL url = new URL(protocol, host, file);
        String proxyName = ConfigLoader.config().getString("proxy.name","localhost");
        int proxyPort = ConfigLoader.config().getInt("proxy.port",3128); 
        java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, new java.net.InetSocketAddress(proxyName,proxyPort));

        HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
        conn.connect();
        conn.disconnect();
        String response = IOUtils.toString(conn.getInputStream());
        
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson( response, JsonObject.class);
        int arraySize=  jsonObject.getAsJsonArray("answer").getAsJsonArray().size();
        for(int i=0;i resolve(String host) {
        log.info("Getting ip for {}",host);
        String ip = null;
        if (InetAddresses.isInetAddress(host)){
            if (host.contains("====")) {
                log.warn("Got special host");
                ip = "127.0.0.1";
            } else {
                ip = host;
            }
            log.info("Raw IP address passed {}",ip);
        }
        else

            try {
                ip = getIPofDomain(host);
                log.info("Resolved IP {}",ip);
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            return Collections.singletonList(InetAddress.getByName(ip));
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public void remapHosts(Map hostRemappings) {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public void remapHost(String originalHost, String remappedHost) {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public void removeHostRemapping(String originalHost) {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public void clearHostRemappings() {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public Map getHostRemappings() {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public Collection getOriginalHostnames(String remappedHost) {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public void clearDNSCache() {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public void setPositiveDNSCacheTimeout(int timeout, TimeUnit timeUnit) {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }

    @Override
    public void setNegativeDNSCacheTimeout(int timeout, TimeUnit timeUnit) {
        log.warn("Called");
        throw new UnsupportedOperationException(new Throwable().getStackTrace()[0].getMethodName() + " is not supported by this host resolver (" + this.getClass().getName() + ")");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy