Example usage for java.net UnknownHostException getMessage

List of usage examples for java.net UnknownHostException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:cn.openwatch.internal.http.loopj.AsyncHttpRequest.java

private void makeRequestWithRetries() throws Exception {
    boolean retry = true;
    Exception cause = null;// w  w  w. ja va2 s. c o  m
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    try {
        while (retry) {
            try {
                makeRequest();
                return;
            } catch (UnknownHostException e) {
                // switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
                // while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
                // (to assist in genuine cases of unknown host) which seems better than outright failure
                cause = new IOException("UnknownHostException exception: " + e.getMessage());
                retry = (executionCount > 0) && retryHandler.retryRequest(e, ++executionCount, context);
            } catch (NullPointerException e) {
                // there's a bug in HttpClient 4.0.x that on some occasions causes
                // DefaultRequestExecutor to throw an NPE, see
                // https://code.google.com/p/android/issues/detail?id=5255
                cause = new IOException("NPE in HttpClient: " + e.getMessage());
                retry = retryHandler.retryRequest((IOException) cause, ++executionCount, context);
            } catch (IOException e) {
                if (isCancelled()) {
                    // Eating exception, as the request was cancelled
                    return;
                }
                cause = e;
                retry = retryHandler.retryRequest((IOException) cause, ++executionCount, context);
            }
            if (retry) {
                responseHandler.sendRetryMessage(executionCount);
            }
        }
    } catch (Exception e) {
        // catch anything else to ensure failure message is propagated
        cause = new Exception("Unhandled exception: " + e.getMessage());
    }

    // cleaned up to throw IOException
    throw (cause);
}

From source file:io.gromit.geolite2.GeoLocation.java

/**
 * Location./*from  www. j ava2s .c  o m*/
 *
 * @param ip the ip
 * @return the map
 */
