org.apache.http.conn.HttpHostConnectException Java Examples

The following examples show how to use org.apache.http.conn.HttpHostConnectException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServerLifeCycleTest.java    From JerryMouse with MIT License 6 votes vote down vote up
@Test(expected = HttpHostConnectException.class)
public void testServerStartAndStop() throws Exception {
    int availablePort = NetUtils.getAvailablePort();
    HttpServer httpSever = new HttpServer("/tmp/static");
    List<Context> contexts = httpSever.getContexts();
    Context context = contexts.get(0);
    context.setPort(availablePort);
    httpSever.start();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:" + availablePort + "/");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String responseStr = EntityUtils.toString(entity);
    assertEquals("/", responseStr);
    httpSever.destroy();
    httpclient.execute(httpget);
}
 
Example #2
Source File: HttpErrorPage.java    From esigate with Apache License 2.0 6 votes vote down vote up
public static CloseableHttpResponse generateHttpResponse(Exception exception) {
    if (exception instanceof HttpHostConnectException) {
        return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Connection refused");
    } else if (exception instanceof ConnectionPoolTimeoutException) {
        return generateHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, "Connection pool timeout");
    } else if (exception instanceof ConnectTimeoutException) {
        return generateHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, "Connect timeout");
    } else if (exception instanceof SocketTimeoutException) {
        return generateHttpResponse(HttpStatus.SC_GATEWAY_TIMEOUT, "Socket timeout");
    } else if (exception instanceof SocketException) {
        return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Socket Exception");
    } else if (exception instanceof ClientProtocolException) {
        String message = exception.getMessage();
        if (message == null && exception.getCause() != null) {
            message = exception.getCause().getMessage();
        }
        return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Protocol error: " + message);
    } else {
        LOG.error("Error retrieving URL", exception);
        return generateHttpResponse(HttpStatus.SC_BAD_GATEWAY, "Error retrieving URL");
    }
}
 
Example #3
Source File: ApacheHttpTransportTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10_000L)
public void testConnectTimeout() {
  // Apache HttpClient doesn't appear to behave correctly on windows
  assumeTrue(!isWindows());

  HttpTransport httpTransport = new ApacheHttpTransport();
  GenericUrl url = new GenericUrl("http://google.com:81");
  try {
    httpTransport.createRequestFactory().buildGetRequest(url).setConnectTimeout(100).execute();
    fail("should have thrown an exception");
  } catch (HttpHostConnectException | ConnectTimeoutException expected) {
    // expected
  } catch (IOException e) {
    fail("unexpected IOException: " + e.getClass().getName());
  }
}
 
Example #4
Source File: JettyServerTest.java    From celos with Apache License 2.0 6 votes vote down vote up
@Test(expected = HttpHostConnectException.class)
public void testServerStopsSpecifyPort() throws Exception {
    int port = getFreePort();

    JettyServer jettyServer = new JettyServer();
    jettyServer.start(port);

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet workflowListGet = new HttpGet("http://localhost:" + port);
    HttpResponse response = httpClient.execute(workflowListGet);

    Assert.assertEquals(response.getStatusLine().getStatusCode(), 403);
    EntityUtils.consume(response.getEntity());
    jettyServer.stop();

    httpClient.execute(workflowListGet);
}
 
Example #5
Source File: DefaultBulkRetryHandler.java    From bboss-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
	public boolean neadRetry(Exception exception, BulkCommand bulkCommand) {
		if (exception instanceof HttpHostConnectException     //NoHttpResponseException 重试
				|| exception instanceof ConnectTimeoutException //连接超时重试
				|| exception instanceof UnknownHostException
				|| exception instanceof NoHttpResponseException
				|| exception instanceof NoServerElasticSearchException
//              || exception instanceof SocketTimeoutException    //响应超时不重试,避免造成业务数据不一致
		) {

			return true;
		}

		if(exception instanceof SocketException){
			String message = exception.getMessage();
			if(message != null && message.trim().equals("Connection reset")) {
				return true;
			}
		}

		return false;
	}
 
Example #6
Source File: HealthChecker.java    From dropwizard-experiment with MIT License 6 votes vote down vote up
public boolean check() throws InterruptedException {
    try {
        HttpResponse<String> health = Unirest.get(url + "/admin/healthcheck")
                .basicAuth("test", "test")
                .asString();

        if (health.getStatus() == 200) {
            log.info("Healthy with {}", health.getBody());
            return true;
        } else {
            log.error("Unhealthy with {}", health.getBody());
            return false;
        }
    } catch (UnirestException e) {
        if (e.getCause() instanceof HttpHostConnectException && duration < maxDuration) {
            log.info("Unable to connect, retrying...");
            duration = duration + DELAY;
            Thread.sleep(TimeUnit.SECONDS.toMillis(DELAY));
            return check();
        }
        log.error("Unable to connect.", e);
        return false;
    }
}
 
