brooklyn.event.feed.http.HttpValueFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-core Show documentation
Show all versions of brooklyn-core Show documentation
Entity implementation classes, events, and other core elements
package brooklyn.event.feed.http;
import java.util.List;
import javax.annotation.Nullable;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import com.google.gson.JsonElement;
public class HttpValueFunctions {
private HttpValueFunctions() {} // instead use static utility methods
public static Function responseCode() {
return new Function() {
@Override public Integer apply(HttpPollValue input) {
return input.getResponseCode();
}
};
}
public static Function responseCodeEquals(final int expected) {
return chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.equalTo(expected)));
}
public static Function responseCodeEquals(final int... expected) {
List expectedList = Lists.newArrayList();
for (int e : expected) {
expectedList.add((Integer)e);
}
return chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.in(expectedList)));
}
public static Function stringContentsFunction() {
return new Function() {
@Override public String apply(HttpPollValue input) {
// TODO Charset?
return new String(input.getContent());
}
};
}
public static Function jsonContents() {
return chain(stringContentsFunction(), JsonFunctions.asJson());
}
public static Function jsonContents(String element, Class expected) {
return jsonContents(new String[] {element}, expected);
}
public static Function jsonContents(String[] elements, Class expected) {
return chain(jsonContents(), JsonFunctions.walk(elements), JsonFunctions.cast(expected));
}
public static Function latency() {
return new Function() {
public Long apply(HttpPollValue input) {
return input.getLatencyFullContent();
}
};
}
public static Function chain(final Function f1, final Function f2) {
return new Function() {
@Override public C apply(@Nullable A input) {
return f2.apply(f1.apply(input));
}
};
}
public static Function chain(final Function f1, final Function f2, final Function f3) {
return new Function() {
@Override public D apply(@Nullable A input) {
return f3.apply(f2.apply(f1.apply(input)));
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy