Search in sources :

Example 21 with HttpException

use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.

the class ElasticSearchIndex method deleteIndex.

@Override
public void deleteIndex() {
    HttpMethod method = new DeleteMethod(ElasticSearchConnector.actualUrl(indexUrl));
    try {
        ElasticSearchConnector connector = new ElasticSearchConnector();
        int statusCode = connector.executeMethod(method);
        if (statusCode == HttpStatus.SC_OK) {
            boolean ok = connector.getBooleanAtJsonPath(new String[] { "ok" }, false);
            boolean acknowledged = connector.getBooleanAtJsonPath(new String[] { "acknowledged" }, false);
            if (!ok || !acknowledged) {
                ZimbraLog.index.debug("Delete index status ok=%b acknowledged=%b", ok, acknowledged);
            }
        } else {
            String error = connector.getStringAtJsonPath(new String[] { "error" });
            if (error != null && error.startsWith("IndexMissingException")) {
                ZimbraLog.index.debug("Unable to delete index for key=%s.  Index is missing", key);
            } else {
                ZimbraLog.index.error("Problem deleting index for key=%s error=%s", key, error);
            }
        }
    } catch (HttpException e) {
        ZimbraLog.index.error("Problem Deleting index with key=" + key, e);
    } catch (IOException e) {
        ZimbraLog.index.error("Problem Deleting index with key=" + key, e);
    }
    haveMappingInfo = false;
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 22 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class ClusterServiceServletImpl method executePostMethod.

private String executePostMethod(final HttpClient client, final PostMethod method) {
    int response = 0;
    String result = null;
    try {
        final Profiler profiler = new Profiler();
        profiler.start();
        response = client.executeMethod(method);
        if (response == HttpStatus.SC_OK) {
            result = method.getResponseBodyAsString();
            profiler.stop();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("POST " + _serviceUrl + " response :" + result + ", responding time: " + profiler.getDurationInMillis() + " ms");
            }
        } else {
            profiler.stop();
            s_logger.error("Invalid response code : " + response + ", from : " + _serviceUrl + ", method : " + method.getParameter("method") + " responding time: " + profiler.getDurationInMillis());
        }
    } catch (final HttpException e) {
        s_logger.error("HttpException from : " + _serviceUrl + ", method : " + method.getParameter("method"));
    } catch (final IOException e) {
        s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method"));
    } catch (final Throwable e) {
        s_logger.error("Exception from : " + _serviceUrl + ", method : " + method.getParameter("method") + ", exception :", e);
    } finally {
        method.releaseConnection();
    }
    return result;
}
Also used : Profiler(com.cloud.utils.Profiler) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Example 23 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class BigSwitchApiTest method testExecuteDeleteObjectException.

@Test(expected = BigSwitchBcfApiException.class)
public void testExecuteDeleteObjectException() throws BigSwitchBcfApiException, IOException {
    _method = mock(DeleteMethod.class);
    when(_method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
    when(_client.executeMethod((HttpMethod) any())).thenThrow(new HttpException());
    try {
        _api.executeDeleteObject("/");
    } finally {
        verify(_method, times(1)).releaseConnection();
    }
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HttpException(org.apache.commons.httpclient.HttpException) Test(org.junit.Test)

Example 24 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class BigSwitchApiTest method testExecuteCreateObjectException.

@Test(expected = BigSwitchBcfApiException.class)
public void testExecuteCreateObjectException() throws BigSwitchBcfApiException, IOException {
    NetworkData network = new NetworkData();
    when(_client.executeMethod((HttpMethod) any())).thenThrow(new HttpException());
    _method = mock(PostMethod.class);
    when(_method.getStatusCode()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    Header header = mock(Header.class);
    when(header.getValue()).thenReturn("text/html");
    when(_method.getResponseHeader("Content-type")).thenReturn(header);
    when(_method.getResponseBodyAsString()).thenReturn("Off to timbuktu, won't be back later.");
    try {
        _api.executeCreateObject(network, "/", Collections.<String, String>emptyMap());
    } finally {
        verify(_method, times(1)).releaseConnection();
    }
}
Also used : Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpException(org.apache.commons.httpclient.HttpException) Test(org.junit.Test)

Example 25 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class UriUtils method checkUrlExistence.

// use http HEAD method to validate url
public static void checkUrlExistence(String url) {
    if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) {
        HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        HeadMethod httphead = new HeadMethod(url);
        try {
            if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) {
                throw new IllegalArgumentException("Invalid URL: " + url);
            }
        } catch (HttpException hte) {
            throw new IllegalArgumentException("Cannot reach URL: " + url);
        } catch (IOException ioe) {
            throw new IllegalArgumentException("Cannot reach URL: " + url);
        }
    }
}
Also used : HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Aggregations

HttpException (org.apache.commons.httpclient.HttpException)61 IOException (java.io.IOException)55 HttpClient (org.apache.commons.httpclient.HttpClient)34 GetMethod (org.apache.commons.httpclient.methods.GetMethod)31 HttpMethod (org.apache.commons.httpclient.HttpMethod)22 InputStream (java.io.InputStream)15 Header (org.apache.commons.httpclient.Header)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)12 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)9 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)8 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)6 Test (org.junit.Test)5 ServiceException (com.zimbra.common.service.ServiceException)4 Server (com.zimbra.cs.account.Server)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 XStream (com.thoughtworks.xstream.XStream)3