org.zalando.riptide.Route Maven / Gradle / Ivy
package org.zalando.riptide;
import com.google.common.reflect.TypeToken;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMessage;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.zalando.fauxpas.ThrowingConsumer;
import org.zalando.fauxpas.ThrowingFunction;
import org.zalando.fauxpas.ThrowingRunnable;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import static com.google.common.base.Throwables.propagateIfPossible;
import static org.zalando.fauxpas.TryWith.tryWith;
/**
*
* @see RoutingTree
*/
@FunctionalInterface
public interface Route {
void execute(final ClientHttpResponse response, final MessageReader reader) throws Exception;
static Route call(final ThrowingRunnable extends Exception> runnable) {
return (response, reader) ->
tryWith(response, (ClientHttpResponse ignored) -> runnable.tryRun());
}
static Route call(final ThrowingConsumer consumer) {
return (response, reader) ->
tryWith(response, consumer);
}
static Route call(final Class type, final ThrowingConsumer consumer) {
return call(TypeToken.of(type), consumer);
}
static Route call(final TypeToken type, final ThrowingConsumer consumer) {
return (response, reader) -> {
final I body = reader.read(type, response);
consumer.tryAccept(body);
};
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static TypeToken> listOf(final Class entityType) {
return Types.listOf(entityType);
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static TypeToken> listOf(final TypeToken entityType) {
return Types.listOf(entityType);
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static TypeToken> responseEntityOf(final Class entityType) {
return Types.responseEntityOf(TypeToken.of(entityType));
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static TypeToken> responseEntityOf(final TypeToken entityType) {
return Types.responseEntityOf(entityType);
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static ThrowingConsumer pass() {
return response -> {
// nothing to do!
};
}
@Deprecated//(since = "2.5.1", forRemoval = true)
static ThrowingFunction headers() {
return HttpMessage::getHeaders;
}
@Deprecated//(since = "2.5.1", forRemoval = true)
static ThrowingFunction location() {
return response ->
response.getHeaders().getLocation();
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static ThrowingConsumer noRoute() {
return response -> {
throw new NoRouteException(response);
};
}
@Deprecated//(since = "2.6.0", forRemoval = true)
static ThrowingConsumer propagate() {
return entity -> {
propagateIfPossible(entity, IOException.class);
throw new IOException(entity);
};
}
@Deprecated//(since = "2.5.1", forRemoval = true)
static Adapter to(
final ThrowingFunction function) {
return consumer ->
response -> consumer.tryAccept(function.tryApply(response));
}
@Deprecated//(since = "2.5.1", forRemoval = true)
@FunctionalInterface
interface Adapter {
ThrowingConsumer andThen(final ThrowingConsumer consumer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy