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

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(Codec codec) {
    return new CodecEntityMiddleware(codec);
  }

  /**
   * @deprecated Use {@link #forCodec(Codec)}
   */
  @Deprecated
  static EntityMiddleware forCodec(EntityCodec codec) {
    return new CodecEntityMiddleware(new CodecAdapter(codec), codec.defaultContentType());
  }

  /**
   * @deprecated Use {@link #forCodec(Codec)}
   */
  @Deprecated
  static EntityMiddleware forCodec(EntityCodec codec, String contentType) {
    return new CodecEntityMiddleware(new CodecAdapter(codec), contentType);
  }

   Middleware, SyncHandler>>
  direct(Class requestEntityClass);

   Middleware, SyncHandler>>
  direct(Class requestEntityClass, Class responseEntityClass);

   Middleware, SyncHandler>>
  response(Class requestEntityClass);

   Middleware, SyncHandler>>
  response(Class requestEntityClass, Class responseEntityClass);

   Middleware, AsyncHandler>>
  asyncDirect(Class requestEntityClass);

   Middleware, AsyncHandler>>
  asyncDirect(Class requestEntityClass, Class responseEntityClass);

   Middleware, AsyncHandler>>
  asyncResponse(Class requestEntityClass);

   Middleware, AsyncHandler>>
  asyncResponse(Class requestEntityClass, Class responseEntityClass);

   Middleware, SyncHandler>>
  serializerDirect(Class responseEntityClass);

   Middleware>, SyncHandler>>
  serializerResponse(Class responseEntityClass);

   Middleware, AsyncHandler>>
  asyncSerializerDirect(Class responseEntityClass);

   Middleware>, AsyncHandler>>
  asyncSerializerResponse(Class 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>> {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy