Search in sources :

Example 1 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project webmagic by code4craft.

the class HttpClientDownloader method getHttpUriRequest.

protected HttpUriRequest getHttpUriRequest(Request request, Site site, Map<String, String> headers, HttpHost proxy) {
    RequestBuilder requestBuilder = selectRequestMethod(request).setUri(request.getUrl());
    if (headers != null) {
        for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
            requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
        }
    }
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    if (site != null) {
        requestConfigBuilder.setConnectionRequestTimeout(site.getTimeOut()).setSocketTimeout(site.getTimeOut()).setConnectTimeout(site.getTimeOut()).setCookieSpec(CookieSpecs.BEST_MATCH);
    }
    if (proxy != null) {
        requestConfigBuilder.setProxy(proxy);
        request.putExtra(Request.PROXY, proxy);
    }
    requestBuilder.setConfig(requestConfigBuilder.build());
    return requestBuilder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Example 2 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project asterixdb by apache.

the class TestExecutor method constructPostMethodUrl.

protected HttpUriRequest constructPostMethodUrl(String statement, URI uri, String stmtParam, List<CompilationUnit.Parameter> otherParams) {
    RequestBuilder builder = RequestBuilder.post(uri);
    if (stmtParam != null) {
        for (CompilationUnit.Parameter param : upsertParam(otherParams, stmtParam, statement)) {
            builder.addParameter(param.getName(), param.getValue());
        }
        builder.addParameter(stmtParam, statement);
    } else {
        // this seems pretty bad - we should probably fix the API and not the client
        builder.setEntity(new StringEntity(statement, StandardCharsets.UTF_8));
    }
    builder.setCharset(StandardCharsets.UTF_8);
    return builder.build();
}
Also used : CompilationUnit(org.apache.asterix.testframework.xml.TestCase.CompilationUnit) StringEntity(org.apache.http.entity.StringEntity) RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Example 3 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project asterixdb by apache.

the class TestExecutor method constructGetMethod.

private HttpUriRequest constructGetMethod(URI endpoint, List<CompilationUnit.Parameter> params) {
    RequestBuilder builder = RequestBuilder.get(endpoint);
    for (CompilationUnit.Parameter param : params) {
        builder.addParameter(param.getName(), param.getValue());
    }
    builder.setCharset(StandardCharsets.UTF_8);
    return builder.build();
}
Also used : CompilationUnit(org.apache.asterix.testframework.xml.TestCase.CompilationUnit) RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Example 4 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project asterixdb by apache.

the class CancellationTestExecutor method constructDeleteMethodUrl.

// Constructs a HTTP DELETE request.
private HttpUriRequest constructDeleteMethodUrl(URI uri, List<TestCase.CompilationUnit.Parameter> otherParams) {
    RequestBuilder builder = RequestBuilder.delete(uri);
    for (TestCase.CompilationUnit.Parameter param : otherParams) {
        builder.addParameter(param.getName(), param.getValue());
    }
    builder.setCharset(StandardCharsets.UTF_8);
    return builder.build();
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Example 5 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project incubator-gobblin by apache.

the class ApacheHttpRequestBuilderTest method testBuildWriteRequest.

/**
 * Build a {@link HttpUriRequest} from a {@link GenericRecord}
 */
public void testBuildWriteRequest() throws IOException {
    String urlTemplate = "http://www.test.com/a/part1:${part1}/a/part2:${part2}";
    String verb = "post";
    ApacheHttpRequestBuilder builder = spy(new ApacheHttpRequestBuilder(urlTemplate, verb, "application/json"));
    ArgumentCaptor<RequestBuilder> requestBuilderArgument = ArgumentCaptor.forClass(RequestBuilder.class);
    Queue<BufferedRecord<GenericRecord>> queue = HttpTestUtils.createQueue(1, false);
    AsyncRequest<GenericRecord, HttpUriRequest> request = builder.buildRequest(queue);
    verify(builder).build(requestBuilderArgument.capture());
    RequestBuilder expected = RequestBuilder.post();
    expected.setUri("http://www.test.com/a/part1:01/a/part2:02?param1=01");
    String payloadStr = "{\"id\":\"id0\"}";
    expected.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()).setEntity(new StringEntity(payloadStr, ContentType.APPLICATION_JSON));
    // Compare HttpUriRequest
    HttpTestUtils.assertEqual(requestBuilderArgument.getValue(), expected);
    Assert.assertEquals(request.getRecordCount(), 1);
    Assert.assertEquals(queue.size(), 0);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StringEntity(org.apache.http.entity.StringEntity) RequestBuilder(org.apache.http.client.methods.RequestBuilder) BufferedRecord(org.apache.gobblin.async.BufferedRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

RequestBuilder (org.apache.http.client.methods.RequestBuilder)23 StringEntity (org.apache.http.entity.StringEntity)8 Map (java.util.Map)6 URI (java.net.URI)4 CompilationUnit (org.apache.asterix.testframework.xml.TestCase.CompilationUnit)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 NameValuePair (org.apache.http.NameValuePair)3 GenericRecord (org.apache.avro.generic.GenericRecord)2 HttpEntity (org.apache.http.HttpEntity)2 HttpHost (org.apache.http.HttpHost)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)2 BasicHeader (org.apache.http.message.BasicHeader)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 PrepareUrl (com.aliyuncs.fc.model.PrepareUrl)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1