Example #7
Source File: StartStopTest.java    From 2017-highload-kv with Apache License 2.0 5 votes vote down vote up
@Test
public void stop() throws Exception {
    storage.stop();
    try {
        // Should not respond after stop
        status();
    } catch (SocketTimeoutException | HttpHostConnectException e) {
        // Do nothing
    }
}
 
Example #8
Source File: HttpClientExecutor.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
public CloseableHttpResponse execute() {
    CloseableHttpResponse response;
    try  {
         response = closeableHttpClient.execute(httpRequestBase, context);
    } catch (SocketTimeoutException ste) {
        throw new RuntimeException("Socket timeout: " + ste.getMessage(), ste);
    } catch (HttpHostConnectException connectEx) {
        throw new RuntimeException("Connection error: " + connectEx.getMessage(), connectEx);
    } catch (IOException e) {

        throw new RuntimeException("Error while executing http request: " + e.getMessage(), e);
    }
    return response;
}
 
Example #9
Source File: NginxStatisticsReader.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Make a http request to http://127.0.0.1:<proxy-port>/nginx_status and find writing count.
 * @param proxyPort
 * @return
 */
private int findWritingCount(int proxyPort) {
    try {
        URL url = new URL("http", "127.0.0.1", proxyPort, "/nginx_status");
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpUriRequest request = new HttpGet(url.toURI());
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("http://127.0.0.1:" + proxyPort + "/nginx_status was not found");
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                (response.getEntity().getContent())));
        String output, result = "";
        while ((output = reader.readLine()) != null) {
            result += output;
        }
        Pattern pattern = Pattern.compile("(Writing: )([0-1]*)");
        Matcher matcher = pattern.matcher(result);
        if (matcher.find()) {
            // Deduct one to remove the above request
            int writingCount = Integer.parseInt(matcher.group(2)) - 1;
            if(log.isDebugEnabled()) {
                log.debug(String.format("Writing count: [proxy] %d [value] %d", proxyPort, writingCount));
            }
            return writingCount;
        }
        throw new RuntimeException("Writing block was not found in nginx_status response");
    } catch (HttpHostConnectException ignore) {
        if(ignore.getMessage().contains("Connection refused")) {
            log.warn("Could not find in-flight request count, connection refused: " +
                    "http://127.0.0.1:" + proxyPort + "/nginx_status");
        }
    } catch (Exception e) {
        log.error("Could not find in-flight request count: http://127.0.0.1:" + proxyPort + "/nginx_status", e);
    }
    return 0;
}
 
Example #10
Source File: DropWizardWebsocketsTest.java    From dropwizard-websockets with MIT License 5 votes vote down vote up
public static void waitUrlAvailable(final String url) throws InterruptedException, IOException {
    for (int i = 0; i < 50; i++) {
        Thread.sleep(100);
        try {
            if (HttpClients.createDefault().execute(new HttpGet(url)).getStatusLine().getStatusCode() > -100)
                break;
        } catch (HttpHostConnectException ex) {
        }
    }
}
 
Example #11
Source File: ExampleIntegrationTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void helloGcs_shouldRunWithFunctionsFramework() throws Throwable {
  String functionUrl = BASE_URL + "/helloGcs"; // URL to your locally-running function

  // Initialize constants
  String name = UUID.randomUUID().toString();
  String jsonStr = gson.toJson(Map.of(
      "data", Map.of(
          "name", name, "resourceState", "exists", "metageneration", 1),
      "context", Map.of(
          "eventType", "google.storage.object.finalize")
  ));

  HttpPost postRequest =  new HttpPost(URI.create(functionUrl));
  postRequest.setEntity(new StringEntity(jsonStr));

  // The Functions Framework Maven plugin process takes time to start up
  // Use resilience4j to retry the test HTTP request until the plugin responds
  RetryRegistry registry = RetryRegistry.of(RetryConfig.custom()
      .maxAttempts(8)
      .retryExceptions(HttpHostConnectException.class)
      .intervalFunction(IntervalFunction.ofExponentialBackoff(200, 2))
      .build());
  Retry retry = registry.retry("my");

  // Perform the request-retry process
  CheckedRunnable retriableFunc = Retry.decorateCheckedRunnable(
      retry, () -> client.execute(postRequest));
  retriableFunc.run();

  // Get Functions Framework plugin process' stdout
  InputStream stdoutStream = emulatorProcess.getErrorStream();
  ByteArrayOutputStream stdoutBytes = new ByteArrayOutputStream();
  stdoutBytes.write(stdoutStream.readNBytes(stdoutStream.available()));

  // Verify desired name value is present
  assertThat(stdoutBytes.toString(StandardCharsets.UTF_8)).contains(
      String.format("File %s uploaded.", name));
}
 
