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(String message) 

Source Link

Document

Constructs a new UnknownHostException with the specified detail message.

Usage

From source file:Main.java

private static int stringToIpAddr(String addrString) throws UnknownHostException {
    try {//from   w  w  w  .ja  va2s.com
        String[] parts = addrString.split("\\.");
        if (parts.length != 4) {
            throw new UnknownHostException(addrString);
        }

        int a = Integer.parseInt(parts[0]);
        int b = Integer.parseInt(parts[1]) << 8;
        int c = Integer.parseInt(parts[2]) << 16;
        int d = Integer.parseInt(parts[3]) << 24;

        return a | b | c | d;
    } catch (NumberFormatException ex) {
        throw new UnknownHostException(addrString);
    }
}

From source file:dk.dr.radio.net.Diverse.java

/**
   * Tjek for om vi er p et netvrk der krver login eller lignende.
   * Se 'Handling Network Sign-On' i http://developer.android.com/reference/java/net/HttpHttpURLConnection.html
   *//*from  w ww.  j av a2 s .co  m*/
  private static void tjekOmdirigering(URL u, HttpURLConnection urlConnection) throws IOException {
      URL u2 = urlConnection.getURL();
      if (!u.getHost().equals(u2.getHost())) {
          // Vi blev omdirigeret
          Log.d("tjekOmdirigering " + u);
          Log.d("tjekOmdirigering " + u2);
          //Log.rapporterFejl(omdirigeringsfejl);
          throw new UnknownHostException("Der blev omdirigeret fra " + u.getHost() + " til " + u2.getHost());
      }
  }

From source file:com.mb.framework.util.property.HostnameAwareList.java

/**
 * default constructor/*from  ww w . j  ava  2  s.  c  o m*/
 * 
 * @throws UnknownHostException
 */
public HostnameAwareList() throws UnknownHostException {
    hostname = InetAddress.getLocalHost().getHostName().toUpperCase();
    logger.debug("Resolved hostname: " + hostname);

    if (StringUtils.isBlank(hostname)) {
        throw new UnknownHostException("Error resolving hostname, hostname is null or empty: " + hostname);
    }
}

From source file:ste.xtest.net.BugFreeXTestHandler.java

@Test
public void hostNotFound() throws Exception {
    final UnknownHostException EXCEPTION = new UnknownHostException("test.nowhere.com");
    XTestHandler.exceptionOnConnect = EXCEPTION;

    try {/*from  ww w. j a va  2  s  .  co  m*/
        URL url = new URL("xtest://test.nowhere.com");
        URLConnection conn = url.openConnection();
        conn.connect();
        fail("UnknownHostException not thrown");
    } catch (UnknownHostException x) {
        //
        // OK
        //
        assertSame(EXCEPTION, x);
    }

}

From source file:com.geocent.owf.openlayers.DataRequestProxy.java

/**
 * Gets the data from the provided remote URL.
 * Note that the data will be returned from the method and not automatically
 * populated into the response object.//  ww w.j av a  2  s.c om
 * 
 * @param request       ServletRequest object containing the request data.
 * @param response      ServletResponse object for the response information.
 * @param url           URL from which the data will be retrieved.
 * @return              Data from the provided remote URL.
 * @throws IOException 
 */
public String getData(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {

    if (!allowUrl(url)) {
        throw new UnknownHostException("Request to invalid host not allowed.");
    }

    HttpURLConnection connection = (HttpURLConnection) (new URL(url).openConnection());
    connection.setRequestMethod(request.getMethod());

    // Detects a request with a payload inside the message body
    if (request.getContentLength() > 0) {
        connection.setRequestProperty("Content-Type", request.getContentType());
        connection.setDoOutput(true);
        connection.setDoInput(true);
        byte[] requestPayload = IOUtils.toByteArray(request.getInputStream());
        connection.getOutputStream().write(requestPayload);
        connection.getOutputStream().flush();
    }

    // Create handler for the given content type and proxy the extracted information
    Handler contentHandler = HandlerFactory.getHandler(connection.getContentType());
    return contentHandler.handleContent(response, connection.getInputStream());
}

From source file:com.cloupia.feature.nimble.http.MySSLSocketFactory.java

public Socket createSocket(String host, int port) throws IOException, UnknownHostException {

    TrustManager[] trustAllCerts = getTrustManager();

    try {//from   ww  w  . j a v  a  2s  .c om

        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        SocketFactory socketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();

        return socketFactory.createSocket(host, port);

    }

    catch (Exception ex) {

        throw new UnknownHostException("Problems to connect " + host + ex.toString());

    }

}

From source file:com.clican.pluto.cms.core.service.impl.SiteServiceImpl.java

public FTPClient getFTPClient(ISite site) throws IOException {
    String url = site.getUrl();/* w  w w.j a  v  a 2 s .  co  m*/
    if (url == null) {
        return null;
    }
    if (url.startsWith("ftp://")) {
        int port = 21;
        String hostname = null;
        try {
            url = url.substring(6);
            if (url.indexOf(":") != -1) {
                hostname = url.substring(0, url.indexOf(":"));
                if (url.endsWith("/")) {
                    port = Integer.parseInt(url.substring(url.indexOf(":") + 1, url.length() - 1));
                } else {
                    port = Integer.parseInt(url.substring(url.indexOf(":") + 1));
                }
            } else {
                if (url.endsWith("/")) {
                    hostname = url.substring(0, url.length() - 1);
                } else {
                    hostname = url;
                }
            }
        } catch (Exception e) {
            throw new UnknownHostException(url);
        }
        FTPClient client = new FTPClient();
        client.connect(hostname, port);
        if (log.isDebugEnabled()) {
            log.debug("Connected to " + url + ".");
            log.debug(client.getReplyString());
        }
        int reply = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            log.warn("FTP server " + url + " refused connection.");
            return null;
        }
        if (StringUtils.isNotEmpty(site.getUsername())) {
            boolean login = client.login(site.getUsername(), site.getPassword());
            if (!login) {
                client.disconnect();
                return null;
            }
        }

        return client;
    }
    return null;
}

From source file:ste.xtest.net.BugFreeStubURLConnectionBehaviour.java

@Test
public void throw_a_network_error() throws Exception {
    StubURLConnection C = new StubURLConnection(new URL(TEST_URL_DUMMY));
    IOException e = new UnknownHostException("a.host.com");
    then(C.error(e)).isSameAs(C);//from   w ww.j  a  va  2  s .co  m

    try {
        C.connect();
        fail("error not thrown");
    } catch (IOException x) {
        then(x).isSameAs(e);
    }

    e = new SocketException();
    then(C.error(e)).isSameAs(C);

    try {
        C.connect();
        fail("error not thrown");
    } catch (IOException x) {
        then(x).isSameAs(e);
    }
}

From source file:bad.robot.http.apache.ApacheExceptionWrappingExecutorTest.java

@Test
public void wrapsUnknownHostException() {
    exception.expect(HttpUnknownHostException.class);
    exception.expect(new ThrowableCauseMatcher(UnknownHostException.class));
    wrapper.submit(throwsException(new UnknownHostException("cheese")));
}

From source file:com.cloupia.feature.nimble.http.MySSLSocketFactory.java

public Socket createSocket(Socket socket, String host, int port, boolean flag)
        throws IOException, UnknownHostException {

    TrustManager[] trustAllCerts = getTrustManager();

    try {//from w ww  .  jav a2  s .  c o  m

        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        SocketFactory socketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();

        return socketFactory.createSocket(host, port);

    }

    catch (Exception ex) {

        throw new UnknownHostException("Problems to connect " + host + ex.toString());

    }

}