Permalink
Browse files

Fixed IgnoreValidationFailed to be safe to use with serialization.

  • Loading branch information...
1 parent 1bd0e3f commit ed34f1defae0702ddc3816ad77728d58ad5a99f7 arjan.tijms committed Feb 20, 2015
@@ -96,6 +96,8 @@ public Cache() {
// have not been set, and there is no @PostContruct for UIComponents.
subscribeToViewEvent(PRE_RENDER, new Callback.Void() {
+ private static final long serialVersionUID = 1L;
+
@Override
public void invoke() {
@@ -113,6 +115,8 @@ public void invoke() {
// After the RENDER_RESPONSE phase, copy the area we need to cache from the response buffer
// and insert it into our cache
subscribeToRequestAfterPhase(RENDER_RESPONSE, new Callback.Void() {
+
+ private static final long serialVersionUID = 1L;
@Override
public void invoke() {
@@ -188,6 +188,8 @@ protected void process(final FacesContext context, final PhaseId phaseId) {
process(context, getModel(phaseId), new Callback.Returning<Void>() {
+ private static final long serialVersionUID = 1L;
+
@Override
public Void invoke() {
processTreeNode(context, phaseId);
@@ -209,6 +211,8 @@ public boolean visitTree(final VisitContext context, final VisitCallback callbac
}
return process(context.getFacesContext(), getModel(PhaseId.ANY_PHASE), new Callback.Returning<Boolean>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke() {
@@ -240,6 +244,8 @@ public void broadcast(FacesEvent event) throws AbortProcessingException {
final FacesEvent wrapped = treeEvent.getWrapped();
process(context, treeEvent.getNode(), new Callback.Returning<Void>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public Void invoke() {
@@ -265,6 +271,8 @@ public Void invoke() {
*/
protected void processTreeNode(final FacesContext context, final PhaseId phaseId) {
processTreeNode(phaseId, new Callback.ReturningWithArgument<Void, TreeNode>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public Void invoke(TreeNode treeNode) {
@@ -290,6 +298,8 @@ public Void invoke(TreeNode treeNode) {
*/
protected boolean visitTreeNode(final VisitContext context, final VisitCallback callback) {
return processTreeNode(PhaseId.ANY_PHASE, new Callback.ReturningWithArgument<Boolean, TreeNode>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke(TreeNode treeNode) {
@@ -95,6 +95,8 @@ protected void process(final FacesContext context, final PhaseId phaseId) {
}
process(context, new Callback.ReturningWithArgument<Void, Tree>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public Void invoke(Tree tree) {
@@ -132,6 +134,8 @@ public boolean visitTree(final VisitContext context, final VisitCallback callbac
}
return process(context.getFacesContext(), new Callback.ReturningWithArgument<Boolean, Tree>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke(Tree tree) {
@@ -66,7 +66,7 @@
@Override
public void setFaceletContext(FaceletContext faceletContext) {
if (getScope().equals("facelet")) {
- faceletContext.getVariableMapper().setVariable(getName(), new ReadOnlyValueExpression(UIComponent.class, new Returning<Object>() { @Override public Object invoke() {
+ faceletContext.getVariableMapper().setVariable(getName(), new ReadOnlyValueExpression(UIComponent.class, new Returning<Object>() { private static final long serialVersionUID = 1L; @Override public Object invoke() {
return foundComponent;
}}));
}
@@ -68,13 +68,15 @@
private enum Type {
LT(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke(List<Comparable> values) {
return new ArrayList<>(new TreeSet<>(values)).equals(values);
}
}),
LTE(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke(List<Comparable> values) {
List<Comparable> sortedValues = new ArrayList<>(values);
@@ -84,6 +86,7 @@ public Boolean invoke(List<Comparable> values) {
}),
GT(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke(List<Comparable> values) {
List<Comparable> sortedValues = new ArrayList<>(new TreeSet<>(values));
@@ -93,6 +96,7 @@ public Boolean invoke(List<Comparable> values) {
}),
GTE(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
+ private static final long serialVersionUID = 1L;
@Override
public Boolean invoke(List<Comparable> values) {
List<Comparable> sortedValues = new ArrayList<>(values);
@@ -12,6 +12,10 @@
*/
package org.omnifaces.eventlistener;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
@@ -24,7 +28,7 @@
public abstract class DefaultPhaseListener implements PhaseListener {
private static final long serialVersionUID = -7252366571645029385L;
- private PhaseId phaseId;
+ private transient PhaseId phaseId; // PhaseId is not serializable
public DefaultPhaseListener(PhaseId phaseId) {
this.phaseId = phaseId;
@@ -44,5 +48,15 @@ public void afterPhase(PhaseEvent event) {
public void beforePhase(PhaseEvent event) {
// NOOP.
}
+
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ out.defaultWriteObject();
+ out.writeObject(phaseId.getName());
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+ in.defaultReadObject();
+ phaseId = PhaseId.phaseIdValueOf((String) in.readObject());
+ }
}
@@ -12,8 +12,12 @@
*/
package org.omnifaces.taghandler;
+import static javax.faces.event.PhaseId.PROCESS_VALIDATIONS;
+import static javax.faces.event.PhaseId.RENDER_RESPONSE;
+import static org.omnifaces.util.Components.findComponent;
import static org.omnifaces.util.Components.getClosestParent;
import static org.omnifaces.util.Components.hasInvokedSubmit;
+import static org.omnifaces.util.Events.subscribeToRequestAfterPhase;
import static org.omnifaces.util.Events.subscribeToViewBeforePhase;
import java.io.IOException;
@@ -92,13 +96,15 @@ public void apply(FaceletContext context, final UIComponent parent) throws IOExc
}
if (ComponentHandler.isNew(parent)) {
- subscribeToViewBeforePhase(PhaseId.PROCESS_VALIDATIONS, new Callback.Void() {
+
+ subscribeToRequestAfterPhase(RENDER_RESPONSE, new Callback.Void() {
+
+ private static final long serialVersionUID = 1L;
@Override
public void invoke() {
- if (hasInvokedSubmit(parent)) {
- getClosestParent(parent, Form.class).setIgnoreValidationFailed(true);
- }
+ subscribeToViewBeforePhase(PROCESS_VALIDATIONS, new IgnoreValidationFailedSetter(parent.getClientId()));
+
}
});
}
@@ -108,5 +114,27 @@ public void invoke() {
}
}
}
+
+ public static class IgnoreValidationFailedSetter implements Callback.Void {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String parentId;
+
+ public IgnoreValidationFailedSetter(String parentId) {
+ this.parentId = parentId;
+ }
+
+ @Override
+ public void invoke() {
+
+ UIComponent parent = findComponent(parentId);
+
+ if (hasInvokedSubmit(parent)) {
+ getClosestParent(parent, Form.class).setIgnoreValidationFailed(true);
+ }
+ }
+ }
+
}
@@ -216,18 +216,22 @@ public void apply(FaceletContext context, final UIComponent parent) throws IOExc
switch (getMethod(method)) {
case validateActual:
- Callback.Void validateTargetBase = new TargetFormInvoker(parent, new WithArgument<UIForm>() { @Override public void invoke(UIForm targetForm) {
-
- final FacesContext context = FacesContext.getCurrentInstance();
-
- Set<ConstraintViolation<?>> violations = validate(targetBase, groups);
-
- if (!violations.isEmpty()) {
- context.validationFailed();
- for (ConstraintViolation<?> violation : violations) {
- context.addMessage(targetForm.getClientId(context), createError(violation.getMessage()));
- }
- }
+ Callback.Void validateTargetBase = new TargetFormInvoker(parent, new WithArgument<UIForm>() {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override public void invoke(UIForm targetForm) {
+
+ final FacesContext context = FacesContext.getCurrentInstance();
+
+ Set<ConstraintViolation<?>> violations = validate(targetBase, groups);
+
+ if (!violations.isEmpty()) {
+ context.validationFailed();
+ for (ConstraintViolation<?> violation : violations) {
+ context.addMessage(targetForm.getClientId(context), createError(violation.getMessage()));
+ }
+ }
}});
@@ -242,11 +246,11 @@ public void apply(FaceletContext context, final UIComponent parent) throws IOExc
//
// E.g. in "h:inputText value=bean.target.property and o:validateBean value=bean.target", this will collect property=[captured value].
- Callback.Void collectPropertyValues = new TargetFormInvoker(parent, new WithArgument<UIForm>() { @Override public void invoke(UIForm targetForm) {
+ Callback.Void collectPropertyValues = new TargetFormInvoker(parent, new WithArgument<UIForm>() { private static final long serialVersionUID = 1L; @Override public void invoke(UIForm targetForm) {
final FacesContext context = FacesContext.getCurrentInstance();
- forEachInputWithMatchingBase(context, targetForm, targetBase, new Operation() { @Override public void invoke(EditableValueHolder v, ValueReference vr) {
+ forEachInputWithMatchingBase(context, targetForm, targetBase, new Operation() { private static final long serialVersionUID = 1L; @Override public void invoke(EditableValueHolder v, ValueReference vr) {
/* (v, vr) -> */ addCollectingValidator(v, vr, properties);
}});
@@ -256,13 +260,13 @@ public void apply(FaceletContext context, final UIComponent parent) throws IOExc
// Callback that uses the values collected by our previous callback (collectPropertyValues) to initialize a copied bean with, and which
// then validated this copy.
- Callback.Void checkConstraints = new TargetFormInvoker(parent, new WithArgument<UIForm>() { @Override public void invoke(UIForm targetForm) {
+ Callback.Void checkConstraints = new TargetFormInvoker(parent, new WithArgument<UIForm>() { private static final long serialVersionUID = 1L; @Override public void invoke(UIForm targetForm) {
final FacesContext context = FacesContext.getCurrentInstance();
// First remove the collecting validator again, since it will otherwise be state saved with the component at the end of the lifecycle.
- forEachInputWithMatchingBase(context, targetForm, targetBase, new Operation() { @Override public void invoke(EditableValueHolder v, ValueReference vr) {
+ forEachInputWithMatchingBase(context, targetForm, targetBase, new Operation() { private static final long serialVersionUID = 1L; @Override public void invoke(EditableValueHolder v, ValueReference vr) {
/* (v, vr) -> */ removeCollectingValidator(v);
}});
@@ -295,6 +299,8 @@ public void apply(FaceletContext context, final UIComponent parent) throws IOExc
} else {
subscribeToViewBeforePhase(PROCESS_VALIDATIONS, new Callback.Void() {
+ private static final long serialVersionUID = 1L;
+
@Override
public void invoke() {
if (hasInvokedSubmit(parent)) {
@@ -311,7 +317,7 @@ private void forEachInputWithMatchingBase(final FacesContext context, UIComponen
forEachComponent(context)
.fromRoot(targetForm)
.ofTypes(EditableValueHolder.class)
- .invoke(new Callback.WithArgument<UIComponent>() { @Override public void invoke(UIComponent component) {
+ .invoke(new Callback.WithArgument<UIComponent>() { private static final long serialVersionUID = 1L; @Override public void invoke(UIComponent component) {
ValueExpression valueExpression = component.getValueExpression("value");
if (valueExpression != null) {
@@ -341,6 +347,8 @@ public void validate(FacesContext context, UIComponent component, Object value)
}
public final class TargetFormInvoker implements Callback.Void {
+
+ private static final long serialVersionUID = 1L;
private final UIComponent parent;
private WithArgument<UIForm> operation;
@@ -379,6 +387,9 @@ public void invoke() {
}
private abstract static class Operation implements Callback.WithArgument<Object[]> {
+
+ private static final long serialVersionUID = 1L;
+
@Override
public void invoke(Object[] args) {
invoke((EditableValueHolder) args[0], (ValueReference) args[1]);
@@ -239,6 +239,9 @@ public void processEvent(ComponentSystemEvent event) throws AbortProcessingExcep
FacesContext context = getContext();
final UIComponent component = event.getComponent();
subscribeToRequestAfterPhase(context.getCurrentPhaseId(), new Callback.Void() {
+
+ private static final long serialVersionUID = 1L;
+
@Override
public void invoke() {
// We can't unsubscribe immediately inside processEvent() itself, as it would otherwise end up in a
@@ -12,6 +12,8 @@
*/
package org.omnifaces.util;
+import java.io.Serializable;
+
/**
* Collection of callback interfaces. Useful in (mini) visitor and strategy patterns.
*
@@ -26,7 +28,7 @@
*
* @author Bauke Scholtz
*/
- public interface Void {
+ public interface Void extends Serializable {
/**
* This method should be invoked by the method where you're passing this callback instance to.
@@ -41,7 +43,7 @@
* @author Bauke Scholtz
* @param <R> The return type.
*/
- public interface Returning<R> {
+ public interface Returning<R> extends Serializable {
/**
* This method should be invoked by the method where you're passing this callback instance to.
@@ -57,7 +59,7 @@
* @author Bauke Scholtz
* @param <A> The argument type.
*/
- public interface WithArgument<A> {
+ public interface WithArgument<A> extends Serializable {
/**
* This method should be invoked by the method where you're passing this callback instance to.
@@ -74,7 +76,7 @@
* @param <R> The return type.
* @param <A> The argument type.
*/
- public interface ReturningWithArgument<R, A> {
+ public interface ReturningWithArgument<R, A> extends Serializable {
/**
* This method should be invoked by the method where you're passing this callback instance to.
@@ -272,6 +272,8 @@ public static void subscribeToRequestAfterPhase(PhaseId phaseId, Callback.WithAr
private static <A> Callback.WithArgument<A> wrap(final Callback.Void callback) {
return new Callback.WithArgument<A>() {
+
+ private static final long serialVersionUID = 1L;
@Override
public void invoke(A argument) {
@@ -155,7 +155,7 @@ private SelectItemsCollector() {}
}
// During each iteration, just resolve all attributes again.
- scopedRunner.invoke(new Callback.Void() { @Override public void invoke() {
+ scopedRunner.invoke(new Callback.Void() { private static final long serialVersionUID = 1L; @Override public void invoke() {
Object itemValue = getItemValue(attributes, item);
Object noSelectionValue = attributes.get("noSelectionValue");

0 comments on commit ed34f1d

Please sign in to comment.