// Path: plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/ErrorResponse.java
// public class ErrorResponse {
// 
// 	private final String errorCode;
// 	private final Iterable<String> statusArguments;
// 
// 	public ErrorResponse(WsError error, Iterable<String> statusArguments) {
// 		this.errorCode = error.name();
// 		this.statusArguments = statusArguments;
// 	}
// 
// 	public String getErrorCode() {
// 		return errorCode;
// 	}
// 
// 	public Iterable<String> getStatusArguments() {
// 		return statusArguments;
// 	}
// 
// }
// 
// Path: plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/WsError.java
// public interface WsError {
// 
// 	// common errors
// 	WsError INTERNAL_ERROR = new WsErrorInternal("INTERNAL_ERROR");
// 	WsError REQUEST_INVALID = new WsErrorInternal("REQUEST_INVALID");
// 	WsError FIELD_REQUIRED = new WsErrorInternal("FIELD_REQUIRED");
// 	WsError EMAIL_INVALID = new WsErrorInternal("EMAIL_INVALID");
// 	WsError COLOR_INVALID = new WsErrorInternal("COLOR_INVALID");
// 
// 	/**
// 	 * Returns the name of the error
// 	 */
// 	String name();
// 
// 	class WsErrorInternal implements WsError {
// 
// 		private final String name;
// 
// 		public WsErrorInternal(String name) {
// 			this.name = name;
// 		}
// 
// 		@Override
// 		public String name() {
// 			return name;
// 		}
// 	}
// 
// }


import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import javax.ws.rs.container.AsyncResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.coreoz.plume.jersey.errors.ErrorResponse;
import com.coreoz.plume.jersey.errors.WsError;
import com.google.common.collect.ImmutableList;
package com.coreoz.plume.jersey.async;





/**
 * Provides a bridge between JAX-RS asynchronous API and Java 8 asynchronous API
 * @see CompletableFuture
 * @see AsyncResponse
 */
public class AsyncJersey {

	private static final Logger logger = LoggerFactory.getLogger(AsyncJersey.class);

	/**
	 * Provides a {@link BiConsumer} from an {@link AsyncResponse}
	 * that should be called when a {@link CompletableFuture} is terminated.<br>
	 * <br>
	 * Usage example:
	 * <code><pre>
	 * &commat;GET
	 * public void fetchAsync(@Suspended AsyncResponse asyncResponse) {
	 *   asyncService
	 *     .fetchDataAsync()
	 *     .whenCompleteAsync(toAsyncConsumer(asyncResponse));
	 * }
	 * </pre></code>
	 */
	public static BiConsumer<? super Object, ? super Throwable> toAsyncConsumer(AsyncResponse asyncResponse) {
		return (responseBody, exception) -> {
			if (exception == null) {
				asyncResponse.resume(responseBody);
			} else {
				logger.error("An exception was raised during the promise execution", exception);
// Path: plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/ErrorResponse.java
// public class ErrorResponse {
// 
// 	private final String errorCode;
// 	private final Iterable<String> statusArguments;
// 
// 	public ErrorResponse(WsError error, Iterable<String> statusArguments) {
// 		this.errorCode = error.name();
// 		this.statusArguments = statusArguments;
// 	}
// 
// 	public String getErrorCode() {
// 		return errorCode;
// 	}
// 
// 	public Iterable<String> getStatusArguments() {
// 		return statusArguments;
// 	}
// 
// }
// 
// Path: plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/WsError.java
// public interface WsError {
// 
// 	// common errors
// 	WsError INTERNAL_ERROR = new WsErrorInternal("INTERNAL_ERROR");
// 	WsError REQUEST_INVALID = new WsErrorInternal("REQUEST_INVALID");
// 	WsError FIELD_REQUIRED = new WsErrorInternal("FIELD_REQUIRED");
// 	WsError EMAIL_INVALID = new WsErrorInternal("EMAIL_INVALID");
// 	WsError COLOR_INVALID = new WsErrorInternal("COLOR_INVALID");
// 
// 	/**
// 	 * Returns the name of the error
// 	 */
// 	String name();
// 
// 	class WsErrorInternal implements WsError {
// 
// 		private final String name;
// 
// 		public WsErrorInternal(String name) {
// 			this.name = name;
// 		}
// 
// 		@Override
// 		public String name() {
// 			return name;
// 		}
// 	}
// 
// }


// Path: plume-web-jersey/src/main/java/com/coreoz/plume/jersey/async/AsyncJersey.java
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import javax.ws.rs.container.AsyncResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.coreoz.plume.jersey.errors.ErrorResponse;
import com.coreoz.plume.jersey.errors.WsError;
import com.google.common.collect.ImmutableList;

package com.coreoz.plume.jersey.async;





/**
 * Provides a bridge between JAX-RS asynchronous API and Java 8 asynchronous API
 * @see CompletableFuture
 * @see AsyncResponse
 */
public class AsyncJersey {

	private static final Logger logger = LoggerFactory.getLogger(AsyncJersey.class);

	/**
	 * Provides a {@link BiConsumer} from an {@link AsyncResponse}
	 * that should be called when a {@link CompletableFuture} is terminated.<br>
	 * <br>
	 * Usage example:
	 * <code><pre>
	 * &commat;GET
	 * public void fetchAsync(@Suspended AsyncResponse asyncResponse) {
	 *   asyncService
	 *     .fetchDataAsync()
	 *     .whenCompleteAsync(toAsyncConsumer(asyncResponse));
	 * }
	 * </pre></code>
	 */
	public static BiConsumer<? super Object, ? super Throwable> toAsyncConsumer(AsyncResponse asyncResponse) {
		return (responseBody, exception) -> {
			if (exception == null) {
				asyncResponse.resume(responseBody);
			} else {
				logger.error("An exception was raised during the promise execution", exception);
				asyncResponse.resume(new ErrorResponse(WsError.INTERNAL_ERROR, ImmutableList.of()));