com.spotify.apollo.entity.EntityMiddleware Maven / Gradle / Ivy
/*
* -\-\-
* Spotify Apollo Entity Middleware
* --
* Copyright (C) 2013 - 2016 Spotify AB
* --
* 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 com.spotify.apollo.entity;
import com.spotify.apollo.Exploratory;
import com.spotify.apollo.RequestContext;
import com.spotify.apollo.Response;
import com.spotify.apollo.route.AsyncHandler;
import com.spotify.apollo.route.Middleware;
import com.spotify.apollo.route.SyncHandler;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import okio.ByteString;
/**
* Apollo {@link Middleware}s for route handlers that work with a typed entity.
*/
@Exploratory
public interface EntityMiddleware {
static EntityMiddleware forCodec(EntityCodec codec) {
return new CodecEntityMiddleware(codec);
}
static EntityMiddleware forCodec(EntityCodec codec, String contentType) {
return new CodecEntityMiddleware(codec, contentType);
}
Middleware, SyncHandler>>
direct(Class extends E> requestEntityClass);
Middleware, SyncHandler>>
direct(Class extends E> requestEntityClass, Class extends R> responseEntityClass);
Middleware, SyncHandler>>
response(Class extends E> requestEntityClass);
Middleware, SyncHandler>>
response(Class extends E> requestEntityClass, Class extends R> responseEntityClass);
Middleware, AsyncHandler>>
asyncDirect(Class extends E> requestEntityClass);
Middleware, AsyncHandler>>
asyncDirect(Class extends E> requestEntityClass, Class extends R> responseEntityClass);
Middleware, AsyncHandler>>
asyncResponse(Class extends E> requestEntityClass);
Middleware, AsyncHandler>>
asyncResponse(Class extends E> requestEntityClass, Class extends R> responseEntityClass);
Middleware, SyncHandler>>
serializerDirect(Class extends R> responseEntityClass);
Middleware>, SyncHandler>>
serializerResponse(Class extends R> responseEntityClass);
Middleware, AsyncHandler>>
asyncSerializerDirect(Class extends R> responseEntityClass);
Middleware>, AsyncHandler>>
asyncSerializerResponse(Class extends R> responseEntityClass);
/**
* A common handler interface for the various entity handlers. This embodies the curried form
* of the handlers so that the actual handler interfaces can have more readable definitions.
*
* @param The handler request entity type
* @param The handler response entity type
*/
@FunctionalInterface
interface CurriedHandler
extends Function> {
}
@FunctionalInterface
interface EntityHandler extends CurriedHandler {
default EntityResponseHandler asResponseHandler() {
return rc -> e -> apply(rc).andThen(Response::forPayload).apply(e);
}
}
@FunctionalInterface
interface EntityResponseHandler extends CurriedHandler> {
}
@FunctionalInterface
interface EntityAsyncHandler extends CurriedHandler> {
default EntityAsyncResponseHandler asResponseHandler() {
return rc -> e -> apply(rc).andThen(s -> s.thenApply(Response::forPayload)).apply(e);
}
}
@FunctionalInterface
interface EntityAsyncResponseHandler
extends CurriedHandler>> {
}
}