Example usage for java.net UnknownHostException UnknownHostException

List of usage examples for java.net UnknownHostException UnknownHostException

Introduction

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

Prototype

public UnknownHostException() 

Source Link

Document

Constructs a new UnknownHostException with no detail message.

Usage

From source file:com.baasbox.service.push.providers.GCMServer.java

public static void validateApiKey(String apikey)
        throws MalformedURLException, IOException, PushInvalidApiKeyException {
    Message message = new Message.Builder().addData("message", "validateAPIKEY").build();
    Sender sender = new Sender(apikey);

    List<String> deviceid = new ArrayList<String>();
    deviceid.add("ABC");

    Map<Object, Object> jsonRequest = new HashMap<Object, Object>();
    jsonRequest.put(JSON_REGISTRATION_IDS, deviceid);
    Map<String, String> payload = message.getData();
    if (!payload.isEmpty()) {
        jsonRequest.put(JSON_PAYLOAD, payload);
    }//from   w  w w .ja va2s  .c o  m
    String requestBody = JSONValue.toJSONString(jsonRequest);

    String url = com.google.android.gcm.server.Constants.GCM_SEND_ENDPOINT;
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

    byte[] bytes = requestBody.getBytes();

    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Authorization", "key=" + apikey);
    OutputStream out = conn.getOutputStream();
    out.write(bytes);
    out.close();

    int status = conn.getResponseCode();
    if (status != 200) {
        if (status == 401) {
            throw new PushInvalidApiKeyException("Wrong api key");
        }
        if (status == 503) {
            throw new UnknownHostException();
        }
    }

}

From source file:com.clustercontrol.ping.factory.RunMonitorPing.java

/**
 * fping?ping?????<BR>// w  ww .  j  a  va 2 s . c o m
 * @param facilityId Ping?ID()collect?facilityID???
 * @return ???????true
 * @throws HinemosUnknown
 */
