Search in sources :

Example 16 with HttpClient

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());
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) IOException(java.io.IOException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 17 with HttpClient

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;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 18 with HttpClient

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;
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 19 with HttpClient

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;
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Test(org.junit.Test)

Example 20 with HttpClient

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;
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Test(org.junit.Test)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)389 GetMethod (org.apache.commons.httpclient.methods.GetMethod)217 PostMethod (org.apache.commons.httpclient.methods.PostMethod)115 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)86 IOException (java.io.IOException)79 InputStream (java.io.InputStream)65 JSONParser (org.json.simple.parser.JSONParser)44 HttpException (org.apache.commons.httpclient.HttpException)43 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)32 ArrayList (java.util.ArrayList)27 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 HashMap (java.util.HashMap)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20 Element (org.w3c.dom.Element)20