Search in sources :

Example 81 with GET

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);
}
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) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) GET(javax.ws.rs.GET)

Example 82 with GET

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;
}
Also used : Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) GET(javax.ws.rs.GET)

Example 83 with GET

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();
}
Also used : InputStream(java.io.InputStream) Date(java.util.Date) NewCookie(javax.ws.rs.core.NewCookie) GET(javax.ws.rs.GET)

Example 84 with GET

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();
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 85 with GET

use of javax.ws.rs.GET in project jersey by jersey.

the class MessageStreamResource method getMessageStream.

/**
     * Get the new SSE message stream channel.
     *
     * @return new SSE message stream channel.
     */
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getMessageStream() {
    LOGGER.info("--> SSE connection received.");
    final EventOutput eventOutput = new EventOutput();
    broadcaster.add(eventOutput);
    return eventOutput;
}
Also used : EventOutput(org.glassfish.jersey.media.sse.EventOutput) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

GET (javax.ws.rs.GET)1140 Path (javax.ws.rs.Path)902 Produces (javax.ws.rs.Produces)734 ApiOperation (io.swagger.annotations.ApiOperation)230 ApiResponses (io.swagger.annotations.ApiResponses)163 IOException (java.io.IOException)139 Response (javax.ws.rs.core.Response)116 WebApplicationException (javax.ws.rs.WebApplicationException)113 Timed (com.codahale.metrics.annotation.Timed)103 ArrayList (java.util.ArrayList)100 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)91 List (java.util.List)83 Map (java.util.Map)80 HashMap (java.util.HashMap)78 URI (java.net.URI)70 TimedResource (org.killbill.commons.metrics.TimedResource)58 TenantContext (org.killbill.billing.util.callcontext.TenantContext)57 ApiResponse (io.swagger.annotations.ApiResponse)52 NotFoundException (org.apache.hadoop.yarn.webapp.NotFoundException)39 Consumes (javax.ws.rs.Consumes)36