From: Artur Signell Date: Mon, 19 Nov 2012 18:19:47 +0000 (+0200) Subject: Changed SystemMessagesProvider parameter into object (#10226) X-Git-Tag: 7.0.0.beta10~97 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1aceeea98320b5c5f6fd71055ef718f1c305e44e;p=vaadin-framework.git Changed SystemMessagesProvider parameter into object (#10226) Change-Id: I7ba22460ae5105e29b88b17bc3e3ac664e9ca980 --- diff --git a/server/src/com/vaadin/server/AbstractCommunicationManager.java b/server/src/com/vaadin/server/AbstractCommunicationManager.java index 1481085247..834d7d8a17 100644 --- a/server/src/com/vaadin/server/AbstractCommunicationManager.java +++ b/server/src/com/vaadin/server/AbstractCommunicationManager.java @@ -590,7 +590,7 @@ public abstract class AbstractCommunicationManager implements Serializable { // var inconsistency; the client is probably out-of-sync SystemMessages ci = response.getService().getSystemMessages( - uI.getLocale()); + uI.getLocale(), request); String msg = ci.getOutOfSyncMessage(); String cap = ci.getOutOfSyncCaption(); if (msg != null || cap != null) { @@ -1042,7 +1042,7 @@ public abstract class AbstractCommunicationManager implements Serializable { } SystemMessages ci = request.getService().getSystemMessages( - ui.getLocale()); + ui.getLocale(), request); // meta instruction for client to enable auto-forward to // sessionExpiredURL after timer expires. diff --git a/server/src/com/vaadin/server/BootstrapHandler.java b/server/src/com/vaadin/server/BootstrapHandler.java index d0e87d5e08..36a09253d9 100644 --- a/server/src/com/vaadin/server/BootstrapHandler.java +++ b/server/src/com/vaadin/server/BootstrapHandler.java @@ -413,7 +413,8 @@ public abstract class BootstrapHandler implements RequestHandler { Locale locale = ServletPortletHelper.findLocale(null, context.getSession(), context.getRequest()); // Get system messages - SystemMessages systemMessages = vaadinService.getSystemMessages(locale); + SystemMessages systemMessages = vaadinService.getSystemMessages(locale, + request); if (systemMessages != null) { // Write the CommunicationError -message to client JSONObject comErrMsg = new JSONObject(); diff --git a/server/src/com/vaadin/server/DefaultSystemMessagesProvider.java b/server/src/com/vaadin/server/DefaultSystemMessagesProvider.java index 08042d3848..7111a8bbbb 100644 --- a/server/src/com/vaadin/server/DefaultSystemMessagesProvider.java +++ b/server/src/com/vaadin/server/DefaultSystemMessagesProvider.java @@ -16,7 +16,6 @@ package com.vaadin.server; -import java.util.Locale; /** * System messages provider using the built-in default system messages. This @@ -34,7 +33,8 @@ public class DefaultSystemMessagesProvider implements SystemMessagesProvider { } @Override - public SystemMessages getSystemMessages(Locale locale) { + public SystemMessages getSystemMessages( + SystemMessagesInfo systemMessagesInfo) { return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; } diff --git a/server/src/com/vaadin/server/SystemMessages.java b/server/src/com/vaadin/server/SystemMessages.java index 647266d8e3..f36a8e8570 100644 --- a/server/src/com/vaadin/server/SystemMessages.java +++ b/server/src/com/vaadin/server/SystemMessages.java @@ -22,8 +22,8 @@ import java.io.Serializable; * Contains the system messages used to notify the user about various critical * situations that can occur. *

- * Customize by overriding the static {@link VaadinSession#getSystemMessages()} - * and returning {@link CustomizedSystemMessages}. + * Use {@link VaadinService#setSystemMessagesProvider(SystemMessagesProvider)} + * to customize. *

*

* The defaults defined in this class are: diff --git a/server/src/com/vaadin/server/SystemMessagesInfo.java b/server/src/com/vaadin/server/SystemMessagesInfo.java new file mode 100644 index 0000000000..30a6e82a90 --- /dev/null +++ b/server/src/com/vaadin/server/SystemMessagesInfo.java @@ -0,0 +1,53 @@ +package com.vaadin.server; + +import java.io.Serializable; +import java.util.Locale; + +public class SystemMessagesInfo implements Serializable { + + private Locale locale; + private VaadinRequest request; + private VaadinService service; + + /** + * The locale of the UI related to the {@link SystemMessages} request. + * + * @return The Locale or null if the locale is not known + */ + public Locale getLocale() { + return locale; + } + + public void setLocale(Locale locale) { + this.locale = locale; + } + + /** + * Gets the request currently in progress. + * + * @return The request currently in progress or null if no request is in + * progress. + */ + public VaadinRequest getRequest() { + return request; + } + + public void setRequest(VaadinRequest request) { + this.request = request; + } + + /** + * Returns the service this SystemMessages request comes from. + * + * @return The service which triggered this request or null of not triggered + * from a service. + */ + public VaadinService getService() { + return service; + } + + public void setService(VaadinService service) { + this.service = service; + } + +} diff --git a/server/src/com/vaadin/server/SystemMessagesProvider.java b/server/src/com/vaadin/server/SystemMessagesProvider.java index 21263a2950..caef8de420 100644 --- a/server/src/com/vaadin/server/SystemMessagesProvider.java +++ b/server/src/com/vaadin/server/SystemMessagesProvider.java @@ -17,7 +17,6 @@ package com.vaadin.server; import java.io.Serializable; -import java.util.Locale; import com.vaadin.ui.UI; @@ -30,16 +29,16 @@ import com.vaadin.ui.UI; */ public interface SystemMessagesProvider extends Serializable { /** - * Gets the system messages to use in the given context. Locale is the only - * piece of information guaranteed to be available, but in most cases some - * or all of {@link VaadinService#getCurrent()}, - * {@link VaadinService#getCurrentRequest()}, - * {@link VaadinSession#getCurrent()} and {@link UI#getCurrent()} can also - * be used to find more information to help the decision. + * Gets the system messages to use in the given context. The + * {@link SystemMessagesInfo} object contains available information but in + * most cases some or both of {@link VaadinSession#getCurrent()} and + * {@link UI#getCurrent()} can also be used to find more information to help + * the decision. * - * @param locale - * the desired locale of the system messages + * @param systemMessagesInfo + * Locale, current request and other information available. * @return a system messages object */ - public SystemMessages getSystemMessages(Locale locale); + public SystemMessages getSystemMessages( + SystemMessagesInfo systemMessagesInfo); } diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 51a71f1cd6..b1843ce9e5 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -628,7 +628,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants { if (getRequestType(request) == RequestType.UIDL) { SystemMessages ci = getService().getSystemMessages( ServletPortletHelper.findLocale(null, vaadinSession, - request)); + request), request); criticalNotification(request, response, ci.getInternalErrorCaption(), ci.getInternalErrorMessage(), null, ci.getInternalErrorURL()); diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index 2a3c04c49e..a4caf6cdd6 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -250,10 +250,16 @@ public abstract class VaadinService implements Serializable { * * @param locale * the desired locale for the system messages + * @param request * @return the system messages to use */ - public SystemMessages getSystemMessages(Locale locale) { - return getSystemMessagesProvider().getSystemMessages(locale); + public SystemMessages getSystemMessages(Locale locale, VaadinRequest request) { + SystemMessagesInfo systemMessagesInfo = new SystemMessagesInfo(); + systemMessagesInfo.setLocale(locale); + systemMessagesInfo.setService(this); + systemMessagesInfo.setRequest(request); + return getSystemMessagesProvider() + .getSystemMessages(systemMessagesInfo); } /** diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index 48e5304cc7..2ef9550823 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -418,7 +418,8 @@ public class VaadinServlet extends HttpServlet implements Constants { if (request.getRequestedSessionId() == null) { // User has cookies disabled SystemMessages systemMessages = getService().getSystemMessages( - ServletPortletHelper.findLocale(null, null, request)); + ServletPortletHelper.findLocale(null, null, request), + request); criticalNotification(request, response, systemMessages.getCookiesDisabledCaption(), systemMessages.getCookiesDisabledMessage(), null, @@ -576,7 +577,7 @@ public class VaadinServlet extends HttpServlet implements Constants { if (getRequestType(request) == RequestType.UIDL) { SystemMessages ci = getService().getSystemMessages( ServletPortletHelper.findLocale(null, vaadinSession, - request)); + request), request); criticalNotification(request, response, ci.getInternalErrorCaption(), ci.getInternalErrorMessage(), null, ci.getInternalErrorURL()); @@ -652,7 +653,8 @@ public class VaadinServlet extends HttpServlet implements Constants { try { SystemMessages ci = getService().getSystemMessages( - ServletPortletHelper.findLocale(null, null, request)); + ServletPortletHelper.findLocale(null, null, request), + request); RequestType requestType = getRequestType(request); if (requestType == RequestType.UIDL) { /* @@ -702,7 +704,7 @@ public class VaadinServlet extends HttpServlet implements Constants { * this case so just use the info provided in the request. */ SystemMessages ci = getService().getSystemMessages( - request.getLocale()); + request.getLocale(), request); RequestType requestType = getRequestType(request); if (requestType == RequestType.UIDL) { // send uidl redirect diff --git a/uitest/src/com/vaadin/tests/applicationservlet/SystemMessagesTest.html b/uitest/src/com/vaadin/tests/applicationservlet/SystemMessagesTest.html new file mode 100644 index 0000000000..a764255b5b --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationservlet/SystemMessagesTest.html @@ -0,0 +1,52 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.applicationservlet.SystemMessagesTest?restartApplication
clickvaadin=runcomvaadintestsapplicationservletSystemMessagesTest::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationservletSystemMessagesTest::Root/VNotification[0]/domChild[0]Internal error*MessagesInfo locale: fi_FI
open/run/com.vaadin.tests.applicationservlet.SystemMessagesTest?restartApplication
selectvaadin=runcomvaadintestsapplicationservletSystemMessagesTest::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VNativeSelect[0]/domChild[0]label=de_DE
clickvaadin=runcomvaadintestsapplicationservletSystemMessagesTest::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationservletSystemMessagesTest::Root/VNotification[0]/domChild[0]Internal error*MessagesInfo locale: de_DE
+ + diff --git a/uitest/src/com/vaadin/tests/applicationservlet/SystemMessagesTest.java b/uitest/src/com/vaadin/tests/applicationservlet/SystemMessagesTest.java new file mode 100644 index 0000000000..516c0ca8e0 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationservlet/SystemMessagesTest.java @@ -0,0 +1,90 @@ +package com.vaadin.tests.applicationservlet; + +import java.util.Locale; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.server.CustomizedSystemMessages; +import com.vaadin.server.SystemMessages; +import com.vaadin.server.SystemMessagesInfo; +import com.vaadin.server.SystemMessagesProvider; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.NativeSelect; + +public class SystemMessagesTest extends AbstractTestUI { + + public class MyButton extends Button { + private boolean fail = false; + + @Override + public void beforeClientResponse(boolean initial) { + super.beforeClientResponse(initial); + if (fail) { + throw new RuntimeException("Failed on purpose"); + } + } + + } + + @Override + protected void setup(VaadinRequest request) { + final NativeSelect localeSelect = new NativeSelect("UI locale"); + localeSelect.setImmediate(true); + localeSelect.addItem(new Locale("en", "US")); + localeSelect.addItem(new Locale("fi", "FI")); + localeSelect.addItem(Locale.GERMANY); + localeSelect.addValueChangeListener(new ValueChangeListener() { + + @Override + public void valueChange(ValueChangeEvent event) { + setLocale((Locale) localeSelect.getValue()); + getSession().getService().setSystemMessagesProvider( + new SystemMessagesProvider() { + + @Override + public SystemMessages getSystemMessages( + SystemMessagesInfo systemMessagesInfo) { + CustomizedSystemMessages csm = new CustomizedSystemMessages(); + // csm.setInternalErrorCaption("Request query string: " + // + ((VaadinServletRequest) systemMessagesInfo + // .getRequest()).getQueryString()); + csm.setInternalErrorMessage("MessagesInfo locale: " + + systemMessagesInfo.getLocale()); + return csm; + + } + }); + } + }); + localeSelect.setValue(new Locale("fi", "FI")); + addComponent(localeSelect); + final MyButton failButton = new MyButton(); + failButton.setCaption("Generate server side error"); + failButton.addClickListener(new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + failButton.fail = true; + } + }); + addComponent(failButton); + + } + + @Override + protected String getTestDescription() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected Integer getTicketNumber() { + // TODO Auto-generated method stub + return null; + } + +}