Skip to content

Commit

Permalink
Cleanup response types from Response since only JSON is supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Negrea committed Mar 17, 2015
1 parent 65b7e16 commit e6f12f7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
*/
package org.hawkular.metrics.api.jaxrs;

import javax.xml.bind.annotation.XmlRootElement;

import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;

/**
* Return information what failed in the REST-call.
* @author Michael Burman
*/
@XmlRootElement
@ApiModel(description = "If REST-call returns other than success, detailed error is returned.")
public class ApiError {
private String errorMsg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static org.hawkular.metrics.core.api.MetricsService.DEFAULT_TENANT_ID;

import java.net.URI;
Expand Down Expand Up @@ -144,8 +143,7 @@ public void createNumericMetric(@Suspended final AsyncResponse asyncResponse,
public void createAvailabilityMetric(@Suspended final AsyncResponse asyncResponse,
@PathParam("tenantId") String tenantId,
@ApiParam(required = true) AvailabilityMetric metric,
@Context UriInfo uriInfo
) {
@Context UriInfo uriInfo) {
if (metric == null) {
Response response = Response.status(Status.BAD_REQUEST).entity(new ApiError("Payload is empty")).build();
asyncResponse.resume(response);
Expand Down Expand Up @@ -315,7 +313,7 @@ public void addNumericData(@Suspended final AsyncResponse asyncResponse,
@ApiParam(value = "List of metrics", required = true)
List<NumericMetric> metrics) {
if (metrics.isEmpty()) {
asyncResponse.resume(Response.ok().type(APPLICATION_JSON_TYPE).build());
asyncResponse.resume(Response.ok().build());
}

for (NumericMetric metric : metrics) {
Expand All @@ -337,7 +335,7 @@ public void addAvailabilityData(@Suspended final AsyncResponse asyncResponse,
@PathParam("tenantId") String tenantId, @ApiParam(value = "List of availability metrics", required = true)
List<AvailabilityMetric> metrics) {
if (metrics.isEmpty()) {
asyncResponse.resume(Response.ok().type(APPLICATION_JSON_TYPE).build());
asyncResponse.resume(Response.ok().build());
}

for (AvailabilityMetric metric : metrics) {
Expand Down Expand Up @@ -659,7 +657,7 @@ public void onSuccess(List<Counter> counters) {
asyncResponse.resume(Response.status(404).entity("Counter[group: " + group + ", name: " +
counter + "] not found").build());
} else {
Response jaxrs = Response.ok(counters.get(0)).type(APPLICATION_JSON_TYPE).build();
Response jaxrs = Response.ok(counters.get(0)).build();
asyncResponse.resume(jaxrs);
}
}
Expand Down Expand Up @@ -692,8 +690,7 @@ public void findMetrics(
metricType = MetricType.fromTextCode(type);
} catch (IllegalArgumentException e) {
ApiError errors = new ApiError("[" + type + "] is not a valid type. Accepted values are num|avail|log");
asyncResponse
.resume(Response.status(Status.BAD_REQUEST).entity(errors).type(APPLICATION_JSON_TYPE).build());
asyncResponse.resume(Response.status(Status.BAD_REQUEST).entity(errors).build());
}
ListenableFuture<List<Metric<?>>> future = metricsService.findMetrics(tenantId, metricType);
Futures.addCallback(future, new SimpleDataCallback<List<Metric<?>>>(asyncResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.hawkular.metrics.api.jaxrs;

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

import java.net.URI;
import java.util.Collection;
Expand Down Expand Up @@ -116,16 +115,15 @@ public void onSuccess(Collection<Tenant> tenants) {
response.resume(Response.ok().status(Status.NO_CONTENT).build());
return;
}
response.resume(Response.status(Status.OK).entity(tenants).type(APPLICATION_JSON_TYPE).build());
response.resume(Response.status(Status.OK).entity(tenants).build());
}

@Override
public void onFailure(Throwable t) {
ApiError errors = new ApiError(
"Failed to fetch tenants due to an "
+ "unexpected error: " + Throwables.getRootCause(t).getMessage());
response.resume(Response.status(Status.INTERNAL_SERVER_ERROR).entity(errors)
.type(APPLICATION_JSON_TYPE).build());
response.resume(Response.status(Status.INTERNAL_SERVER_ERROR).entity(errors).build());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package org.hawkular.metrics.api.jaxrs.callback;

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

import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

Expand All @@ -43,7 +40,7 @@ public NoDataCallback(AsyncResponse response) {

@Override
public void onSuccess(Object result) {
response.resume(Response.ok().type(MediaType.APPLICATION_JSON_TYPE).build());
response.resume(Response.ok().build());
}

@Override
Expand All @@ -52,15 +49,14 @@ public void onFailure(Throwable t) {
ApiError errors = new ApiError(
"A metric input id already exists " + ":"
+ Throwables.getRootCause(t).getMessage());
response.resume(Response.status(Status.BAD_REQUEST).entity(errors).type(APPLICATION_JSON_TYPE).build());
response.resume(Response.status(Status.BAD_REQUEST).entity(errors).build());
} else if (t instanceof NoResultsException) {
response.resume(Response.ok().status(Status.NO_CONTENT).build());
} else {
ApiError errors = new ApiError(
"Failed to perform operation due to an error: "
+ Throwables.getRootCause(t).getMessage());
response.resume(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errors)
.type(MediaType.APPLICATION_JSON_TYPE).build());
response.resume(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errors).build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.hawkular.metrics.api.jaxrs.callback;

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

import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
Expand All @@ -31,9 +29,9 @@ public SimpleDataCallback(AsyncResponse response) {
@Override
public void onSuccess(Object responseData) {
if (responseData == null) {
response.resume(Response.status(Status.NO_CONTENT).type(APPLICATION_JSON_TYPE).build());
response.resume(Response.status(Status.NO_CONTENT).build());
} else {
response.resume(Response.ok(responseData).type(APPLICATION_JSON_TYPE).build());
response.resume(Response.ok(responseData).build());
}
}
}

0 comments on commit e6f12f7

Please sign in to comment.