Search in sources :

Example 16 with PutMethod

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();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) PutMethod(org.apache.commons.httpclient.methods.PutMethod) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException) IOException(java.io.IOException)

Example 17 with PutMethod

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();
    }
}
Also used : NeutronRestFactory(org.apache.cloudstack.network.opendaylight.api.NeutronRestFactory) NeutronRestApi(org.apache.cloudstack.network.opendaylight.api.NeutronRestApi) NeutronInvalidCredentialsException(org.apache.cloudstack.network.opendaylight.api.NeutronInvalidCredentialsException) PutMethod(org.apache.commons.httpclient.methods.PutMethod) NeutronRestApiException(org.apache.cloudstack.network.opendaylight.api.NeutronRestApiException)

Example 18 with PutMethod

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;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 19 with PutMethod

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

Example 20 with PutMethod

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;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) AuthenticationException(org.apache.shiro.authc.AuthenticationException) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HashSet(java.util.HashSet)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)37 Test (org.junit.Test)13 HttpClient (org.apache.commons.httpclient.HttpClient)12 Header (org.apache.commons.httpclient.Header)8 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)8 GetMethod (org.apache.commons.httpclient.methods.GetMethod)8 IOException (java.io.IOException)7 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 Map (java.util.Map)5 Note (org.apache.zeppelin.notebook.Note)5 Pair (com.zimbra.common.util.Pair)4 Account (com.zimbra.cs.account.Account)4 HttpException (org.apache.commons.httpclient.HttpException)4 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)4 PostMethod (org.apache.commons.httpclient.methods.PostMethod)4 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)4 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)3 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)3 Paragraph (org.apache.zeppelin.notebook.Paragraph)3 TypeToken (com.google.gson.reflect.TypeToken)2