use of org.apache.commons.httpclient.methods.RequestEntity in project zeppelin by apache.
the class AbstractTestRestApi method httpPut.
protected static PutMethod httpPut(String path, String body, String user, String pwd) throws IOException {
LOG.info("Connecting to {}", url + path);
HttpClient httpClient = new HttpClient();
PutMethod putMethod = new PutMethod(url + path);
putMethod.addRequestHeader("Origin", url);
RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
putMethod.setRequestEntity(entity);
if (userAndPasswordAreNotBlank(user, pwd)) {
putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
}
httpClient.executeMethod(putMethod);
LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText());
return putMethod;
}
use of org.apache.commons.httpclient.methods.RequestEntity in project pinot by linkedin.
the class WebHdfsV1Client method uploadSegment.
// This method is based on:
// https://hadoop.apache.org/docs/r1.0.4/webhdfs.html#CREATE
public synchronized boolean uploadSegment(String webHdfsPath, String localFilePath) {
// Step 1: Submit a HTTP PUT request without automatically following
// redirects and without sending the file data.
String firstPutReqString = String.format(WEB_HDFS_UPLOAD_PATH_TEMPLATE, _protocol, _host, _port, webHdfsPath, _overwrite, _permission);
HttpMethod firstPutReq = new PutMethod(firstPutReqString);
try {
LOGGER.info("Trying to send request: {}.", firstPutReqString);
int firstResponseCode = _httpClient.executeMethod(firstPutReq);
if (firstResponseCode != 307) {
LOGGER.error(String.format("Failed to execute the first PUT request to upload segment to webhdfs: %s. " + "Expected response code 307, but get %s. Response body: %s", firstPutReqString, firstResponseCode, firstPutReq.getResponseBodyAsString()));
return false;
}
} catch (Exception e) {
LOGGER.error(String.format("Failed to execute the first request to upload segment to webhdfs: %s.", firstPutReqString), e);
return false;
} finally {
firstPutReq.releaseConnection();
}
// Step 2: Submit another HTTP PUT request using the URL in the Location
// header with the file data to be written.
String redirectedReqString = firstPutReq.getResponseHeader(LOCATION).getValue();
PutMethod redirectedReq = new PutMethod(redirectedReqString);
File localFile = new File(localFilePath);
RequestEntity requestEntity = new FileRequestEntity(localFile, "application/binary");
redirectedReq.setRequestEntity(requestEntity);
try {
LOGGER.info("Trying to send request: {}.", redirectedReqString);
int redirectedResponseCode = _httpClient.executeMethod(redirectedReq);
if (redirectedResponseCode != 201) {
LOGGER.error(String.format("Failed to execute the redirected PUT request to upload segment to webhdfs: %s. " + "Expected response code 201, but get %s. Response: %s", redirectedReqString, redirectedResponseCode, redirectedReq.getResponseBodyAsString()));
}
return true;
} catch (IOException e) {
LOGGER.error(String.format("Failed to execute the redirected request to upload segment to webhdfs: %s.", redirectedReqString), e);
return false;
} finally {
redirectedReq.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.RequestEntity in project openhab1-addons by openhab.
the class CcuClient method sendScript.
/**
* Main method for sending a TclRega script and parsing the XML result.
*/
@SuppressWarnings("unchecked")
private synchronized <T> T sendScript(String script, Class<T> clazz) throws HomematicClientException {
PostMethod post = null;
try {
script = StringUtils.trim(script);
if (StringUtils.isEmpty(script)) {
throw new RuntimeException("Homematic TclRegaScript is empty!");
}
if (TRACE_ENABLED) {
logger.trace("TclRegaScript: {}", script);
}
post = new PostMethod(context.getConfig().getTclRegaUrl());
RequestEntity re = new ByteArrayRequestEntity(script.getBytes("ISO-8859-1"));
post.setRequestEntity(re);
httpClient.executeMethod(post);
String result = post.getResponseBodyAsString();
result = StringUtils.substringBeforeLast(result, "<xml><exec>");
if (TRACE_ENABLED) {
logger.trace("Result TclRegaScript: {}", result);
}
Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
um.setListener(new CommonUnmarshallerListener());
return (T) um.unmarshal(new StringReader(result));
} catch (Exception ex) {
throw new HomematicClientException(ex.getMessage(), ex);
} finally {
if (post != null) {
post.releaseConnection();
}
}
}
use of org.apache.commons.httpclient.methods.RequestEntity in project zm-mailbox by Zimbra.
the class DavRequest method getHttpMethod.
public HttpMethod getHttpMethod(String baseUrl) {
String url = mRedirectUrl;
if (url == null) {
// schedule-outbox-URL in the same report.
try {
// This will throw an exception when the URL is a relative one.
new URL(mUri);
url = mUri;
} catch (MalformedURLException e) {
url = baseUrl + mUri;
}
}
if (mDoc == null)
return new GetMethod(url) {
@Override
public String getName() {
return mMethod;
}
};
PutMethod m = new PutMethod(url) {
RequestEntity re;
@Override
public String getName() {
return mMethod;
}
@Override
protected RequestEntity generateRequestEntity() {
return re;
}
@Override
public void setRequestEntity(RequestEntity requestEntity) {
re = requestEntity;
super.setRequestEntity(requestEntity);
}
};
DocumentRequestEntity re = new DocumentRequestEntity(mDoc);
m.setRequestEntity(re);
return m;
}
use of org.apache.commons.httpclient.methods.RequestEntity in project zm-mailbox by Zimbra.
the class DavMethod method toHttpMethod.
public HttpMethod toHttpMethod(DavContext ctxt, String targetUrl) throws IOException, DavException {
if (ctxt.getUpload() != null && ctxt.getUpload().getSize() > 0) {
PostMethod method = new PostMethod(targetUrl) {
@Override
public String getName() {
return getMethodName();
}
};
RequestEntity reqEntry;
if (ctxt.hasRequestMessage()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(baos);
writer.write(ctxt.getRequestMessage());
reqEntry = new ByteArrayRequestEntity(baos.toByteArray());
} else {
// this could be a huge upload
reqEntry = new InputStreamRequestEntity(ctxt.getUpload().getInputStream(), ctxt.getUpload().getSize());
}
method.setRequestEntity(reqEntry);
return method;
}
return new GetMethod(targetUrl) {
@Override
public String getName() {
return getMethodName();
}
};
}
Aggregations