private boolean collectFping(ArrayList<String> facilityList, ArrayList<Integer> priporityList)
        throws HinemosUnknown {

    //???
    if (m_now != null) {
        m_nodeDate = m_now.getTime();
    }

    //??????
    // hosts[] IP(String ??)
    // hostsv6[]  IPv6(String??)
    // node     IP????
    // target   nodo?
    HashSet<String> hosts = new HashSet<String>();
    HashSet<String> hostsv6 = new HashSet<String>();
    m_Target = new Hashtable<String, String[]>();

    String facilityId = null;
    int version = 4;
    String[] node;
    for (int index = 0; index < facilityList.size(); index++) {
        facilityId = facilityList.get(index);
        if (facilityId != null && !"".equals(facilityId)) {

            node = new String[2];

            try {

                // ??
                NodeInfo info = new RepositoryControllerBean().getNode(facilityId);

                //m_log.error(facilityAttrMap.get(FacilityAttributeConstant.IPPROTOCOLNUMBER));
                //???????version?

                if (info.getIpAddressVersion() != null) {
                    version = info.getIpAddressVersion();
                } else {
                    version = 4;
                }

                if (version == 6) {

                    InetAddress[] ip = InetAddress.getAllByName(info.getIpAddressV6());

                    if (ip.length != 1) {
                        //IPInetAddress??????1????????
                        //UnnownHostExcption
                        UnknownHostException e = new UnknownHostException();
                        m_log.info("collectFping() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
                        throw e;
                    }

                    node[0] = ip[0].getHostAddress();

                    if (node[0] != null && !node[0].equals("")) {

                        //IPHashSet?????
                        hostsv6.add(node[0]);
                    }
                } else {
                    node[0] = info.getIpAddressV4();
                    if (node[0] != null && !node[0].equals("")) {

                        //IPHashSet?????
                        hosts.add(node[0]);
                    }
                }
                if (node[0] != null && !node[0].equals("")) {
                    node[1] = info.getNodeName();
                    //target??????
                    m_Target.put(facilityId, node);
                }
            } catch (FacilityNotFound e) {
                m_message = MessageConstant.MESSAGE_COULD_NOT_GET_NODE_ATTRIBUTES_PING.getMessage();
                m_messageOrg = e.getMessage();
                return false;
            } catch (UnknownHostException e) {
                // ???
            }
        }
    }

    if (m_reachabilityFping == null) {
        m_reachabilityFping = new ReachAddressFping(m_runCount, m_runInterval, m_pingTimeout);
    }

    boolean result = true;
    boolean resultTmp = true;
    //IPv4???fping??
    if (hosts.size() != 0) {

        result = m_reachabilityFping.isReachable(hosts, 4);
        m_MsgErr = m_reachabilityFping.getM_errMsg();
    }
    //IPv6???fping6??
    if (hostsv6.size() != 0) {

        resultTmp = m_reachabilityFping.isReachable(hostsv6, 6);
        m_MsgErrV6 = m_reachabilityFping.getM_errMsg();

        //??&????
        result = result & resultTmp;
    }
    return result;
}

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * All GemFire code should use this method instead of InetAddress.getLocalHost(). See bug #40619
 *//*  w  w w .java  2s .  c o  m*/
public static InetAddress getLocalHost() throws UnknownHostException {
    if (localHost == null) {
        throw new UnknownHostException();
    }
    return localHost;
}

From source file:com.okta.tools.awscli.java

private static void samlFailHandler(int requestStatus, CloseableHttpResponse responseSAML)
        throws UnknownHostException {
    if (responseSAML.getStatusLine().getStatusCode() == 500) {
        //incorrectly formatted app url
        throw new UnknownHostException();
    } else if (responseSAML.getStatusLine().getStatusCode() != 200) {
        //other//from   w w w.  j  a v  a  2s .  c om
        throw new RuntimeException(
                "Failed : HTTP error code : " + responseSAML.getStatusLine().getStatusCode());
    }
}

From source file:com.ibm.amc.demo.provider.AmcDemoCommands.java

private Device getDevice(DeviceContext deviceContext) throws InvalidCredentialsException, AMPIOException {
    if ("invalid".equals(deviceContext.getPassword())) {
        throw new InvalidCredentialsException();
    }//www .  j a  va 2 s .  c  o  m
    if ("unknown".equals(deviceContext.getHostname())) {
        throw new AMPIOException(new UnknownHostException());
    }
    Device device = devices.get(deviceContext.getHostname());
    if (device == null) {
        device = new Device(deviceContext);
        devices.put(deviceContext.getHostname(), device);
    }
    return device;
}

From source file:com.clustercontrol.nodemap.session.NodeMapControllerBean.java

/**
 * fping?ping?????<BR>//from w ww . ja v a  2 s.c  om
 * @param facilityId Ping?ID()collect?facilityID???
 * @return ping??
 * @throws HinemosUnknown
 * @throws NodeMapException 
 */
public List<String> pingToFacilityList(List<String> facilityList) throws HinemosUnknown, NodeMapException {

    String message = "";
    String messageOrg = "";
    //??????
    // hosts[] IP(String ??)
    // hostsv6[]  IPv6(String??)
    // node     IP????
    // target   nodo?
    HashSet<String> hosts = new HashSet<String>();
    HashSet<String> hostsv6 = new HashSet<String>();
    // ip?name
    Hashtable<String, List<String>> facilityNameTable = new Hashtable<>();

    String facilityId = null;
    int version = 4;
    String[] node;
    for (int index = 0; index < facilityList.size(); index++) {
        facilityId = facilityList.get(index);
        if (facilityId != null && !"".equals(facilityId)) {
            node = new String[2];
            try {

                // ??
                NodeInfo info = new RepositoryControllerBean().getNode(facilityId);

                if (info.getIpAddressVersion() != null) {
                    version = info.getIpAddressVersion();
                } else {
                    version = 4;
                }

                if (version == 6) {

                    InetAddress[] ip = InetAddress.getAllByName(info.getIpAddressV6());

                    if (ip.length != 1) {
                        //IPInetAddress??????1????????
                        //UnnownHostExcption
                        UnknownHostException e = new UnknownHostException();
                        m_log.info("pingToFacilityList() : " + e.getClass().getSimpleName() + ", "
                                + e.getMessage());
                        throw e;
                    }

                    node[0] = ip[0].getHostAddress();
                    if (node[0] != null && !node[0].equals("")) {
                        //IPHashSet?????
                        hostsv6.add(node[0]);
                    }
                } else {
                    node[0] = info.getIpAddressV4();
                    if (node[0] != null && !node[0].equals("")) {

                        //IPHashSet?????
                        hosts.add(node[0]);
                    }
                }
                if (node[0] != null && !node[0].equals("")) {
                    node[1] = info.getNodeName();
                    //target??????
                    List<String> facilitys = facilityNameTable.get(node[0]);
                    if (facilitys == null) {
                        facilitys = new ArrayList<>();
                    }
                    facilitys.add(facilityId);
                    facilityNameTable.put(node[0], facilitys);
                }
            } catch (FacilityNotFound e) {
                message = MessageConstant.MESSAGE_COULD_NOT_GET_NODE_ATTRIBUTES_PING.getMessage() + ","
                        + facilityId;
                messageOrg = e.getMessage();
                throw new NodeMapException(message + ", " + messageOrg, e);
            } catch (UnknownHostException e) {
                // ???
            }
        }
    }

    int runCount = 0;
    int runInterval = 0;
    int pingTimeout = 0;
    try {
        // [](default:1?19)
        String runCountKey = "nodemap.ping.runcount";
        runCount = HinemosPropertyUtil
                .getHinemosPropertyNum(runCountKey, Long.valueOf(PingRunCountConstant.TYPE_COUNT_01))
                .intValue();
        CommonValidator.validateInt(runCountKey, runCount, 1, 9);

        // [ms](default:1000?05000)
        String runIntervalKey = "nodemap.ping.runinterval";
        runInterval = HinemosPropertyUtil
                .getHinemosPropertyNum(runIntervalKey, Long.valueOf(PingRunIntervalConstant.TYPE_SEC_02))
                .intValue();
        CommonValidator.validateInt(runIntervalKey, runInterval, 0, 5 * 1000);

        // [ms](default:5000?13600000)
        String pintTimeoutKey = "nodemap.ping.timeout";
        pingTimeout = HinemosPropertyUtil
                .getHinemosPropertyNum(pintTimeoutKey, Long.valueOf(PingRunIntervalConstant.TYPE_SEC_05))
                .intValue();
        CommonValidator.validateInt(pintTimeoutKey, pingTimeout, 1, 60 * 60 * 1000);
    } catch (Exception e) {
        m_log.warn("pingToFacilityList() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
        throw new HinemosUnknown(e.getMessage(), e);
    }

    ReachAddressFping reachabilityFping = new ReachAddressFping(runCount, runInterval, pingTimeout);

    boolean result = true;
    boolean resultTmp = true;
    ArrayList<String> msgErr = new ArrayList<>();
    ArrayList<String> msgErrV6 = new ArrayList<>();
    Hashtable<String, PingResult> fpingResultSet = new Hashtable<String, PingResult>();
    Hashtable<String, PingResult> fpingResultSetV6 = new Hashtable<String, PingResult>();

    RunMonitorPing monitorPing = new RunMonitorPing();
    //IPv4???fping??
    if (hosts.size() != 0) {
        result = reachabilityFping.isReachable(hosts, 4);
        msgErr = reachabilityFping.getM_errMsg();
    }
    //IPv6???fping6??
    if (hostsv6.size() != 0) {
        resultTmp = reachabilityFping.isReachable(hostsv6, 6);
        msgErrV6 = reachabilityFping.getM_errMsg();
    }

    if (!result || !resultTmp) {
        return null;
    }
    List<String> retList = new ArrayList<>();
    fpingResultSet = monitorPing.wrapUpFping(msgErr, runCount, 4);
    fpingResultSetV6 = monitorPing.wrapUpFping(msgErrV6, runCount, 6);
    //IPv4????????IPv6?????
    m_log.debug("pingToFacilityList(): before fpingResultSet check");
    if (fpingResultSet.size() == 0) {
        m_log.debug("pingToFacilityList(): after fpingResultSet check");
        fpingResultSet = fpingResultSetV6;
    }
    //IPv4??????IPv6??
    else if (fpingResultSetV6.size() != 0) {
        fpingResultSet.putAll(fpingResultSetV6);
    }
    for (Map.Entry<String, List<String>> ipAdd : facilityNameTable.entrySet()) {
        PingResult pingResult = fpingResultSet.get(ipAdd.getKey());
        for (String facility : ipAdd.getValue()) {
            retList.add(facility + " : " + pingResult.getMesseageOrg());
        }
    }
    return retList;
}