Example #12
Source File: ExampleIntegrationTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void helloPubSub_shouldRunWithFunctionsFramework() throws Throwable {
  String functionUrl = BASE_URL + "/helloPubsub"; // URL to your locally-running function

  // Initialize constants
  String name = UUID.randomUUID().toString();
  String nameBase64 = Base64.getEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8));

  String jsonStr = gson.toJson(Map.of("data", Map.of("data", nameBase64)));

  HttpPost postRequest =  new HttpPost(URI.create(functionUrl));
  postRequest.setEntity(new StringEntity(jsonStr));

  // The Functions Framework Maven plugin process takes time to start up
  // Use resilience4j to retry the test HTTP request until the plugin responds
  RetryRegistry registry = RetryRegistry.of(RetryConfig.custom()
      .maxAttempts(8)
      .retryExceptions(HttpHostConnectException.class)
      .intervalFunction(IntervalFunction.ofExponentialBackoff(200, 2))
      .build());
  Retry retry = registry.retry("my");

  // Perform the request-retry process
  CheckedRunnable retriableFunc = Retry.decorateCheckedRunnable(
      retry, () -> client.execute(postRequest));
  retriableFunc.run();

  // Get Functions Framework plugin process' stdout
  InputStream stdoutStream = emulatorProcess.getErrorStream();
  ByteArrayOutputStream stdoutBytes = new ByteArrayOutputStream();
  stdoutBytes.write(stdoutStream.readNBytes(stdoutStream.available()));

  // Verify desired name value is present
  assertThat(stdoutBytes.toString(StandardCharsets.UTF_8)).contains(
      String.format("Hello %s!", name));
}
 
Example #13
Source File: PrometheusPullServerTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = { HttpHostConnectException.class }, expectedExceptionsMessageRegExp = ".* failed: Connection refused \\(Connection refused\\)")
public void testQuit() throws IOException {
    int port = 8181;
    CollectorRegistry registry = new CollectorRegistry();
    PrometheusPullServer exporter = new PrometheusPullServer(port, registry);
    exporter.quit();

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(String.format("http://localhost:%d/metrics", port));
    client.execute(request);
}
 
