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