if (!handleVariables(request, response, callback, session, uI)) {
// var inconsistency; the client is probably out-of-sync
- SystemMessages ci = response.getService().getSystemMessages();
+ SystemMessages ci = response.getService().getSystemMessages(
+ uI.getLocale());
String msg = ci.getOutOfSyncMessage();
String cap = ci.getOutOfSyncCaption();
if (msg != null || cap != null) {
}
}
- SystemMessages ci = request.getService().getSystemMessages();
+ SystemMessages ci = request.getService().getSystemMessages(
+ ui.getLocale());
// meta instruction for client to enable auto-forward to
// sessionExpiredURL after timer expires.
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
appConfig.put("initialParams", parameterMap);
+ // Use locale from session if set, else from the request
+ Locale locale = ServletPortletHelper.findLocale(null,
+ context.getSession(), context.getRequest());
// Get system messages
- SystemMessages systemMessages = vaadinService.getSystemMessages();
+ SystemMessages systemMessages = vaadinService.getSystemMessages(locale);
if (systemMessages != null) {
// Write the CommunicationError -message to client
JSONObject comErrMsg = new JSONObject();
--- /dev/null
+/*
+ * Copyright 2012 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.server;
+
+import java.util.Locale;
+
+/**
+ * System messages provider using the built-in default system messages. This
+ * singleton is accessed using {@link #get()}.
+ *
+ * @author Vaadin Ltd
+ * @since 7.0.0
+ */
+public class DefaultSystemMessagesProvider implements SystemMessagesProvider {
+
+ private static final DefaultSystemMessagesProvider instance = new DefaultSystemMessagesProvider();
+
+ private DefaultSystemMessagesProvider() {
+ // Singleton
+ }
+
+ @Override
+ public SystemMessages getSystemMessages(Locale locale) {
+ return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES;
+ }
+
+ /**
+ * Gets the instance.
+ *
+ * @return the default system messages provider.
+ */
+ public static SystemMessagesProvider get() {
+ return instance;
+ }
+
+}
package com.vaadin.server;
import java.io.Serializable;
+import java.util.Locale;
import java.util.Properties;
import com.vaadin.LegacyApplication;
import com.vaadin.shared.ApplicationConstants;
+import com.vaadin.ui.Component;
import com.vaadin.ui.UI;
/*
}
}
+ /**
+ * Helper to find the most most suitable Locale. These potential sources are
+ * checked in order until a Locale is found:
+ * <ol>
+ * <li>The passed component (or UI) if not null</li>
+ * <li>{@link UI#getCurrent()} if defined</li>
+ * <li>The passed session if not null</li>
+ * <li>{@link VaadinServiceSession#getCurrent()} if defined</li>
+ * <li>The passed request if not null</li>
+ * <li>{@link VaadinService#getCurrentRequest()} if defined</li>
+ * <li>{@link Locale#getDefault()}</li>
+ * </ol>
+ */
+ static Locale findLocale(Component component, VaadinServiceSession session,
+ VaadinRequest request) {
+ if (component == null) {
+ component = UI.getCurrent();
+ }
+ if (component != null) {
+ Locale locale = component.getLocale();
+ if (locale != null) {
+ return locale;
+ }
+ }
+
+ if (session == null) {
+ session = VaadinServiceSession.getCurrent();
+ }
+ if (session != null) {
+ Locale locale = session.getLocale();
+ if (locale != null) {
+ return locale;
+ }
+ }
+
+ if (request == null) {
+ request = VaadinService.getCurrentRequest();
+ }
+ if (request != null) {
+ Locale locale = request.getLocale();
+ if (locale != null) {
+ return locale;
+ }
+ }
+
+ return Locale.getDefault();
+ }
+
}
--- /dev/null
+/*
+ * Copyright 2012 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.server;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+import com.vaadin.ui.UI;
+
+/**
+ * Gives out system messages based on Locale. Registered using
+ * {@link VaadinService#setSystemMessagesProvider(SystemMessagesProvider)}.
+ *
+ * @author Vaadin Ltd
+ * @since 7.0.0
+ */
+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 VaadinServiceSession#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
+ * @return a system messages object
+ */
+ public SystemMessages getSystemMessages(Locale locale);
+}
// if this was an UIDL request, response UIDL back to client
if (getRequestType(request) == RequestType.UIDL) {
- SystemMessages ci = getService().getSystemMessages();
+ SystemMessages ci = getService().getSystemMessages(
+ ServletPortletHelper.findLocale(null, vaadinSession,
+ request));
criticalNotification(request, response,
ci.getInternalErrorCaption(), ci.getInternalErrorMessage(),
null, ci.getInternalErrorURL());
return getPortlet().getPortletContext().getMimeType(resourceName);
}
- @Override
- public SystemMessages getSystemMessages() {
- return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES;
- }
-
@Override
public File getBaseDirectory() {
PortletContext context = getPortlet().getPortletContext();
private final EventRouter eventRouter = new EventRouter();
+ private SystemMessagesProvider systemMessagesProvider = DefaultSystemMessagesProvider
+ .get();
+
/**
* Creates a new vaadin service based on a deployment configuration
*
}
/**
- * Gets the system messages object
+ * Sets the system messages provider to use for getting system messages to
+ * display to users of this service.
+ *
+ * @see #getSystemMessagesProvider()
+ *
+ * @param systemMessagesProvider
+ * the system messages provider; <code>null</code> is not
+ * allowed.
+ */
+ public void setSystemMessagesProvider(
+ SystemMessagesProvider systemMessagesProvider) {
+ if (systemMessagesProvider == null) {
+ throw new IllegalArgumentException(
+ "SystemMessagesProvider can not be null.");
+ }
+ this.systemMessagesProvider = systemMessagesProvider;
+ }
+
+ /**
+ * Gets the system messages provider currently defined for this service.
+ * <p>
+ * By default, the {@link DefaultSystemMessagesProvider} which always
+ * provides the built-in default {@link SystemMessages} is used.
+ * </p>
*
- * @return the system messages object
+ * @see #setSystemMessagesProvider(SystemMessagesProvider)
+ * @see SystemMessagesProvider
+ * @see SystemMessages
+ *
+ * @return the system messages provider; not <code>null</code>
+ */
+ public SystemMessagesProvider getSystemMessagesProvider() {
+ return systemMessagesProvider;
+ }
+
+ /**
+ * Gets the system message to use for a specific locale. This method may
+ * also be implemented to use information from current instances of various
+ * objects, which means that this method might return different values for
+ * the same locale under different circumstances.
+ *
+ * @param locale
+ * the desired locale for the system messages
+ * @return the system messages to use
*/
- public abstract SystemMessages getSystemMessages();
+ public SystemMessages getSystemMessages(Locale locale) {
+ return getSystemMessagesProvider().getSystemMessages(locale);
+ }
/**
* Returns the context base directory.
// This can be removed if cookieless mode (#3228) is supported
if (request.getRequestedSessionId() == null) {
// User has cookies disabled
- SystemMessages systemMessages = getService()
- .getSystemMessages();
+ SystemMessages systemMessages = getService().getSystemMessages(
+ ServletPortletHelper.findLocale(null, null, request));
criticalNotification(request, response,
systemMessages.getCookiesDisabledCaption(),
systemMessages.getCookiesDisabledMessage(), null,
Throwable e) throws IOException, ServletException {
// if this was an UIDL request, response UIDL back to client
if (getRequestType(request) == RequestType.UIDL) {
- SystemMessages ci = getService().getSystemMessages();
+ SystemMessages ci = getService().getSystemMessages(
+ ServletPortletHelper.findLocale(null, vaadinSession,
+ request));
criticalNotification(request, response,
ci.getInternalErrorCaption(), ci.getInternalErrorMessage(),
null, ci.getInternalErrorURL());
}
try {
- SystemMessages ci = getService().getSystemMessages();
+ SystemMessages ci = getService().getSystemMessages(
+ ServletPortletHelper.findLocale(null, null, request));
RequestType requestType = getRequestType(request);
if (requestType == RequestType.UIDL) {
/*
}
try {
- SystemMessages ci = getService().getSystemMessages();
+ /*
+ * We might have a UI, but we don't want to leak any information in
+ * this case so just use the info provided in the request.
+ */
+ SystemMessages ci = getService().getSystemMessages(
+ request.getLocale());
RequestType requestType = getRequestType(request);
if (requestType == RequestType.UIDL) {
// send uidl redirect
return getServlet().getServletContext().getMimeType(resourceName);
}
- @Override
- public SystemMessages getSystemMessages() {
- return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES;
- }
-
@Override
public File getBaseDirectory() {
final String realPath = VaadinServlet.getResourcePath(