From b8c85228d742988e5763564914b48a9bdf428c48 Mon Sep 17 00:00:00 2001 From: BenDol Date: Sun, 20 Sep 2015 21:56:55 +1200 Subject: [PATCH 1/2] Add ExceptionHandler example with latest changes. --- carstore/pom.xml | 8 +- .../dispatch/DispatchExceptionHandler.java | 81 +++++++++++++++++++ .../carstore/client/gin/SharedModule.java | 3 + .../client/security/LoggedInGatekeeper.java | 2 +- .../server/dispatch/LogInHandler.java | 6 +- 5 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 carstore/src/main/java/com/gwtplatform/carstore/client/dispatch/DispatchExceptionHandler.java diff --git a/carstore/pom.xml b/carstore/pom.xml index f94a0014..07827e17 100644 --- a/carstore/pom.xml +++ b/carstore/pom.xml @@ -5,7 +5,7 @@ com.gwtplatform carstore - 1.5 + 1.6-SNAPSHOT war GWTP Carstore Simple application used for testing GWTP features. @@ -13,7 +13,7 @@ 2.7.0 - 1.5 + 1.6-SNAPSHOT 2.1.2 1.4.2 1.2.1 @@ -41,8 +41,8 @@ 0.9.6 3.2 2.1 - 2.18 - 2.18 + 2.18.1 + 2.18.1 2.7.0 2.14 diff --git a/carstore/src/main/java/com/gwtplatform/carstore/client/dispatch/DispatchExceptionHandler.java b/carstore/src/main/java/com/gwtplatform/carstore/client/dispatch/DispatchExceptionHandler.java new file mode 100644 index 00000000..d118bdcc --- /dev/null +++ b/carstore/src/main/java/com/gwtplatform/carstore/client/dispatch/DispatchExceptionHandler.java @@ -0,0 +1,81 @@ +/* + * Copyright 2015 ArcBees Inc. + * + * 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.gwtplatform.carstore.client.dispatch; + +import javax.inject.Inject; +import javax.inject.Provider; + +import com.gwtplatform.carstore.client.application.ApplicationPresenter; +import com.gwtplatform.carstore.client.application.event.DisplayMessageEvent; +import com.gwtplatform.carstore.client.application.widget.message.Message; +import com.gwtplatform.carstore.client.application.widget.message.MessageStyle; +import com.gwtplatform.dispatch.client.ExceptionHandler; +import com.gwtplatform.dispatch.rest.shared.ActionResponseException; +import com.gwtplatform.dispatch.rest.shared.RestActionException; +import com.gwtplatform.dispatch.shared.ActionException; + +public class DispatchExceptionHandler implements ExceptionHandler { + + private final Provider appPresenterProvider; + + @Inject + DispatchExceptionHandler(Provider appPresenterProvider) { + this.appPresenterProvider = appPresenterProvider; + } + + @Override + public Status onFailure(Throwable e) { + if (e instanceof ActionException) { + return handleActionExceptions((ActionException) e); + } + + return Status.CONTINUE; + } + + private Status handleActionExceptions(ActionException e) { + Status status; + if (e instanceof RestActionException) { + // This is a REST action exception + status = handleRestActionExceptions((RestActionException) e); + } else { + // This is an RPC action exception + status = handleRpcActionExceptions(e); + } + + return status; + } + + private Status handleRestActionExceptions(RestActionException e) { + Status status = Status.CONTINUE; + + if (e instanceof ActionResponseException) { + // Unauthorized access exception + if (((ActionResponseException) e).getStatusCode() == 401) { + DisplayMessageEvent.fire(appPresenterProvider.get(), new Message( + "You do not have access to this part of the system.", MessageStyle.ERROR)); + + status = Status.STOP; + } + } + + return status; + } + + private Status handleRpcActionExceptions(ActionException e) { + return Status.CONTINUE; + } +} diff --git a/carstore/src/main/java/com/gwtplatform/carstore/client/gin/SharedModule.java b/carstore/src/main/java/com/gwtplatform/carstore/client/gin/SharedModule.java index d9aee543..a9a11f8e 100644 --- a/carstore/src/main/java/com/gwtplatform/carstore/client/gin/SharedModule.java +++ b/carstore/src/main/java/com/gwtplatform/carstore/client/gin/SharedModule.java @@ -16,6 +16,7 @@ package com.gwtplatform.carstore.client.gin; +import com.gwtplatform.carstore.client.dispatch.DispatchExceptionHandler; import com.gwtplatform.carstore.client.dispatch.rest.AppRestDispatchHooks; import com.gwtplatform.carstore.client.dispatch.rest.RestInterceptorRegistry; import com.gwtplatform.carstore.client.dispatch.rpc.AppRpcDispatchHooks; @@ -39,10 +40,12 @@ protected void configure() { install(new RestDispatchAsyncModule.Builder() .dispatchHooks(AppRestDispatchHooks.class) .interceptorRegistry(RestInterceptorRegistry.class) + .exceptionHandler(DispatchExceptionHandler.class) .build()); install(new RpcDispatchAsyncModule.Builder() .dispatchHooks(AppRpcDispatchHooks.class) .interceptorRegistry(RpcInterceptorRegistry.class) + .exceptionHandler(DispatchExceptionHandler.class) .build()); // CarStore modules diff --git a/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java b/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java index 862948d0..a44c2434 100644 --- a/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java +++ b/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java @@ -21,7 +21,7 @@ import com.gwtplatform.mvp.client.annotations.DefaultGatekeeper; import com.gwtplatform.mvp.client.proxy.Gatekeeper; -@DefaultGatekeeper +//@DefaultGatekeeper public class LoggedInGatekeeper implements Gatekeeper { private final CurrentUser currentUser; diff --git a/carstore/src/main/java/com/gwtplatform/carstore/server/dispatch/LogInHandler.java b/carstore/src/main/java/com/gwtplatform/carstore/server/dispatch/LogInHandler.java index 21240a85..6441add5 100644 --- a/carstore/src/main/java/com/gwtplatform/carstore/server/dispatch/LogInHandler.java +++ b/carstore/src/main/java/com/gwtplatform/carstore/server/dispatch/LogInHandler.java @@ -68,9 +68,9 @@ public LogInResult execute(LogInAction action, ExecutionContext context) throws loggedInCookie = loginCookieDao.createSessionCookie(userDto); } - logger.info("LogInHandlerexecut(): actiontype=" + getActionType()); - logger.info("LogInHandlerexecut(): currentUserDto=" + currentUserDto); - logger.info("LogInHandlerexecut(): loggedInCookie=" + loggedInCookie); + logger.info("LogInHandler#execute(): actiontype=" + getActionType()); + logger.info("LogInHandler#execute(): currentUserDto=" + currentUserDto); + logger.info("LogInHandler#execute(): loggedInCookie=" + loggedInCookie); return new LogInResult(action.getActionType(), currentUserDto, loggedInCookie); } From ac389aba76366ee087d5b8dcf0352681051f611c Mon Sep 17 00:00:00 2001 From: BenDol Date: Sun, 20 Sep 2015 22:00:09 +1200 Subject: [PATCH 2/2] Add the DefaultGatekeeper back. --- .../carstore/client/security/LoggedInGatekeeper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java b/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java index a44c2434..862948d0 100644 --- a/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java +++ b/carstore/src/main/java/com/gwtplatform/carstore/client/security/LoggedInGatekeeper.java @@ -21,7 +21,7 @@ import com.gwtplatform.mvp.client.annotations.DefaultGatekeeper; import com.gwtplatform.mvp.client.proxy.Gatekeeper; -//@DefaultGatekeeper +@DefaultGatekeeper public class LoggedInGatekeeper implements Gatekeeper { private final CurrentUser currentUser;