From 8e0cdb15e6a818e08964104a525825048039bdc9 Mon Sep 17 00:00:00 2001 From: minux Date: Wed, 18 Mar 2020 14:18:18 +0900 Subject: [PATCH] Fix AuthSuccessHandler to use HttpRequest and HttpResponse (#2562) Motivation: Related: #2323 Result: - (Breaking) `AuthSuccessHandler` and `AuthFailureHandler` now does not have type parameters. --- .../server/auth/AuthFailureHandler.java | 22 +++++++----------- .../armeria/server/auth/AuthService.java | 7 +++--- .../server/auth/AuthServiceBuilder.java | 17 ++++++++------ .../server/auth/AuthSuccessHandler.java | 23 ++++++++----------- 4 files changed, 30 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/com/linecorp/armeria/server/auth/AuthFailureHandler.java b/core/src/main/java/com/linecorp/armeria/server/auth/AuthFailureHandler.java index 0dc911761a0..103ae7c0176 100644 --- a/core/src/main/java/com/linecorp/armeria/server/auth/AuthFailureHandler.java +++ b/core/src/main/java/com/linecorp/armeria/server/auth/AuthFailureHandler.java @@ -19,32 +19,26 @@ import com.linecorp.armeria.common.HttpRequest; import com.linecorp.armeria.common.HttpResponse; -import com.linecorp.armeria.common.Request; -import com.linecorp.armeria.common.Response; -import com.linecorp.armeria.common.RpcRequest; -import com.linecorp.armeria.common.RpcResponse; +import com.linecorp.armeria.server.HttpService; import com.linecorp.armeria.server.Service; import com.linecorp.armeria.server.ServiceRequestContext; /** * A callback which is invoked to handle an authorization failure indicated by {@link Authorizer}. * - * @param the type of incoming {@link Request}. Must be {@link HttpRequest} or {@link RpcRequest}. - * @param the type of outgoing {@link Response}. Must be {@link HttpResponse} or {@link RpcResponse}. - * * @see AuthServiceBuilder#onFailure(AuthFailureHandler) */ @FunctionalInterface -public interface AuthFailureHandler { +public interface AuthFailureHandler { /** - * Invoked when the authorization of the specified {@link Request} has failed. + * Invoked when the authorization of the specified {@link HttpRequest} has failed. * * @param delegate the next {@link Service} in the decoration chain - * @param ctx the {@link ServiceRequestContext} of {@code req} - * @param req the {@link Request} being handled - * @param cause {@code null} if {@code req} has been rejected by the {@link Authorizer}. + * @param ctx the {@link ServiceRequestContext} + * @param req the {@link HttpRequest} being handled + * @param cause {@code null} if the {@link HttpRequest} has been rejected by the {@link Authorizer}. * non-{@code null} if the {@link Authorizer} raised an {@link Exception}. */ - O authFailed(Service delegate, ServiceRequestContext ctx, - I req, @Nullable Throwable cause) throws Exception; + HttpResponse authFailed(HttpService delegate, ServiceRequestContext ctx, + HttpRequest req, @Nullable Throwable cause) throws Exception; } diff --git a/core/src/main/java/com/linecorp/armeria/server/auth/AuthService.java b/core/src/main/java/com/linecorp/armeria/server/auth/AuthService.java index 7155c7f5247..a57b919ad22 100644 --- a/core/src/main/java/com/linecorp/armeria/server/auth/AuthService.java +++ b/core/src/main/java/com/linecorp/armeria/server/auth/AuthService.java @@ -72,12 +72,11 @@ public static AuthServiceBuilder builder() { } private final Authorizer authorizer; - private final AuthSuccessHandler successHandler; - private final AuthFailureHandler failureHandler; + private final AuthSuccessHandler successHandler; + private final AuthFailureHandler failureHandler; AuthService(HttpService delegate, Authorizer authorizer, - AuthSuccessHandler successHandler, - AuthFailureHandler failureHandler) { + AuthSuccessHandler successHandler, AuthFailureHandler failureHandler) { super(delegate); this.authorizer = authorizer; this.successHandler = successHandler; diff --git a/core/src/main/java/com/linecorp/armeria/server/auth/AuthServiceBuilder.java b/core/src/main/java/com/linecorp/armeria/server/auth/AuthServiceBuilder.java index 7b9e3d95c22..60fbfd1f095 100644 --- a/core/src/main/java/com/linecorp/armeria/server/auth/AuthServiceBuilder.java +++ b/core/src/main/java/com/linecorp/armeria/server/auth/AuthServiceBuilder.java @@ -37,8 +37,8 @@ public final class AuthServiceBuilder { @Nullable private Authorizer authorizer; - private AuthSuccessHandler successHandler = Service::serve; - private AuthFailureHandler failureHandler = (delegate, ctx, req, cause) -> { + private AuthSuccessHandler successHandler = Service::serve; + private AuthFailureHandler failureHandler = (delegate, ctx, req, cause) -> { if (cause != null) { AuthService.logger.warn("Unexpected exception during authorization.", cause); } @@ -144,7 +144,7 @@ public AuthServiceBuilder addTokenAuthorizer( * Sets the {@link AuthSuccessHandler} which handles successfully authorized requests. * By default, the request will be delegated to the next {@link HttpService}. */ - public AuthServiceBuilder onSuccess(AuthSuccessHandler successHandler) { + public AuthServiceBuilder onSuccess(AuthSuccessHandler successHandler) { this.successHandler = requireNonNull(successHandler, "successHandler"); return this; } @@ -154,7 +154,7 @@ public AuthServiceBuilder onSuccess(AuthSuccessHandler failureHandler) { + public AuthServiceBuilder onFailure(AuthFailureHandler failureHandler) { this.failureHandler = requireNonNull(failureHandler, "failureHandler"); return this; } @@ -167,15 +167,18 @@ public AuthService build(HttpService delegate) { successHandler, failureHandler); } + private AuthService build(HttpService delegate, Authorizer authorizer) { + return new AuthService(requireNonNull(delegate, "delegate"), authorizer, + successHandler, failureHandler); + } + /** * Returns a newly-created decorator that decorates an {@link HttpService} with a new * {@link AuthService} based on the {@link Authorizer}s added to this builder. */ public Function newDecorator() { final Authorizer authorizer = authorizer(); - final AuthSuccessHandler successHandler = this.successHandler; - final AuthFailureHandler failureHandler = this.failureHandler; - return service -> new AuthService(service, authorizer, successHandler, failureHandler); + return delegate -> build(delegate, authorizer); } private Authorizer authorizer() { diff --git a/core/src/main/java/com/linecorp/armeria/server/auth/AuthSuccessHandler.java b/core/src/main/java/com/linecorp/armeria/server/auth/AuthSuccessHandler.java index 0fec0cd2354..fd4e900c965 100644 --- a/core/src/main/java/com/linecorp/armeria/server/auth/AuthSuccessHandler.java +++ b/core/src/main/java/com/linecorp/armeria/server/auth/AuthSuccessHandler.java @@ -17,29 +17,24 @@ import com.linecorp.armeria.common.HttpRequest; import com.linecorp.armeria.common.HttpResponse; -import com.linecorp.armeria.common.Request; -import com.linecorp.armeria.common.Response; -import com.linecorp.armeria.common.RpcRequest; -import com.linecorp.armeria.common.RpcResponse; -import com.linecorp.armeria.server.Service; +import com.linecorp.armeria.server.HttpService; import com.linecorp.armeria.server.ServiceRequestContext; /** * A callback which is invoked to handle an authorization success indicated by {@link Authorizer}. * - * @param the type of incoming {@link Request}. Must be {@link HttpRequest} or {@link RpcRequest}. - * @param the type of outgoing {@link Response}. Must be {@link HttpResponse} or {@link RpcResponse}. - * * @see AuthServiceBuilder#onSuccess(AuthSuccessHandler) */ @FunctionalInterface -public interface AuthSuccessHandler { +public interface AuthSuccessHandler { + /** - * Invoked when the authorization of the specified {@link Request} has succeeded. + * Invoked when the authorization of the specified {@link HttpRequest} has succeeded. * - * @param delegate the next {@link Service} in the decoration chain - * @param ctx the {@link ServiceRequestContext} of {@code req} - * @param req the {@link Request} being handled + * @param delegate the next {@link HttpService} in the decoration chain + * @param ctx the {@link ServiceRequestContext} + * @param req the {@link HttpRequest} being handled */ - O authSucceeded(Service delegate, ServiceRequestContext ctx, I req) throws Exception; + HttpResponse authSucceeded( + HttpService delegate, ServiceRequestContext ctx, HttpRequest req) throws Exception; }