Skip to content

Commit

Permalink
HWKMETRICS-154 List metrics endpoint fails with 500 Internal Server E…
Browse files Browse the repository at this point in the history
…rror when "type" param is missing

Fixed MetricHandler to make sure the param is not null.

Also, MetricType now has a JAX-RS converter which makes the code cleaner.
  • Loading branch information
tsegismont committed Jun 23, 2015
1 parent c8af239 commit 5e88a31
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.executeAsync;

import java.util.ArrayList;
Expand Down Expand Up @@ -70,33 +71,32 @@ public class MetricHandler {
@GET
@Path("/")
@ApiOperation(value = "Find tenant's metric definitions.", notes = "Does not include any metric values. ",
response = List.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved at least one metric "
+ "definition."),
@ApiResponse(code = 204, message = "No metrics found."),
@ApiResponse(code = 400, message = "Given type is not a valid type.", response = ApiError.class),
response = List.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved at least one metric definition.",
response = ApiError.class),
@ApiResponse(code = 204, message = "No metrics found.",
response = ApiError.class),
@ApiResponse(code = 400, message = "Missing or invalid type parameter type.", response = ApiError.class),
@ApiResponse(code = 500, message = "Failed to retrieve metrics due to unexpected error.",
response = ApiError.class)
})
public void findMetrics(
@Suspended final AsyncResponse asyncResponse,
@ApiParam(
value = "Queried metric type",
required = true,
allowableValues = "[gauge, availability, counter]")
@QueryParam("type") String type) {

try {
metricsService.findMetrics(tenantId, MetricType.fromTextCode(type))
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));

} catch (IllegalArgumentException e) {
ApiError error = new ApiError("[" + type + "] is not a valid type. Accepted values are gauge|avail|log");
asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).entity(error).build());
@ApiParam(value = "Queried metric type",
required = true,
allowableValues = "[gauge, availability, counter]")
@QueryParam("type") MetricType metricType
) {
if (metricType == null) {
asyncResponse.resume(badRequest(new ApiError("Missing type param")));
return;
}
metricsService.findMetrics(tenantId, metricType)
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

import org.hawkular.metrics.core.api.MetricType;

import com.google.common.collect.ImmutableMap;

/**
Expand All @@ -39,6 +41,7 @@ public ConvertersProvider() {
paramConverters = paramConvertersBuilder
.put(Duration.class, new DurationConverter())
.put(Tags.class, new TagsConverter())
.put(MetricType.class, new MetricTypeConverter())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.api.jaxrs.param;

import javax.ws.rs.ext.ParamConverter;

import org.hawkular.metrics.core.api.MetricType;

/**
* A JAX-RS {@link ParamConverter} for {@link MetricType} parameters.
*
* @author Thomas Segismont
*/
public class MetricTypeConverter implements ParamConverter<MetricType> {

@Override
public MetricType fromString(String s) {
return MetricType.fromTextCode(s);
}

@Override
public String toString(MetricType metricType) {
return metricType.getText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.param;

import static java.util.stream.Collectors.joining;

import java.util.Arrays;

import org.hawkular.metrics.core.api.MetricType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* @author Thomas Segismont
*/
public class MetricTypeConverterTest {

@Rule
public final ExpectedException expectedException = ExpectedException.none();

@Test
public void shouldThrowIllegalArgumentExceptionWithInvalidText() throws Exception {
expectedException.expect(IllegalArgumentException.class);

String invalidText = Arrays.stream(MetricType.values()).map(MetricType::getText).collect(joining("."));
new MetricTypeConverter().fromString(invalidText);
}
}

0 comments on commit 5e88a31

Please sign in to comment.