use of org.apache.http.client.methods.RequestBuilder in project jbpm by kiegroup.
the class RESTWorkItemHandler method configureRequest.
protected Object configureRequest(String method, String urlStr, Map<String, Object> params, String acceptHeaderValue) {
if (HTTP_CLIENT_API_43) {
RequestBuilder builder = null;
if ("GET".equals(method)) {
builder = RequestBuilder.get().setUri(urlStr);
addAcceptHeader(builder, acceptHeaderValue);
} else if ("POST".equals(method)) {
builder = RequestBuilder.post().setUri(urlStr);
setBody(builder, params);
addAcceptHeader(builder, acceptHeaderValue);
} else if ("PUT".equals(method)) {
builder = RequestBuilder.put().setUri(urlStr);
setBody(builder, params);
addAcceptHeader(builder, acceptHeaderValue);
} else if ("DELETE".equals(method)) {
builder = RequestBuilder.delete().setUri(urlStr);
} else {
throw new IllegalArgumentException("Method " + method + " is not supported");
}
return builder;
} else {
HttpRequestBase theMethod = null;
if ("GET".equals(method)) {
theMethod = new HttpGet(urlStr);
addAcceptHeader(theMethod, acceptHeaderValue);
} else if ("POST".equals(method)) {
theMethod = new HttpPost(urlStr);
setBody(theMethod, params);
addAcceptHeader(theMethod, acceptHeaderValue);
} else if ("PUT".equals(method)) {
theMethod = new HttpPut(urlStr);
setBody(theMethod, params);
addAcceptHeader(theMethod, acceptHeaderValue);
} else if ("DELETE".equals(method)) {
theMethod = new HttpDelete(urlStr);
} else {
throw new IllegalArgumentException("Method " + method + " is not supported");
}
return theMethod;
}
}
use of org.apache.http.client.methods.RequestBuilder in project feign by OpenFeign.
the class ApacheHttpClient method toHttpUriRequest.
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws URISyntaxException {
RequestBuilder requestBuilder = RequestBuilder.create(request.httpMethod().name());
// per request timeouts
RequestConfig requestConfig = (client instanceof Configurable ? RequestConfig.copy(((Configurable) client).getConfig()) : RequestConfig.custom()).setConnectTimeout(options.connectTimeoutMillis()).setSocketTimeout(options.readTimeoutMillis()).build();
requestBuilder.setConfig(requestConfig);
URI uri = new URIBuilder(request.url()).build();
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
// request query params
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset());
for (NameValuePair queryParam : queryParams) {
requestBuilder.addParameter(queryParam);
}
// request headers
boolean hasAcceptHeader = false;
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
// doesn't like us to set it as well.
continue;
}
for (String headerValue : headerEntry.getValue()) {
requestBuilder.addHeader(headerName, headerValue);
}
}
// some servers choke on the default accept string, so we'll set it to anything
if (!hasAcceptHeader) {
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
}
// request body
if (request.body() != null) {
HttpEntity entity = null;
if (request.charset() != null) {
ContentType contentType = getContentType(request);
String content = new String(request.body(), request.charset());
entity = new StringEntity(content, contentType);
} else {
entity = new ByteArrayEntity(request.body());
}
requestBuilder.setEntity(entity);
} else {
requestBuilder.setEntity(new ByteArrayEntity(new byte[0]));
}
return requestBuilder.build();
}
use of org.apache.http.client.methods.RequestBuilder in project dropwizard by dropwizard.
the class DropwizardApacheConnector method buildApacheRequest.
/**
* Build a new Apache's {@link org.apache.http.client.methods.HttpUriRequest}
* from Jersey's {@link org.glassfish.jersey.client.ClientRequest}
* <p>
* Convert a method, URI, body, headers and override a user-agent if necessary
* </p>
*
* @param jerseyRequest representation of an HTTP request in Jersey
* @return a new {@link org.apache.http.client.methods.HttpUriRequest}
*/
private HttpUriRequest buildApacheRequest(ClientRequest jerseyRequest) {
final RequestBuilder builder = RequestBuilder.create(jerseyRequest.getMethod()).setUri(jerseyRequest.getUri()).setEntity(getHttpEntity(jerseyRequest));
for (String headerName : jerseyRequest.getHeaders().keySet()) {
builder.addHeader(headerName, jerseyRequest.getHeaderString(headerName));
}
final Optional<RequestConfig> requestConfig = addJerseyRequestConfig(jerseyRequest);
requestConfig.ifPresent(builder::setConfig);
return builder.build();
}
use of org.apache.http.client.methods.RequestBuilder in project cloudstack by apache.
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 incubator-gobblin by apache.
the class ApacheHttpRequestBuilder method buildWriteRequest.
/**
* Build a write request from a single record
*/
private ApacheHttpRequest<GenericRecord> buildWriteRequest(BufferedRecord<GenericRecord> record) {
if (record == null) {
return null;
}
ApacheHttpRequest<GenericRecord> request = new ApacheHttpRequest<>();
HttpOperation httpOperation = HttpUtils.toHttpOperation(record.getRecord());
// Set uri
URI uri = HttpUtils.buildURI(urlTemplate, httpOperation.getKeys(), httpOperation.getQueryParams());
if (uri == null) {
return null;
}
RequestBuilder builder = RequestBuilder.create(verb.toUpperCase());
builder.setUri(uri);
// Set headers
Map<String, String> headers = httpOperation.getHeaders();
if (headers != null && headers.size() != 0) {
for (Map.Entry<String, String> header : headers.entrySet()) {
builder.setHeader(header.getKey(), header.getValue());
}
}
// Add payload
int bytesWritten = addPayload(builder, httpOperation.getBody());
if (bytesWritten == -1) {
throw new RuntimeException("Fail to write payload into request");
}
request.setRawRequest(build(builder));
request.markRecord(record, bytesWritten);
return request;
}
Aggregations