Skip to content

Instantly share code, notes, and snippets.

@wakim
Created December 3, 2014 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakim/2effb0fb074389bae7b4 to your computer and use it in GitHub Desktop.
Save wakim/2effb0fb074389bae7b4 to your computer and use it in GitHub Desktop.
FacesContextWrapper
package br.com.wakim.teste_jsf.util;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.ExternalContextWrapper;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.context.Flash;
import javax.faces.lifecycle.Lifecycle;
public class CustomFacesContextFactory extends FacesContextFactory {
FacesContextFactory delegate;
public CustomFacesContextFactory(FacesContextFactory facesContextFactory) {
delegate = facesContextFactory;
}
@Override
public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException {
return new FacesContextWrapper(delegate.getFacesContext(context, request, response, lifecycle));
}
public static class FacesContextWrapper extends javax.faces.context.FacesContextWrapper {
FacesContext wrapped;
ExternalContext exWrapped;
public FacesContextWrapper(FacesContext wrapped) {
this.wrapped = wrapped;
setCurrentInstance(this);
// 3. preset wrapping CustomExternalContext
exWrapped = new CustomExternalContext(this.wrapped.getExternalContext());
}
@Override
public FacesContext getWrapped() {
return wrapped;
}
@Override
public ExternalContext getExternalContext() {
return exWrapped;
}
}
public static class CustomExternalContext extends ExternalContextWrapper {
ExternalContext wrapped;
Flash flashWrapped;
public CustomExternalContext(ExternalContext wrapped) {
this.wrapped = wrapped;
}
@Override
public ExternalContext getWrapped() {
return wrapped;
}
@Override
public Flash getFlash() {
if(flashWrapped == null) {
flashWrapped = new CustomFlash(super.getFlash());
}
return flashWrapped;
}
}
public static class CustomFlash extends Flash {
Flash wrapped;
Set<Object> readedKeys = new HashSet<Object>();
public void cleanReadedKeys() {
for(Object key : readedKeys) {
wrapped.remove(key);
}
readedKeys.clear();
}
public CustomFlash(Flash wrapped) {
this.wrapped = wrapped;
}
@Override
public void clear() {
wrapped.clear();
readedKeys.clear();
}
@Override
public boolean containsKey(Object key) {
return wrapped.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return wrapped.containsValue(value);
}
@Override
public Set<java.util.Map.Entry<String, Object>> entrySet() {
// TODO Verificar se isso eh chamado em algum momento...
return wrapped.entrySet();
}
@Override
public Object get(Object key) {
readedKeys.add(key);
return wrapped.get(key);
}
@Override
public boolean isEmpty() {
return wrapped.isEmpty();
}
@Override
public Set<String> keySet() {
return wrapped.keySet();
}
@Override
public Object put(String key, Object value) {
return wrapped.put(key, value);
}
@Override
public void putAll(Map<? extends String, ? extends Object> m) {
wrapped.putAll(m);
}
@Override
public Object remove(Object key) {
readedKeys.remove(key);
return wrapped.remove(key);
}
@Override
public int size() {
return wrapped.size();
}
@Override
public Collection<Object> values() {
// TODO Verificar se isso eh chamado...
return wrapped.values();
}
@Override
public boolean isKeepMessages() {
return wrapped.isKeepMessages();
}
@Override
public void setKeepMessages(boolean newValue) {
wrapped.setKeepMessages(newValue);
}
@Override
public boolean isRedirect() {
return wrapped.isRedirect();
}
@Override
public void setRedirect(boolean newValue) {
wrapped.setRedirect(newValue);
}
@Override
public void putNow(String key, Object value) {
wrapped.putNow(key, value);
}
@Override
public void keep(String key) {
wrapped.keep(key);
readedKeys.remove(key);
}
@Override
public void doPrePhaseActions(FacesContext ctx) {
wrapped.doPrePhaseActions(ctx);
}
@Override
public void doPostPhaseActions(FacesContext ctx) {
wrapped.doPostPhaseActions(ctx);
}
}
}
<factory>
<faces-context-factory>br.com.wakim.teste_jsf.util.CustomFacesContextFactory</faces-context-factory>
</factory>
<lifecycle>
<phase-listener>br.com.wakim.teste_jsf.util.FlashPhaseListener</phase-listener>
</lifecycle>
package br.com.wakim.teste_jsf.util;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import br.com.wakim.teste_jsf.util.CustomFacesContextFactory.CustomFlash;
public class FlashPhaseListener implements PhaseListener {
private static final long serialVersionUID = -928494117948466390L;
@Override
public void afterPhase(PhaseEvent event) {
((CustomFlash) FacesContext.getCurrentInstance().getExternalContext().getFlash()).cleanReadedKeys();
}
@Override
public void beforePhase(PhaseEvent event) {
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment