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