use of org.apache.http.client.methods.RequestBuilder in project rdf4j by eclipse.
the class RDF4JProtocolSession method getUpdateMethod.
@Override
protected HttpUriRequest getUpdateMethod(QueryLanguage ql, String update, String baseURI, Dataset dataset, boolean includeInferred, int maxExecutionTime, Binding... bindings) {
RequestBuilder builder = null;
String transactionURL = getTransactionURL();
if (transactionURL != null) {
builder = RequestBuilder.put(transactionURL);
builder.addHeader("Content-Type", Protocol.SPARQL_UPDATE_MIME_TYPE + "; charset=utf-8");
builder.addParameter(Protocol.ACTION_PARAM_NAME, Action.UPDATE.toString());
for (NameValuePair nvp : getUpdateMethodParameters(ql, null, baseURI, dataset, includeInferred, maxExecutionTime, bindings)) {
builder.addParameter(nvp);
}
// in a PUT request, we carry the only actual update string as the
// request body - the rest is sent as request parameters
builder.setEntity(new StringEntity(update, UTF8));
pingTransaction();
} else {
builder = RequestBuilder.post(getUpdateURL());
builder.addHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8");
builder.setEntity(new UrlEncodedFormEntity(getUpdateMethodParameters(ql, update, baseURI, dataset, includeInferred, maxExecutionTime, bindings), UTF8));
}
// applications
for (Map.Entry<String, String> additionalHeader : getAdditionalHttpHeaders().entrySet()) {
builder.addHeader(additionalHeader.getKey(), additionalHeader.getValue());
}
return builder.build();
}
use of org.apache.http.client.methods.RequestBuilder in project spring-cloud-netflix by spring-cloud.
the class RibbonApacheHttpRequestTests method testEntity.
void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException {
String lengthString = String.valueOf(entityValue.length());
Long length = null;
URI uri = URI.create("http://example.com");
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
if (addContentLengthHeader) {
headers.add("Content-Length", lengthString);
length = (long) entityValue.length();
}
RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<RequestBuilder>() {
@Override
public boolean accepts(Class builderClass) {
return builderClass == RequestBuilder.class;
}
@Override
public void customize(RequestBuilder builder) {
builder.addHeader("from-customizer", "foo");
}
};
RibbonCommandContext context = new RibbonCommandContext("example", method, uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
context.setContentLength(length);
RibbonApacheHttpRequest httpRequest = new RibbonApacheHttpRequest(context);
HttpUriRequest request = httpRequest.toRequest(RequestConfig.custom().build());
assertThat("request is wrong type", request, is(instanceOf(HttpEntityEnclosingRequest.class)));
assertThat("uri is wrong", request.getURI().toString(), startsWith(uri.toString()));
if (addContentLengthHeader) {
assertThat("Content-Length is missing", request.getFirstHeader("Content-Length"), is(notNullValue()));
assertThat("Content-Length is wrong", request.getFirstHeader("Content-Length").getValue(), is(equalTo(lengthString)));
}
assertThat("from-customizer is missing", request.getFirstHeader("from-customizer"), is(notNullValue()));
assertThat("from-customizer is wrong", request.getFirstHeader("from-customizer").getValue(), is(equalTo("foo")));
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
assertThat("entity is missing", entityRequest.getEntity(), is(notNullValue()));
HttpEntity entity = entityRequest.getEntity();
assertThat("contentLength is wrong", entity.getContentLength(), is(equalTo((long) entityValue.length())));
assertThat("content is missing", entity.getContent(), is(notNullValue()));
String string = StreamUtils.copyToString(entity.getContent(), Charset.forName("UTF-8"));
assertThat("content is wrong", string, is(equalTo(entityValue)));
}
use of org.apache.http.client.methods.RequestBuilder in project spring-cloud-netflix by spring-cloud.
the class RibbonApacheHttpRequest method toRequest.
public HttpUriRequest toRequest(final RequestConfig requestConfig) {
final RequestBuilder builder = RequestBuilder.create(this.context.getMethod());
builder.setUri(this.uri);
for (final String name : this.context.getHeaders().keySet()) {
final List<String> values = this.context.getHeaders().get(name);
for (final String value : values) {
builder.addHeader(name, value);
}
}
for (final String name : this.context.getParams().keySet()) {
final List<String> values = this.context.getParams().get(name);
for (final String value : values) {
builder.addParameter(name, value);
}
}
if (this.context.getRequestEntity() != null) {
final BasicHttpEntity entity;
entity = new BasicHttpEntity();
entity.setContent(this.context.getRequestEntity());
// if the entity contentLength isn't set, transfer-encoding will be set
// to chunked in org.apache.http.protocol.RequestContent. See gh-1042
Long contentLength = this.context.getContentLength();
if ("GET".equals(this.context.getMethod()) && (contentLength == null || contentLength < 0)) {
entity.setContentLength(0);
} else if (contentLength != null) {
entity.setContentLength(contentLength);
}
builder.setEntity(entity);
}
customize(this.context.getRequestCustomizers(), builder);
builder.setConfig(requestConfig);
return builder.build();
}
use of org.apache.http.client.methods.RequestBuilder in project cosmic by MissionCriticalCloud.
the class HttpUriRequestBuilder method build.
public HttpUriRequest build() {
validate();
final RequestBuilder builder = RequestBuilder.create(method.toString()).setUri(buildUri());
if (!methodParameters.isEmpty()) {
for (final Entry<String, String> entry : methodParameters.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
}
if (jsonPayload.isPresent()) {
builder.addHeader(new BasicHeader(CONTENT_TYPE, JSON_CONTENT_TYPE)).setEntity(new StringEntity(jsonPayload.get(), ContentType.create(JSON_CONTENT_TYPE, Consts.UTF_8)));
}
return builder.build();
}
use of org.apache.http.client.methods.RequestBuilder in project core-ng-project by neowu.
the class HTTPClientImpl method httpRequest.
HttpUriRequest httpRequest(HTTPRequest request) {
HTTPMethod method = request.method();
String uri = request.uri();
logger.debug("[request] method={}, uri={}", method, uri);
RequestBuilder builder = RequestBuilder.create(method.name());
try {
builder.setUri(uri);
} catch (IllegalArgumentException e) {
throw new HTTPClientException("uri is invalid, uri=" + uri, "INVALID_URL", e);
}
request.headers().forEach((name, value) -> {
logger.debug("[request:header] {}={}", name, new FieldParam(name, value));
builder.setHeader(name, value);
});
request.params().forEach((name, value) -> {
logger.debug("[request:param] {}={}", name, value);
builder.addParameter(name, value);
});
byte[] body = request.body();
if (body != null) {
ContentType contentType = request.contentType();
logRequestBody(request, contentType);
org.apache.http.entity.ContentType type = org.apache.http.entity.ContentType.create(contentType.mediaType(), contentType.charset().orElse(null));
builder.setEntity(new ByteArrayEntity(request.body(), type));
}
return builder.build();
}
Aggregations