use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity 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.ByteArrayRequestEntity 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.ByteArrayRequestEntity in project zaproxy by zaproxy.
the class HttpMethodHelper method createRequestMethodNew.
// Not used - all abstract using Generic method but GET cannot be used.
public HttpMethod createRequestMethodNew(HttpRequestHeader header, HttpBody body) throws URIException {
HttpMethod httpMethod = null;
String method = header.getMethod();
URI uri = header.getURI();
String version = header.getVersion();
httpMethod = new GenericMethod(method);
httpMethod.setURI(uri);
HttpMethodParams httpParams = httpMethod.getParams();
// default to use HTTP 1.0
httpParams.setVersion(HttpVersion.HTTP_1_0);
if (version.equalsIgnoreCase(HttpHeader.HTTP11)) {
httpParams.setVersion(HttpVersion.HTTP_1_1);
}
// set various headers
int pos = 0;
// ZAP: FindBugs fix - always initialise pattern
Pattern pattern = patternCRLF;
String delimiter = CRLF;
String msg = header.getHeadersAsString();
if ((pos = msg.indexOf(CRLF)) < 0) {
if ((pos = msg.indexOf(LF)) < 0) {
delimiter = LF;
pattern = patternLF;
}
} else {
delimiter = CRLF;
pattern = patternCRLF;
}
String[] split = pattern.split(msg);
String token = null;
String name = null;
String value = null;
for (int i = 0; i < split.length; i++) {
token = split[i];
if (token.equals("")) {
continue;
}
if ((pos = token.indexOf(":")) < 0) {
return null;
}
name = token.substring(0, pos).trim();
value = token.substring(pos + 1).trim();
httpMethod.addRequestHeader(name, value);
}
// set body if post method or put method
if (body != null && body.length() > 0) {
EntityEnclosingMethod generic = (EntityEnclosingMethod) httpMethod;
// generic.setRequestEntity(new StringRequestEntity(body.toString()));
generic.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
}
httpMethod.setFollowRedirects(false);
return httpMethod;
}
use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity in project zaproxy by zaproxy.
the class GenericMethod method generateRequestEntity.
/**
* Generates a request entity from the post parameters, if present. Calls
* {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
*
* @since 3.0
*/
@Override
protected RequestEntity generateRequestEntity() {
if (!this.params.isEmpty()) {
// Use a ByteArrayRequestEntity instead of a StringRequestEntity.
// This is to avoid potential encoding issues. Form url encoded strings
// are ASCII by definition but the content type may not be. Treating the content
// as bytes allows us to keep the current charset without worrying about how
// this charset will effect the encoding of the form url encoded string.
String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
ByteArrayRequestEntity entity = new ByteArrayRequestEntity(EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE);
return entity;
}
return super.generateRequestEntity();
}
use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity 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