public RestClientImpl(Client client, WebResource webResource, AsyncWebResource asyncWebResource, String url) { this.client = client; this.client.setReadTimeout(READ_TIMEOUT); this.webResource = webResource; this.asyncWebResource = asyncWebResource; this.url = url; }
/** * Make HTTP GET call to the given url * * @param url */ public void get(String url) { ClientConfig cc = new DefaultNonBlockingClientConfig(); Client c = NonBlockingClient.create(cc); AsyncWebResource awr = c.asyncResource(url); // makes HTTP GET call awr.get(new TypeListener<ClientResponse>(ClientResponse.class) { public void onComplete(Future<ClientResponse> f) throws InterruptedException { try { System.out.println("Got Response. HTTP Code = " + f.get().getStatus()); } catch (ExecutionException e) { System.out.println("Something went wrong!"); } } }); System.out.println("Continued to execute..."); System.out.println("Done!"); }
public void buildAsync(TypeListener<ClientResponse> listener) { AsyncWebResource.Builder builder = getAsyncWR(protocol, url, ajaxUsage); if(mediaType != null) { builder = builder.type(mediaType); } switch(method) { case GET : builder.get(listener); break; case POST : builder.post(listener, requestEntity); break; case DELETE : builder.delete(listener); break; default : throw new PinterestRuntimeException("Unknown HTTP method"); } }
protected AsyncWebResource.Builder getAsyncWR(Protocol protocol, String url, boolean useAJAX) { if(asyncClient == null) { asyncClient = NonBlockingClient.create(getClientConfig()); } AsyncWebResource.Builder wr = null; String requestURL = String.format("%s://%s/%s", protocol.name().toLowerCase(PINTEREST_LOCALE), PINTEREST_DOMAIN, url); wr = asyncClient.asyncResource(UriBuilder.fromUri(requestURL).build()).getRequestBuilder(); wr.header("Referer", "https://pinterest.com/"); if(accessToken != null) { wr = wr.header(COOKIE_HEADER_NAME, accessToken.generateCookieHeader()); wr = wr.header("X-CSRFToken", accessToken.getCsrfToken().getValue()); if(useAJAX) { wr = wr.header("X-Requested-With", "XMLHttpRequest"); } } return wr; }
public <T> Future<T> process(String url, final ITypeListener<T> listener, WebServicesAsyncHandler<T> handler) throws IOException { AsyncWebResource wr = client.asyncResource(url); return process(wr, listener, handler); }
public <T> Future<T> process(final AsyncWebResource wr, final ITypeListener<T> listener, final WebServicesAsyncHandler<T> handler) throws IOException { return SecureExecutor.execute(new SecureExecutor.WorkLoad<Future<T>>() { @Override public Future<T> run() { return handler.process(wr, listener); } }); }
@Override public Future<T> process(AsyncWebResource webResource, ITypeListener<T> listener) { return webResource.get(listener); }
@Override public Future<T> process(AsyncWebResource webResource, ITypeListener<T> listener) { return webResource.delete(listener); }
/** * Returns jersey HTTP resource to access to the remote replication servers * * @param nexusUrl URL of the remote server * @param login Username on the remote server * @param password User's password * @return Jersey HTTP client */ private AsyncWebResource.Builder getService(String nexusUrl, String login, String password) { Client client = getClient(login, password); client.setExecutorService(jerseyHttpClientExecutor); AsyncWebResource webResource = client.asyncResource(UriBuilder.fromUri(nexusUrl).build()); webResource = webResource.path("service").path("local").path("artifact").path("maven").path("update"); return webResource.accept(MediaType.APPLICATION_XML_TYPE) .type(MediaType.APPLICATION_XML_TYPE); }
/** * Obtains a class that can be used to call a remote resource asynchronously. * * @param uri the URI of the resource, not null * @return a class that can be used to call a remote resource, not null */ public AsyncWebResource accessAsync(final URI uri) { return getClient().asyncResource(uri); }
public abstract Future<T> process(AsyncWebResource webResource, ITypeListener<T> listener);