Example usage for java.net UnknownHostException printStackTrace

List of usage examples for java.net UnknownHostException printStackTrace

Introduction

In this page you can find the example usage for java.net UnknownHostException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:net.itransformers.idiscover.discoveryhelpers.xml.SnmpForXslt.java

public static String getNameByDNS(String ipAddress) {
    if (mockSnmpForXslt != null) {
        return mockSnmpForXslt.getNameByDNS(ipAddress);
    }//from w w w  .j  a  v a  2  s  . c  o m

    //        InetAddress inetAddress = new InetAddress();
    InetAddress address = null;
    try {
        address = InetAddress.getByName(ipAddress);
    } catch (UnknownHostException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    return address.getHostAddress();

}

From source file:com.whatlookingfor.common.utils.StringUtils.java

/**
 * ?IP//  w ww  . j  ava 2  s .  c  o  m
 *
 * @param request 
 * @return ip?
 */
public static final String getHost(HttpServletRequest request) {
    String ip = request.getHeader("X-Forwarded-For");
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Real-IP");
    }
    if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    if ("127.0.0.1".equals(ip)) {
        InetAddress inet = null;
        try { // ????IP
            inet = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        ip = inet.getHostAddress();
    }
    // ?IPIP,IP','
    if (ip != null && ip.length() > 15) {
        if (ip.indexOf(",") > 0) {
            ip = ip.substring(0, ip.indexOf(","));
        }
    }
    return ip;
}

From source file:net.itransformers.idiscover.discoveryhelpers.xml.SnmpForXslt.java

public static String checkBogons(String ipAddress) {
    if (ipAddress != null) {
        try {//from   ww  w. j  a v  a 2 s .c  om
            cidrUtils = new CIDRUtils("0.0.0.0/8");

            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("127.0.0.0/8");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("169.254.0.0/16");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("192.0.0.0/24");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("192.0.2.0/24");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("224.0.0.0/4");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("240.0.0.0/4");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            cidrUtils = new CIDRUtils("255.255.255.255/32");
            if (cidrUtils.isInRange(ipAddress)) {
                return null;
            }
            return ipAddress;
        } catch (UnknownHostException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    } else {
        return null;
    }

}

From source file:com.lostad.app.base.util.RequestUtil.java

/**
  * PostHttps?//from   w w  w  . j  av a 2  s .  c om
  * @param url;  ?
  * @param json    
  * @return
  * @throws Exception
  */
public static synchronized String postHttps(String url, String json) throws Exception {
    // ?
    HttpParams httpParameters = new BasicHttpParams();
    // 
    HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
    // socket
    HttpConnectionParams.setSoTimeout(httpParameters, 3000);
    // ?HttpClient ?
    HttpClient hc = HttpClientManager.getHttpClientSSL(httpParameters);
    HttpPost post = new HttpPost(url);
    // ???
    post.addHeader("Content-Type", "application/json;charset=utf-8");
    // ??
    post.addHeader("Accept", "application/json");
    // 
    StringEntity entity = new StringEntity(json, "UTF-8");
    post.setEntity(entity);
    post.setParams(httpParameters);
    HttpResponse response = null;
    try {
        response = hc.execute(post);
    } catch (UnknownHostException e) {
        throw new Exception("Unable to access " + e.getLocalizedMessage());
    } catch (SocketException e) {
        e.printStackTrace();
    }
    int sCode = response.getStatusLine().getStatusCode();
    if (sCode == HttpStatus.SC_OK) {
        return EntityUtils.toString(response.getEntity());
    } else
        throw new Exception("StatusCode is " + sCode);
}

From source file:org.opennms.features.newts.converter.eventd.EventdStresser.java

private static void setIpAddresses() {
    try {/* w  w w  .  j  a va2s  .c o  m*/
        m_trapSink = InetAddress.getByName(DEFAULT_IPADDRESS);
        m_agentAddress = m_trapSink;
    } catch (UnknownHostException e1) {
        e1.printStackTrace();
        System.exit(1);
    }
}

From source file:com.lm.lic.manager.util.GenUtil.java

/**
 * @param request//  w  w w  . jav  a 2s. co  m
 * @return
 */
public static String findDomain(HttpServletRequest request) {
    String domain = null;
    String remoteHost = request.getRemoteHost();
    try {
        InetAddress inetAddress = Inet4Address.getByName(remoteHost);
        domain = inetAddress.getHostName();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return domain;
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param json/*from ww w  .java  2 s.co  m*/
 * @return Sender address as String without intermediate conversion to InetSocketAddress.
 */
public static String getSenderAddressAsString(JSONObject json) {
    if (json instanceof JSONMessenger.JSONObjectWrapper)
        try {
            return getSenderAddress((byte[]) ((JSONMessenger.JSONObjectWrapper) json).obj).toString();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }
    // else
    try {
        return json.has(MessageNIOTransport.SNDR_ADDRESS_FIELD)
                ? (json.getString(MessageNIOTransport.SNDR_ADDRESS_FIELD))
                : null;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param json/*from  www  . j a v a  2 s  .c o  m*/
 * @return Socket address of the sender recorded in this JSON message at
 *         receipt time.
 */
public static InetSocketAddress getSenderAddress(JSONObject json) {
    if (json instanceof JSONMessenger.JSONObjectWrapper)
        try {
            return getSenderAddress((byte[]) ((JSONMessenger.JSONObjectWrapper) json).obj);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }
    // else
    try {
        InetSocketAddress isa = json.has(MessageNIOTransport.SNDR_ADDRESS_FIELD)
                ? Util.getInetSocketAddressFromStringStrict(
                        json.getString(MessageNIOTransport.SNDR_ADDRESS_FIELD))
                : null;
        return isa;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param json/* ww  w  .  j av a 2 s.c  om*/
 * @return Socket address of the recorded recorded in this JSON message at
 *         receipt time. Sometimes a sender needs to know on which of its
 *         possibly multiple listening sockets this message was received, so
 *         we insert it into the packet at receipt time.
 */
public static InetSocketAddress getReceiverAddress(JSONObject json) {
    if (json instanceof JSONMessenger.JSONObjectWrapper)
        try {
            return getReceiverAddress((byte[]) ((JSONMessenger.JSONObjectWrapper) json).obj);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }
    InetSocketAddress isa = null;
    try {
        isa = json.has(MessageNIOTransport.RCVR_ADDRESS_FIELD)
                ? Util.getInetSocketAddressFromStringStrict(
                        json.getString(MessageNIOTransport.RCVR_ADDRESS_FIELD))
                : null;

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return isa;
}

From source file:sorcer.core.SorcerEnv.java

/**
 * Overwrites defined properties in sorcer.env (sorcer.home,
 * provider.webster.interface, provider.webster.port) with those defined as
 * JVM system properties./*  ww w.  j ava  2 s . c o m*/
 * 
 * @param properties
 * @throws ConfigurationException
 */
private static void update(Properties properties) throws ConfigurationException {
    Enumeration<?> e = properties.propertyNames();
    String key, value, evalue = null;
    String pattern = "${" + "localhost" + "}";
    //      String userDirPattern = "${user.home}";
    // first substitute for this localhost
    while (e.hasMoreElements()) {
        key = (String) e.nextElement();
        value = properties.getProperty(key);
        if (value.equals(pattern)) {
            try {
                value = getHostAddress();
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
            }
            properties.put(key, value);
        }
        /*      if (value.equals(userDirPattern)) {
                 value = System.getProperty("user.home");            
                 properties.put(key, value);
              }*/
    }
    // now substitute other entries accordingly 
    e = properties.propertyNames();
    while (e.hasMoreElements()) {
        key = (String) e.nextElement();
        value = properties.getProperty(key);
        evalue = expandStringProperties(value, true);
        // try SORCER env properties
        if (evalue == null)
            evalue = expandStringProperties(value, false);
        if (evalue != null)
            properties.put(key, evalue);
        if (value.equals(pattern)) {
            try {
                evalue = getHostAddress();
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }
            properties.put(key, evalue);
        }
    }
}