Example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity.

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:com.cubeia.backoffice.operator.client.OperatorServiceClientHTTP.java

@Override
public void updateConfig(Long operatorId, Map<OperatorConfigParamDTO, String> config) {
    PutMethod method = new PutMethod(String.format(baseUrl + UPDATE_CONFIG, operatorId));
    prepareMethod(method);/*w  w  w  .jav a 2  s.c o m*/
    try {
        String data = serialize(config);
        method.setRequestEntity(new StringRequestEntity(data, "application/json", "UTF-8"));

        // Execute the method.
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return;
        }
        assertResponseCodeOK(method, statusCode);

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.cubeia.backoffice.users.client.UserServiceClientHTTP.java

@Override
public void updateUser(User user) {
    String resource = String.format(baseUrl + USER, user.getUserId());
    PutMethod method = new PutMethod(resource);
    try {//from w w w.  j a va  2  s.  com
        String data = serialize(user);
        method.setRequestEntity(new StringRequestEntity(data, "application/json", "UTF-8"));

        // Execute the HTTP Call
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return;
        }
        assertResponseCodeOK(method, statusCode);

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:de.mpg.escidoc.http.UserGroup.java

@SuppressWarnings("deprecation")
public Boolean createUserGroups() {
    String userGroupName = Util.input("Name of the UserGroup to create: ");
    String userGroupLabel = Util.input("Label of the UserGroup to create: ");
    Document responseXML = null;//from  w ww.j  a  va2 s  .  c  o  m
    if (this.USER_HANDLE != null) {
        PutMethod put = new PutMethod(this.FRAMEWORK_URL + "/aa/user-group");
        put.setRequestHeader("Cookie", "escidocCookie=" + this.USER_HANDLE);
        try {
            System.out.println("Request body sent to Server: ");
            put.setRequestEntity(new StringRequestEntity(Util.getCreateXml(userGroupName, userGroupLabel)));
            this.client.executeMethod(put);
            if (put.getStatusCode() != 200) {
                System.out.println("Server StatusCode: " + put.getStatusCode());
                return false;
            }
            System.out.println("Server response: ");
            responseXML = Util.inputStreamToXmlDocument(put.getResponseBodyAsStream());
            Util.xmlToString(responseXML);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("Error in creatUserGroup: No userHandle available");
    }
    return true;
}

From source file:com.cloud.network.nicira.NiciraNvpApi.java

private <T> void executeUpdateObject(T newObject, String uri, Map<String, String> parameters)
        throws NiciraNvpApiException {
    String url;//from   w  w  w  . jav a2  s .  co  m
    try {
        url = new URL(_protocol, _host, uri).toString();
    } catch (MalformedURLException e) {
        s_logger.error("Unable to build Nicira API URL", e);
        throw new NiciraNvpApiException("Connection to NVP Failed");
    }

    Gson gson = new Gson();

    PutMethod pm = new PutMethod(url);
    pm.setRequestHeader("Content-Type", "application/json");
    try {
        pm.setRequestEntity(new StringRequestEntity(gson.toJson(newObject), "application/json", null));
    } catch (UnsupportedEncodingException e) {
        throw new NiciraNvpApiException("Failed to encode json request body", e);
    }

    executeMethod(pm);

    if (pm.getStatusCode() != HttpStatus.SC_OK) {
        String errorMessage = responseToErrorMessage(pm);
        pm.releaseConnection();
        s_logger.error("Failed to update object : " + errorMessage);
        throw new NiciraNvpApiException("Failed to update object : " + errorMessage);
    }
    pm.releaseConnection();
}

From source file:com.intuit.tank.httpclient3.TankHttpClient3.java

@Override
public void doPut(BaseRequest request) {
    try {/* ww w.j  a v a 2 s .  co  m*/
        PutMethod httpput = new PutMethod(request.getRequestUrl());
        // Multiple calls can be expensive, so get it once
        String requestBody = request.getBody();
        StringRequestEntity entity = new StringRequestEntity(requestBody, request.getContentType(),
                request.getContentTypeCharSet());
        httpput.setRequestEntity(entity);
        sendRequest(request, httpput, requestBody);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cubeia.backoffice.wallet.client.WalletServiceClientHTTP.java

@Override
public void updateAccount(Account account) {
    String resource = String.format(baseUrl + ACCOUNT, account.getId());
    PutMethod method = new PutMethod(resource);
    try {//from   w  ww  . j  a  va 2  s  .  c o m
        String data = serialize(account);
        method.setRequestEntity(new StringRequestEntity(data, MIME_TYPE_JSON, DEFAULT_CHAR_ENCODING));
        // Execute the HTTP Call
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return;
        }
        assertResponseCodeOK(method, statusCode);

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.cubeia.backoffice.wallet.client.WalletServiceClientHTTP.java

@Override
public AccountQueryResult listAccounts(ListAccountsRequest request) {
    String uri = baseUrl + ACCOUNTS;
    PutMethod method = new PutMethod(uri);
    try {/*from   ww  w.  j  av  a  2s  .c o  m*/
        String data = serialize(request);
        method.setRequestEntity(new StringRequestEntity(data, MIME_TYPE_JSON, DEFAULT_CHAR_ENCODING));

        // Execute the HTTP Call
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return null;
        }
        if (statusCode == HttpStatus.SC_OK) {
            InputStream body = method.getResponseBodyAsStream();
            return parseJson(body, AccountQueryResult.class);

        } else {
            throw new RuntimeException(
                    "Failed to list accounts, RESPONSE CODE: " + statusCode + " url: " + uri);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed listing accounts via url " + uri, e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.cubeia.backoffice.wallet.client.WalletServiceClientHTTP.java

@Override
public EntriesQueryResult listEntries(ListEntriesRequest request) {
    String uri = baseUrl + ENTRIES;
    PutMethod method = new PutMethod(uri);
    try {//w w  w.  j  a  v  a  2s  .c om
        String data = serialize(request);
        method.setRequestEntity(new StringRequestEntity(data, MIME_TYPE_JSON, DEFAULT_CHAR_ENCODING));

        // Execute the HTTP Call
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return null;
        }
        if (statusCode == HttpStatus.SC_OK) {
            InputStream body = method.getResponseBodyAsStream();
            return parseJson(body, EntriesQueryResult.class);

        } else {
            throw new RuntimeException("Failed to list transactions, RESPONSE CODE: " + statusCode);
        }

    } catch (Exception e) {
        throw new RuntimeException("Failed listing entries via url " + uri, e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.cubeia.backoffice.wallet.client.WalletServiceClientHTTP.java

@Override
public void updateCurrency(Currency currency) {
    String resource = String.format(baseUrl + CURRENCIES, currency.getCode());
    PutMethod method = new PutMethod(resource);
    try {/*  w  w  w  . j av  a2s .  c  o m*/
        String data = serialize(currency);
        method.setRequestEntity(new StringRequestEntity(data, MIME_TYPE_JSON, DEFAULT_CHAR_ENCODING));

        // Execute the HTTP Call
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return;
        }
        assertResponseCodeOK(method, statusCode);

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.cubeia.backoffice.wallet.client.WalletServiceClientHTTP.java

@Override
public TransactionQueryResult listTransactions(ListTransactionsRequest request) {
    String uri = baseUrl + TRANSACTIONS;
    PutMethod method = new PutMethod(uri);
    try {//from   w ww.  ja va 2s  .  c om
        String data = serialize(request);
        method.setRequestEntity(new StringRequestEntity(data, MIME_TYPE_JSON, DEFAULT_CHAR_ENCODING));

        // Execute the HTTP Call
        int statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return null;
        }
        if (statusCode == HttpStatus.SC_OK) {
            InputStream body = method.getResponseBodyAsStream();
            return parseJson(body, TransactionQueryResult.class);

        } else {
            throw new RuntimeException(
                    "Failed to list transactions, RESPONSE CODE: " + statusCode + " url: " + uri);
        }

    } catch (Exception e) {
        throw new RuntimeException("Failed listing transactions via url " + uri, e);
    } finally {
        method.releaseConnection();
    }
}