Search in sources :

Example 46 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project cloudstack by apache.

the class NeutronPortAdapterTest method gsonNeutronPortMarshalingTest.

@Test
public void gsonNeutronPortMarshalingTest() throws NeutronRestApiException {
    NeutronPort port = new NeutronPort();
    port.setId(UUID.fromString("ca31aa7f-84c7-416d-bc00-1f84927367e0"));
    port.setName("test_gre");
    port.setAdminStateUp(true);
    port.setDeviceId(UUID.fromString("ca31aa7f-84c7-416d-bc00-1f84927367e0"));
    port.setMacAddress("ca31aa7f-84c7-416d-bc00-1f84927367e0");
    port.setNetworkId(UUID.fromString("ca31aa7f-84c7-416d-bc00-1f84927367e0"));
    port.setStatus("ACTIVE");
    port.setTenantId("wilder");
    NeutronPortWrapper portWrapper = new NeutronPortWrapper();
    portWrapper.setPort(port);
    StringRequestEntity entity;
    try {
        entity = new StringRequestEntity(gsonNeutronPort.toJson(portWrapper), "application/json", null);
        String actual = entity.getContent();
        Assert.assertEquals(jsonString, actual);
    } catch (UnsupportedEncodingException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) NeutronPort(org.apache.cloudstack.network.opendaylight.api.model.NeutronPort) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronPortWrapper(org.apache.cloudstack.network.opendaylight.api.model.NeutronPortWrapper) Test(org.junit.Test)

Example 47 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project cloudstack by apache.

the class NeutronNetworksNorthboundAction method updateNeutronNetwork.

public <T> void updateNeutronNetwork(final String networkId, final NeutronNetworkWrapper newNetworkWrapper) throws NeutronRestApiException {
    try {
        String uri = NeutronNorthboundEnum.NETWORK_PARAM_URI.getUri();
        uri = MessageFormat.format(uri, networkId);
        StringRequestEntity entity = new StringRequestEntity(gsonNeutronNetwork.toJson(newNetworkWrapper), JSON_CONTENT_TYPE, null);
        executePut(uri, entity);
    } catch (UnsupportedEncodingException e) {
        throw new NeutronRestApiException("Failed to encode json request body", e);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 48 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project cloudstack by apache.

the class NeutronPortsNorthboundAction method updateNeutronPort.

public <T> void updateNeutronPort(final String portId, final NeutronPortWrapper newPortWrapper) throws NeutronRestApiException {
    try {
        String uri = NeutronNorthboundEnum.PORTS_PARAM_URI.getUri();
        uri = MessageFormat.format(uri, portId);
        StringRequestEntity entity = new StringRequestEntity(gsonNeutronPort.toJson(newPortWrapper), JSON_CONTENT_TYPE, null);
        executePut(uri, entity);
    } catch (UnsupportedEncodingException e) {
        throw new NeutronRestApiException("Failed to encode json request body", e);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 49 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project cloudstack by apache.

the class NeutronPortsNorthboundAction method createNeutronPort.

@SuppressWarnings("unchecked")
public <T> T createNeutronPort(final NeutronPortWrapper newPortWrapper) throws NeutronRestApiException {
    try {
        String uri = NeutronNorthboundEnum.PORTS_URI.getUri();
        StringRequestEntity entity = new StringRequestEntity(gsonNeutronPort.toJson(newPortWrapper), JSON_CONTENT_TYPE, null);
        String bodystring = executePost(uri, entity);
        T result = (T) gsonNeutronPort.fromJson(bodystring, TypeToken.get(NeutronPortWrapper.class).getType());
        return result;
    } catch (UnsupportedEncodingException e) {
        throw new NeutronRestApiException("Failed to encode json request body", e);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) NeutronPortWrapper(org.apache.cloudstack.network.opendaylight.api.model.NeutronPortWrapper)

Example 50 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity 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)

Aggregations

StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)102 PostMethod (org.apache.commons.httpclient.methods.PostMethod)63 HttpClient (org.apache.commons.httpclient.HttpClient)33 Test (org.junit.Test)23 PutMethod (org.apache.commons.httpclient.methods.PutMethod)19 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)18 IOException (java.io.IOException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)12 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)12 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)12 Header (org.apache.commons.httpclient.Header)9 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9 GetMethod (org.apache.commons.httpclient.methods.GetMethod)9 InputStream (java.io.InputStream)8 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 StringWriter (java.io.StringWriter)6 Map (java.util.Map)6 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)6 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)6