Skip to content

Commit

Permalink
feat: add command/reply adapters to manage command migration
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumelamirand committed Feb 15, 2024
1 parent f61e3e0 commit 23b4605
Show file tree
Hide file tree
Showing 54 changed files with 1,046 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import io.gravitee.exchange.api.channel.exception.ChannelTimeoutException;
import io.gravitee.exchange.api.channel.exception.ChannelUnknownCommandException;
import io.gravitee.exchange.api.command.Command;
import io.gravitee.exchange.api.command.CommandAdapter;
import io.gravitee.exchange.api.command.CommandHandler;
import io.gravitee.exchange.api.command.Reply;
import io.gravitee.exchange.api.command.ReplyHandler;
import io.gravitee.exchange.api.command.ReplyAdapter;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import java.util.List;
import java.util.Map;

/**
* @author Guillaume LAMIRAND (guillaume.lamirand at graviteesource.com)
Expand Down Expand Up @@ -78,8 +78,13 @@ public interface Channel {
* Add customs {@link CommandHandler} to this channel
*/
void addCommandHandlers(final List<CommandHandler<? extends Command<?>, ? extends Reply<?>>> commandHandlers);

/**
* Add customs {@link CommandAdapter} to this channel
*/
void addCommandAdapters(final List<CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> commandAdapters);
/**
* Add customs {@link Reply} to this channel
* Add customs {@link ReplyAdapter} to this channel
*/
void addReplyHandlers(final List<ReplyHandler<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> replyHandlers);
void addReplyAdapters(final List<ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> replyAdapters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,35 @@
package io.gravitee.exchange.api.command;

import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleSource;

/**
* @author Guillaume LAMIRAND (guillaume.lamirand at graviteesource.com)
* @author GraviteeSource Team
*/
@FunctionalInterface
public interface ReplyHandler<C extends Command<?>, D extends Command<?>, R extends Reply<?>> {
public interface CommandAdapter<C1 extends Command<?>, C2 extends Command<?>, R extends Reply<?>> {
/**
* Returns the type of command handled by this reply handler.
* Returns the type of command supported by this adapter.
*
* @return the type of command handled.
* @return the type of command supported.
*/
String handleType();
String supportType();

/**
* Method invoked before sending the command in order to fill extra information into the command.
* Method invoked before sending the command.
* @return the command with all necessary information.
* @return the command adapted
*/
default Single<D> decorate(C command) {
return (Single<D>) Single.just(command);
default Single<C2> adapt(C1 command) {
return (Single<C2>) Single.just(command);
}

/**
* Method invoke after a reply has been received.
*
* @param reply the reply.
* @return the same reply altered if necessary.
*/
default Single<R> handle(R reply) {
return Single.just(reply);
}

/**
* Method invoke when an error is raised when dealing with a reply.
* Method invoke when an error is raised when sending a command.
*
* @param throwable a throwable
*/
default Single<R> handleError(final C command, final Throwable throwable) {
default Single<R> onError(final Command<?> command, final Throwable throwable) {
return Single.error(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
public interface CommandHandler<C extends Command<?>, R extends Reply<?>> {
/**
* Returns the type of command handled by this command handler.
* Returns the type of command supported by this command handler.
* The type is used to determine the right handler to use when a command need to be handled.
* @return the type of command handled.
* @return the type of command supported.
*/
String handleType();
String supportType();

/**
* Method invoked when a command of the expected type is received.
Expand All @@ -36,6 +36,6 @@ public interface CommandHandler<C extends Command<?>, R extends Reply<?>> {
* @return the reply with a status indicating if the command has been successfully handled or not.
*/
default Single<R> handle(C command) {
return Single.error(new RuntimeException("Handle command of type " + handleType() + " is not implemented"));
return Single.error(new RuntimeException("Handle command of type " + supportType() + " is not implemented"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,8 @@
package io.gravitee.exchange.api.command;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.gravitee.exchange.api.command.goodbye.GoodByeCommand;
import io.gravitee.exchange.api.command.goodbye.GoodByeReply;
import io.gravitee.exchange.api.command.healtcheck.HealthCheckCommand;
import io.gravitee.exchange.api.command.healtcheck.HealthCheckReply;
import io.gravitee.exchange.api.command.hello.HelloCommand;
import io.gravitee.exchange.api.command.hello.HelloReply;
import io.gravitee.exchange.api.command.noreply.NoReply;
import io.gravitee.exchange.api.command.primary.PrimaryCommand;
import io.gravitee.exchange.api.command.primary.PrimaryReply;
import io.gravitee.exchange.api.command.unknown.UnknownCommand;
import io.gravitee.exchange.api.command.unknown.UnknownReply;
import io.gravitee.exchange.api.websocket.protocol.legacy.IgnoredReply;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.exchange.api.command;

import io.reactivex.rxjava3.core.Single;

/**
* @author Guillaume LAMIRAND (guillaume.lamirand at graviteesource.com)
* @author GraviteeSource Team
*/
@FunctionalInterface
public interface ReplyAdapter<R1 extends Reply<?>, R2 extends Reply<?>> {
/**
* Returns the type of reply handled by this adapter
*
* @return the type of reply supported.
*/
String supportType();

/**
* Method invoked when receiving the reply
* @return the reply adapted
*/
default Single<R2> adapt(R1 reply) {
return (Single<R2>) Single.just(reply);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public HelloCommand(final HelloCommandPayload helloCommandPayload) {
this();
this.payload = helloCommandPayload;
}

public HelloCommand(final String id, final HelloCommandPayload helloCommandPayload) {
this();
this.id = id;
this.payload = helloCommandPayload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class UnknownCommandHandler implements CommandHandler<UnknownCommand, UnknownReply> {

@Override
public String handleType() {
public String supportType() {
return UnknownCommand.COMMAND_TYPE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
package io.gravitee.exchange.api.connector;

import io.gravitee.exchange.api.command.Command;
import io.gravitee.exchange.api.command.CommandAdapter;
import io.gravitee.exchange.api.command.CommandHandler;
import io.gravitee.exchange.api.command.Reply;
import io.gravitee.exchange.api.command.ReplyHandler;
import io.gravitee.exchange.api.command.ReplyAdapter;
import java.util.List;

public interface ConnectorCommandHandlersFactory {
/**
* Build a map of command handlers dedicated to the specified context.
* Build a list of command handlers dedicated to the specified context.
*
* @param connectorCommandContext the command context
* @return a list of command handlers
Expand All @@ -33,12 +34,20 @@ public interface ConnectorCommandHandlersFactory {
);

/**
* Build a map of reply handlers dedicated to the specified context.
* Build a list of command decorators dedicated to the specified context.
*
* @param connectorCommandContext the command context
* @return a list of command handlers
* @return a list of command decorators
*/
List<ReplyHandler<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> buildReplyHandlers(
List<CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> buildCommandAdapters(
final ConnectorCommandContext connectorCommandContext
);

/**
* Build a list of command decorators dedicated to the specified context.
*
* @param connectorCommandContext the command context
* @return a list of command decorators
*/
List<ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> buildReplyAdapters(final ConnectorCommandContext connectorCommandContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@
package io.gravitee.exchange.api.controller;

import io.gravitee.exchange.api.command.Command;
import io.gravitee.exchange.api.command.CommandAdapter;
import io.gravitee.exchange.api.command.CommandHandler;
import io.gravitee.exchange.api.command.Reply;
import io.gravitee.exchange.api.command.ReplyHandler;
import io.gravitee.exchange.api.command.ReplyAdapter;
import io.gravitee.exchange.api.connector.ConnectorCommandContext;
import java.util.List;
import java.util.Map;

public interface ControllerCommandHandlersFactory {
/**
* Build a map of command handlers dedicated to the specified context.
*
* @param controllerCommandContext the command context
* @return a map of command handlers indexed by type.
* @return a list of command handlers indexed by type.
*/
List<CommandHandler<? extends Command<?>, ? extends Reply<?>>> buildCommandHandlers(
final ControllerCommandContext controllerCommandContext
);

/**
* Build a map of reply handlers dedicated to the specified context.
* Build a list of command decorators dedicated to the specified context.
*
* @param controllerCommandContext the command context
* @return a map of command handlers indexed by type.
* @return a list of command decorators
*/
List<ReplyHandler<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> buildReplyHandlers(
List<CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> buildCommandAdapters(
final ControllerCommandContext controllerCommandContext
);

/**
* Build a list of command decorators dedicated to the specified context.
*
* @param controllerCommandContext the command context
* @return a list of command decorators
*/
List<ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> buildReplyAdapters(final ControllerCommandContext controllerCommandContext);
}
Loading

0 comments on commit 23b4605

Please sign in to comment.