use of org.apache.http.HttpEntityEnclosingRequest in project newsrob by marianokamp.
the class CountingInputStream method toCurl.
/**
* Generates a cURL command equivalent to the given request.
*/
public static String toCurl(HttpUriRequest request) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("curl ");
for (Header header : request.getAllHeaders()) {
builder.append("--header \"");
builder.append(header.toString().trim());
builder.append("\" ");
}
URI uri = request.getURI();
// relative URI. We want an absolute URI.
if (request instanceof RequestWrapper) {
HttpRequest original = ((RequestWrapper) request).getOriginal();
if (original instanceof HttpUriRequest) {
uri = ((HttpUriRequest) original).getURI();
}
}
builder.append("\"");
builder.append(uri);
builder.append("\"");
if (request instanceof HttpEntityEnclosingRequest && !uri.toString().contains("ClientLogin")) {
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.isRepeatable()) {
if (entity.getContentLength() < (8192 * 4)) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream);
String entityString = stream.toString();
builder.append(" --data-ascii \"").append(entityString).append("\"");
} else {
builder.append(" [TOO MUCH DATA TO INCLUDE]");
}
}
}
return builder.toString();
}
use of org.apache.http.HttpEntityEnclosingRequest in project camel by apache.
the class BasicValidationHandler method handle.
public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException {
if (expectedMethod != null && !expectedMethod.equals(request.getRequestLine().getMethod())) {
response.setStatusCode(HttpStatus.SC_METHOD_FAILURE);
return;
}
if (!validateQuery(request)) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
return;
}
if (expectedContent != null) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
String content = EntityUtils.toString(entity);
if (!expectedContent.equals(content)) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
return;
}
}
response.setStatusCode(HttpStatus.SC_OK);
if (responseContent != null) {
response.setEntity(new StringEntity(responseContent, "ASCII"));
}
}
use of org.apache.http.HttpEntityEnclosingRequest in project robovm by robovm.
the class HttpService method handleRequest.
public void handleRequest(final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException {
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
HttpResponse response = null;
try {
HttpRequest request = conn.receiveRequestHeader();
request.setParams(new DefaultedHttpParams(request.getParams(), this.params));
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
// Downgrade protocol version if greater than HTTP/1.1
ver = HttpVersion.HTTP_1_1;
}
if (request instanceof HttpEntityEnclosingRequest) {
if (((HttpEntityEnclosingRequest) request).expectContinue()) {
response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_CONTINUE, context);
response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
if (this.expectationVerifier != null) {
try {
this.expectationVerifier.verify(request, response, context);
} catch (HttpException ex) {
response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
handleException(ex, response);
}
}
if (response.getStatusLine().getStatusCode() < 200) {
// Send 1xx response indicating the server expections
// have been met
conn.sendResponseHeader(response);
conn.flush();
response = null;
conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
}
} else {
conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
}
}
if (response == null) {
response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
this.processor.process(request, context);
doService(request, response, context);
}
// Make sure the request content is fully consumed
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
entity.consumeContent();
}
}
} catch (HttpException ex) {
response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
handleException(ex, response);
}
this.processor.process(response, context);
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
conn.flush();
if (!this.connStrategy.keepAlive(response, context)) {
conn.close();
}
}
use of org.apache.http.HttpEntityEnclosingRequest in project robovm by robovm.
the class RequestContent method process.
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (request instanceof HttpEntityEnclosingRequest) {
if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (request.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity == null) {
request.addHeader(HTTP.CONTENT_LEN, "0");
return;
}
// Must specify a transfer encoding or a content length
if (entity.isChunked() || entity.getContentLength() < 0) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
}
request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else {
request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !request.containsHeader(HTTP.CONTENT_TYPE)) {
request.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
request.addHeader(entity.getContentEncoding());
}
}
}
use of org.apache.http.HttpEntityEnclosingRequest in project cloudstack by apache.
the class ClusterServiceServletHttpHandler method parseRequest.
@SuppressWarnings("deprecation")
private void parseRequest(HttpRequest request) throws IOException {
if (request instanceof HttpEntityEnclosingRequest) {
final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
final String body = EntityUtils.toString(entityRequest.getEntity());
if (body != null) {
final String[] paramArray = body.split("&");
if (paramArray != null) {
for (final String paramEntry : paramArray) {
final String[] paramValue = paramEntry.split("=");
if (paramValue.length != 2) {
continue;
}
final String name = URLDecoder.decode(paramValue[0]);
final String value = URLDecoder.decode(paramValue[1]);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Parsed request parameter " + name + "=" + value);
}
request.getParams().setParameter(name, value);
}
}
}
}
}
Aggregations