I think the method signature should be:

```
private <T extends HttpEntityEnclosingRequestBase> T addHeaderToRequest(String headerName, String headerValue, T request)
```

This way you can rid of all the unnecessary (and potentially unsafe) casts above.