From 753b2da0266963b8ebfdef4a5f47f90216359661 Mon Sep 17 00:00:00 2001 From: Rodrigo Turini Date: Wed, 17 Sep 2014 12:23:10 -0300 Subject: [PATCH 1/2] checking included before including --- .../caelum/vraptor/validator/Messages.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java index 364097114..f0a2e145a 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java @@ -25,10 +25,11 @@ import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; +import br.com.caelum.vraptor.Result; + /** * Managed class that stores all application messages like errors, warnings and info. This * class is useful to display messages categorized by severity in your view. To choose a severity @@ -52,7 +53,7 @@ public class Messages { private Map> messages = new HashMap<>(); private boolean unhandledErrors = false; - private final HttpServletRequest request; + private final Result result; /** * @deprecated CDI eyes only @@ -60,19 +61,17 @@ public class Messages { protected Messages() { this(null); } - + @Inject - public Messages(HttpServletRequest request) { - this.request = request; + public Messages(Result result) { + this.result = result; } - - /** - * Put this instance into request attributes to allow this instance survive between requests using flash - * scope. - */ + @PostConstruct - public void init() { - request.setAttribute(ATTRIBUTE_KEY, this); + protected void init() { + if(!result.included().containsKey(ATTRIBUTE_KEY)){ + result.include(ATTRIBUTE_KEY, this); + } } public Messages add(Message message) { From e00641a2d9961950455c88fc8501b335713115d9 Mon Sep 17 00:00:00 2001 From: Rodrigo Turini Date: Wed, 17 Sep 2014 13:53:10 -0300 Subject: [PATCH 2/2] adding MessagesProducer --- .../caelum/vraptor/validator/Messages.java | 31 +-------------- .../vraptor/validator/MessagesProducer.java | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java index f0a2e145a..759f8af31 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java @@ -22,14 +22,10 @@ import java.util.List; import java.util.Map; -import javax.annotation.PostConstruct; -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; +import javax.enterprise.inject.Vetoed; import org.slf4j.Logger; -import br.com.caelum.vraptor.Result; - /** * Managed class that stores all application messages like errors, warnings and info. This * class is useful to display messages categorized by severity in your view. To choose a severity @@ -43,37 +39,14 @@ * @since 4.1 * @author Otávio S Garcia */ -@RequestScoped +@Vetoed public class Messages { - public static final String ATTRIBUTE_KEY = "vmessages"; - private final static Logger log = getLogger(Messages.class); private Map> messages = new HashMap<>(); private boolean unhandledErrors = false; - private final Result result; - - /** - * @deprecated CDI eyes only - */ - protected Messages() { - this(null); - } - - @Inject - public Messages(Result result) { - this.result = result; - } - - @PostConstruct - protected void init() { - if(!result.included().containsKey(ATTRIBUTE_KEY)){ - result.include(ATTRIBUTE_KEY, this); - } - } - public Messages add(Message message) { get(message.getSeverity()).add(message); if(Severity.ERROR.equals(message.getSeverity())) { diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java new file mode 100644 index 000000000..e1d4f3f5f --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/MessagesProducer.java @@ -0,0 +1,38 @@ +package br.com.caelum.vraptor.validator; + +import static com.google.common.base.Objects.firstNonNull; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; + +import br.com.caelum.vraptor.Result; + +@RequestScoped +public class MessagesProducer { + + private static final String MESSAGES_KEY = "vmessages"; + + private final Result result; + + /** + * @deprecated CDI eyes only + */ + protected MessagesProducer() { + this(null); + } + + @Inject + public MessagesProducer(Result result) { + this.result = result; + } + + @Produces @RequestScoped + public Messages create() { + Messages messages = (Messages) result.included().get(MESSAGES_KEY); + messages = firstNonNull(messages, new Messages()); + result.include(MESSAGES_KEY, messages); + return messages; + } + +} \ No newline at end of file