public Response sendRequest(Method method, String url, Representation representation) { this.logger.debug("Method: " + method.getName() + " url: " + url); Request request = new Request(); request.setResourceRef(url); request.setMethod(method); if (!Method.GET.equals(method) && !Method.DELETE.equals(method)) { request.setEntity(representation); } request.setChallengeResponse(this.challenge); return this.restClient.handle(request); }
/** * Constructor. * * @param helper The parent HTTP client helper. * @param method The method name. * @param requestUri The request URI. * @param hasEntity Indicates if the call will have an entity to send to the server. * @throws IOException */ public HttpMethodCall( HttpClientHelper helper, final String method, String requestUri, boolean hasEntity) throws IOException { super(helper, method, requestUri); this.clientHelper = helper; if (requestUri.startsWith("http")) { if (method.equalsIgnoreCase(Method.GET.getName())) { this.httpMethod = new GetMethod(requestUri); } else if (method.equalsIgnoreCase(Method.POST.getName())) { this.httpMethod = new PostMethod(requestUri); } else if (method.equalsIgnoreCase(Method.PUT.getName())) { this.httpMethod = new PutMethod(requestUri); } else if (method.equalsIgnoreCase(Method.HEAD.getName())) { this.httpMethod = new HeadMethod(requestUri); } else if (method.equalsIgnoreCase(Method.DELETE.getName())) { this.httpMethod = new DeleteMethod(requestUri); } else if (method.equalsIgnoreCase(Method.CONNECT.getName())) { final HostConfiguration host = new HostConfiguration(); host.setHost(new URI(requestUri, false)); this.httpMethod = new ConnectMethod(host); } else if (method.equalsIgnoreCase(Method.OPTIONS.getName())) { this.httpMethod = new OptionsMethod(requestUri); } else if (method.equalsIgnoreCase(Method.TRACE.getName())) { this.httpMethod = new TraceMethod(requestUri); } else { this.httpMethod = new EntityEnclosingMethod(requestUri) { @Override public String getName() { return method; } }; } this.httpMethod.setFollowRedirects(this.clientHelper.isFollowRedirects()); this.httpMethod.setDoAuthentication(false); if (this.clientHelper.getRetryHandler() != null) { try { this.httpMethod .getParams() .setParameter( HttpMethodParams.RETRY_HANDLER, Engine.loadClass(this.clientHelper.getRetryHandler()).newInstance()); } catch (Exception e) { this.clientHelper .getLogger() .log( Level.WARNING, "An error occurred during the instantiation of the retry handler.", e); } } this.responseHeadersAdded = false; setConfidential( this.httpMethod.getURI().getScheme().equalsIgnoreCase(Protocol.HTTPS.getSchemeName())); } else { throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here"); } }