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 feign by OpenFeign.
the class ApacheHttpClient method toHttpUriRequest.
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
RequestBuilder requestBuilder = RequestBuilder.create(request.method());
//per request timeouts
RequestConfig requestConfig = 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().name());
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);
}
return requestBuilder.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 asterixdb by apache.
the class TestExecutor method constructPostMethod.
private HttpUriRequest constructPostMethod(URI uri, List<CompilationUnit.Parameter> params) {
RequestBuilder builder = RequestBuilder.post(uri);
for (CompilationUnit.Parameter param : params) {
builder.addParameter(param.getName(), param.getValue());
}
builder.setCharset(StandardCharsets.UTF_8);
return builder.build();
}
use of org.apache.http.client.methods.RequestBuilder in project asterixdb by apache.
the class TestExecutor method constructPostMethodJson.
protected HttpUriRequest constructPostMethodJson(String statement, URI uri, String stmtParam, List<CompilationUnit.Parameter> otherParams) {
if (stmtParam == null) {
throw new NullPointerException("Statement parameter required.");
}
RequestBuilder builder = RequestBuilder.post(uri);
ObjectMapper om = new ObjectMapper();
ObjectNode content = om.createObjectNode();
for (CompilationUnit.Parameter param : upsertParam(otherParams, stmtParam, statement)) {
content.put(param.getName(), param.getValue());
}
try {
builder.setEntity(new StringEntity(om.writeValueAsString(content), ContentType.APPLICATION_JSON));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
builder.setCharset(StandardCharsets.UTF_8);
return builder.build();
}
Aggregations