use of org.apache.commons.httpclient.HttpClient in project hive by apache.
the class TestWebHCatE2e method doHttpCall.
/**
* Does a basic HTTP GET and returns Http Status code + response body
* Will add the dummy user query string
*/
private static MethodCallRetVal doHttpCall(String uri, HTTP_METHOD_TYPE type, Map<String, Object> data, NameValuePair[] params) throws IOException {
HttpClient client = new HttpClient();
HttpMethod method;
switch(type) {
case GET:
method = new GetMethod(uri);
break;
case DELETE:
method = new DeleteMethod(uri);
break;
case PUT:
method = new PutMethod(uri);
if (data == null) {
break;
}
String msgBody = JsonBuilder.mapToJson(data);
LOG.info("Msg Body: " + msgBody);
StringRequestEntity sre = new StringRequestEntity(msgBody, "application/json", charSet);
((PutMethod) method).setRequestEntity(sre);
break;
default:
throw new IllegalArgumentException("Unsupported method type: " + type);
}
if (params == null) {
method.setQueryString(new NameValuePair[] { new NameValuePair("user.name", username) });
} else {
NameValuePair[] newParams = new NameValuePair[params.length + 1];
System.arraycopy(params, 0, newParams, 1, params.length);
newParams[0] = new NameValuePair("user.name", username);
method.setQueryString(newParams);
}
String actualUri = "no URI";
try {
//should this be escaped string?
actualUri = method.getURI().toString();
LOG.debug(type + ": " + method.getURI().getEscapedURI());
int httpStatus = client.executeMethod(method);
LOG.debug("Http Status Code=" + httpStatus);
String resp = method.getResponseBodyAsString();
LOG.debug("response: " + resp);
return new MethodCallRetVal(httpStatus, resp, actualUri, method.getName());
} catch (IOException ex) {
LOG.error("doHttpCall() failed", ex);
} finally {
method.releaseConnection();
}
return new MethodCallRetVal(-1, "Http " + type + " failed; see log file for details", actualUri, method.getName());
}
use of org.apache.commons.httpclient.HttpClient in project zeppelin by apache.
the class AbstractTestRestApi method httpGet.
protected static GetMethod httpGet(String path, String user, String pwd) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url + path);
getMethod.addRequestHeader("Origin", url);
if (userAndPasswordAreNotBlank(user, pwd)) {
getMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
}
httpClient.executeMethod(getMethod);
LOG.info("{} - {}", getMethod.getStatusCode(), getMethod.getStatusText());
return getMethod;
}
use of org.apache.commons.httpclient.HttpClient in project zeppelin by apache.
the class AbstractTestRestApi method httpDelete.
protected static DeleteMethod httpDelete(String path, String user, String pwd) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
DeleteMethod deleteMethod = new DeleteMethod(url + path);
deleteMethod.addRequestHeader("Origin", url);
if (userAndPasswordAreNotBlank(user, pwd)) {
deleteMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
}
httpClient.executeMethod(deleteMethod);
LOG.info("{} - {}", deleteMethod.getStatusCode(), deleteMethod.getStatusText());
return deleteMethod;
}
use of org.apache.commons.httpclient.HttpClient in project zeppelin by apache.
the class DirAccessTest method testDirAccessOk.
@Test
public void testDirAccessOk() throws Exception {
synchronized (this) {
System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_SERVER_DEFAULT_DIR_ALLOWED.getVarName(), "true");
AbstractTestRestApi.startUp();
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(getUrlToTest() + "/app/");
httpClient.executeMethod(getMethod);
AbstractTestRestApi.shutDown();
assert getMethod.getStatusCode() == HttpStatus.SC_OK;
}
}
use of org.apache.commons.httpclient.HttpClient in project zeppelin by apache.
the class DirAccessTest method testDirAccessForbidden.
@Test
public void testDirAccessForbidden() throws Exception {
synchronized (this) {
System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_SERVER_DEFAULT_DIR_ALLOWED.getVarName(), "false");
AbstractTestRestApi.startUp();
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(getUrlToTest() + "/app/");
httpClient.executeMethod(getMethod);
AbstractTestRestApi.shutDown();
assert getMethod.getStatusCode() == HttpStatus.SC_FORBIDDEN;
}
}
Aggregations