All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.riptide.Route Maven / Gradle / Ivy

package org.zalando.riptide;

/*
 * ⁣​
 * Riptide
 * ⁣⁣
 * Copyright (C) 2015 - 2016 Zalando SE
 * ⁣⁣
 * 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.
 * ​⁣
 */

import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMessage;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.net.URI;
import java.util.List;

@FunctionalInterface
public interface Route {

    void execute(final ClientHttpResponse response, final MessageReader reader) throws Exception;

    static Route call(final ThrowingRunnable consumer) {
        return (response, reader) ->
                consumer.run();
    }

    static Route call(final ThrowingConsumer consumer) {
        return (response, reader) ->
                consumer.accept(response);
    }

    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) ->
                consumer.accept(reader.read(type, response));
    }

    static  TypeToken> listOf(final Class entityType) {
        return listOf(TypeToken.of(entityType));
    }

    @SuppressWarnings("serial")
    static  TypeToken> listOf(final TypeToken entityType) {
        final TypeToken> listType = new TypeToken>() {
        };

        final TypeParameter elementType = new TypeParameter() {
        };

        return listType.where(elementType, entityType);
    }

    static ThrowingConsumer pass() {
        return response -> {

        };
    }

    static ThrowingFunction headers() {
        return HttpMessage::getHeaders;
    }

    static ThrowingFunction location() {
        return response ->
                response.getHeaders().getLocation();
    }

    static  ThrowingConsumer propagate() {
        return entity -> {
            if (entity instanceof IOException) {
                throw (IOException) entity;
            } else {
                throw new IOException(entity);
            }
        };
    }

    static  Adapter to(final ThrowingFunction function) {
        return consumer ->
                response -> consumer.accept(function.apply(response));
    }

    @FunctionalInterface
    interface Adapter {
        ThrowingConsumer andThen(final ThrowingConsumer consumer);
    }

}