-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for Event and AdminEvent subscriptions
- Loading branch information
blevine
committed
Nov 2, 2024
1 parent
9d0f804
commit 84bdfab
Showing
11 changed files
with
353 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
graphql/src/main/java/net/brianlevine/keycloak/graphql/SubscriptionServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package net.brianlevine.keycloak.graphql; | ||
|
||
|
||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.core.http.HttpServerOptions; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.handler.graphql.ApolloWSHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
public class SubscriptionServer extends AbstractVerticle { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionServer.class); | ||
private static final int DEFAULT_PORT = 8081; | ||
|
||
@Override | ||
public void start() { | ||
|
||
String sPort = System.getenv("SUBSCRIPTION_PORT"); | ||
int port = (sPort != null) ? Integer.parseInt(sPort) : DEFAULT_PORT; | ||
|
||
LOGGER.info("Starting SubscriptionServer on port {}...", port); | ||
|
||
Router router = Router.router(vertx); | ||
Map<String, Object> context = new HashMap<>(); | ||
context.put("vertex", getVertx()); | ||
|
||
//noinspection deprecation | ||
ApolloWSHandler h = ApolloWSHandler.create(GraphQLController.getSchema()).beforeExecute((a) -> { | ||
a.builder().graphQLContext(context); | ||
}); | ||
router.route("/graphql").handler(h); | ||
|
||
HttpServerOptions httpServerOptions = new HttpServerOptions() | ||
.addWebSocketSubProtocol("graphql-ws") | ||
.setLogActivity(true); | ||
|
||
vertx.createHttpServer(httpServerOptions) | ||
.requestHandler(router) | ||
.listen(port, "0.0.0.0") | ||
.onComplete( | ||
(e) -> LOGGER.info("SubscriptionServer started on port {}", e.actualPort()), | ||
(t) -> LOGGER.error("SubscriptionServer FAILED to start: ", t) | ||
).onFailure((e) -> LOGGER.error("SubscriptionServer FAILED to start: ", e)); | ||
} | ||
|
||
// private GraphQL createGraphQL() { | ||
// String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString(); | ||
// | ||
// SchemaParser schemaParser = new SchemaParser(); | ||
// TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema); | ||
// | ||
// RuntimeWiring runtimeWiring = newRuntimeWiring() | ||
// .type("Subscription", builder -> builder.dataFetcher("links", this::linksFetcher)) | ||
// .build(); | ||
// | ||
// SchemaGenerator schemaGenerator = new SchemaGenerator(); | ||
// GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); | ||
// | ||
// return GraphQL.newGraphQL(graphQLSchema) | ||
// .build(); | ||
// } | ||
|
||
|
||
|
||
|
||
// private Publisher<Link> linksFetcher(DataFetchingEnvironment env) { | ||
// return Flowable.interval(1, TimeUnit.SECONDS) // Ticks | ||
// .zipWith(Flowable.fromIterable(links), (tick, link) -> link) // Emit link on each tick | ||
// .observeOn(RxHelper.scheduler(context)); // Observe on the verticle context thread | ||
// } | ||
} |
62 changes: 62 additions & 0 deletions
62
...src/main/java/net/brianlevine/keycloak/graphql/events/MulticastEventListenerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.brianlevine.keycloak.graphql.events; | ||
|
||
import io.reactivex.rxjava3.processors.MulticastProcessor; | ||
import org.jboss.logging.Logger; | ||
import org.keycloak.events.Event; | ||
import org.keycloak.events.EventListenerProvider; | ||
import org.keycloak.events.admin.AdminEvent; | ||
|
||
/** | ||
* An EventListenerProvider that sends Keycloak Events and AdminEvents to | ||
* a reactive MulticaseProcessor which can be subscribed to to receive those events. | ||
* This is used to implement the server side for Event and AdminEvent GraphQL subscriptions. | ||
*/ | ||
public class MulticastEventListenerProvider | ||
implements EventListenerProvider { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(MulticastEventListenerProvider.class); | ||
|
||
private final MulticastProcessor<Event> eventMulticastProcessor; | ||
private final MulticastProcessor<AdminEvent> adminEventMulticastProcessor; | ||
|
||
public MulticastEventListenerProvider(MulticastProcessor<Event> eventMulticastProcessor, MulticastProcessor<AdminEvent> adminEventMulticastProcessor) { | ||
this.eventMulticastProcessor = eventMulticastProcessor; | ||
this.adminEventMulticastProcessor = adminEventMulticastProcessor; | ||
} | ||
|
||
@Override | ||
public void onEvent(Event event) { | ||
sendEvent(event); | ||
} | ||
|
||
|
||
@Override | ||
public void onEvent(AdminEvent adminEvent, boolean b) { | ||
sendEvent(adminEvent); | ||
} | ||
|
||
private void sendEvent(Event e) { | ||
if (eventMulticastProcessor.hasSubscribers()) { | ||
boolean res = eventMulticastProcessor.offer(e); | ||
|
||
if (!res) { | ||
LOGGER.warn("multicastProcessor.offer() returned false"); | ||
} | ||
} | ||
} | ||
|
||
private void sendEvent(AdminEvent e) { | ||
if (adminEventMulticastProcessor.hasSubscribers()) { | ||
boolean res = adminEventMulticastProcessor.offer(e); | ||
|
||
if (!res) { | ||
LOGGER.warn("multicastProcessor.offer() returned false"); | ||
} | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...n/java/net/brianlevine/keycloak/graphql/events/MulticastEventListenerProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.brianlevine.keycloak.graphql.events; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.reactivex.rxjava3.processors.MulticastProcessor; | ||
|
||
import org.keycloak.Config; | ||
import org.keycloak.events.Event; | ||
import org.keycloak.events.EventListenerProvider; | ||
import org.keycloak.events.EventListenerProviderFactory; | ||
import org.keycloak.events.admin.AdminEvent; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
|
||
/** | ||
* Creates MulticastEventListenerProviders for Keycloak Events and AdminEvents | ||
*/ | ||
@AutoService(EventListenerProviderFactory.class) | ||
public class MulticastEventListenerProviderFactory implements EventListenerProviderFactory { | ||
private static MulticastProcessor<Event> eventMulticastProcessor; | ||
private static MulticastProcessor<AdminEvent> adminEventMulticastProcessor; | ||
|
||
public static MulticastProcessor<Event> getEventMulticastProcessor() { | ||
return eventMulticastProcessor; | ||
} | ||
|
||
public static MulticastProcessor<AdminEvent> getAdminEventMulticastProcessor() { | ||
return adminEventMulticastProcessor; | ||
} | ||
|
||
@Override | ||
public EventListenerProvider create(KeycloakSession keycloakSession) { | ||
return new MulticastEventListenerProvider(eventMulticastProcessor, adminEventMulticastProcessor); | ||
} | ||
|
||
@Override | ||
public void init(Config.Scope scope) { | ||
eventMulticastProcessor = MulticastProcessor.create(false); | ||
eventMulticastProcessor.startUnbounded(); | ||
adminEventMulticastProcessor = MulticastProcessor.create(false); | ||
adminEventMulticastProcessor.startUnbounded(); | ||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory keycloakSessionFactory) { | ||
|
||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "multicast-event-listener"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
graphql/src/main/java/net/brianlevine/keycloak/graphql/rest/GraphQLConsoleService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.