Skip to content

Commit

Permalink
Remove EntityCreatedObserver and update code where needed
Browse files Browse the repository at this point in the history
Due to timing issues, the blocking observable could return before the observer has a non null response object. The result could then be "No Content" instead of "Created"
  • Loading branch information
tsegismont authored and Stefan Negrea committed Aug 13, 2015
1 parent 5b4f5b0 commit 1757463
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.noContent;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToAvailabilities;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToAvailabilityDataPoints;
Expand Down Expand Up @@ -48,8 +50,6 @@

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.filter.TenantFilter;
import org.hawkular.metrics.api.jaxrs.handler.observer.EntityCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.model.Availability;
import org.hawkular.metrics.api.jaxrs.model.AvailabilityDataPoint;
import org.hawkular.metrics.api.jaxrs.param.Duration;
Expand Down Expand Up @@ -114,11 +114,12 @@ public Response createAvailabilityMetric(
Metric<?> metric = new Metric<DataPoint<?>>(new MetricId(tenantId, AVAILABILITY, metricDefinition.getId()),
metricDefinition.getTags(), metricDefinition.getDataRetention());
try {
EntityCreatedObserver<MetricAlreadyExistsException> observer = new MetricCreatedObserver(location);
Observable<Void> observable = metricsService.createMetric(metric);
observable.subscribe(observer);
observable.toBlocking().lastOrDefault(null);
return observer.getResponse();
return Response.created(location).build();
} catch (MetricAlreadyExistsException e) {
String message = "A metric with name [" + e.getMetric().getId().getName() + "] already exists";
return Response.status(Response.Status.CONFLICT).entity(new ApiError(message)).build();
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import static org.hawkular.metrics.api.jaxrs.filter.TenantFilter.TENANT_HEADER_NAME;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToCounterDataPoints;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToCounters;
Expand All @@ -41,14 +43,13 @@
import javax.ws.rs.core.UriInfo;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.observer.EntityCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.model.Counter;
import org.hawkular.metrics.api.jaxrs.model.CounterDataPoint;
import org.hawkular.metrics.api.jaxrs.model.GaugeDataPoint;
import org.hawkular.metrics.api.jaxrs.request.MetricDefinition;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricAlreadyExistsException;
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.core.api.MetricsService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -105,13 +106,13 @@ public Response createCounter(
Metric<Double> metric = new Metric<>(new MetricId(tenantId, COUNTER, metricDefinition.getId()),
metricDefinition.getTags(), metricDefinition.getDataRetention());
URI location = uriInfo.getBaseUriBuilder().path("/counters/{id}").build(metric.getId().getName());

try {
EntityCreatedObserver<?> observer = new MetricCreatedObserver(location);
Observable<Void> observable = metricsService.createMetric(metric);
observable.subscribe(observer);
observable.toBlocking().lastOrDefault(null);
return observer.getResponse();
return Response.created(location).build();
} catch (MetricAlreadyExistsException e) {
String message = "A metric with name [" + e.getMetric().getId().getName() + "] already exists";
return Response.status(Response.Status.CONFLICT).entity(new ApiError(message)).build();
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import static org.hawkular.metrics.api.jaxrs.filter.TenantFilter.TENANT_HEADER_NAME;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.badRequest;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToGaugeDataPoints;
Expand Down Expand Up @@ -46,8 +48,6 @@
import javax.ws.rs.core.UriInfo;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.observer.EntityCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.model.Gauge;
import org.hawkular.metrics.api.jaxrs.model.GaugeDataPoint;
import org.hawkular.metrics.api.jaxrs.param.Duration;
Expand All @@ -58,6 +58,7 @@
import org.hawkular.metrics.core.api.BucketedOutput;
import org.hawkular.metrics.core.api.Buckets;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricAlreadyExistsException;
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.core.api.MetricsService;

Expand Down Expand Up @@ -107,13 +108,13 @@ public Response createGaugeMetric(
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, metricDefinition.getId()),
metricDefinition.getTags(), metricDefinition.getDataRetention());
URI location = uriInfo.getBaseUriBuilder().path("/gauges/{id}").build(metric.getId().getName());

try {
EntityCreatedObserver<?> observer = new MetricCreatedObserver(location);
Observable<Void> observable = metricsService.createMetric(metric);
observable.subscribe(observer);
observable.toBlocking().lastOrDefault(null);
return observer.getResponse();
return Response.created(location).build();
} catch (MetricAlreadyExistsException e) {
String message = "A metric with name [" + e.getMetric().getId().getName() + "] already exists";
return Response.status(Response.Status.CONFLICT).entity(new ApiError(message)).build();
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
import javax.ws.rs.core.UriInfo;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.observer.EntityCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.TenantCreatedObserver;
import org.hawkular.metrics.api.jaxrs.request.TenantParam;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.core.api.MetricsService;
import org.hawkular.metrics.core.api.Tenant;
import org.hawkular.metrics.core.api.TenantAlreadyExistsException;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
Expand Down Expand Up @@ -75,15 +74,15 @@ public Response createTenant(@ApiParam(required = true) TenantParam params,
@Context UriInfo uriInfo
) {
URI location = uriInfo.getBaseUriBuilder().path("/tenants").build();
EntityCreatedObserver<?> observer = new TenantCreatedObserver(location);
try {
Observable<Void> observable = metricsService.createTenant(new Tenant(params.getId()));
observable.subscribe(observer);
observable.toBlocking().lastOrDefault(null);
return observer.getResponse();
return Response.created(location).build();
} catch (TenantAlreadyExistsException e) {
String message = "A tenant with id [" + e.getTenantId() + "] already exists";
return Response.status(Response.Status.CONFLICT).entity(new ApiError(message)).build();
} catch (Exception e) {
Response response = observer.getResponse();
return response != null ? response : ApiUtils.serverError(e);
return ApiUtils.serverError(e);
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 1757463

Please sign in to comment.