Example usage for java.net ConnectException ConnectException

List of usage examples for java.net ConnectException ConnectException

Introduction

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

Prototype

public ConnectException() 

Source Link

Document

Construct a new ConnectException with no detailed message.

Usage

From source file:eu.vital.TrustManager.connectors.dms.DMSManager.java

private String queryWithExceptions(String dms_endpoint, String body, String method)
        throws SocketTimeoutException, ConnectException, IOException, InterruptedException {
    Cookie ck;/*ww  w.  j av  a  2  s. c o m*/
    //String internalToken;
    CloseableHttpClient httpclient;
    HttpRequestBase httpaction;
    //boolean wasEmpty;
    //int code;

    httpclient = HttpClients.createDefault();

    URI uri = null;
    try {
        // Prepare to forward the request to the proxy
        uri = new URI(dms_URL + "/" + dms_endpoint);
    } catch (URISyntaxException e1) {
        java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, e1);
    }

    if (method.equals("GET")) {
        httpaction = new HttpGet(uri);
    } else {
        httpaction = new HttpPost(uri);
    }

    // Get token or authenticate if null or invalid
    //internalToken = client.getToken();
    ck = new Cookie("vitalAccessToken", cookie.substring(17));

    httpaction.setHeader("Cookie", ck.toString());
    httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000)
            .setSocketTimeout(5000).build());
    httpaction.setHeader("Content-Type", javax.ws.rs.core.MediaType.APPLICATION_JSON);

    StringEntity strEntity = new StringEntity(body, StandardCharsets.UTF_8);

    if (method.equals("POST")) {
        ((HttpPost) httpaction).setEntity(strEntity);
    }

    // Execute and get the response.
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpaction);
    } catch (ClientProtocolException e) {
        throw new ClientProtocolException();
    } catch (IOException e) {
        try {
            // Try again with a higher timeout
            try {
                Thread.sleep(1000); // do not retry immediately
            } catch (InterruptedException e1) {
                throw new InterruptedException();
                // e1.printStackTrace();
            }
            httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(7000)
                    .setConnectTimeout(7000).setSocketTimeout(7000).build());
            response = httpclient.execute(httpaction);
        } catch (ClientProtocolException ea) {
            java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, ea);
            throw new ClientProtocolException();
        } catch (IOException ea) {
            try {
                // Try again with a higher timeout
                try {
                    Thread.sleep(1000); // do not retry immediately
                } catch (InterruptedException e1) {
                    java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, e1);
                    throw new InterruptedException();
                }
                httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(12000)
                        .setConnectTimeout(12000).setSocketTimeout(12000).build());
                response = httpclient.execute(httpaction);
            } catch (ClientProtocolException eaa) {
                java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa);
                throw new ClientProtocolException();
            } catch (SocketTimeoutException eaa) {
                java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa);
                throw new SocketTimeoutException();
            } catch (ConnectException eaa) {
                java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa);
                throw new ConnectException();
            } catch (ConnectTimeoutException eaa) {
                java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa);
                throw new ConnectTimeoutException();
            }
        }
    }

    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_ACCEPTED) {
        if (statusCode == 503) {
            java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null,
                    "httpStatusCode 503");
            throw new ServiceUnavailableException();
        } else if (statusCode == 502) {
            java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null,
                    "httpStatusCode 502");
            throw new ServerErrorException(502);
        } else if (statusCode == 401) {
            java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null,
                    "could't Athorize the DMS");
            throw new NotAuthorizedException("could't Athorize the DMS");
        } else {
            java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null,
                    "httpStatusCode 500");
            throw new ServiceUnavailableException();
        }
    }

    HttpEntity entity;
    entity = response.getEntity();
    String respString = "";

    if (entity != null) {
        try {
            respString = EntityUtils.toString(entity);
            response.close();
        } catch (ParseException | IOException e) {
            java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    return respString;

}

From source file:org.apache.atlas.AtlasClientTest.java

@Test
public void shouldRetryWithSameClientIfSingleAddressIsUsed() throws URISyntaxException, AtlasServiceException {
    setupRetryParams();// w w w . j a  v  a 2 s  . c  o  m

    ResourceCreator resourceCreator = mock(ResourceCreator.class);
    WebResource resourceObject = mock(WebResource.class);
    when(resourceObject.getURI()).thenReturn(new URI("http://localhost:31000/api/atlas/types"));

    WebResource.Builder builder = getBuilder(resourceObject);

    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    String activeStatus = "{\"Status\":\"ACTIVE\"}";
    when(response.getEntity(String.class)).thenReturn(activeStatus);
    when(response.getLength()).thenReturn(activeStatus.length());

    when(builder.method(AtlasClient.API.LIST_TYPES.getMethod(), ClientResponse.class, null))
            .thenThrow(
                    new ClientHandlerException("simulating exception in calling API", new ConnectException()))
            .thenReturn(response);

    when(resourceCreator.createResource()).thenReturn(resourceObject);
    when(configuration.getString("atlas.http.authentication.type", "simple")).thenReturn("simple");

    AtlasClient atlasClient = getClientForTest("http://localhost:31000");

    atlasClient.setService(resourceObject);
    atlasClient.setConfiguration(configuration);

    atlasClient.callAPIWithRetries(AtlasClient.API.LIST_TYPES, null, resourceCreator);

    verify(client).destroy();
    verify(client, times(2)).resource(UriBuilder.fromUri("http://localhost:31000").build());
}

From source file:org.apache.atlas.AtlasClientTest.java

@Test
public void shouldRetryAPICallsOnServiceUnavailable() throws AtlasServiceException, URISyntaxException {
    setupRetryParams();// www. j  av a2s . co m

    ResourceCreator resourceCreator = mock(ResourceCreator.class);
    WebResource resourceObject = mock(WebResource.class);
    when(resourceObject.getURI()).thenReturn(new URI("http://localhost:31000/api/atlas/types"))
            .thenReturn(new URI("http://localhost:41000/api/atlas/types"))
            .thenReturn(new URI("http://localhost:41000/api/atlas/types"));

    WebResource.Builder builder = getBuilder(resourceObject);

    ClientResponse firstResponse = mock(ClientResponse.class);
    when(firstResponse.getStatus()).thenReturn(Response.Status.SERVICE_UNAVAILABLE.getStatusCode());
    when(firstResponse.getClientResponseStatus()).thenReturn(ClientResponse.Status.SERVICE_UNAVAILABLE);

    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    String activeStatus = "{\"Status\":\"ACTIVE\"}";
    when(response.getEntity(String.class)).thenReturn(activeStatus);
    when(response.getLength()).thenReturn(activeStatus.length());

    when(builder.method(AtlasClient.API.LIST_TYPES.getMethod(), ClientResponse.class, null))
            .thenThrow(
                    new ClientHandlerException("simulating exception in calling API", new ConnectException()))
            .thenReturn(firstResponse).thenReturn(response);

    when(resourceCreator.createResource()).thenReturn(resourceObject);

    AtlasClient atlasClient = getClientForTest("http://localhost:31000", "http://localhost:41000");
    atlasClient.setService(resourceObject);
    atlasClient.setConfiguration(configuration);

    atlasClient.callAPIWithRetries(AtlasClient.API.LIST_TYPES, null, resourceCreator);

    verify(client).destroy();
    verify(client).resource(UriBuilder.fromUri("http://localhost:31000").build());
    verify(client).resource(UriBuilder.fromUri("http://localhost:41000").build());
}

From source file:hudson.FilePathTest.java

@Issue("JENKINS-26196")
@Test//  ww  w . j a  va2 s  . com
public void installIfNecessarySkipsDownloadWhenErroneous() throws Exception {
    File tmp = temp.getRoot();
    final FilePath d = new FilePath(tmp);
    d.child(".timestamp").touch(123000);
    final HttpURLConnection con = mock(HttpURLConnection.class);
    final URL url = someUrlToZipFile(con);
    when(con.getResponseCode()).thenReturn(HttpURLConnection.HTTP_GATEWAY_TIMEOUT);
    when(con.getResponseMessage()).thenReturn("Gateway Timeout");
    when(con.getInputStream()).thenThrow(new ConnectException());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String message = "going ahead";
    assertFalse(d.installIfNecessaryFrom(url, new StreamTaskListener(baos), message));
    verify(con).setIfModifiedSince(123000);
    String log = baos.toString();
    assertFalse(log, log.contains(message));
    assertTrue(log, log.contains("504 Gateway Timeout"));
}