use of org.apache.commons.httpclient.methods.PutMethod in project cloudstack by apache.
the class Action method executePut.
protected String executePut(final String uri) throws NeutronRestApiException {
try {
validateCredentials();
} catch (NeutronInvalidCredentialsException e) {
throw new NeutronRestApiException("Invalid credentials!", e);
}
NeutronRestFactory factory = NeutronRestFactory.getInstance();
NeutronRestApi neutronRestApi = factory.getNeutronApi(PutMethod.class);
PutMethod putMethod = (PutMethod) neutronRestApi.createMethod(url, uri);
try {
String encodedCredentials = encodeCredentials();
putMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
neutronRestApi.executeMethod(putMethod);
if (putMethod.getStatusCode() != HttpStatus.SC_OK) {
String errorMessage = responseToErrorMessage(putMethod);
putMethod.releaseConnection();
s_logger.error("Failed to update object : " + errorMessage);
throw new NeutronRestApiException("Failed to create object : " + errorMessage);
}
return putMethod.getResponseBodyAsString();
} catch (NeutronRestApiException e) {
s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
} catch (IOException e) {
throw new NeutronRestApiException("Failed to load json response body", e);
} finally {
putMethod.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.PutMethod in project cloudstack by apache.
the class Action method executePut.
protected void executePut(final String uri, final StringRequestEntity entity) throws NeutronRestApiException {
try {
validateCredentials();
} catch (NeutronInvalidCredentialsException e) {
throw new NeutronRestApiException("Invalid credentials!", e);
}
NeutronRestFactory factory = NeutronRestFactory.getInstance();
NeutronRestApi neutronRestApi = factory.getNeutronApi(PutMethod.class);
PutMethod putMethod = (PutMethod) neutronRestApi.createMethod(url, uri);
try {
putMethod.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE);
putMethod.setRequestEntity(entity);
String encodedCredentials = encodeCredentials();
putMethod.setRequestHeader("Authorization", "Basic " + encodedCredentials);
neutronRestApi.executeMethod(putMethod);
if (putMethod.getStatusCode() != HttpStatus.SC_OK) {
String errorMessage = responseToErrorMessage(putMethod);
putMethod.releaseConnection();
s_logger.error("Failed to update object : " + errorMessage);
throw new NeutronRestApiException("Failed to create object : " + errorMessage);
}
} catch (NeutronRestApiException e) {
s_logger.error("NeutronRestApiException caught while trying to execute HTTP Method on the Neutron Controller", e);
throw new NeutronRestApiException("API call to Neutron Controller Failed", e);
} finally {
putMethod.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.PutMethod in project cloudstack by apache.
the class BigSwitchBcfApi method executeUpdateObject.
protected <T> String executeUpdateObject(final T newObject, final String uri, final Map<String, String> parameters) throws BigSwitchBcfApiException, IllegalArgumentException {
checkInvariants();
PutMethod pm = (PutMethod) createMethod("put", uri, _port);
setHttpHeader(pm);
try {
pm.setRequestEntity(new StringRequestEntity(gson.toJson(newObject), CONTENT_JSON, null));
} catch (UnsupportedEncodingException e) {
throw new BigSwitchBcfApiException("Failed to encode json request body", e);
}
executeMethod(pm);
String hash = checkResponse(pm, "BigSwitch HTTP update failed: ");
pm.releaseConnection();
return hash;
}
use of org.apache.commons.httpclient.methods.PutMethod in project sling by apache.
the class PutMethodServletTest method testPutMethodServletDefaultRT.
public void testPutMethodServletDefaultRT() throws Exception {
final PutMethod put = new PutMethod(testNodeNORT.nodeUrl);
final int status = httpClient.executeMethod(put);
assertFalse("PUT to testNodeRT should not return 200", 200 == status);
}
use of org.apache.commons.httpclient.methods.PutMethod in project zeppelin by apache.
the class ZeppelinHubRealm method authenticateUser.
/**
* Send to ZeppelinHub a login request based on the request body which is a JSON that contains 2
* fields "login" and "password".
*
* @param requestBody JSON string of ZeppelinHub payload.
* @return Account object with login, name (if set in ZeppelinHub), and mail.
* @throws AuthenticationException if fail to login.
*/
protected User authenticateUser(String requestBody) {
PutMethod put = new PutMethod(Joiner.on("/").join(zeppelinhubUrl, USER_LOGIN_API_ENDPOINT));
String responseBody = StringUtils.EMPTY;
String userSession = StringUtils.EMPTY;
try {
put.setRequestEntity(new StringRequestEntity(requestBody, JSON_CONTENT_TYPE, UTF_8_ENCODING));
int statusCode = httpClient.executeMethod(put);
if (statusCode != HttpStatus.SC_OK) {
LOG.error("Cannot login user, HTTP status code is {} instead on 200 (OK)", statusCode);
put.releaseConnection();
throw new AuthenticationException("Couldnt login to ZeppelinHub. " + "Login or password incorrect");
}
responseBody = put.getResponseBodyAsString();
userSession = put.getResponseHeader(USER_SESSION_HEADER).getValue();
put.releaseConnection();
} catch (IOException e) {
LOG.error("Cannot login user", e);
throw new AuthenticationException(e.getMessage());
}
User account = null;
try {
account = gson.fromJson(responseBody, User.class);
} catch (JsonParseException e) {
LOG.error("Cannot deserialize ZeppelinHub response to User instance", e);
throw new AuthenticationException("Cannot login to ZeppelinHub");
}
// Add ZeppelinHub user_session token this singleton map, this will help ZeppelinHubRepo
// to get specific information about the current user.
UserSessionContainer.instance.setSession(account.login, userSession);
/* TODO(khalid): add proper roles and add listener */
HashSet<String> userAndRoles = new HashSet<String>();
userAndRoles.add(account.login);
ZeppelinServer.notebookWsServer.broadcastReloadedNoteList(new org.apache.zeppelin.user.AuthenticationInfo(account.login), userAndRoles);
return account;
}
Aggregations