public void commitHeaders(ClientRequest request, RequestBuilder requestBuilder) { MultivaluedMap<String, String> headers = request.getHeaders(); for (Map.Entry<String, List<String>> header : headers.entrySet()) { List<String> values = header.getValue(); for (String value : values) { requestBuilder.addHeader(header.getKey(), value); } } }
@Override public ClientResponse execute(ClientRequest request) throws Exception { RequestBuilder requestBuilder = new RequestBuilder(request.getHttpMethod()).setUrl(request.getUri()); requestBuilder.setFollowRedirects(request.followRedirects()); if (request.getBody() != null && !request.getFormParameters().isEmpty()) { throw new RuntimeException("You cannot send both form parameters and an entity body"); } commitHeaders(request, requestBuilder); if (!request.getFormParameters().isEmpty()) { for (Map.Entry<String, List<String>> formParam : request.getFormParameters().entrySet()) { List<String> values = formParam.getValue(); for (String value : values) { requestBuilder.addParameter(formParam.getKey(), value); } } } else if (request.getBody() != null) { if (request.getHttpMethod().equals("GET")) { throw new RuntimeException("A GET request cannot have a body."); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); request.writeRequestBody(request.getHeadersAsObjects(), baos); requestBuilder.setBody(baos.toByteArray()); } Response rawResponse = this.httpClient.executeRequest(requestBuilder.build()).get(); BaseClientResponse response = new BaseClientResponse( new SimpleBaseClientResponseStreamFactory(rawResponse), this, request.getErrorStatusCriteria()); response.setStatus(rawResponse.getStatusCode()); response.setHeaders(extractHeaders(rawResponse)); response.setProviders(request.getProviders()); return response; }