Skip to content

Commit

Permalink
[HWKMETRICS-185] Adjust application structure to match json providers…
Browse files Browse the repository at this point in the history
… delivered with each application server.
  • Loading branch information
Stefan Negrea committed Aug 13, 2015
1 parent ac27d9b commit 20c8978
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Optional;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.ApiError;
Expand Down Expand Up @@ -52,12 +53,12 @@ private ApiUtils() {
}

public static Response collectionToResponse(Collection<?> collection) {
return collection.isEmpty() ? noContent() : Response.ok(collection).build();
return collection.isEmpty() ? noContent() : Response.ok(collection).type(MediaType.APPLICATION_JSON).build();
}

public static Response serverError(Throwable t, String message) {
String errorMsg = message + ": " + Throwables.getRootCause(t).getMessage();
return Response.serverError().entity(new ApiError(errorMsg)).build();
return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ApiError(errorMsg)).build();
}

public static Response serverError(Throwable t) {
Expand Down
10 changes: 10 additions & 0 deletions api/metrics-api-jaxrs-1.1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,34 @@
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.10.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
<version>1.0.2.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP4</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.3.9.Final-redhat-7</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.joining;

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.spi.StringConverter;

import com.google.common.collect.ImmutableBiMap;

/**
* A JAX-RS {@link ParamConverter} for {@link Duration} parameters.
*
* @author Thomas Segismont
*/
@Provider
public class DurationConverter implements StringConverter<Duration> {
private static final ImmutableBiMap<String, TimeUnit> STRING_UNITS = Duration.UNITS.inverse();
private static final Pattern REGEXP = Pattern.compile(
"(\\d+)"
+ "("
+ Duration.UNITS.values().stream().collect(joining("|"))
+ ")"
);

@Override
public Duration fromString(String value) {
Matcher matcher = REGEXP.matcher(value);
checkArgument(matcher.matches(), "Invalid duration %s", value);
return new Duration(Long.valueOf(matcher.group(1)), STRING_UNITS.get(matcher.group(2)));
}

@Override
public String toString(Duration duration) {
return duration.getValue() + Duration.UNITS.get(duration.getTimeUnit());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.joining;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.spi.StringConverter;

/**
* A JAX-RS {@link ParamConverter} for {@link Tags} parameters. The string format is a list of tags in the {@code
* name:value} form, comma-separated.
*
* @author Thomas Segismont
*/
@Provider
public class TagsConverter implements StringConverter<Tags> {

@Override
public Tags fromString(String value) {
if (value.isEmpty()) {
return new Tags(Collections.emptyMap());
}
checkArgument(!value.trim().isEmpty(), "Invalid tags: %s", value);
Map<String, String> tags = new HashMap<>();
String previousToken = null;
StringTokenizer tokenizer = new StringTokenizer(value, Tags.LIST_DELIMITER, true);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (previousToken == null) {
checkArgument(!Tags.LIST_DELIMITER.equals(token), "Invalid tags: %s", value);
} else {
checkArgument(!Tags.LIST_DELIMITER.equals(previousToken));
}
if (Tags.LIST_DELIMITER.equals(token)) {
previousToken = null;
continue;
}
int colonIndex = token.indexOf(Tags.TAG_DELIMITER);
checkArgument(hasExpectedForm(token, colonIndex), "Invalid tags: %s", value);
tags.put(token.substring(0, colonIndex), token.substring(colonIndex + 1));
previousToken = token;
}
return new Tags(tags);
}

private boolean hasExpectedForm(String token, int colonIndex) {
return colonIndex > 0 && colonIndex < token.length();
}

@Override
public String toString(Tags value) {
return value.getTags().entrySet().stream()
.map(e -> e.getKey() + Tags.TAG_DELIMITER + e.getValue())
.collect(joining(Tags.LIST_DELIMITER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
-->
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider"/>
<module name="org.jboss.resteasy.resteasy-jettison-provider"/>
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import"/>
<module name="org.jboss.resteasy.resteasy-jettison-provider" services="import"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
package org.hawkular.metrics.api.jaxrs.param;

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

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.joining;

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.Provider;

import com.google.common.collect.ImmutableBiMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
*/
package org.hawkular.metrics.api.jaxrs.param;

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

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.joining;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.Provider;

/**
* A JAX-RS {@link ParamConverter} for {@link Tags} parameters. The string format is a list of tags in the {@code
Expand Down

0 comments on commit 20c8978

Please sign in to comment.