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();
}
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;
}
}
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);
}
}
Aggregations