Search in sources :

Example 1 with ClassicTestClient

use of org.apache.hc.core5.testing.classic.ClassicTestClient in project httpcomponents-core by apache.

the class ClassicTestClientAdapter method execute.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Object> execute(final String defaultURI, final Map<String, Object> request) throws Exception {
    // check the request for missing items.
    if (defaultURI == null) {
        throw new HttpException("defaultURL cannot be null");
    }
    if (request == null) {
        throw new HttpException("request cannot be null");
    }
    if (!request.containsKey(PATH)) {
        throw new HttpException("Request path should be set.");
    }
    if (!request.containsKey(METHOD)) {
        throw new HttpException("Request method should be set.");
    }
    final Timeout timeout;
    if (request.containsKey(TIMEOUT)) {
        timeout = Timeout.ofMilliseconds((long) request.get(TIMEOUT));
    } else {
        timeout = null;
    }
    final ClassicTestClient client = new ClassicTestClient(SocketConfig.custom().setSoTimeout(timeout).build());
    // Append the path to the defaultURI.
    String tempDefaultURI = defaultURI;
    if (!defaultURI.endsWith("/")) {
        tempDefaultURI += "/";
    }
    final URI startingURI = new URI(tempDefaultURI + request.get(PATH));
    final URI uri;
    // append each parameter in the query to the uri.
    @SuppressWarnings("unchecked") final Map<String, String> queryMap = (Map<String, String>) request.get(QUERY);
    if (queryMap != null) {
        final String existingQuery = startingURI.getRawQuery();
        final StringBuilder newQuery = new StringBuilder(existingQuery == null ? "" : existingQuery);
        // append each parm to the query
        for (final Entry<String, String> parm : queryMap.entrySet()) {
            newQuery.append("&").append(parm.getKey()).append("=").append(parm.getValue());
        }
        // create a uri with the new query.
        uri = new URI(startingURI.getRawSchemeSpecificPart(), startingURI.getRawUserInfo(), startingURI.getHost(), startingURI.getPort(), startingURI.getRawPath(), newQuery.toString(), startingURI.getRawFragment());
    } else {
        uri = startingURI;
    }
    final BasicClassicHttpRequest httpRequest = new BasicClassicHttpRequest(request.get(METHOD).toString(), uri);
    if (request.containsKey(PROTOCOL_VERSION)) {
        httpRequest.setVersion((ProtocolVersion) request.get(PROTOCOL_VERSION));
    }
    // call addHeader for each header in headers.
    @SuppressWarnings("unchecked") final Map<String, String> headersMap = (Map<String, String>) request.get(HEADERS);
    if (headersMap != null) {
        for (final Entry<String, String> header : headersMap.entrySet()) {
            httpRequest.addHeader(header.getKey(), header.getValue());
        }
    }
    // call setEntity if a body is specified.
    final String requestBody = (String) request.get(BODY);
    if (requestBody != null) {
        final String requestContentType = (String) request.get(CONTENT_TYPE);
        final StringEntity entity = requestContentType != null ? new StringEntity(requestBody, ContentType.parse(requestContentType)) : new StringEntity(requestBody);
        httpRequest.setEntity(entity);
    }
    client.start(null);
    // Now start the request.
    final HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
    final HttpCoreContext context = HttpCoreContext.create();
    try (final ClassicHttpResponse response = client.execute(host, httpRequest, context)) {
        // Prepare the response.  It will contain status, body, headers, and contentType.
        final HttpEntity entity = response.getEntity();
        final String body = entity == null ? null : EntityUtils.toString(entity);
        final String contentType = entity == null ? null : entity.getContentType();
        // prepare the returned information
        final Map<String, Object> ret = new HashMap<>();
        ret.put(STATUS, response.getCode());
        // convert the headers to a Map
        final Map<String, Object> headerMap = new HashMap<>();
        for (final Header header : response.getHeaders()) {
            headerMap.put(header.getName(), header.getValue());
        }
        ret.put(HEADERS, headerMap);
        ret.put(BODY, body);
        ret.put(CONTENT_TYPE, contentType);
        return ret;
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) HashMap(java.util.HashMap) Timeout(org.apache.hc.core5.util.Timeout) URI(java.net.URI) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) HttpHost(org.apache.hc.core5.http.HttpHost) ClassicTestClient(org.apache.hc.core5.testing.classic.ClassicTestClient) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) HttpException(org.apache.hc.core5.http.HttpException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 Header (org.apache.hc.core5.http.Header)1 HttpEntity (org.apache.hc.core5.http.HttpEntity)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)1 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)1 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)1 ClassicTestClient (org.apache.hc.core5.testing.classic.ClassicTestClient)1 Timeout (org.apache.hc.core5.util.Timeout)1