-
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.
[HWKMETRICS-185] Adjust application structure to match json providers…
… delivered with each application server.
- Loading branch information
Stefan Negrea
committed
Aug 13, 2015
1 parent
ac27d9b
commit 20c8978
Showing
9 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
...s-api-jaxrs-1.1/src/main/java/org/hawkular/metrics/api/jaxrs/param/DurationConverter.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,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()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...trics-api-jaxrs-1.1/src/main/java/org/hawkular/metrics/api/jaxrs/param/TagsConverter.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,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)); | ||
} | ||
} |
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
File renamed without changes.
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
File renamed without changes.
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