Search in sources :

Example 6 with Destination

use of org.glassfish.jersey.examples.rx.domain.Destination in project jersey by jersey.

the class ObservableAgentResource method recommended.

private Observable<List<Recommendation>> recommended(final Queue<String> errors) {
    destination.register(RxObservableInvokerProvider.class);
    // Recommended places.
    final Observable<Destination> recommended = destination.path("recommended").request().header("Rx-User", "RxJava").rx(RxObservableInvoker.class).get(new GenericType<List<Destination>>() {
    }).onErrorReturn(throwable -> {
        errors.offer("Recommended: " + throwable.getMessage());
        return Collections.emptyList();
    }).flatMap(Observable::from).cache();
    forecast.register(RxObservableInvokerProvider.class);
    // Forecasts. (depend on recommended destinations)
    final Observable<Forecast> forecasts = recommended.flatMap(destination -> forecast.resolveTemplate("destination", destination.getDestination()).request().rx(RxObservableInvoker.class).get(Forecast.class).onErrorReturn(throwable -> {
        errors.offer("Forecast: " + throwable.getMessage());
        return new Forecast(destination.getDestination(), "N/A");
    }));
    calculation.register(RxObservableInvokerProvider.class);
    // Calculations. (depend on recommended destinations)
    final Observable<Calculation> calculations = recommended.flatMap(destination -> calculation.resolveTemplate("from", "Moon").resolveTemplate("to", destination.getDestination()).request().rx(RxObservableInvoker.class).get(Calculation.class).onErrorReturn(throwable -> {
        errors.offer("Calculation: " + throwable.getMessage());
        return new Calculation("Moon", destination.getDestination(), -1);
    }));
    return Observable.zip(recommended, forecasts, calculations, Recommendation::new).toList();
}
Also used : Produces(javax.ws.rs.Produces) RxObservableInvokerProvider(org.glassfish.jersey.client.rx.rxjava.RxObservableInvokerProvider) GET(javax.ws.rs.GET) AsyncResponse(javax.ws.rs.container.AsyncResponse) RxObservableInvoker(org.glassfish.jersey.client.rx.rxjava.RxObservableInvoker) Path(javax.ws.rs.Path) Singleton(javax.inject.Singleton) Suspended(javax.ws.rs.container.Suspended) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) Uri(org.glassfish.jersey.server.Uri) Destination(org.glassfish.jersey.examples.rx.domain.Destination) Observable(rx.Observable) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) List(java.util.List) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) Schedulers(rx.schedulers.Schedulers) Queue(java.util.Queue) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) RxObservableInvoker(org.glassfish.jersey.client.rx.rxjava.RxObservableInvoker)

Aggregations

GET (javax.ws.rs.GET)6 AgentResponse (org.glassfish.jersey.examples.rx.domain.AgentResponse)6 Calculation (org.glassfish.jersey.examples.rx.domain.Calculation)6 Destination (org.glassfish.jersey.examples.rx.domain.Destination)6 Forecast (org.glassfish.jersey.examples.rx.domain.Forecast)6 Recommendation (org.glassfish.jersey.examples.rx.domain.Recommendation)6 List (java.util.List)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)5 GenericType (javax.ws.rs.core.GenericType)5 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 WebTarget (javax.ws.rs.client.WebTarget)4 AsyncResponse (javax.ws.rs.container.AsyncResponse)4 Suspended (javax.ws.rs.container.Suspended)4 Uri (org.glassfish.jersey.server.Uri)4 Collections (java.util.Collections)3 Queue (java.util.Queue)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Singleton (javax.inject.Singleton)2