/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* 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 de.odysseus.calyxo.control.impl;
import javax.servlet.http.HttpServletRequest;
import de.odysseus.calyxo.base.Messages;
import de.odysseus.calyxo.control.MessageSupport;
/**
* Default message support class.
* <p/>
* This class simply saves errors, warnings and infos into certain request
* scope attributes.
*
* @author Christoph Beck
*/
public class DefaultMessageSupport extends MessageSupport {
/**
* Request scope key used to save error messages.
*/
private static final String ERRORS_KEY =
"de.odysseus.calyxo.control.messages.errors";
/**
* Request scope key used to save warning messages.
*/
private static final String WARNINGS_KEY =
"de.odysseus.calyxo.control.messages.warnings";
/**
* Request scope key used to save info messages.
*/
private static final String INFOS_KEY =
"de.odysseus.calyxo.control.messages.infos";
/**
* Save the specified messages into request scope.
* This will replace any existing messages.
* @param request the request we are processing
* @param messages the messages to be saved
* @param key request scope messages key
*/
protected void set(HttpServletRequest request, Messages messages, String key) {
if (messages == null || messages.isEmpty()) {
request.removeAttribute(key);
} else {
request.setAttribute(key, messages);
}
}
/**
* Get messages from request scope.
* @param request the request we are processing
* @param create if <code>true</code>, create, save and return a new instance
* if the requested messages are not present
* @param key request scope messages key
* @return messages instance or <code>null</code>
*/
protected Messages get(HttpServletRequest request, boolean create, String key) {
Messages messages = (Messages)request.getAttribute(key);
if (messages == null && create) {
request.setAttribute(key, messages = new Messages());
}
return messages;
}
/**
* Delegate to {@link #set(HttpServletRequest, Messages, String)}.
* @see de.odysseus.calyxo.control.MessageSupport#setErrors(javax.servlet.http.HttpServletRequest, de.odysseus.calyxo.base.Messages)
*/
public void setErrors(HttpServletRequest request, Messages messages) {
set(request, messages, ERRORS_KEY);
}
/**
* Delegate to {@link #get(HttpServletRequest, boolean, String)}.
* @see de.odysseus.calyxo.control.MessageSupport#getErrors(javax.servlet.http.HttpServletRequest, boolean)
*/
public Messages getErrors(HttpServletRequest request, boolean create) {
return get(request, create, ERRORS_KEY);
}
/**
* Delegate to {@link #set(HttpServletRequest, Messages, String)}.
* @see de.odysseus.calyxo.control.MessageSupport#setWarnings(javax.servlet.http.HttpServletRequest, de.odysseus.calyxo.base.Messages)
*/
public void setWarnings(HttpServletRequest request, Messages messages) {
set(request, messages, WARNINGS_KEY);
}
/**
* Delegate to {@link #get(HttpServletRequest, boolean, String)}.
* @see de.odysseus.calyxo.control.MessageSupport#getWarnings(javax.servlet.http.HttpServletRequest, boolean)
*/
public Messages getWarnings(HttpServletRequest request, boolean create) {
return get(request, create, WARNINGS_KEY);
}
/**
* Delegate to {@link #set(HttpServletRequest, Messages, String)}.
* @see de.odysseus.calyxo.control.MessageSupport#setInfos(javax.servlet.http.HttpServletRequest, de.odysseus.calyxo.base.Messages)
*/
public void setInfos(HttpServletRequest request, Messages messages) {
set(request, messages, INFOS_KEY);
}
/**
* Delegate to {@link #get(HttpServletRequest, boolean, String)}.
* @see de.odysseus.calyxo.control.MessageSupport#getInfos(javax.servlet.http.HttpServletRequest, boolean)
*/
public Messages getInfos(HttpServletRequest request, boolean create) {
return get(request, create, INFOS_KEY);
}
}