use of javax.ws.rs.GET in project jersey by jersey.
the class ListenableFutureAgentResource method recommended.
private ListenableFuture<AgentResponse> recommended(final AgentResponse response) {
destination.register(RxListenableFutureInvokerProvider.class);
// Get a list of recommended destinations ...
final ListenableFuture<List<Destination>> destinations = destination.path("recommended").request().header("Rx-User", "Guava").rx(RxListenableFutureInvoker.class).get(new GenericType<List<Destination>>() {
});
// ... transform them to Recommendation instances ...
final ListenableFuture<List<Recommendation>> recommendations = Futures.transform(destinations, (AsyncFunction<List<Destination>, List<Recommendation>>) destinationList -> {
final List<Recommendation> recommendationList = Lists.newArrayList(Lists.transform(destinationList, destination -> new Recommendation(destination.getDestination(), null, 0)));
return Futures.immediateFuture(recommendationList);
});
// ... add forecasts and calculations ...
final ListenableFuture<List<List<Recommendation>>> filledRecommendations = Futures.successfulAsList(Arrays.asList(// Add Forecasts to Recommendations.
forecasts(recommendations), // Add Forecasts to Recommendations.
calculations(recommendations)));
// ... and transform the list into agent response with filled recommendations.
return Futures.transform(filledRecommendations, (AsyncFunction<List<List<Recommendation>>, AgentResponse>) input -> {
response.setRecommended(input.get(0));
return Futures.immediateFuture(response);
});
}
use of javax.ws.rs.GET in project jersey by jersey.
the class ObservableAgentResource method observable.
@GET
public void observable(@Suspended final AsyncResponse async) {
final long time = System.nanoTime();
final Queue<String> errors = new ConcurrentLinkedQueue<>();
Observable.just(new AgentResponse()).zipWith(visited(errors), (response, visited) -> {
response.setVisited(visited);
return response;
}).zipWith(recommended(errors), (response, recommendations) -> {
response.setRecommended(recommendations);
return response;
}).observeOn(Schedulers.io()).subscribe(response -> {
response.setProcessingTime((System.nanoTime() - time) / 1000000);
async.resume(response);
}, async::resume);
}
use of javax.ws.rs.GET in project jersey by jersey.
the class SyncAgentResource method sync.
@GET
public AgentResponse sync() {
final long time = System.nanoTime();
final AgentResponse response = new AgentResponse();
final Queue<String> errors = new ConcurrentLinkedQueue<>();
// Obtain visited destinations.
try {
response.setVisited(destination.path("visited").request().header("Rx-User", "Sync").get(new GenericType<List<Destination>>() {
}));
} catch (final Throwable throwable) {
errors.offer("Visited: " + throwable.getMessage());
}
// Obtain recommended destinations. (does not depend on visited ones)
List<Destination> recommended = Collections.emptyList();
try {
recommended = destination.path("recommended").request().header("Rx-User", "Sync").get(new GenericType<List<Destination>>() {
});
} catch (final Throwable throwable) {
errors.offer("Recommended: " + throwable.getMessage());
}
// Forecasts. (depend on recommended destinations)
final Map<String, Forecast> forecasts = new HashMap<>();
for (final Destination dest : recommended) {
try {
forecasts.put(dest.getDestination(), forecast.resolveTemplate("destination", dest.getDestination()).request().get(Forecast.class));
} catch (final Throwable throwable) {
errors.offer("Forecast: " + throwable.getMessage());
}
}
// Calculations. (depend on recommended destinations)
final Map<String, Calculation> calculations = new HashMap<>();
recommended.stream().forEach(destination -> {
try {
calculations.put(destination.getDestination(), calculation.resolveTemplate("from", "Moon").resolveTemplate("to", destination.getDestination()).request().get(Calculation.class));
} catch (final Throwable throwable) {
errors.offer("Calculation: " + throwable.getMessage());
}
});
// Recommendations.
final List<Recommendation> recommendations = new ArrayList<>(recommended.size());
for (final Destination dest : recommended) {
final Forecast fore = forecasts.get(dest.getDestination());
final Calculation calc = calculations.get(dest.getDestination());
recommendations.add(new Recommendation(dest.getDestination(), fore != null ? fore.getForecast() : "N/A", calc != null ? calc.getPrice() : -1));
}
// Do something with errors.
// ...
response.setRecommended(recommendations);
response.setProcessingTime((System.nanoTime() - time) / 1000000);
return response;
}
use of javax.ws.rs.GET in project jersey by jersey.
the class FormResource method getForm.
/**
* Produce a form from a static HTML file packaged with the compiled class
* @return a stream from which the HTML form can be read.
*/
@GET
public Response getForm() {
Date now = new Date();
InputStream entity = this.getClass().getClassLoader().getResourceAsStream("form.html");
return Response.ok(entity).cookie(new NewCookie("date", now.toString())).build();
}
use of javax.ws.rs.GET in project jersey by jersey.
the class SparklinesResource method smooth.
@Path("smooth")
@GET
public Response smooth(@DefaultValue("2") @QueryParam("step") final int step, @DefaultValue("true") @QueryParam("min-m") final boolean hasMin, @DefaultValue("true") @QueryParam("max-m") final boolean hasMax, @DefaultValue("true") @QueryParam("last-m") final boolean hasLast, @DefaultValue("blue") @QueryParam("min-color") final ColorParam minColor, @DefaultValue("green") @QueryParam("max-color") final ColorParam maxColor, @DefaultValue("red") @QueryParam("last-color") final ColorParam lastColor) {
final BufferedImage image = new BufferedImage(data.size() * step - 4, imageHeight, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = image.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.gray);
final int[] xs = new int[data.size()];
final int[] ys = new int[data.size()];
final int gap = 4;
final float d = (limits.width() + 1) / (float) (imageHeight - gap);
for (int i = 0, x = 0; i < data.size(); i++, x += step) {
final int v = data.get(i);
xs[i] = x;
ys[i] = imageHeight - 3 - (int) ((v - limits.lower()) / d);
}
g.drawPolyline(xs, ys, data.size());
if (hasMin) {
final int i = data.indexOf(Collections.min(data));
g.setColor(minColor);
g.fillRect(xs[i] - step / 2, ys[i] - step / 2, step, step);
}
if (hasMax) {
final int i = data.indexOf(Collections.max(data));
g.setColor(maxColor);
g.fillRect(xs[i] - step / 2, ys[i] - step / 2, step, step);
}
if (hasMax) {
g.setColor(lastColor);
g.fillRect(xs[xs.length - 1] - step / 2, ys[ys.length - 1] - step / 2, step, step);
}
return Response.ok(image).tag(tag).build();
}
Aggregations