Skip to content

ApacheHttpClient4xInstrumentation#onReturn() always expects HttpResponse as return type. #1

@smathangi

Description

@smathangi

There are few methods in HttpClient which return a custom response entity rather than raw HttpResponse object.
Eg: https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html#execute(org.apache.http.client.methods.HttpUriRequest,%20org.apache.http.client.ResponseHandler).

Current instrumentation logic around onReturn() is throwing "IncompatibleClassChangeError" error if above HttpClient api is invoked with custom return type (Eg: String etc).
Eg: java.lang.IncompatibleClassChangeError: Class java.lang.String does not implement the requested interface org.apache.http.HttpResponse

https://github.com/glowroot/instrumentation/blob/master/instrumentation/apache-http-client/src/main/java/org/glowroot/instrumentation/apachehttpclient/ApacheHttpClient4xInstrumentation.java#L128

Example app code:


public String parse(String jwt) {
        try {
            String bearerJwt = jwt.startsWith("Bearer ") ? jwt : "Bearer " + jwt;
            HttpGet request = new HttpGet(baseUrl + "/details");
            request.addHeader("Authorization", bearerJwt);

            return httpClient.execute(request, httpResponse -> {
                checkStatusIs2xx(httpResponse);
                return EntityUtils.toString(httpResponse.getEntity());
            });
        } catch (IOException e) {
            throw new ServiceTokenParsingException(e);
        }
    }

Log trace :

2019-09-24T10:33:23.314 ERROR [http-nio-4452-exec-3] o.a.c.c.C.[.[localhost].[/].[dispatcherServlet]java.lang.IncompatibleClassChangeError: Class java.lang.String does not implement the requested interface org.apache.http.HttpResponse
	at org.glowroot.instrumentation.apachehttpclient.ApacheHttpClient4xInstrumentation.onReturnCommon(ApacheHttpClient4xInstrumentation.java:128)
	at org.glowroot.instrumentation.apachehttpclient.ApacheHttpClient4xInstrumentation.access$500(ApacheHttpClient4xInstrumentation.java:39)
	at org.glowroot.instrumentation.apachehttpclient.ApacheHttpClient4xInstrumentation$ExecuteAdvice.onReturn(ApacheHttpClient4xInstrumentation.java:72)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:140)
	at uk.gov.hmcts.reform.auth.parser.idam.core.service.token.HttpComponentsBasedServiceTokenParser.parse(HttpComponentsBasedServiceTokenParser.java:28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions