From fa7065ccedcf527ba162d3817553ac044e734295 Mon Sep 17 00:00:00 2001 From: valeriolopes Date: Thu, 19 Mar 2015 13:11:00 -0300 Subject: [PATCH 1/6] fixed referer retrieval bug (if the context path string was present in the url host an invalid referer was returned) --- .../vraptor/view/DefaultRefererResult.java | 214 ++++++----- .../view/DefaultRefererResultTest.java | 349 ++++++++++-------- 2 files changed, 308 insertions(+), 255 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java index ff53b023f..c5087532a 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java @@ -1,102 +1,112 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - * All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static com.google.common.base.Preconditions.checkState; - -import java.util.ArrayList; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.ReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.MethodNotAllowedException; -import br.com.caelum.vraptor.http.route.Router; -import br.com.caelum.vraptor.validator.Message; - -@RequestScoped -public class DefaultRefererResult implements RefererResult { - - private final MutableRequest request; - private final Result result; - private final Router router; - private final ParametersProvider provider; - private ReflectionProvider reflectionProvider; - - /** - * @deprecated CDI eyes only - */ - protected DefaultRefererResult() { - this(null, null, null, null, null); - } - - @Inject - public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, - ReflectionProvider reflectionProvider) { - this.result = result; - this.request = request; - this.router = router; - this.provider = provider; - this.reflectionProvider = reflectionProvider; - } - - @Override - public void forward() throws IllegalStateException { - String referer = getReferer(); - - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).forwardTo(referer); - } - } - - private void executeMethod(ControllerMethod method, Object instance) { - Object[] args = provider.getParametersFor(method, new ArrayList()); - reflectionProvider.invoke(instance, method.getMethod(), args); - } - - @Override - public void redirect() throws IllegalStateException { - String referer = getReferer(); - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).redirectTo(referer); - } - } - - private String getReferer() { - String referer = request.getHeader("Referer"); - checkState(referer != null, "The Referer header was not specified"); - - String path = request.getContextPath(); - return referer.substring(referer.indexOf(path) + path.length()); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource + * All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static com.google.common.base.Preconditions.checkState; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.ReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.MethodNotAllowedException; +import br.com.caelum.vraptor.http.route.Router; +import br.com.caelum.vraptor.validator.Message; + +@RequestScoped +public class DefaultRefererResult implements RefererResult { + + private final MutableRequest request; + private final Result result; + private final Router router; + private final ParametersProvider provider; + private ReflectionProvider reflectionProvider; + + /** + * @deprecated CDI eyes only + */ + protected DefaultRefererResult() { + this(null, null, null, null, null); + } + + @Inject + public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, + ReflectionProvider reflectionProvider) { + this.result = result; + this.request = request; + this.router = router; + this.provider = provider; + this.reflectionProvider = reflectionProvider; + } + + @Override + public void forward() throws IllegalStateException { + String referer = getReferer(); + + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).forwardTo(referer); + } + } + + private void executeMethod(ControllerMethod method, Object instance) { + Object[] args = provider.getParametersFor(method, new ArrayList()); + reflectionProvider.invoke(instance, method.getMethod(), args); + } + + @Override + public void redirect() throws IllegalStateException { + String referer = getReferer(); + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).redirectTo(referer); + } + } + + private String getReferer() { + String referer = request.getHeader("Referer"); + checkState(referer != null, "The Referer header was not specified"); + + String refererPath = referer; + try { + refererPath = new URL(referer).getPath(); + } catch(MalformedURLException e) { + //Maybe a relative path? + refererPath = referer; + } + + String path = request.getContextPath(); + return refererPath.substring(refererPath.indexOf(path) + path.length()); + } + +} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java index 75514260a..a4cbf73c1 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java @@ -1,153 +1,196 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Method; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.DefaultControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.DefaultReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.Router; - -public class DefaultRefererResultTest { - - private @Mock Result result; - private @Mock MutableRequest request; - private @Mock Router router; - private @Mock ParametersProvider provider; - private DefaultRefererResult refererResult; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.forward(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.redirect(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.forward(); - - verify(page).forwardTo("/no-controller"); - } - - @Test - public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.redirect(); - - verify(page).redirectTo("/no-controller"); - } - - public static class RefererController { - public void index() { - - } - } - - @Test - public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - @Test - public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Method; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.DefaultControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.DefaultReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.Router; + +public class DefaultRefererResultTest { + + private @Mock Result result; + private @Mock MutableRequest request; + private @Mock Router router; + private @Mock ParametersProvider provider; + private DefaultRefererResult refererResult; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.forward(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.redirect(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.forward(); + + verify(page).forwardTo("/no-controller"); + } + + @Test + public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.redirect(); + + verify(page).redirectTo("/no-controller"); + } + + public static class RefererController { + public void index() { + + } + } + + @Test + public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + @Test + public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + +} From f41182d4075efb7c09d96daed28acf58d7b83c30 Mon Sep 17 00:00:00 2001 From: valeriolopes Date: Thu, 19 Mar 2015 13:14:37 -0300 Subject: [PATCH 2/6] fixed referer retrieval bug - if the context path string was present in the referer host an invalid referer was returned --- .../vraptor/view/DefaultRefererResult.java | 224 +++++----- .../view/DefaultRefererResultTest.java | 392 +++++++++--------- 2 files changed, 308 insertions(+), 308 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java index c5087532a..5939d3e74 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java @@ -1,112 +1,112 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - * All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static com.google.common.base.Preconditions.checkState; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.ReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.MethodNotAllowedException; -import br.com.caelum.vraptor.http.route.Router; -import br.com.caelum.vraptor.validator.Message; - -@RequestScoped -public class DefaultRefererResult implements RefererResult { - - private final MutableRequest request; - private final Result result; - private final Router router; - private final ParametersProvider provider; - private ReflectionProvider reflectionProvider; - - /** - * @deprecated CDI eyes only - */ - protected DefaultRefererResult() { - this(null, null, null, null, null); - } - - @Inject - public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, - ReflectionProvider reflectionProvider) { - this.result = result; - this.request = request; - this.router = router; - this.provider = provider; - this.reflectionProvider = reflectionProvider; - } - - @Override - public void forward() throws IllegalStateException { - String referer = getReferer(); - - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).forwardTo(referer); - } - } - - private void executeMethod(ControllerMethod method, Object instance) { - Object[] args = provider.getParametersFor(method, new ArrayList()); - reflectionProvider.invoke(instance, method.getMethod(), args); - } - - @Override - public void redirect() throws IllegalStateException { - String referer = getReferer(); - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).redirectTo(referer); - } - } - - private String getReferer() { - String referer = request.getHeader("Referer"); - checkState(referer != null, "The Referer header was not specified"); - - String refererPath = referer; - try { - refererPath = new URL(referer).getPath(); - } catch(MalformedURLException e) { - //Maybe a relative path? - refererPath = referer; - } - - String path = request.getContextPath(); - return refererPath.substring(refererPath.indexOf(path) + path.length()); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource + * All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static com.google.common.base.Preconditions.checkState; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.ReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.MethodNotAllowedException; +import br.com.caelum.vraptor.http.route.Router; +import br.com.caelum.vraptor.validator.Message; + +@RequestScoped +public class DefaultRefererResult implements RefererResult { + + private final MutableRequest request; + private final Result result; + private final Router router; + private final ParametersProvider provider; + private ReflectionProvider reflectionProvider; + + /** + * @deprecated CDI eyes only + */ + protected DefaultRefererResult() { + this(null, null, null, null, null); + } + + @Inject + public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, + ReflectionProvider reflectionProvider) { + this.result = result; + this.request = request; + this.router = router; + this.provider = provider; + this.reflectionProvider = reflectionProvider; + } + + @Override + public void forward() throws IllegalStateException { + String referer = getReferer(); + + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).forwardTo(referer); + } + } + + private void executeMethod(ControllerMethod method, Object instance) { + Object[] args = provider.getParametersFor(method, new ArrayList()); + reflectionProvider.invoke(instance, method.getMethod(), args); + } + + @Override + public void redirect() throws IllegalStateException { + String referer = getReferer(); + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).redirectTo(referer); + } + } + + private String getReferer() { + String referer = request.getHeader("Referer"); + checkState(referer != null, "The Referer header was not specified"); + + String refererPath = null; + try { + refererPath = new URL(referer).getPath(); + } catch(MalformedURLException e) { + //Maybe a relative path? + refererPath = referer; + } + + String ctxPath = request.getContextPath(); + return refererPath.substring(refererPath.indexOf(ctxPath) + ctxPath.length()); + } + +} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java index a4cbf73c1..ff4556bc0 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java @@ -1,196 +1,196 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Method; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.DefaultControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.DefaultReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.Router; - -public class DefaultRefererResultTest { - - private @Mock Result result; - private @Mock MutableRequest request; - private @Mock Router router; - private @Mock ParametersProvider provider; - private DefaultRefererResult refererResult; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.forward(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.redirect(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.forward(); - - verify(page).forwardTo("/no-controller"); - } - - @Test - public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.redirect(); - - verify(page).redirectTo("/no-controller"); - } - - public static class RefererController { - public void index() { - - } - } - - @Test - public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - @Test - public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } - - @Test - public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); - when(request.getContextPath()).thenReturn("/vrap"); - when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); - - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - - @Test - public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); - when(request.getContextPath()).thenReturn("/vrap"); - when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); - - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Method; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.DefaultControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.DefaultReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.Router; + +public class DefaultRefererResultTest { + + private @Mock Result result; + private @Mock MutableRequest request; + private @Mock Router router; + private @Mock ParametersProvider provider; + private DefaultRefererResult refererResult; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.forward(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.redirect(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.forward(); + + verify(page).forwardTo("/no-controller"); + } + + @Test + public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.redirect(); + + verify(page).redirectTo("/no-controller"); + } + + public static class RefererController { + public void index() { + + } + } + + @Test + public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + @Test + public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + +} From 93b9a96f392ca4880c9f9b7e3653bf1dd1d90264 Mon Sep 17 00:00:00 2001 From: valeriolopes Date: Thu, 19 Mar 2015 13:29:50 -0300 Subject: [PATCH 3/6] Revert "fixed referer retrieval bug - if the context path string was present in" This reverts commit f41182d4075efb7c09d96daed28acf58d7b83c30. --- .../vraptor/view/DefaultRefererResult.java | 224 +++++----- .../view/DefaultRefererResultTest.java | 392 +++++++++--------- 2 files changed, 308 insertions(+), 308 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java index 5939d3e74..c5087532a 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java @@ -1,112 +1,112 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - * All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static com.google.common.base.Preconditions.checkState; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.ReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.MethodNotAllowedException; -import br.com.caelum.vraptor.http.route.Router; -import br.com.caelum.vraptor.validator.Message; - -@RequestScoped -public class DefaultRefererResult implements RefererResult { - - private final MutableRequest request; - private final Result result; - private final Router router; - private final ParametersProvider provider; - private ReflectionProvider reflectionProvider; - - /** - * @deprecated CDI eyes only - */ - protected DefaultRefererResult() { - this(null, null, null, null, null); - } - - @Inject - public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, - ReflectionProvider reflectionProvider) { - this.result = result; - this.request = request; - this.router = router; - this.provider = provider; - this.reflectionProvider = reflectionProvider; - } - - @Override - public void forward() throws IllegalStateException { - String referer = getReferer(); - - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).forwardTo(referer); - } - } - - private void executeMethod(ControllerMethod method, Object instance) { - Object[] args = provider.getParametersFor(method, new ArrayList()); - reflectionProvider.invoke(instance, method.getMethod(), args); - } - - @Override - public void redirect() throws IllegalStateException { - String referer = getReferer(); - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).redirectTo(referer); - } - } - - private String getReferer() { - String referer = request.getHeader("Referer"); - checkState(referer != null, "The Referer header was not specified"); - - String refererPath = null; - try { - refererPath = new URL(referer).getPath(); - } catch(MalformedURLException e) { - //Maybe a relative path? - refererPath = referer; - } - - String ctxPath = request.getContextPath(); - return refererPath.substring(refererPath.indexOf(ctxPath) + ctxPath.length()); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource + * All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static com.google.common.base.Preconditions.checkState; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.ReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.MethodNotAllowedException; +import br.com.caelum.vraptor.http.route.Router; +import br.com.caelum.vraptor.validator.Message; + +@RequestScoped +public class DefaultRefererResult implements RefererResult { + + private final MutableRequest request; + private final Result result; + private final Router router; + private final ParametersProvider provider; + private ReflectionProvider reflectionProvider; + + /** + * @deprecated CDI eyes only + */ + protected DefaultRefererResult() { + this(null, null, null, null, null); + } + + @Inject + public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, + ReflectionProvider reflectionProvider) { + this.result = result; + this.request = request; + this.router = router; + this.provider = provider; + this.reflectionProvider = reflectionProvider; + } + + @Override + public void forward() throws IllegalStateException { + String referer = getReferer(); + + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).forwardTo(referer); + } + } + + private void executeMethod(ControllerMethod method, Object instance) { + Object[] args = provider.getParametersFor(method, new ArrayList()); + reflectionProvider.invoke(instance, method.getMethod(), args); + } + + @Override + public void redirect() throws IllegalStateException { + String referer = getReferer(); + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).redirectTo(referer); + } + } + + private String getReferer() { + String referer = request.getHeader("Referer"); + checkState(referer != null, "The Referer header was not specified"); + + String refererPath = referer; + try { + refererPath = new URL(referer).getPath(); + } catch(MalformedURLException e) { + //Maybe a relative path? + refererPath = referer; + } + + String path = request.getContextPath(); + return refererPath.substring(refererPath.indexOf(path) + path.length()); + } + +} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java index ff4556bc0..a4cbf73c1 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java @@ -1,196 +1,196 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Method; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.DefaultControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.DefaultReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.Router; - -public class DefaultRefererResultTest { - - private @Mock Result result; - private @Mock MutableRequest request; - private @Mock Router router; - private @Mock ParametersProvider provider; - private DefaultRefererResult refererResult; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.forward(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.redirect(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.forward(); - - verify(page).forwardTo("/no-controller"); - } - - @Test - public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.redirect(); - - verify(page).redirectTo("/no-controller"); - } - - public static class RefererController { - public void index() { - - } - } - - @Test - public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - @Test - public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } - - @Test - public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); - when(request.getContextPath()).thenReturn("/vrap"); - when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); - - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - - @Test - public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); - when(request.getContextPath()).thenReturn("/vrap"); - when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); - - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Method; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.DefaultControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.DefaultReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.Router; + +public class DefaultRefererResultTest { + + private @Mock Result result; + private @Mock MutableRequest request; + private @Mock Router router; + private @Mock ParametersProvider provider; + private DefaultRefererResult refererResult; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.forward(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.redirect(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.forward(); + + verify(page).forwardTo("/no-controller"); + } + + @Test + public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.redirect(); + + verify(page).redirectTo("/no-controller"); + } + + public static class RefererController { + public void index() { + + } + } + + @Test + public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + @Test + public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + +} From 3f05d6f87f97c5af9fc4871fc9774bdc01e64458 Mon Sep 17 00:00:00 2001 From: valeriolopes Date: Thu, 19 Mar 2015 13:31:14 -0300 Subject: [PATCH 4/6] Revert "fixed referer retrieval bug (if the context path string was present in" This reverts commit fa7065ccedcf527ba162d3817553ac044e734295. --- .../vraptor/view/DefaultRefererResult.java | 214 +++++------ .../view/DefaultRefererResultTest.java | 349 ++++++++---------- 2 files changed, 255 insertions(+), 308 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java index c5087532a..ff53b023f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java @@ -1,112 +1,102 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - * All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static com.google.common.base.Preconditions.checkState; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.ReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.MethodNotAllowedException; -import br.com.caelum.vraptor.http.route.Router; -import br.com.caelum.vraptor.validator.Message; - -@RequestScoped -public class DefaultRefererResult implements RefererResult { - - private final MutableRequest request; - private final Result result; - private final Router router; - private final ParametersProvider provider; - private ReflectionProvider reflectionProvider; - - /** - * @deprecated CDI eyes only - */ - protected DefaultRefererResult() { - this(null, null, null, null, null); - } - - @Inject - public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, - ReflectionProvider reflectionProvider) { - this.result = result; - this.request = request; - this.router = router; - this.provider = provider; - this.reflectionProvider = reflectionProvider; - } - - @Override - public void forward() throws IllegalStateException { - String referer = getReferer(); - - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).forwardTo(referer); - } - } - - private void executeMethod(ControllerMethod method, Object instance) { - Object[] args = provider.getParametersFor(method, new ArrayList()); - reflectionProvider.invoke(instance, method.getMethod(), args); - } - - @Override - public void redirect() throws IllegalStateException { - String referer = getReferer(); - try { - ControllerMethod method = router.parse(referer, HttpMethod.GET, request); - executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); - } catch (ControllerNotFoundException | MethodNotAllowedException e) { - result.use(page()).redirectTo(referer); - } - } - - private String getReferer() { - String referer = request.getHeader("Referer"); - checkState(referer != null, "The Referer header was not specified"); - - String refererPath = referer; - try { - refererPath = new URL(referer).getPath(); - } catch(MalformedURLException e) { - //Maybe a relative path? - refererPath = referer; - } - - String path = request.getContextPath(); - return refererPath.substring(refererPath.indexOf(path) + path.length()); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource + * All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static com.google.common.base.Preconditions.checkState; + +import java.util.ArrayList; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.ReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.MethodNotAllowedException; +import br.com.caelum.vraptor.http.route.Router; +import br.com.caelum.vraptor.validator.Message; + +@RequestScoped +public class DefaultRefererResult implements RefererResult { + + private final MutableRequest request; + private final Result result; + private final Router router; + private final ParametersProvider provider; + private ReflectionProvider reflectionProvider; + + /** + * @deprecated CDI eyes only + */ + protected DefaultRefererResult() { + this(null, null, null, null, null); + } + + @Inject + public DefaultRefererResult(Result result, MutableRequest request, Router router, ParametersProvider provider, + ReflectionProvider reflectionProvider) { + this.result = result; + this.request = request; + this.router = router; + this.provider = provider; + this.reflectionProvider = reflectionProvider; + } + + @Override + public void forward() throws IllegalStateException { + String referer = getReferer(); + + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).forwardTo(referer); + } + } + + private void executeMethod(ControllerMethod method, Object instance) { + Object[] args = provider.getParametersFor(method, new ArrayList()); + reflectionProvider.invoke(instance, method.getMethod(), args); + } + + @Override + public void redirect() throws IllegalStateException { + String referer = getReferer(); + try { + ControllerMethod method = router.parse(referer, HttpMethod.GET, request); + executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); + } catch (ControllerNotFoundException | MethodNotAllowedException e) { + result.use(page()).redirectTo(referer); + } + } + + private String getReferer() { + String referer = request.getHeader("Referer"); + checkState(referer != null, "The Referer header was not specified"); + + String path = request.getContextPath(); + return referer.substring(referer.indexOf(path) + path.length()); + } + +} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java index a4cbf73c1..75514260a 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java @@ -1,196 +1,153 @@ -/*** - * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. - * - * 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 br.com.caelum.vraptor.view; - -import static br.com.caelum.vraptor.view.Results.logic; -import static br.com.caelum.vraptor.view.Results.page; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Method; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import br.com.caelum.vraptor.Result; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.controller.DefaultControllerMethod; -import br.com.caelum.vraptor.controller.HttpMethod; -import br.com.caelum.vraptor.core.DefaultReflectionProvider; -import br.com.caelum.vraptor.http.MutableRequest; -import br.com.caelum.vraptor.http.ParametersProvider; -import br.com.caelum.vraptor.http.route.ControllerNotFoundException; -import br.com.caelum.vraptor.http.route.Router; - -public class DefaultRefererResultTest { - - private @Mock Result result; - private @Mock MutableRequest request; - private @Mock Router router; - private @Mock ParametersProvider provider; - private DefaultRefererResult refererResult; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.forward(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { - when(request.getHeader("Referer")).thenReturn(null); - - try { - refererResult.redirect(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.forward(); - - verify(page).forwardTo("/no-controller"); - } - - @Test - public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { - PageResult page = mock(PageResult.class); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); - doReturn(page).when(result).use(page()); - - refererResult.redirect(); - - verify(page).redirectTo("/no-controller"); - } - - public static class RefererController { - public void index() { - - } - } - - @Test - public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - @Test - public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); - when(request.getContextPath()).thenReturn("/vraptor"); - when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } - - @Test - public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); - when(request.getContextPath()).thenReturn("/vrap"); - when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); - - doReturn(logic).when(result).use(logic()); - when(logic.redirectTo(RefererController.class)).thenReturn(controller); - - refererResult.redirect(); - - verify(logic).redirectTo(RefererController.class); - verify(controller).index(); - } - - @Test - public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { - LogicResult logic = mock(LogicResult.class); - RefererController controller = mock(RefererController.class); - - Method index = RefererController.class.getMethod("index"); - ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); - - when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); - when(request.getContextPath()).thenReturn("/vrap"); - when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); - - doReturn(logic).when(result).use(logic()); - when(logic.forwardTo(RefererController.class)).thenReturn(controller); - - refererResult.forward(); - - verify(logic).forwardTo(RefererController.class); - verify(controller).index(); - } - -} +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved. + * + * 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 br.com.caelum.vraptor.view; + +import static br.com.caelum.vraptor.view.Results.logic; +import static br.com.caelum.vraptor.view.Results.page; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Method; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.controller.DefaultControllerMethod; +import br.com.caelum.vraptor.controller.HttpMethod; +import br.com.caelum.vraptor.core.DefaultReflectionProvider; +import br.com.caelum.vraptor.http.MutableRequest; +import br.com.caelum.vraptor.http.ParametersProvider; +import br.com.caelum.vraptor.http.route.ControllerNotFoundException; +import br.com.caelum.vraptor.http.route.Router; + +public class DefaultRefererResultTest { + + private @Mock Result result; + private @Mock MutableRequest request; + private @Mock Router router; + private @Mock ParametersProvider provider; + private DefaultRefererResult refererResult; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + refererResult = new DefaultRefererResult(result, request, router, provider, new DefaultReflectionProvider()); + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.forward(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception { + when(request.getHeader("Referer")).thenReturn(null); + + try { + refererResult.redirect(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + + } + } + + @Test + public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.forward(); + + verify(page).forwardTo("/no-controller"); + } + + @Test + public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { + PageResult page = mock(PageResult.class); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); + doReturn(page).when(result).use(page()); + + refererResult.redirect(); + + verify(page).redirectTo("/no-controller"); + } + + public static class RefererController { + public void index() { + + } + } + + @Test + public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + @Test + public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); + when(request.getContextPath()).thenReturn("/vraptor"); + when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method); + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } +} From 602c752939c698fed3ce3113b7f19f483617052a Mon Sep 17 00:00:00 2001 From: valeriolopes Date: Thu, 19 Mar 2015 13:36:32 -0300 Subject: [PATCH 5/6] Fix referer retrieval bug If the context path string was present in the referer host, the getReferer() method returned an invalid referer --- .../vraptor/view/DefaultRefererResult.java | 13 +++++- .../view/DefaultRefererResultTest.java | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java index ff53b023f..85f0129c7 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java @@ -21,6 +21,8 @@ import static br.com.caelum.vraptor.view.Results.page; import static com.google.common.base.Preconditions.checkState; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import javax.enterprise.context.RequestScoped; @@ -95,8 +97,15 @@ private String getReferer() { String referer = request.getHeader("Referer"); checkState(referer != null, "The Referer header was not specified"); - String path = request.getContextPath(); - return referer.substring(referer.indexOf(path) + path.length()); + String refererPath= null; + try { + refererPath = new URL(referer).getPath(); + } catch(MalformedURLException e) { + //Maybe a relative path? + refererPath = referer; + } + String ctxPath = request.getContextPath(); + return refererPath.substring(refererPath.indexOf(ctxPath) + ctxPath.length()); } } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java index 75514260a..e1e913aee 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java @@ -150,4 +150,47 @@ public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception { verify(logic).forwardTo(RefererController.class); verify(controller).index(); } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldRedirectCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/vrap/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + } From cecb90ef08ec0f9efa5ebcca3edf3b5ea29fd400 Mon Sep 17 00:00:00 2001 From: valeriolopes Date: Thu, 19 Mar 2015 14:16:27 -0300 Subject: [PATCH 6/6] Enhancement on referer retrieval On proxied app servers is common to have the context path suppressed from the external URL, although it is still valid within the app server context. If the request referer path does not start with the internal context, it should return the entire path. --- .../vraptor/view/DefaultRefererResult.java | 5 ++- .../view/DefaultRefererResultTest.java | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java index 85f0129c7..9dbd1d022 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/view/DefaultRefererResult.java @@ -105,7 +105,10 @@ private String getReferer() { refererPath = referer; } String ctxPath = request.getContextPath(); - return refererPath.substring(refererPath.indexOf(ctxPath) + ctxPath.length()); + + //if the context path is not in the beggining we should return the entire path + //this is useful for proxied app servers which hide the ctx path from url + return refererPath.startsWith(ctxPath) ? refererPath.substring(ctxPath.length()) : refererPath; } } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java index e1e913aee..2bb90d954 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java @@ -193,4 +193,46 @@ public void whenRefererContainsCtxPathStrInTheHostItShouldForwardCorrectly() thr verify(controller).index(); } + @Test + public void whenRefererDoesNotContainsCtxPathItShouldRedirectCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.redirectTo(RefererController.class)).thenReturn(controller); + + refererResult.redirect(); + + verify(logic).redirectTo(RefererController.class); + verify(controller).index(); + } + + @Test + public void whenRefererDoesNotContainsCtxPathItShouldForwardCorrectly() throws Exception { + LogicResult logic = mock(LogicResult.class); + RefererController controller = mock(RefererController.class); + + Method index = RefererController.class.getMethod("index"); + ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index); + + when(request.getHeader("Referer")).thenReturn("http://vraptor.test.com/anything/ok"); + when(request.getContextPath()).thenReturn("/vrap"); + when(router.parse("/anything/ok", HttpMethod.GET, request)).thenReturn(method); + + doReturn(logic).when(result).use(logic()); + when(logic.forwardTo(RefererController.class)).thenReturn(controller); + + refererResult.forward(); + + verify(logic).forwardTo(RefererController.class); + verify(controller).index(); + } + }