private URI calculateNewRequestURI(URI newBaseURI, URI requestURI, boolean proxy) {
String baseURIPath = newBaseURI.getRawPath();
String reqURIPath = requestURI.getRawPath();
UriBuilder builder = new UriBuilderImpl().uri(newBaseURI);
String basePath = reqURIPath.startsWith(baseURIPath) ? baseURIPath : getBaseURI().getRawPath();
builder.path(reqURIPath.equals(basePath) ? "" : reqURIPath.substring(basePath.length()));
String newQuery = newBaseURI.getRawQuery();
if (newQuery == null) {