Search in sources :

Example 26 with Header

use of org.apache.commons.httpclient.Header 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 27 with Header

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

the class BigSwitchApiTest method testExecuteUpdateObjectOK.

@Test
public void testExecuteUpdateObjectOK() throws BigSwitchBcfApiException, IOException {
    NetworkData network = new NetworkData();
    _method = mock(PutMethod.class);
    when(_method.getResponseHeader("X-BSN-BVS-HASH-MATCH")).thenReturn(new Header("X-BSN-BVS-HASH-MATCH", UUID.randomUUID().toString()));
    when(_method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
    String hash = _api.executeUpdateObject(network, "/", Collections.<String, String>emptyMap());
    verify(_method, times(1)).releaseConnection();
    verify(_client, times(1)).executeMethod(_method);
    assertNotEquals(hash, "");
    assertNotEquals(hash, BigSwitchBcfApi.HASH_CONFLICT);
    assertNotEquals(hash, BigSwitchBcfApi.HASH_IGNORE);
}
Also used : Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Test(org.junit.Test)

Example 28 with Header

use of org.apache.commons.httpclient.Header in project maven-plugins by apache.

the class ClassicJiraDownloader method download.

/**
     * Downloads the given link using the configured HttpClient, possibly following redirects.
     *
     * @param cl the HttpClient
     * @param link the URL to JIRA
     */
private void download(final HttpClient cl, final String link) {
    InputStream in = null;
    OutputStream out = null;
    try {
        GetMethod gm = new GetMethod(link);
        getLog().info("Downloading from JIRA at: " + link);
        gm.setFollowRedirects(true);
        cl.executeMethod(gm);
        StatusLine sl = gm.getStatusLine();
        if (sl == null) {
            getLog().error("Unknown error validating link: " + link);
            return;
        }
        // if we get a redirect, do so
        if (gm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header locationHeader = gm.getResponseHeader("Location");
            if (locationHeader == null) {
                getLog().warn("Site sent redirect, but did not set Location header");
            } else {
                String newLink = locationHeader.getValue();
                getLog().debug("Following redirect to " + newLink);
                download(cl, newLink);
            }
        }
        if (gm.getStatusCode() == HttpStatus.SC_OK) {
            in = gm.getResponseBodyAsStream();
            if (!output.getParentFile().exists()) {
                output.getParentFile().mkdirs();
            }
            // write the response to file
            out = new FileOutputStream(output);
            IOUtil.copy(in, out);
            out.close();
            out = null;
            in.close();
            in = null;
            getLog().debug("Downloading from JIRA was successful");
        } else {
            getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]");
        }
    } catch (HttpException e) {
        if (getLog().isDebugEnabled()) {
            getLog().error("Error downloading issues from JIRA:", e);
        } else {
            getLog().error("Error downloading issues from JIRA url: " + e.getLocalizedMessage());
        }
    } catch (IOException e) {
        if (getLog().isDebugEnabled()) {
            getLog().error("Error downloading issues from JIRA:", e);
        } else {
            getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage());
        }
    } finally {
        IOUtil.close(out);
        IOUtil.close(in);
    }
}
Also used : StatusLine(org.apache.commons.httpclient.StatusLine) Header(org.apache.commons.httpclient.Header) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Example 29 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class HttpTestBase method getContent.

/** retrieve the contents of given URL and assert its content type
     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
     * @param httpMethod supports just GET and POST methods
     * @throws IOException
     * @throws HttpException */
public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException {
    HttpMethodBase method = null;
    if (HTTP_METHOD_GET.equals(httpMethod)) {
        method = new GetMethod(url);
    } else if (HTTP_METHOD_POST.equals(httpMethod)) {
        method = new PostMethod(url);
    } else {
        fail("Http Method not supported in this test suite, method: " + httpMethod);
    }
    if (params != null) {
        final NameValuePair[] nvp = new NameValuePair[0];
        method.setQueryString(params.toArray(nvp));
    }
    final int status = httpClient.executeMethod(method);
    final String content = getResponseBodyAsStream(method, 0);
    assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
    final Header h = method.getResponseHeader("Content-Type");
    if (expectedContentType == null) {
        if (h != null) {
            fail("Expected null Content-Type, got " + h.getValue());
        }
    } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
    // no check
    } else if (h == null) {
        fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
    } else {
        assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
    }
    return content.toString();
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 30 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class SelectorAuthenticationResponseCodeTest method assertPostStatus.

// TODO - move this method into commons.testing
protected HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams, List<Header> headers, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    if (headers != null) {
        for (Header header : headers) {
            post.addRequestHeader(header);
        }
    }
    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }
    final int status = H.getHttpClient().executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Aggregations

Header (org.apache.commons.httpclient.Header)88 GetMethod (org.apache.commons.httpclient.methods.GetMethod)22 Test (org.junit.Test)22 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)19 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 NameValuePair (org.apache.commons.httpclient.NameValuePair)16 ArrayList (java.util.ArrayList)15 InputStream (java.io.InputStream)13 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5