Search in sources :

Example 66 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project rhsm-qe by RedHatQE.

the class CandlepinTasks method doHTTPRequest.

protected static HttpMethod doHTTPRequest(HttpClient client, HttpMethod method, String username, String password) throws Exception {
    String server = method.getURI().getHost();
    int port = method.getURI().getPort();
    setCredentials(client, server, port, username, password);
    log.finer("Running HTTP request: " + method.getName() + " on " + method.getURI() + " with credentials for '" + username + "' on server '" + server + "'...");
    if (method instanceof PostMethod) {
        RequestEntity entity = ((PostMethod) method).getRequestEntity();
        log.finer("HTTP Request entity: " + (entity == null ? "" : ((StringRequestEntity) entity).getContent()));
    }
    log.finer("HTTP Request Headers: " + interpose(", ", (Object[]) method.getRequestHeaders()));
    // TODO: Need to decide if a bugzilla should be opened against stage for these exceptions
    try {
        int responseCode = client.executeMethod(method);
        log.finer("HTTP server returned: " + responseCode);
    } catch (java.net.SocketException e) {
        if (e.getMessage().trim().equals("Connection reset")) {
            // try again after 5 seconds
            log.warning("Encountered a '" + e.getMessage().trim() + "' SocketException while attempting an HTTP request.  Re-attempting one more time...");
            Thread.sleep(5000);
            int responseCode = client.executeMethod(method);
            log.finer("HTTP server returned: " + responseCode);
        } else
            throw (e);
    }
    return method;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) JSONObject(org.json.JSONObject) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity)

Example 67 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project rhsm-qe by RedHatQE.

the class CandlepinTasks method putResourceUsingRESTfulAPI.

public static String putResourceUsingRESTfulAPI(String authenticator, String password, String url, String path, JSONObject jsonData) throws Exception {
    PutMethod put = new PutMethod(url + path);
    if (jsonData != null) {
        put.setRequestEntity(new StringRequestEntity(jsonData.toString(), "application/json", null));
        put.addRequestHeader("accept", "application/json");
        put.addRequestHeader("content-type", "application/json");
    }
    // log the curl alternative to HTTP request
    // Example: curl --insecure --user testuser1:password --request PUT --data '{"autoheal":"false"}' --header 'accept: application/json' --header 'content-type: application/json' https://jsefler-onprem-62candlepin.usersys.redhat.com:8443/candlepin/consumers/e60d7786-1f61-4dec-ad19-bde068dd3c19
    String user = (authenticator.equals("")) ? "" : "--user " + authenticator + ":" + password + " ";
    String request = "--request " + put.getName() + " ";
    String data = (jsonData == null) ? "" : "--data '" + jsonData + "' ";
    String headers = "";
    if (jsonData != null)
        for (org.apache.commons.httpclient.Header header : put.getRequestHeaders()) headers += "--header '" + header.toString().trim() + "' ";
    log.info("SSH alternative to HTTP request: curl --stderr /dev/null --insecure " + user + request + data + headers + put.getURI());
    String response = getHTTPResponseAsString(client, put, authenticator, password);
    if (response != null) {
        JSONObject responseAsJSONObect = new JSONObject(response);
        if (responseAsJSONObect.has("displayMessage")) {
            log.warning("Attempt to PUT resource '" + path + "' failed: " + responseAsJSONObect.getString("displayMessage"));
        }
    }
    return response;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) JSONObject(org.json.JSONObject) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 68 with StringRequestEntity

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

the class HttpSendReceiveClientStep method buildMethod.

HttpMethodBase buildMethod(boolean rollback, WorkflowContext ctxt) throws Exception {
    String methodName = params.getOrDefault("method" + (rollback ? "-rollback" : ""), "GET").trim().toUpperCase();
    HttpMethodBase m = null;
    switch(methodName) {
        case "GET":
            m = new GetMethod();
            m.setFollowRedirects(true);
            break;
        case "POST":
            m = new PostMethod();
            break;
        case "PUT":
            m = new PutMethod();
            break;
        case "DELETE":
            m = new DeleteMethod();
            m.setFollowRedirects(true);
            break;
        default:
            throw new IllegalStateException("Unsupported HTTP method: '" + methodName + "'");
    }
    Map<String, String> templateParams = new HashMap<>();
    templateParams.put("invocationId", ctxt.getInvocationId());
    templateParams.put("dataset.id", Long.toString(ctxt.getDataset().getId()));
    templateParams.put("dataset.identifier", ctxt.getDataset().getIdentifier());
    templateParams.put("dataset.globalId", ctxt.getDataset().getGlobalId());
    templateParams.put("dataset.displayName", ctxt.getDataset().getDisplayName());
    templateParams.put("dataset.citation", ctxt.getDataset().getCitation());
    templateParams.put("minorVersion", Long.toString(ctxt.getNextMinorVersionNumber()));
    templateParams.put("majorVersion", Long.toString(ctxt.getNextVersionNumber()));
    templateParams.put("releaseStatus", (ctxt.getType() == TriggerType.PostPublishDataset) ? "done" : "in-progress");
    m.addRequestHeader("Content-Type", params.getOrDefault("contentType", "text/plain"));
    String urlKey = rollback ? "rollbackUrl" : "url";
    String url = params.get(urlKey);
    try {
        m.setURI(new URI(process(url, templateParams), true));
    } catch (URIException ex) {
        throw new IllegalStateException("Illegal URL: '" + url + "'");
    }
    String bodyKey = (rollback ? "rollbackBody" : "body");
    if (params.containsKey(bodyKey) && m instanceof EntityEnclosingMethod) {
        String body = params.get(bodyKey);
        ((EntityEnclosingMethod) m).setRequestEntity(new StringRequestEntity(process(body, templateParams)));
    }
    return m;
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HashMap(java.util.HashMap) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) URI(org.apache.commons.httpclient.URI) URIException(org.apache.commons.httpclient.URIException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 69 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project xwiki-platform by xwiki.

the class AbstractHttpTest method executePut.

protected PutMethod executePut(String uri, String string, String mediaType) throws Exception {
    HttpClient httpClient = new HttpClient();
    PutMethod putMethod = new PutMethod(uri);
    RequestEntity entity = new StringRequestEntity(string, mediaType, "UTF-8");
    putMethod.setRequestEntity(entity);
    httpClient.executeMethod(putMethod);
    return putMethod;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity)

Example 70 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project xwiki-platform by xwiki.

the class AbstractHttpTest method executePost.

protected PostMethod executePost(String uri, String string, String mediaType, String userName, String password) throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
    httpClient.getParams().setAuthenticationPreemptive(true);
    PostMethod postMethod = new PostMethod(uri);
    postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
    RequestEntity entity = new StringRequestEntity(string, mediaType, "UTF-8");
    postMethod.setRequestEntity(entity);
    httpClient.executeMethod(postMethod);
    return postMethod;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

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