Search in sources :

Example 1 with HttpMethodBase

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

use of org.apache.commons.httpclient.HttpMethodBase in project pinot by linkedin.

the class ServerSegmentCompletionProtocolHandler method doHttp.

private SegmentCompletionProtocol.Response doHttp(SegmentCompletionProtocol.Request request, Part[] parts) {
    SegmentCompletionProtocol.Response response = SegmentCompletionProtocol.RESP_NOT_SENT;
    HttpClient httpClient = new HttpClient();
    ControllerLeaderLocator leaderLocator = ControllerLeaderLocator.getInstance();
    final String leaderAddress = leaderLocator.getControllerLeader();
    if (leaderAddress == null) {
        LOGGER.error("No leader found {}", this.toString());
        return SegmentCompletionProtocol.RESP_NOT_LEADER;
    }
    final String url = request.getUrl(leaderAddress);
    HttpMethodBase method;
    if (parts != null) {
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams()));
        method = postMethod;
    } else {
        method = new GetMethod(url);
    }
    LOGGER.info("Sending request {} for {}", url, this.toString());
    try {
        int responseCode = httpClient.executeMethod(method);
        if (responseCode >= 300) {
            LOGGER.error("Bad controller response code {} for {}", responseCode, this.toString());
            return response;
        } else {
            response = new SegmentCompletionProtocol.Response(method.getResponseBodyAsString());
            LOGGER.info("Controller response {} for {}", response.toJsonString(), this.toString());
            if (response.getStatus().equals(SegmentCompletionProtocol.ControllerResponseStatus.NOT_LEADER)) {
                leaderLocator.refreshControllerLeader();
            }
            return response;
        }
    } catch (IOException e) {
        LOGGER.error("IOException {}", this.toString(), e);
        leaderLocator.refreshControllerLeader();
        return response;
    }
}
Also used : SegmentCompletionProtocol(com.linkedin.pinot.common.protocols.SegmentCompletionProtocol) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)

Example 3 with HttpMethodBase

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

the class NeutronRestApi method createMethod.

public HttpMethodBase createMethod(final URL neutronUrl, final String uri) throws NeutronRestApiException {
    String url;
    try {
        String formattedUrl = neutronUrl.toString() + uri;
        url = new URL(formattedUrl).toString();
        Constructor<? extends HttpMethodBase> httpMethodConstructor = httpClazz.getConstructor(String.class);
        HttpMethodBase httpMethod = httpMethodConstructor.newInstance(url);
        return httpMethod;
    } catch (MalformedURLException e) {
        String error = "Unable to build Neutron API URL";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    } catch (NoSuchMethodException e) {
        String error = "Unable to build Neutron API URL due to reflection error";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    } catch (SecurityException e) {
        String error = "Unable to build Neutron API URL due to security violation";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    } catch (InstantiationException e) {
        String error = "Unable to build Neutron API due to instantiation error";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    } catch (IllegalAccessException e) {
        String error = "Unable to build Neutron API URL due to absence of access modifier";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    } catch (IllegalArgumentException e) {
        String error = "Unable to build Neutron API URL due to wrong argument in constructor";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    } catch (InvocationTargetException e) {
        String error = "Unable to build Neutron API URL due to target error";
        s_logger.error(error, e);
        throw new NeutronRestApiException(error, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

HttpMethodBase (org.apache.commons.httpclient.HttpMethodBase)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 SegmentCompletionProtocol (com.linkedin.pinot.common.protocols.SegmentCompletionProtocol)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Header (org.apache.commons.httpclient.Header)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 NameValuePair (org.apache.commons.httpclient.NameValuePair)1 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)1 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)1