diff options
author | Artur <artur@vaadin.com> | 2017-06-15 10:21:50 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-06-15 10:21:50 +0300 |
commit | 7a0149592fff8a9d82e8e25bbc8837ed2b4382b5 (patch) | |
tree | 437056ea30eece99c18ca5cdf0d8d88cfeaf4422 /server | |
parent | 51947ca4dc73267fa5b583054bf5ca1b714002ae (diff) | |
download | vaadin-framework-7a0149592fff8a9d82e8e25bbc8837ed2b4382b5.tar.gz vaadin-framework-7a0149592fff8a9d82e8e25bbc8837ed2b4382b5.zip |
Make view parameter map available in ViewChangeEvent (#9529)
Diffstat (limited to 'server')
3 files changed, 105 insertions, 2 deletions
diff --git a/server/src/main/java/com/vaadin/navigator/Navigator.java b/server/src/main/java/com/vaadin/navigator/Navigator.java index 8b9e39b354..da5dfa0f87 100644 --- a/server/src/main/java/com/vaadin/navigator/Navigator.java +++ b/server/src/main/java/com/vaadin/navigator/Navigator.java @@ -755,6 +755,7 @@ public class Navigator implements Serializable { * * @return The parameters from the navigation state as a map * @see #getStateParameterMap(String) + * @since 8.1 */ public Map<String, String> getStateParameterMap() { return getStateParameterMap(DEFAULT_STATE_PARAMETER_SEPARATOR); @@ -771,21 +772,22 @@ public class Navigator implements Serializable { * from each other * @return The parameters from the navigation state as a map * @see #getStateParameterMap() + * @since 8.1 */ public Map<String, String> getStateParameterMap(String separator) { return parseStateParameterMap(Objects.requireNonNull(separator)); } /** - * Parses the state parameter map using given separator String. + * Parses the state parameter to a map using the given separator string. * * @param separator * the string (typically one character) used to separate values * from each other * @return The navigation state as Map<String, String>. + * @since 8.1 */ protected Map<String, String> parseStateParameterMap(String separator) { - Map<String, String> parameterMap = new HashMap<>(); if (getState() == null || getState().isEmpty()) { return Collections.emptyMap(); } @@ -800,9 +802,28 @@ public class Navigator implements Serializable { parameterString = state.substring(viewSeparatorLocation + 1, state.length()); } + return parseParameterStringToMap(parameterString, separator); + } + + /** + * Parses the given parameter string to a map using the given separator + * string. + * + * @param parameterString + * the parameter string to parse + * @param separator + * the string (typically one character) used to separate values + * from each other + * @return The navigation state as Map<String, String>. + * @since 8.1 + */ + protected Map<String, String> parseParameterStringToMap( + String parameterString, String separator) { if (parameterString.isEmpty()) { return Collections.emptyMap(); } + + Map<String, String> parameterMap = new HashMap<>(); String[] parameters = parameterString.split(separator); for (int i = 0; i < parameters.length; i++) { String[] keyAndValue = parameters[i] diff --git a/server/src/main/java/com/vaadin/navigator/ViewChangeListener.java b/server/src/main/java/com/vaadin/navigator/ViewChangeListener.java index 9238f1bae9..c5a1c68c65 100644 --- a/server/src/main/java/com/vaadin/navigator/ViewChangeListener.java +++ b/server/src/main/java/com/vaadin/navigator/ViewChangeListener.java @@ -18,6 +18,7 @@ package com.vaadin.navigator; import java.io.Serializable; import java.util.EventObject; +import java.util.Map; /** * Interface for listening to View changes before and after they occur. @@ -104,6 +105,33 @@ public interface ViewChangeListener extends Serializable { public String getParameters() { return parameters; } + + /** + * Returns the parameters for the view being activated parsed to a map, + * using {@literal &} as the parameter separator character. + * + * @return navigation parameters (potentially bookmarkable) for the new + * view + * @since 8.1 + */ + public Map<String, String> getParameterMap() { + return getParameterMap("&"); + } + + /** + * Returns the parameters for the view being activated parsed to a map, + * using the given string as the parameter separator character. + * + * @param separator + * the parameter separator string to use + * @return navigation parameters (potentially bookmarkable) for the new + * view + * @since 8.1 + */ + public Map<String, String> getParameterMap(String separator) { + return getNavigator().parseParameterStringToMap(getParameters(), + separator); + } } /** diff --git a/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java b/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java index 022b388840..fbc55d57cc 100644 --- a/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java +++ b/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.fail; import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.atomic.AtomicReference; import org.easymock.EasyMock; import org.easymock.IArgumentMatcher; @@ -1060,4 +1061,57 @@ public class NavigatorTest { manager.setState(state); return navigator; } + + @Test + public void parameterMapFromViewChangeEvent() { + // create navigator to test + Navigator navigator = createNavigatorWithState("foo"); + View view1 = EasyMock.createMock(View.class); + View view2 = EasyMock.createMock(View.class); + ViewProvider provider = new ViewProvider() { + + @Override + public String getViewName(String viewAndParameters) { + if (viewAndParameters.contains("/")) { + return viewAndParameters.substring(0, + viewAndParameters.indexOf('/')); + } else { + return viewAndParameters; + } + } + + @Override + public View getView(String viewName) { + if (viewName.equals("view1")) { + return view1; + } else if (viewName.equals("view2")) { + return view2; + } else { + return null; + } + } + }; + navigator.addProvider(provider); + + AtomicReference<Map<String, String>> mapRef = new AtomicReference<>(); + AtomicReference<Map<String, String>> mapRefB = new AtomicReference<>(); + navigator.addViewChangeListener(new ViewChangeListener() { + @Override + public boolean beforeViewChange(ViewChangeEvent event) { + mapRef.set(event.getParameterMap()); + mapRefB.set(event.getParameterMap("b")); + return true; + } + }); + + navigator.navigateTo("view1"); + + Assert.assertTrue(mapRef.get().isEmpty()); + Assert.assertTrue(mapRefB.get().isEmpty()); + navigator.navigateTo("view1/a&b=c&d"); + + assertMap(mapRef.get(), entry("a", ""), entry("b", "c"), + entry("d", "")); + assertMap(mapRefB.get(), entry("a&", ""), entry("", "c&d")); + } } |