All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.micro.server.rest.client.nio.AsyncRestClient Maven / Gradle / Ivy

The newest version!
package com.aol.micro.server.rest.client.nio;

import java.util.concurrent.CompletableFuture;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.experimental.Wither;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;

import com.aol.micro.server.rest.jackson.JacksonFeature;
import com.aol.micro.server.rest.jackson.JacksonUtil;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;

@Wither
@Builder
@AllArgsConstructor
public class AsyncRestClient {

	private final Client client;
	private final String contentType;
	private final String accept;
	private final Class response;
	private final JavaType genericResponse;

	public AsyncRestClient(int readTimeout, int connectTimeout) {

		this.client = initClient(readTimeout, connectTimeout);
		contentType = MediaType.APPLICATION_JSON;
		accept = MediaType.APPLICATION_JSON;
		response = (Class) String.class;
		genericResponse = null;
	}

	public  AsyncRestClient withResponse(Class response) {
		return new AsyncRestClient(client, contentType, accept, response,null);
	}
	public  AsyncRestClient withGenericResponse(Class responseClass, Class... genericResponse) {
		return new AsyncRestClient(client, contentType, accept, null,JacksonUtil.getMapper().getTypeFactory().constructParametricType(responseClass,genericResponse));
	}

	protected Client initClient(int rt, int ct) {

		ClientConfig clientConfig = new ClientConfig();
		clientConfig.property(ClientProperties.CONNECT_TIMEOUT, ct);
		clientConfig.property(ClientProperties.READ_TIMEOUT, rt);

		ClientBuilder.newBuilder().register(JacksonFeature.class);
		Client client = ClientBuilder.newClient(clientConfig);
		client.register(JacksonFeature.class);
		JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
		provider.setMapper(JacksonUtil.getMapper());
		client.register(provider);
		return client;

	}

	public CompletableFuture get(final String url) {
		CompletableFuture result = new CompletableFuture();
		client.target(url).request(accept).accept(accept).async()
				.get(new InvocationCallback() {
					@Override
					public void completed(String complete) {
						buildResponse(result, complete);
					}

					

					@Override
					public void failed(Throwable ex) {
						result.completeExceptionally(ex);
					}
				});
		return result;

	}
	private void buildResponse(CompletableFuture result,
			String complete) {
		if(normalNotGenericResponseType()){
			if (shouldJustReturnString()){
				thenJustReturnString(result, complete);
			}
			else{
				convertToStandardEntity(result, complete);
			}
		} else
			convertToGenericEntity(result, complete);
	}

	private void convertToGenericEntity(CompletableFuture result,
			String complete) {
		result.complete((T) JacksonUtil.convertFromJson(
				complete, genericResponse));
	}

	private void convertToStandardEntity(CompletableFuture result,
			String complete) {
		result.complete((T) JacksonUtil.convertFromJson(
				complete, response));
	}

	private void thenJustReturnString(CompletableFuture result,
			String complete) {
		result.complete((T) complete);
	}

	private boolean shouldJustReturnString() {
		return String.class.equals(response);
	}

	private boolean normalNotGenericResponseType() {
		return this.genericResponse==null;
	}

	public  CompletableFuture post(final String queryResourceUrl,
			final V request) {
	
		CompletableFuture result = new CompletableFuture();
		final WebTarget webResource = client.target(queryResourceUrl);

		webResource
				.request(accept)
				.accept(accept)
				.async()
				.post(Entity.entity(request, contentType),
						new InvocationCallback() {
							@Override
							public void completed(String complete) {
								buildResponse(result,complete);
							
							}

							@Override
							public void failed(Throwable ex) {
								result.completeExceptionally(ex);
							}
						});
		return result;

	}

	public  CompletableFuture put(final String queryResourceUrl,
			final V request) {

		CompletableFuture result = new CompletableFuture<>();
		final WebTarget webResource = client.target(queryResourceUrl);

		webResource
				.request(accept)
				.accept(accept)
				.async()
				.put(Entity.entity(request, contentType),
						new InvocationCallback() {
							@Override
							public void completed(String complete) {
								buildResponse(result,complete);
							}

							@Override
							public void failed(Throwable ex) {
								result.completeExceptionally(ex);
							}
						});
		return result;

	}
	public CompletableFuture delete(final String queryResourceUrl) {

		CompletableFuture result = new CompletableFuture<>();
		final WebTarget webResource = client.target(queryResourceUrl);

		webResource
				.request(accept)
				.accept(accept)
				.async()
				.delete(new InvocationCallback() {
							@Override
							public void completed(String complete) {
								buildResponse(result,complete);
							}

							@Override
							public void failed(Throwable ex) {
								result.completeExceptionally(ex);
							}
						});
		return result;

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy