24
24
25
25
import io .rsocket .ConnectionSetupPayload ;
26
26
import io .rsocket .RSocket ;
27
- import io .rsocket .RSocketFactory ;
28
27
import io .rsocket .SocketAcceptor ;
29
28
import io .rsocket .frame .FrameType ;
30
29
import reactor .core .publisher .Mono ;
@@ -344,18 +343,15 @@ public SocketAcceptor serverResponder() {
344
343
}
345
344
346
345
/**
347
- * Return an adapter for a client side
348
- * {@link io.rsocket.RSocketFactory.ClientRSocketFactory#acceptor(BiFunction)
349
- * acceptor} that delegate to this {@link RSocketMessageHandler} for
350
- * handling.
351
- * <p>The initial {@link ConnectionSetupPayload} can be processed with a
352
- * {@link ConnectMapping @ConnectionMapping} method but, unlike the
353
- * server side, such a method is merely a callback and cannot prevent the
354
- * connection unless the method throws an error immediately. Such a method
355
- * can also start requests to the server but must do so decoupled from
356
- * handling and from the current thread.
357
- * <p>Subsequent stream requests can be handled with
358
- * {@link MessageMapping MessageMapping} methods.
346
+ * Return an adapter for a client side responder that can be used to set
347
+ * {@link io.rsocket.RSocketFactory.ClientRSocketFactory#acceptor(Function)}.
348
+ * The responder delegates requests to this {@code RSocketMessageHandler}
349
+ * for handling via {@code @MessageMapping} methods.
350
+ * <p>The initial {@link ConnectionSetupPayload} can be accessed through a
351
+ * {@link ConnectMapping @ConnectionMapping} method, but such a method is
352
+ * only a callback just before the connection is made and cannot "accept"
353
+ * or prevent the connection. Such a method can also start requests to the
354
+ * server but must do so decoupled from handling and the current thread.
359
355
*/
360
356
public BiFunction <ConnectionSetupPayload , RSocket , RSocket > clientResponder () {
361
357
return (setupPayload , sendingRSocket ) -> {
@@ -375,17 +371,13 @@ private MessagingRSocket createResponder(ConnectionSetupPayload setupPayload, RS
375
371
MimeType metaMimeType = StringUtils .hasText (s ) ? MimeTypeUtils .parseMimeType (s ) : this .defaultMetadataMimeType ;
376
372
Assert .notNull (metaMimeType , "No `metadataMimeType` in ConnectionSetupPayload and no default value" );
377
373
378
- RSocketStrategies strategies = getRSocketStrategies ();
379
- RSocketRequester requester = RSocketRequester .wrap (rsocket , dataMimeType , metaMimeType , strategies );
380
-
381
- Assert .state (this .metadataExtractor != null ,
382
- () -> "No MetadataExtractor. Was afterPropertiesSet not called?" );
374
+ RSocketRequester requester = RSocketRequester .wrap (
375
+ rsocket , dataMimeType , metaMimeType , this .strategies );
383
376
384
- Assert .state (getRouteMatcher () != null ,
385
- () -> "No RouteMatcher. Was afterPropertiesSet not called?" );
377
+ Assert .state (getRouteMatcher () != null , () -> "No RouteMatcher. Was afterPropertiesSet not called?" );
386
378
387
- return new MessagingRSocket (dataMimeType , metaMimeType , this .metadataExtractor , requester ,
388
- this , getRouteMatcher (), strategies );
379
+ return new MessagingRSocket (dataMimeType , metaMimeType , this .metadataExtractor ,
380
+ requester , this , getRouteMatcher (), this . strategies );
389
381
}
390
382
391
383
private boolean isDataMimeTypeSupported (MimeType dataMimeType ) {
@@ -399,39 +391,39 @@ private boolean isDataMimeTypeSupported(MimeType dataMimeType) {
399
391
return false ;
400
392
}
401
393
402
- public static ClientRSocketFactoryConfigurer clientResponder (Object ... handlers ) {
403
- return new ResponderConfigurer (handlers );
404
- }
405
-
406
-
407
- private static final class ResponderConfigurer implements ClientRSocketFactoryConfigurer {
408
-
409
- private final List <Object > handlers = new ArrayList <>();
410
-
411
- @ Nullable
412
- private RSocketStrategies strategies ;
413
-
414
-
415
- private ResponderConfigurer (Object ... handlers ) {
416
- Assert .notEmpty (handlers , "No handlers" );
417
- for (Object obj : handlers ) {
418
- this .handlers .add (obj instanceof Class ? BeanUtils .instantiateClass ((Class <?>) obj ) : obj );
419
- }
420
- }
394
+ /**
395
+ * Static factory method for a configurer of a client side responder with
396
+ * annotated handler methods. This is intended to be passed into
397
+ * {@link RSocketRequester.Builder#rsocketFactory(ClientRSocketFactoryConfigurer)}.
398
+ * <p>In effect a shortcut to create and initialize
399
+ * {@code RSocketMessageHandler} with the given strategies and handlers,
400
+ * and use {@link #clientResponder()} to obtain the responder.
401
+ * For more advanced scenarios, e.g. discovering handlers through a custom
402
+ * stereotype annotation, consider declaring {@code RSocketMessageHandler}
403
+ * as a bean, and then obtain the responder from it.
404
+ * @param strategies the strategies to set on the created
405
+ * {@code RSocketMessageHandler}
406
+ * @param candidateHandlers a list of Objects and/or Classes with annotated
407
+ * handler methods; used to call {@link #setHandlers(List)} with
408
+ * on the created {@code RSocketMessageHandler}
409
+ * @return a configurer that may be passed into
410
+ * {@link RSocketRequester.Builder#rsocketFactory(ClientRSocketFactoryConfigurer)}
411
+ */
412
+ public static ClientRSocketFactoryConfigurer clientResponder (
413
+ RSocketStrategies strategies , Object ... candidateHandlers ) {
421
414
422
- @ Override
423
- public void configureWithStrategies (RSocketStrategies strategies ) {
424
- this .strategies = strategies ;
415
+ Assert .notEmpty (candidateHandlers , "No handlers" );
416
+ List <Object > handlers = new ArrayList <>(candidateHandlers .length );
417
+ for (Object obj : candidateHandlers ) {
418
+ handlers .add (obj instanceof Class ? BeanUtils .instantiateClass ((Class <?>) obj ) : obj );
425
419
}
426
420
427
- @ Override
428
- public void configure (RSocketFactory .ClientRSocketFactory factory ) {
421
+ return rsocketFactory -> {
429
422
RSocketMessageHandler handler = new RSocketMessageHandler ();
430
- handler .setHandlers (this . handlers );
431
- handler .setRSocketStrategies (this . strategies );
423
+ handler .setHandlers (handlers );
424
+ handler .setRSocketStrategies (strategies );
432
425
handler .afterPropertiesSet ();
433
- factory .acceptor (handler .clientResponder ());
434
- }
426
+ rsocketFactory .acceptor (handler .clientResponder ());
427
+ };
435
428
}
436
-
437
429
}
0 commit comments