Search in sources :

Example 1 with RequestContent

use of org.apache.hc.core5.http.protocol.RequestContent in project datagear by datageartech.

the class HttpDataSet method resolveResult.

@Override
protected TemplateResolvedDataSetResult resolveResult(DataSetQuery query, List<DataSetProperty> properties, boolean resolveProperties) throws DataSetException {
    String uri = null;
    String headerContent = null;
    String requestContent = null;
    try {
        uri = resolveTemplateUri(query);
        headerContent = resolveTemplateHeaderContent(query);
        requestContent = resolveTemplateRequestContent(query);
        ClassicHttpRequest request = createHttpRequest(uri);
        setHttpHeaders(request, headerContent);
        setHttpEntity(request, requestContent);
        JsonResponseHandler responseHandler = new JsonResponseHandler();
        responseHandler.setProperties(properties);
        responseHandler.setResponseDataJsonPath(getResponseDataJsonPath());
        responseHandler.setDataSetQuery(query);
        ResolvedDataSetResult result = this.httpClient.execute(request, responseHandler);
        return new TemplateResolvedDataSetResult(result.getResult(), result.getProperties(), buildResolvedTemplate(uri, headerContent, requestContent));
    } catch (DataSetException e) {
        throw e;
    } catch (Throwable t) {
        throw new DataSetSourceParseException(t, buildResolvedTemplate(uri, headerContent, requestContent));
    }
}
Also used : ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) DataSetException(org.datagear.analysis.DataSetException) ResolvedDataSetResult(org.datagear.analysis.ResolvedDataSetResult)

Example 2 with RequestContent

use of org.apache.hc.core5.http.protocol.RequestContent in project datagear by datageartech.

the class HttpDataSet method setHttpEntity.

protected void setHttpEntity(ClassicHttpRequest request, String requestContent) throws Throwable {
    if (REQUEST_CONTENT_TYPE_FORM_URLENCODED.equals(this.requestContentType)) {
        List<NameValuePair> params = toNameValuePairs(requestContent);
        if (params == NOT_NAME_VALUE_PAIR_OBJ_ARRAY_JSON)
            throw new RequestContentNotNameValueObjArrayJsonException(requestContent);
        request.setEntity(new UrlEncodedFormEntity(params, Charset.forName(this.requestContentCharset)));
    } else if (REQUEST_CONTENT_TYPE_JSON.equals(this.requestContentType)) {
        ContentType contentType = ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), Charset.forName(this.requestContentCharset));
        StringEntity entity = new StringEntity(requestContent, contentType);
        request.setEntity(entity);
    } else
        throw new DataSetException("Request content type [" + this.requestContentType + "] is not supported");
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.NameValuePair) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ContentType(org.apache.hc.core5.http.ContentType) DataSetException(org.datagear.analysis.DataSetException) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity)

Example 3 with RequestContent

use of org.apache.hc.core5.http.protocol.RequestContent in project httpcomponents-core by apache.

the class TestStandardInterceptors method testRequestContentOverwriteHeaders.

@Test
public void testRequestContentOverwriteHeaders() throws Exception {
    final RequestContent interceptor = new RequestContent(true);
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
    request.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "10"));
    request.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "whatever"));
    request.setEntity(new StringEntity(""));
    interceptor.process(request, request.getEntity(), context);
    final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNotNull(h1);
    Assertions.assertEquals("0", h1.getValue());
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 4 with RequestContent

use of org.apache.hc.core5.http.protocol.RequestContent in project httpcomponents-core by apache.

the class TestStandardInterceptors method testRequestContentAddHeaders.

@Test
public void testRequestContentAddHeaders() throws Exception {
    final RequestContent interceptor = new RequestContent(true);
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
    request.setEntity(new StringEntity(""));
    interceptor.process(request, request.getEntity(), context);
    final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNotNull(h1);
    Assertions.assertEquals("0", h1.getValue());
    final Header h2 = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
    Assertions.assertNull(h2);
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Aggregations

StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 Header (org.apache.hc.core5.http.Header)2 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)2 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)2 DataSetException (org.datagear.analysis.DataSetException)2 Test (org.junit.jupiter.api.Test)2 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)1 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)1 ContentType (org.apache.hc.core5.http.ContentType)1 NameValuePair (org.apache.hc.core5.http.NameValuePair)1 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)1 ResolvedDataSetResult (org.datagear.analysis.ResolvedDataSetResult)1