Example #14
Source File: JapiClientTransfer.java    From japi with MIT License 5 votes vote down vote up
private Result postFile(String url, List<String[]> datas, File file) {
    if (!file.exists()) {
        throw new JapiException(file.getAbsolutePath() + " file not exist.");
    }
    HttpPost httpPost = new HttpPost(url);
    Integer tryCount = 0;
    while (reties > tryCount) {
        try {
            final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            multipartEntity.setCharset(Charset.forName("utf-8"));
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (String[] nameValue : datas) {
                multipartEntity.addPart(nameValue[0], new StringBody(nameValue[1], ContentType.APPLICATION_JSON));
            }
            multipartEntity.addBinaryBody("file", file);
            httpPost.setEntity(multipartEntity.build());
            httpPost.setHeader("token", token);
            String result = EntityUtils.toString(HTTP_CLIENT.execute(httpPost).getEntity());
            Result result1 = JSON.parseObject(result, ResultImpl.class);
            if (result1.getCode() != 0) {
                throw new JapiException(result1.getMsg());
            }
            return result1;
        } catch (IOException e) {
            if (e instanceof HttpHostConnectException) {
                tryCount++;
                LOGGER.warn("try connect server " + tryCount + " count.");
                try {
                    TimeUnit.SECONDS.sleep(tryTime);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    if (tryCount <= reties) {
        LOGGER.error("server connect failed.");
    }
    return null;
}
 
Example #15
Source File: JapiClientTransfer.java    From japi with MIT License 5 votes vote down vote up
private Result postValues(String url, List<String[]> datas) {
    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> valuePairs = new ArrayList<>();
    for (String[] keyValue : datas) {
        valuePairs.add(new BasicNameValuePair(keyValue[0], keyValue[1]));
    }
    Integer tryCount = 0;
    while (reties > tryCount) {
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(valuePairs, "utf-8"));
            httpPost.setHeader("token", token);
            String result = EntityUtils.toString(HTTP_CLIENT.execute(httpPost).getEntity(), Consts.UTF_8);
            Result result1 = JSON.parseObject(result, ResultImpl.class);
            if (result1.getCode() != 0) {
                throw new JapiException(result1.getMsg());
            }
            return result1;
        } catch (IOException e) {
            if (e instanceof HttpHostConnectException) {
                tryCount++;
                LOGGER.warn("try connect server " + tryCount + " count.");
                try {
                    TimeUnit.SECONDS.sleep(tryTime);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    if (tryCount <= reties) {
        LOGGER.error("server connect failed.");
    }
    return null;
}
 
Example #16
Source File: FlowableClientService.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public FlowableServiceException wrapException(Exception e, HttpUriRequest request) {
    if (e instanceof HttpHostConnectException) {
        return new FlowableServiceException("Unable to connect to the Flowable server.");
    } else if (e instanceof ConnectTimeoutException) {
        return new FlowableServiceException("Connection to the Flowable server timed out.");
    } else {
        // Use the raw exception message
        return new FlowableServiceException(e.getClass().getName() + ": " + e.getMessage());
    }
}
 
Example #17
Source File: OptimizelyHttpClientTest.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
@Test(expected = HttpHostConnectException.class)
public void testProxySettings() throws IOException {
    OptimizelyHttpClient optimizelyHttpClient = OptimizelyHttpClient.builder().build();

    // If this request succeeds then the proxy config was not picked up.
    HttpGet get = new HttpGet("https://www.optimizely.com");
    optimizelyHttpClient.execute(get);
}
 
Example #18
Source File: SimpleMetricsPublishingServiceTest.java    From geode-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void stop_hasNoHttpEndpointRunning() {
  subject.start(metricsSession);
  subject.stop(metricsSession);

  HttpGet request = new HttpGet("http://localhost:9000/");

  Throwable thrown = catchThrowable(() -> HttpClientBuilder.create().build().execute(request));

  assertThat(thrown).isInstanceOf(HttpHostConnectException.class);
}
 
Example #19
Source File: HttpClientTest.java    From netcrusher-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testClosed() throws Exception {
    HttpUriRequest request = composeRequest();

    crusher.close();

    try {
        http.execute(request);
        Assert.fail("Exception is expected");
    } catch (HttpHostConnectException e) {
        LOGGER.debug("Exception dump", e);
    }
}
 
Example #20
Source File: AviRestUtils.java    From sdk with Apache License 2.0 5 votes vote down vote up
/**
 * This method sets a custom HttpRequestRetryHandler in order to enable a custom
 * exception recovery mechanism.
 * 
 * @return A HttpRequestRetryHandler representing handling of the retryHandler.
 */
private static HttpRequestRetryHandler retryHandler(AviCredentials creds) {
	return (exception, executionCount, context) -> {

		if (executionCount >= creds.getNumApiRetries()) {
			// Do not retry if over max retry count
			return false;
		}
		if (exception instanceof InterruptedIOException) {
			// Timeout
			return false;
		}
		if (exception instanceof UnknownHostException) {
			// Unknown host
			return false;
		}
		if (exception instanceof SSLException) {
			// SSL handshake exception
			return false;
		}
		if (exception instanceof HttpHostConnectException) {
			return true;
		}
		HttpClientContext clientContext = HttpClientContext.adapt(context);
		HttpRequest request = clientContext.getRequest();
		boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
		if (idempotent) {
			// Retry if the request is considered idempotent
			return true;
		}
		return false;
	};
}
 
Example #21
Source File: RetryableRestTemplateTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
  socketTimeoutException.initCause(new SocketTimeoutException());

  httpHostConnectException
      .initCause(new HttpHostConnectException(new ConnectTimeoutException(), new HttpHost(serviceOne, 80)));
  connectTimeoutException.initCause(new ConnectTimeoutException());
}
 
Example #22
Source File: RetryableRestTemplate.java    From apollo with Apache License 2.0 5 votes vote down vote up
private boolean canRetry(Throwable e, HttpMethod method) {
  Throwable nestedException = e.getCause();
  if (method == HttpMethod.GET) {
    return nestedException instanceof SocketTimeoutException
           || nestedException instanceof HttpHostConnectException
           || nestedException instanceof ConnectTimeoutException;
  }
  return nestedException instanceof HttpHostConnectException
         || nestedException instanceof ConnectTimeoutException;
}
 
Example #23
Source File: UpDownService.java    From whirlpool with Apache License 2.0 5 votes vote down vote up
@Override
protected void collectData(Gson gson, String user, List<String> subscriptions) {
    Map<String, String> subscriptionData = new HashMap<>();
    for (String url : subscriptions) {
        try (CloseableHttpClient httpClient = HttpClientHelper.buildHttpClient()) {
            HttpUriRequest query = RequestBuilder.get()
                    .setUri(url)
                    .build();
            try (CloseableHttpResponse queryResponse = httpClient.execute(query)) {
                HttpEntity entity = queryResponse.getEntity();
                if (entity != null) {
                    String data = EntityUtils.toString(entity);
                    if (data != null && data.length() > 0) {
                        if (data.contains("www.dnsrsearch.com")) {
                            subscriptionData.put(url, "not found");
                        } else {
                            subscriptionData.put(url, "up");
                        }
                    } else {
                        subscriptionData.put(url, "down");
                    }
                } else {
                    subscriptionData.put(url, "down");
                }
            }
        } catch (HttpHostConnectException hhce) {
            subscriptionData.put(url, "down");
        } catch (Throwable throwable) {
            logger.error(throwable.getMessage(), throwable);
            subscriptionData.put(url, "down");
        }
    }

    DataResponse response = new DataResponse();
    response.setType("UpDownResponse");
    response.setId(user);
    response.setResult(MessageConstants.SUCCESS);
    response.setSubscriptionData(subscriptionData);
    responseQueue.add(gson.toJson(response));
}
 
Example #24
Source File: JettyServerTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test(expected = HttpHostConnectException.class)
public void testServerStops() throws Exception {
    JettyServer jettyServer = new JettyServer();
    int port = jettyServer.start();

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet workflowListGet = new HttpGet("http://localhost:" + port);
    HttpResponse response = httpClient.execute(workflowListGet);

    Assert.assertEquals(response.getStatusLine().getStatusCode(), 403);
    EntityUtils.consume(response.getEntity());
    jettyServer.stop();

    httpClient.execute(workflowListGet);
}
 
Example #25
Source File: SimpleMetricsPublishingServiceTest.java    From geode-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void stop_hasNoHttpEndpointRunning() {
  subject.start(metricsSession);
  subject.stop(metricsSession);

  HttpGet request = new HttpGet("http://localhost:9000/");

  Throwable thrown = catchThrowable(() -> HttpClientBuilder.create().build().execute(request));

  assertThat(thrown).isInstanceOf(HttpHostConnectException.class);
}
 
Example #26
Source File: ActivitiClientService.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public ActivitiServiceException wrapException(Exception e, HttpUriRequest request) {
	if (e instanceof HttpHostConnectException) {
		return new ActivitiServiceException("Unable to connect to the Activiti server.");
	} else if (e instanceof ConnectTimeoutException) {
		return new ActivitiServiceException("Connection to the Activiti server timed out.");
	} else {
		// Use the raw exception message
		return new ActivitiServiceException(e.getClass().getName() + ": " + e.getMessage());
	}
}
 
Example #27
Source File: ExceptionUtils.java    From cos-java-sdk-v5 with MIT License 5 votes vote down vote up
public static CosClientException createClientException(IOException ex) {
    String errorCode = ClientExceptionConstants.UNKNOWN;
    if (ex instanceof ConnectTimeoutException) {
        errorCode = ClientExceptionConstants.CONNECTION_TIMEOUT;
    } else if (ex instanceof UnknownHostException) {
        errorCode = ClientExceptionConstants.UNKNOWN_HOST;
    } else if (ex instanceof HttpHostConnectException) {
        errorCode = ClientExceptionConstants.HOST_CONNECT;
    } else if (ex instanceof SocketTimeoutException) {
        errorCode = ClientExceptionConstants.SOCKET_TIMEOUT;
    }

    return new CosClientException(ex.getMessage(), errorCode, ex);
}
 
Example #28
Source File: CelosClientServerTest.java    From celos with Apache License 2.0 4 votes vote down vote up
@Test(expected = HttpHostConnectException.class)
public void testServerStops() throws Exception {
    celosClient.getWorkflowList();
    celosServer.stopServer();
    celosClient.getWorkflowList();
}
 
Example #29
Source File: Util.java    From XPrivacy with GNU General Public License v3.0 4 votes vote down vote up
public static void bug(XHook hook, Throwable ex) {
	if (ex instanceof InvocationTargetException) {
		InvocationTargetException exex = (InvocationTargetException) ex;
		if (exex.getTargetException() != null)
			ex = exex.getTargetException();
	}

	int priority;
	if (ex instanceof ActivityShare.AbortException)
		priority = Log.WARN;
	else if (ex instanceof ActivityShare.ServerException)
		priority = Log.WARN;
	else if (ex instanceof ConnectTimeoutException)
		priority = Log.WARN;
	else if (ex instanceof FileNotFoundException)
		priority = Log.WARN;
	else if (ex instanceof HttpHostConnectException)
		priority = Log.WARN;
	else if (ex instanceof NameNotFoundException)
		priority = Log.WARN;
	else if (ex instanceof NoClassDefFoundError)
		priority = Log.WARN;
	else if (ex instanceof OutOfMemoryError)
		priority = Log.WARN;
	else if (ex instanceof RuntimeException)
		priority = Log.WARN;
	else if (ex instanceof SecurityException)
		priority = Log.WARN;
	else if (ex instanceof SocketTimeoutException)
		priority = Log.WARN;
	else if (ex instanceof SSLPeerUnverifiedException)
		priority = Log.WARN;
	else if (ex instanceof StackOverflowError)
		priority = Log.WARN;
	else if (ex instanceof TransactionTooLargeException)
		priority = Log.WARN;
	else if (ex instanceof UnknownHostException)
		priority = Log.WARN;
	else if (ex instanceof UnsatisfiedLinkError)
		priority = Log.WARN;
	else
		priority = Log.ERROR;

	boolean xprivacy = false;
	for (StackTraceElement frame : ex.getStackTrace())
		if (frame.getClassName() != null && frame.getClassName().startsWith("biz.bokhorst.xprivacy")) {
			xprivacy = true;
			break;
		}
	if (!xprivacy)
		priority = Log.WARN;

	log(hook, priority, ex.toString() + " uid=" + Process.myUid() + "\n" + Log.getStackTraceString(ex));
}
 
Example #30
Source File: CrawlerDownloader.java    From tom-crawler with Apache License 2.0 4 votes vote down vote up
@Override
public Page download(Request request, Task task) {
    if (task == null || task.getSite() == null) {
        throw new NullPointerException("task or site can not be null");
    }
    CloseableHttpResponse httpResponse = null;
    CloseableHttpClient httpClient = getHttpClient(task.getSite());
    Proxy proxy = proxyProvider != null ? proxyProvider.getProxy(task) : null;
    HttpClientRequestContext requestContext = httpUriRequestConverter.convert(request, task.getSite(), proxy);
    Page page = Page.fail();
    try {
        httpResponse = httpClient.execute(requestContext.getHttpUriRequest(), requestContext.getHttpClientContext());
        page = handleResponse(request, request.getCharset() != null ? request.getCharset() : task.getSite().getCharset(), httpResponse, task);
        onSuccess(request);
        logger.debug("downloading page success {}", request.getUrl());
    } catch (IOException e) {
        if (e instanceof ConnectionClosedException) {
            logger.error("Premature end of chunk coded message body: {}", request.getUrl());
        } else if (e instanceof SSLHandshakeException) {
            logger.error("Remote host closed connection during handshake: {}", request.getUrl());
        } else if (e instanceof SSLException) {
            logger.error("SSL peer shut down incorrectly:[HttpClient]  {}", request.getUrl());
        } else if (e instanceof SocketTimeoutException) {
            logger.error("download page time out:{}", request.getUrl());
        } else if (e instanceof NoHttpResponseException) {
            logger.error("failed to respond:{}", request.getUrl());
        } else if (e instanceof HttpHostConnectException) {
            logger.error("Connect to proxy timed out:{}", request.getUrl());
        } else if (e instanceof TruncatedChunkException) {
            logger.error("TruncatedChunkException:{}, msg:{}", request.getUrl(), e.getMessage());
        } else {
            logger.error("download page error:{} ", request.getUrl(), e);
        }
        onError(request);
    } finally {
        if (httpResponse != null) {
            //ensure the connection is released back to pool
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
        if (proxyProvider != null && proxy != null) {
            proxyProvider.returnProxy(proxy, page, task);
        }
    }
    return page;
}