public Map<String, Object> location(String ip) {
    CityResponse cityResponse;
    try {
        cityResponse = this.databaseReader.city(InetAddress.getByName(ip));
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException(ip + " is not valid", e);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    if (cityResponse == null) {
        return null;
    }
    City city = null;
    Country country = null;
    Continent continent = null;
    Subdivision one = null;
    Subdivision two = null;
    Map<String, Object> data = new LinkedHashMap<>();
    data.put("ip", ip);
    if (cityResponse.getLocation() != null) {
        data.put("latitude", cityResponse.getLocation().getLatitude());
        data.put("longitude", cityResponse.getLocation().getLongitude());
    }
    if (cityResponse.getCity() != null) {
        data.put("cityName", cityResponse.getCity().getName());
        city = this.cityFinder.find(cityResponse.getCity().getGeoNameId());
    }
    if (city == null && cityResponse.getLocation() != null) {
        city = this.cityFinder.find(cityResponse.getLocation().getLongitude(),
                cityResponse.getLocation().getLatitude());
    }
    //if city does not match country, remove it
    if (city != null && cityResponse.getCountry() != null
            && !cityResponse.getCountry().getIsoCode().equals(city.getCountryIsoCode())) {
        city = null;
    }
    if (cityResponse.getCountry() != null) {
        country = countryFinder.find(cityResponse.getCountry().getGeoNameId());
    }
    if (country == null && city != null) {
        country = countryFinder.find(city.getCountryIsoCode());
    }
    if (continent == null && cityResponse.getContinent() != null) {
        continent = continentFinder.find(cityResponse.getContinent().getCode());
    }
    if (continent == null && country != null) {
        continent = continentFinder.find(country.getContinent());
    }
    if (city != null) {
        data.put("cityName", city.getName());
        TimeZone timeZone = timeZoneFinder.find(city.getTimeZone());
        if (timeZone != null) {
            HashMap<String, Object> timeZoneMap = new HashMap<>();
            timeZoneMap.put("name", timeZone.getName());
            timeZoneMap.put("dtsOffset", timeZone.getDtsOffset());
            timeZoneMap.put("utcOffset", timeZone.getUtcOffset());
            timeZoneMap.put("currentOffset", timeZone.getCurrentOffset());
            timeZoneMap.put("changedAt", timeZone.getChangedAt());
            data.put("timeZone", timeZoneMap);
        }
        one = subdivisionFinder.find(city.getCountryIsoCode(), city.getSubdivisionOne());
        two = subdivisionFinder.find(city.getCountryIsoCode(), city.getSubdivisionOne(),
                city.getSubdivisionTwo());
    } else if (cityResponse.getSubdivisions() != null && cityResponse.getSubdivisions().size() > 0) {
        one = subdivisionFinder.find(cityResponse.getSubdivisions().get(0).getGeoNameId());
        if (cityResponse.getSubdivisions().size() > 1) {
            two = subdivisionFinder.find(cityResponse.getSubdivisions().get(1).getGeoNameId());
        }
    }
    List<String> subdivisions = new ArrayList<>();
    if (two != null) {
        subdivisions.add(two.getName());
    }
    if (one != null) {
        subdivisions.add(one.getName());
    }
    if (subdivisions != null && subdivisions.size() > 0) {
        data.put("subdivisions", subdivisions);
    }
    Map<String, Object> countryMap = new LinkedHashMap<>();
    if (country != null) {
        countryMap.put("capital", country.getCapital());
        countryMap.put("currencyCode", country.getCurrencyCode());
        countryMap.put("currencyName", country.getCurrencyName());
        countryMap.put("language", country.getLanguage());
        countryMap.put("name", country.getName());
        countryMap.put("phone", country.getPhone());
        countryMap.put("iso", country.getIso());
    } else if (cityResponse.getCountry() != null) {
        countryMap.put("name", cityResponse.getCountry().getName());
        countryMap.put("iso", cityResponse.getCountry().getIsoCode());
    }
    if (countryMap.size() > 0) {
        data.put("country", countryMap);
    }
    Map<String, Object> continentMap = new LinkedHashMap<>();
    if (continent != null) {
        continentMap.put("iso", continent.getIso());
        continentMap.put("name", continent.getName());
    } else if (cityResponse.getContinent() != null) {
        continentMap.put("iso", cityResponse.getContinent().getCode());
        continentMap.put("name", cityResponse.getContinent().getName());
    }
    if (continentMap.size() > 0) {
        data.put("continent", continentMap);
    }
    return data;
}

From source file:cn.com.loopj.android.http.AsyncHttpRequest.java

private void makeRequestWithRetries() throws IOException {
    boolean retry = true;
    IOException cause = null;/*from  ww w.  j a  va  2s .  c o  m*/
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    try {
        while (retry) {
            try {
                makeRequest();
                return;
            } catch (UnknownHostException e) {
                // switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
                // while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
                // (to assist in genuine cases of unknown host) which seems better than outright failure
                cause = new IOException("UnknownHostException exception: " + e.getMessage());
                retry = (executionCount > 0) && retryHandler.retryRequest(e, ++executionCount, context);
            } catch (NullPointerException e) {
                // there's a bug in HttpClient 4.0.x that on some occasions causes
                // DefaultRequestExecutor to throw an NPE, see
                // https://code.google.com/p/android/issues/detail?id=5255
                cause = new IOException("NPE in HttpClient: " + e.getMessage());
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            } catch (IOException e) {
                if (isCancelled()) {
                    // Eating exception, as the request was cancelled
                    return;
                }
                cause = e;
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            }
            if (retry) {
                responseHandler.sendRetryMessage(executionCount);
            }
        }
    } catch (Exception e) {
        // catch anything else to ensure failure message is propagated
        AsyncHttpClient.log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
        cause = new IOException("Unhandled exception: " + e.getMessage());
    }

    // cleaned up to throw IOException
    throw (cause);
}

From source file:com.skydragon.gplay.loopj.android.http.AsyncHttpRequest.java

private void makeRequestWithRetries() throws IOException {
    boolean retry = true;
    IOException cause = null;//from  ww  w.j a  v  a 2 s .  c o m
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    try {
        while (retry) {
            try {
                makeRequest();
                return;
            } catch (UnknownHostException e) {
                // switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
                // while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
                // (to assist in genuine cases of unknown host) which seems better than outright failure
                cause = new IOException("UnknownHostException exception: " + e.getMessage());
                retry = (executionCount > 0) && retryHandler.retryRequest(e, ++executionCount, context);
            } catch (NullPointerException e) {
                // there's a bug in HttpClient 4.0.x that on some occasions causes
                // DefaultRequestExecutor to throw an NPE, see
                // http://code.google.com/p/android/issues/detail?id=5255
                cause = new IOException("NPE in HttpClient: " + e.getMessage());
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            } catch (IOException e) {
                if (isCancelled()) {
                    // Eating exception, as the request was cancelled
                    return;
                }
                cause = e;
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            }
            if (retry) {
                responseHandler.sendRetryMessage(executionCount);
            }
        }
    } catch (Exception e) {
        // catch anything else to ensure failure message is propagated
        AsyncHttpClient.log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
        cause = new IOException("Unhandled exception: " + e.getMessage());
    }

    // cleaned up to throw IOException
    throw (cause);
}

From source file:flex.messaging.services.http.proxy.RequestFilter.java

/**
 * Send the request.// ww w  .  j a  v  a2 s . c  om
 *
 * @param context the context
 */
protected void sendRequest(ProxyContext context) {
    Target target = context.getTarget();
    String method = context.getMethod();
    HttpMethod httpMethod = context.getHttpMethod();
    final String BEGIN = "-- Begin ";
    final String END = "-- End ";
    final String REQUEST = " request --";

    if (httpMethod instanceof EntityEnclosingMethod) {
        Object data = processBody(context);
        Class dataClass = data.getClass();
        if (data instanceof String) {
            String requestString = (String) data;
            if (Log.isInfo()) {
                Logger logger = Log.getLogger(HTTPProxyService.LOG_CATEGORY);
                logger.debug(BEGIN + method + REQUEST);
                logger.debug(StringUtils.prettifyString(requestString));
                logger.debug(END + method + REQUEST);
            }

            try {
                StringRequestEntity requestEntity = new StringRequestEntity(requestString, null, "UTF-8");
                ((EntityEnclosingMethod) httpMethod).setRequestEntity(requestEntity);
            } catch (UnsupportedEncodingException ex) {
                ProxyException pe = new ProxyException(CAUGHT_ERROR);
                pe.setDetails(CAUGHT_ERROR, "1", new Object[] { ex });
                throw pe;
            }
        } else if (dataClass.isArray() && Byte.TYPE.equals(dataClass.getComponentType())) {
            byte[] dataBytes = (byte[]) data;
            ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(dataBytes,
                    context.getContentType());
            ((EntityEnclosingMethod) httpMethod).setRequestEntity(requestEntity);
        } else if (data instanceof InputStream) {
            InputStreamRequestEntity requestEntity = new InputStreamRequestEntity((InputStream) data,
                    context.getContentType());
            ((EntityEnclosingMethod) httpMethod).setRequestEntity(requestEntity);
        }
        //TODO: Support multipart post
        //else
        //{
        //FIXME: Throw exception if unhandled data type
        //}
    } else if (httpMethod instanceof GetMethod) {
        Object req = processBody(context);

        if (req instanceof String) {
            String requestString = (String) req;
            if (Log.isInfo()) {
                Logger logger = Log.getLogger(HTTPProxyService.LOG_CATEGORY);
                logger.debug(BEGIN + method + REQUEST);
                logger.debug(StringUtils.prettifyString(requestString));
                logger.debug(END + method + REQUEST);
            }

            if (!"".equals(requestString)) {
                String query = context.getHttpMethod().getQueryString();
                if (query != null) {
                    query += "&" + requestString;
                } else {
                    query = requestString;
                }
                context.getHttpMethod().setQueryString(query);
            }
        }
    }

    context.getHttpClient().setHostConfiguration(target.getHostConfig());

    try {
        context.getHttpClient().executeMethod(context.getHttpMethod());
    } catch (UnknownHostException uhex) {
        ProxyException pe = new ProxyException();
        pe.setMessage(UNKNOWN_HOST, new Object[] { uhex.getMessage() });
        pe.setCode(ProxyException.CODE_SERVER_PROXY_REQUEST_FAILED);
        throw pe;
    } catch (Exception ex) {
        // FIXME: JRB - could be more specific by looking for timeout and sending 504 in that case.
        // rfc2616 10.5.5 504 - could get more specific if we parse the HttpException
        ProxyException pe = new ProxyException(CAUGHT_ERROR);
        pe.setDetails(CAUGHT_ERROR, "1", new Object[] { ex.getMessage() });
        pe.setCode(ProxyException.CODE_SERVER_PROXY_REQUEST_FAILED);
        throw pe;
    }
}

From source file:com.mirth.connect.connectors.mllp.MllpMessageReceiver.java

public void doConnect() throws ConnectException {
    disposing.set(false);//from  w  w  w  .  j  a  va 2  s  .  co  m

    if (connector.isServerMode()) {
        URI uri = endpoint.getEndpointURI().getUri();
        try {
            serverSocket = createServerSocket(uri);
            monitoringController.updateStatus(connector, connectorType, Event.INITIALIZED);
        } catch (UnknownHostException e) {
            logger.error(e.getClass().getName() + ": " + e.getMessage());
            throw new org.mule.providers.ConnectException(new Message("tcp", 1, uri), e, this);
        } catch (Exception e) {
            throw new org.mule.providers.ConnectException(new Message("tcp", 1, uri), e, this);
        }
        try {
            getWorkManager().scheduleWork(this, WorkManager.INDEFINITE, null, null);
        } catch (WorkException e) {
            throw new ConnectException(new Message(Messages.FAILED_TO_SCHEDULE_WORK), e, this);
        }
    } else {
        // If client mode, just start up one thread.
        Thread llpReceiver = new Thread(this);
        llpReceiver.start();
        monitoringController.updateStatus(connector, connectorType, Event.INITIALIZED);
    }
}

From source file:com.haoqee.chat.global.VoiceTask.java

private void makeRequestWithRetries(HttpUriRequest request) throws IOException {
    if (isResume && targetUrl != null) {
        File downloadFile = new File(targetUrl);
        long fileLen = 0;
        if (downloadFile.isFile() && downloadFile.exists()) {
            fileLen = downloadFile.length();
        }/*from  ww w .j a  v a2 s.c  om*/
        if (fileLen > 0)
            request.setHeader("RANGE", "bytes=" + fileLen + "-");
    }

    boolean retry = true;
    IOException cause = null;
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            if (!isCancelled()) {
                HttpResponse response = client.execute(request, context);
                if (!isCancelled()) {
                    handleResponse(response);
                }
            }
            return;
        } catch (UnknownHostException e) {
            publishProgress(UPDATE_FAILURE, e, "unknownHostExceptioncan't resolve host");
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // HttpClient 4.0.x ?bug
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (Exception e) {
            cause = new IOException("Exception" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }
    if (cause != null)
        throw cause;
    else
        throw new IOException("");
}

From source file:cn.com.dfc.pl.afinal.http.HttpHandler.java

private void makeRequestWithRetries(HttpUriRequest request) throws IOException {
    if (isResume && targetUrl != null) {
        File downloadFile = new File(targetUrl);
        long fileLen = 0;
        if (downloadFile.isFile() && downloadFile.exists()) {
            fileLen = downloadFile.length();
        }//  w w w.ja  va  2s .c om
        if (fileLen > 0)
            request.setHeader("RANGE", "bytes=" + fileLen + "-");
    }

    boolean retry = true;
    IOException cause = null;
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            if (!isCancelled()) {
                HttpResponse response = client.execute(request, context);
                if (!isCancelled()) {
                    handleResponse(response);
                }
            }
            return;
        } catch (UnknownHostException e) {
            publishProgress(UPDATE_FAILURE, e, 0, "unknownHostExceptioncan't resolve host");
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // HttpClient 4.0.x ?bug
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (Exception e) {
            cause = new IOException("Exception" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }
    if (cause != null)
        throw cause;
    else
        throw new IOException("");
}

From source file:com.dongfang.net.http.HttpHandler.java

@SuppressWarnings("unchecked")
private ResponseInfo<T> sendRequest(HttpRequestBase request) throws HttpException {
    if (autoResume && isDownloadingFile) {
        File downloadFile = new File(fileSavePath);
        long fileLen = 0;
        if (downloadFile.isFile() && downloadFile.exists()) {
            fileLen = downloadFile.length();
        }/*from   ww w  .j  a  v  a  2  s .  co m*/
        if (fileLen > 0) {
            request.setHeader("RANGE", "bytes=" + fileLen + "-");
        }
    }

    boolean retry = true;
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        IOException exception = null;
        try {
            // ??
            // if (request.getMethod().equals(HttpRequest.HttpMethod.GET.toString())) {
            // String result = HttpUtils.sHttpGetCache.get(requestUrl);
            // if (result != null) {
            // return new ResponseInfo<T>(null, (T) result, true);
            // }
            // }

            ResponseInfo<T> responseInfo = null;
            if (!isCancelled()) {
                HttpResponse response = client.execute(request, context);
                responseInfo = handleResponse(response);
            }
            return responseInfo;
        } catch (UnknownHostException e) {
            exception = e;
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        } catch (IOException e) {
            exception = e;
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        } catch (NullPointerException e) {
            exception = new IOException(e.getMessage());
            exception.initCause(e);
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        } catch (HttpException e) {
            throw e;
        } catch (Throwable e) {
            exception = new IOException(e.getMessage());
            exception.initCause(e);
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        }
        if (!retry && exception != null) {
            throw new HttpException(exception);
        }
    }
    return null;
}

From source file:com.flyn.net.asynchttp.AsyncHttpRequest.java

private void makeRequestWithRetries() throws IOException {
    boolean retry = true;
    IOException cause = null;//from   w w  w  .j  a va 2s.com
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    try {
        while (retry) {
            try {
                makeRequest();
                return;
            } catch (UnknownHostException e) {
                // switching between WI-FI and mobile data networks can
                // cause a retry which then results in an
                // UnknownHostException
                // while the WI-FI is initialising. The retry logic will be
                // invoked here, if this is NOT the first retry
                // (to assist in genuine cases of unknown host) which seems
                // better than outright failure
                cause = new IOException("UnknownHostException exception: " + e.getMessage());
                retry = (executionCount > 0) && retryHandler.retryRequest(cause, ++executionCount, context);
            } catch (NullPointerException e) {
                // there's a bug in HttpClient 4.0.x that on some occasions
                // causes
                // DefaultRequestExecutor to throw an NPE, see
                // http://code.google.com/p/android/issues/detail?id=5255
                cause = new IOException("NPE in HttpClient: " + e.getMessage());
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            } catch (IOException e) {
                if (isCancelled()) {
                    // Eating exception, as the request was cancelled
                    return;
                }
                cause = e;
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            }
            if (retry && (responseHandler != null)) {
                responseHandler.sendRetryMessage(executionCount);
            }
        }
    } catch (Exception e) {
        // catch anything else to ensure failure message is propagated
        Log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
        cause = new IOException("Unhandled exception: " + e.getMessage());
    }

    // cleaned up to throw IOException
    throw (cause);
}