Skip to content

Commit

Permalink
Merge pull request #800 from caelum/fixingMessages
Browse files Browse the repository at this point in the history
checking included before including
  • Loading branch information
Turini committed Sep 17, 2014
2 parents 1dd7e4e + e00641a commit 824b9c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.enterprise.inject.Vetoed;

import org.slf4j.Logger;

Expand All @@ -42,39 +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<Severity, List<Message>> messages = new HashMap<>();
private boolean unhandledErrors = false;

private final HttpServletRequest request;

/**
* @deprecated CDI eyes only
*/
protected Messages() {
this(null);
}

@Inject
public Messages(HttpServletRequest request) {
this.request = request;
}

/**
* 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);
}

public Messages add(Message message) {
get(message.getSeverity()).add(message);
if(Severity.ERROR.equals(message.getSeverity())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 824b9c6

Please sign in to comment.