-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from jsanda/HWKMETRICS-41
Do not return null from core API methods
- Loading branch information
Showing
24 changed files
with
859 additions
and
923 deletions.
There are no files selected for viewing
514 changes: 257 additions & 257 deletions
514
api/metrics-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/MetricHandler.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
api/metrics-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/util/ApiUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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.util; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import javax.ws.rs.container.AsyncResponse; | ||
import javax.ws.rs.core.Response; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Throwables; | ||
import com.google.common.util.concurrent.FutureCallback; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import org.hawkular.metrics.api.jaxrs.ApiError; | ||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
public class ApiUtils { | ||
|
||
private ApiUtils() { | ||
} | ||
|
||
public static final Function<Void, Response> MAP_VOID = v -> Response.ok().build(); | ||
|
||
public static final Function<Optional<?>, Response> MAP_VALUE = optional -> | ||
optional.map(value -> Response.ok(value).build()).orElse(noContent()); | ||
|
||
public static final Function<Collection<?>, Response> MAP_COLLECTION = collection -> | ||
collection.isEmpty() ? noContent() : Response.ok(collection).build(); | ||
|
||
public static final Function<Map<?, ?>, Response> MAP_MAP = map -> | ||
map.isEmpty() ? noContent() : Response.ok(map).build(); | ||
|
||
public static Response noContent() { | ||
return Response.noContent().build(); | ||
} | ||
|
||
public static ListenableFuture<Response> emptyPayload() { | ||
return badRequest(new ApiError("Payload is empty")); | ||
} | ||
|
||
public static ListenableFuture<Response> badRequest(ApiError error) { | ||
return Futures.immediateFuture(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); | ||
} | ||
|
||
public static void executeAsync(AsyncResponse asyncResponse, | ||
java.util.function.Supplier<ListenableFuture<Response>> supplier) { | ||
ListenableFuture<Response> future = supplier.get(); | ||
Futures.addCallback(future, new FutureCallback<Response>() { | ||
@Override | ||
public void onSuccess(Response response) { | ||
asyncResponse.resume(response); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t) { | ||
String msg = "Failed to perform operation due to an error: " + Throwables.getRootCause(t).getMessage(); | ||
asyncResponse.resume(Response.serverError().entity(new ApiError(msg)).build()); | ||
} | ||
}); | ||
} | ||
|
||
public static Map<String, String> decodeTags(String tags) { | ||
return Arrays.stream(tags.split(",")) | ||
.map(s -> s.split(":")) | ||
.collect(toMap(a -> a[0], a -> a[1])); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.