From 9ab327238e2f3556b46b54d938cf030db6021f85 Mon Sep 17 00:00:00 2001 From: spg Date: Wed, 13 Nov 2013 17:02:43 -0500 Subject: [PATCH 1/4] Custom ActionException subclasses are not rethrown as new ActionException Former-commit-id: 28f149243969bc1df4fde81e0ba2880f83d74fee --- gwtp-core/gwtp-dispatch-server-guice/pom.xml | 10 +++-- .../ActionExceptionThrownByHandler.java | 6 +++ .../ActionExceptionThrownByValidator.java | 6 +++ .../server/ActionThrownByHandlerTest.java | 43 +++++++++++++++++++ .../server/ActionThrownByValidatorTest.java | 42 ++++++++++++++++++ .../server/ActionValidatorThatThrows.java | 13 ++++++ .../HandlerThatThrowsActionException.java | 23 ++++++++++ .../dispatch/server/ServiceModule.java | 42 ++++++++++++++++++ .../dispatch/server/SomeAction.java | 16 +++++++ .../server/AbstractDispatchServiceImpl.java | 4 +- 10 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ServiceModule.java create mode 100644 gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/SomeAction.java diff --git a/gwtp-core/gwtp-dispatch-server-guice/pom.xml b/gwtp-core/gwtp-dispatch-server-guice/pom.xml index 213d012605..2d8d4294cc 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/pom.xml +++ b/gwtp-core/gwtp-dispatch-server-guice/pom.xml @@ -34,6 +34,10 @@ com.gwtplatform gwtp-dispatch-test test - - - \ No newline at end of file + + + org.jukito + jukito + + + diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java new file mode 100644 index 0000000000..6cd3d5aecc --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java @@ -0,0 +1,6 @@ +package com.gwtplatform.dispatch.server; + +import com.gwtplatform.dispatch.shared.ActionException; + +public class ActionExceptionThrownByHandler extends ActionException { +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java new file mode 100644 index 0000000000..04a413f91d --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java @@ -0,0 +1,6 @@ +package com.gwtplatform.dispatch.server; + +import com.gwtplatform.dispatch.shared.ActionException; + +public class ActionExceptionThrownByValidator extends ActionException { +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java new file mode 100644 index 0000000000..5aa85f3886 --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java @@ -0,0 +1,43 @@ +package com.gwtplatform.dispatch.server; + +import javax.inject.Inject; + +import org.jukito.JukitoModule; +import org.jukito.JukitoRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl; +import com.gwtplatform.dispatch.server.guice.actionvalidator.DefaultActionValidator; +import com.gwtplatform.dispatch.shared.ActionException; +import com.gwtplatform.dispatch.shared.ServiceException; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + + +@RunWith(JukitoRunner.class) +public class ActionThrownByHandlerTest { + public static class MyModule extends JukitoModule { + @Override + protected void configureTest() { + install(new ServiceModule(DefaultActionValidator.class)); + } + } + + @Inject + DispatchServiceImpl service; + + @Test + public void exceptionThrownByHandlerIsNotWrappedInActionException() throws ServiceException { + try { + service.execute("", new SomeAction()); + fail(); + } catch (ActionException e) { + assertThat(e, instanceOf(ActionExceptionThrownByHandler.class)); + assertEquals(0, e.getStackTrace().length); + } + } +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java new file mode 100644 index 0000000000..99553e2bf6 --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java @@ -0,0 +1,42 @@ +package com.gwtplatform.dispatch.server; + +import javax.inject.Inject; + +import org.jukito.JukitoModule; +import org.jukito.JukitoRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl; +import com.gwtplatform.dispatch.shared.ActionException; +import com.gwtplatform.dispatch.shared.ServiceException; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + + +@RunWith(JukitoRunner.class) +public class ActionThrownByValidatorTest { + public static class MyModule extends JukitoModule { + @Override + protected void configureTest() { + install(new ServiceModule(ActionValidatorThatThrows.class)); + } + } + + @Inject + DispatchServiceImpl service; + + @Test + public void exceptionThrownByValidatorIsNotWrappedInActionException() throws ServiceException { + try { + service.execute("", new SomeAction()); + fail(); + } catch (ActionException e) { + assertThat(e, instanceOf(ActionExceptionThrownByValidator.class)); + assertEquals(0, e.getStackTrace().length); + } + } +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java new file mode 100644 index 0000000000..42ae0badce --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java @@ -0,0 +1,13 @@ +package com.gwtplatform.dispatch.server; + +import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator; +import com.gwtplatform.dispatch.shared.Action; +import com.gwtplatform.dispatch.shared.ActionException; +import com.gwtplatform.dispatch.shared.Result; + +public class ActionValidatorThatThrows implements ActionValidator { + @Override + public boolean isValid(Action action) throws ActionException { + throw new ActionExceptionThrownByValidator(); + } +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java new file mode 100644 index 0000000000..c394a3293f --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java @@ -0,0 +1,23 @@ +package com.gwtplatform.dispatch.server; + +import javax.inject.Inject; + +import com.gwtplatform.dispatch.server.actionhandler.AbstractActionHandler; +import com.gwtplatform.dispatch.shared.ActionException; +import com.gwtplatform.dispatch.shared.NoResult; + +public class HandlerThatThrowsActionException extends AbstractActionHandler { + @Inject + HandlerThatThrowsActionException() { + super(SomeAction.class); + } + + @Override + public NoResult execute(SomeAction action, ExecutionContext context) throws ActionException { + throw new ActionExceptionThrownByHandler(); + } + + @Override + public void undo(SomeAction action, NoResult result, ExecutionContext context) throws ActionException { + } +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ServiceModule.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ServiceModule.java new file mode 100644 index 0000000000..3562f4eb5e --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ServiceModule.java @@ -0,0 +1,42 @@ +package com.gwtplatform.dispatch.server; + +import com.google.inject.AbstractModule; +import com.google.inject.Singleton; +import com.gwtplatform.dispatch.server.actionhandler.ActionHandler; +import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorClass; +import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorMap; +import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorMapImpl; +import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorRegistry; +import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator; +import com.gwtplatform.dispatch.server.guice.DispatchImpl; +import com.gwtplatform.dispatch.server.guice.actionhandlervalidator.ActionHandlerValidatorLinker; +import com.gwtplatform.dispatch.server.guice.actionhandlervalidator.LazyActionHandlerValidatorRegistryImpl; +import com.gwtplatform.dispatch.shared.Action; +import com.gwtplatform.dispatch.shared.Result; + +public class ServiceModule extends AbstractModule { + private final Class actionValidator; + + public ServiceModule(Class actionValidator) { + this.actionValidator = actionValidator; + } + + @Override + protected void configure() { + bind(Dispatch.class).to(DispatchImpl.class); + bind(ActionHandlerValidatorRegistry.class).to( + LazyActionHandlerValidatorRegistryImpl.class).in(Singleton.class); + + bindHandler(SomeAction.class, HandlerThatThrowsActionException.class, actionValidator); + requestStaticInjection(ActionHandlerValidatorLinker.class); + } + + protected , R extends Result> void bindHandler( + Class actionClass, Class> handlerClass, + Class actionValidator) { + + bind(ActionHandlerValidatorMap.class).toInstance( + new ActionHandlerValidatorMapImpl(actionClass, + new ActionHandlerValidatorClass(handlerClass, actionValidator))); + } +} diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/SomeAction.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/SomeAction.java new file mode 100644 index 0000000000..990dc6ab5f --- /dev/null +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/SomeAction.java @@ -0,0 +1,16 @@ +package com.gwtplatform.dispatch.server; + +import com.gwtplatform.dispatch.shared.Action; +import com.gwtplatform.dispatch.shared.NoResult; + +public class SomeAction implements Action { + @Override + public String getServiceName() { + return null; + } + + @Override + public boolean isSecured() { + return false; + } +} diff --git a/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java b/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java index 81f596a154..614d9084c3 100644 --- a/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java +++ b/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java @@ -90,7 +90,9 @@ public Result execute(String cookieSentByRPC, Action action) throws ActionExc logger.log(Level.WARNING, newMessage, e); } - throw new ActionException(e.getMessage()); + e.setStackTrace(new StackTraceElement[]{}); + + throw e; } catch (ServiceException e) { if (logger.isLoggable(Level.WARNING)) { logger.log(Level.WARNING, "Service exception while executing " + action.getClass().getName() + ": " + From c956f5bab2698b5e2091c80ffe3648e184ac2d9b Mon Sep 17 00:00:00 2001 From: spg Date: Thu, 14 Nov 2013 10:26:08 -0500 Subject: [PATCH 2/4] CR Fix Former-commit-id: 62bc4eef73f54af5e929f056162c5a0c33329fdc --- .../ActionExceptionThrownByHandler.java | 19 +++++++++++++++++++ .../ActionExceptionThrownByValidator.java | 19 +++++++++++++++++++ .../server/ActionThrownByHandlerTest.java | 18 +++++++++++++++++- .../server/ActionThrownByValidatorTest.java | 18 +++++++++++++++++- .../server/ActionValidatorThatThrows.java | 18 +++++++++++++++++- .../HandlerThatThrowsActionException.java | 18 +++++++++++++++++- .../dispatch/server/ServiceModule.java | 16 ++++++++++++++++ .../dispatch/server/SomeAction.java | 16 ++++++++++++++++ .../server/AbstractDispatchServiceImpl.java | 4 ++++ 9 files changed, 142 insertions(+), 4 deletions(-) diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java index 6cd3d5aecc..96610f5e07 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByHandler.java @@ -1,6 +1,25 @@ +/** + * Copyright 2011 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.dispatch.server; import com.gwtplatform.dispatch.shared.ActionException; public class ActionExceptionThrownByHandler extends ActionException { + public ActionExceptionThrownByHandler(Throwable cause) { + super("", cause); + } } diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java index 04a413f91d..f82d2be8bc 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionExceptionThrownByValidator.java @@ -1,6 +1,25 @@ +/** + * Copyright 2011 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.dispatch.server; import com.gwtplatform.dispatch.shared.ActionException; public class ActionExceptionThrownByValidator extends ActionException { + public ActionExceptionThrownByValidator(Throwable cause) { + super("", cause); + } } diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java index 5aa85f3886..f608ad3ae2 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByHandlerTest.java @@ -1,3 +1,19 @@ +/** + * Copyright 2011 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.dispatch.server; import javax.inject.Inject; @@ -17,7 +33,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; - @RunWith(JukitoRunner.class) public class ActionThrownByHandlerTest { public static class MyModule extends JukitoModule { @@ -38,6 +53,7 @@ public void exceptionThrownByHandlerIsNotWrappedInActionException() throws Servi } catch (ActionException e) { assertThat(e, instanceOf(ActionExceptionThrownByHandler.class)); assertEquals(0, e.getStackTrace().length); + assertEquals(0, e.getCause().getStackTrace().length); } } } diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java index 99553e2bf6..6545b351c1 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionThrownByValidatorTest.java @@ -1,3 +1,19 @@ +/** + * Copyright 2011 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.dispatch.server; import javax.inject.Inject; @@ -16,7 +32,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; - @RunWith(JukitoRunner.class) public class ActionThrownByValidatorTest { public static class MyModule extends JukitoModule { @@ -37,6 +52,7 @@ public void exceptionThrownByValidatorIsNotWrappedInActionException() throws Ser } catch (ActionException e) { assertThat(e, instanceOf(ActionExceptionThrownByValidator.class)); assertEquals(0, e.getStackTrace().length); + assertEquals(0, e.getCause().getStackTrace().length); } } } diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java index 42ae0badce..992df0d805 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/ActionValidatorThatThrows.java @@ -1,3 +1,19 @@ +/** + * Copyright 2011 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.dispatch.server; import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator; @@ -8,6 +24,6 @@ public class ActionValidatorThatThrows implements ActionValidator { @Override public boolean isValid(Action action) throws ActionException { - throw new ActionExceptionThrownByValidator(); + throw new ActionExceptionThrownByValidator(new Exception()); } } diff --git a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java index c394a3293f..4ca686085b 100644 --- a/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java +++ b/gwtp-core/gwtp-dispatch-server-guice/src/test/java/com/gwtplatform/dispatch/server/HandlerThatThrowsActionException.java @@ -1,3 +1,19 @@ +/** + * Copyright 2011 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.dispatch.server; import javax.inject.Inject; @@ -14,7 +30,7 @@ public class HandlerThatThrowsActionException extends AbstractActionHandler action) throws ActionExc logger.log(Level.WARNING, newMessage, e); } + if (e.getCause() != null) { + e.getCause().setStackTrace(new StackTraceElement[]{}); + } + e.setStackTrace(new StackTraceElement[]{}); throw e; From 2223545538793197f735821a69a981ba783ed72c Mon Sep 17 00:00:00 2001 From: spg Date: Thu, 14 Nov 2013 10:44:21 -0500 Subject: [PATCH 3/4] Recursively remove all stacktraces from a Throwable Former-commit-id: a920502c343a4a2ca44119ef8e72d9625a097047 --- .../server/AbstractDispatchServiceImpl.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java b/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java index 91d4de4498..7c1c5bbc03 100644 --- a/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java +++ b/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java @@ -48,15 +48,13 @@ public abstract class AbstractDispatchServiceImpl extends RemoteServiceServlet implements DispatchService { private static final long serialVersionUID = -4753225025940949024L; private static final String noSecurityCookieMessage = "You have to define a security cookie in order to use " + - "secured actions. See com.gwtplatform.dispatch.shared" + - ".SecurityCookie for details."; + "secured actions. See com.gwtplatform.dispatch.shared" + + ".SecurityCookie for details."; private static final String xsrfAttackMessage = "Cookie provided by RPC doesn't match request cookie, " + - "aborting action, possible XSRF attack. (Maybe you forgot to set " + - "the security cookie?)"; - + "aborting action, possible XSRF attack. (Maybe you forgot to set " + + "the security cookie?)"; protected final Dispatch dispatch; protected final Logger logger; - protected RequestProvider requestProvider; protected AbstractDispatchServiceImpl(Logger logger, @@ -86,28 +84,24 @@ public Result execute(String cookieSentByRPC, Action action) throws ActionExc } catch (ActionException e) { if (logger.isLoggable(Level.WARNING)) { String newMessage = "Action exception while executing " + action.getClass().getName() + ": " + - e.getMessage(); + e.getMessage(); logger.log(Level.WARNING, newMessage, e); } - if (e.getCause() != null) { - e.getCause().setStackTrace(new StackTraceElement[]{}); - } - - e.setStackTrace(new StackTraceElement[]{}); + removeStacktraces(e); throw e; } catch (ServiceException e) { if (logger.isLoggable(Level.WARNING)) { logger.log(Level.WARNING, "Service exception while executing " + action.getClass().getName() + ": " + - e.getMessage(), e); + e.getMessage(), e); } throw new ServiceException(e.getMessage()); } catch (RuntimeException e) { if (logger.isLoggable(Level.WARNING)) { logger.log(Level.WARNING, "Unexpected exception while executing " + action.getClass().getName() + ": " + - "" + e.getMessage(), e); + "" + e.getMessage(), e); } throw new ServiceException(e.getMessage()); @@ -184,4 +178,16 @@ private boolean cookieMatch(String cookieSentByRPC) throws ServiceException { return cookieInRequest.equals(cookieSentByRPC); } + + /** + * Recursively removes all stacktraces from a Throwable and its cause + */ + private void removeStacktraces(Throwable e) { + if (e == null) { + return; + } + + e.setStackTrace(new StackTraceElement[]{}); + removeStacktraces(e.getCause()); + } } From e1380c2893fc4c57842412e532802ff8afd16899 Mon Sep 17 00:00:00 2001 From: spg Date: Thu, 14 Nov 2013 17:29:00 -0500 Subject: [PATCH 4/4] Documentation about stacktrace removal Former-commit-id: b5907989ec24527950cd544b174e6a1b82c16ebb --- .../dispatch/server/AbstractDispatchServiceImpl.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java b/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java index 7c1c5bbc03..c58099a04c 100644 --- a/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java +++ b/gwtp-core/gwtp-dispatch-server/src/main/java/com/gwtplatform/dispatch/server/AbstractDispatchServiceImpl.java @@ -69,6 +69,12 @@ public String getSecurityCookieName() { return null; } + /** + * {@link ActionException} and {@link ServiceException} will have their stacktraces (and stacktraces of their + * causes) removed for security purposes. + * + * @see {@link DispatchService} for further API docs + */ @Override public Result execute(String cookieSentByRPC, Action action) throws ActionException, ServiceException {