|
4 | 4 | import java.lang.reflect.Field; |
5 | 5 |
|
6 | 6 | import javax.faces.FactoryFinder; |
7 | | -import javax.faces.application.Application; |
8 | | -import javax.faces.application.ApplicationFactory; |
| 7 | +import javax.faces.context.FacesContext; |
| 8 | +import javax.faces.context.FacesContextFactory; |
9 | 9 | import javax.faces.validator.ValidatorException; |
| 10 | +import javax.servlet.ServletContext; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import javax.servlet.http.HttpServletResponse; |
10 | 13 |
|
11 | 14 | import org.ocpsoft.logging.Logger; |
12 | 15 | import org.ocpsoft.prettyfaces.annotation.JSFValidator; |
13 | 16 | import org.ocpsoft.prettyfaces.core.util.NullComponent; |
| 17 | +import org.ocpsoft.prettyfaces.core.util.NullLifecycle; |
14 | 18 | import org.ocpsoft.rewrite.annotation.api.ClassContext; |
15 | 19 | import org.ocpsoft.rewrite.annotation.api.FieldContext; |
16 | 20 | import org.ocpsoft.rewrite.annotation.spi.AnnotationHandler; |
17 | 21 | import org.ocpsoft.rewrite.bind.BindingBuilder; |
18 | 22 | import org.ocpsoft.rewrite.bind.Validator; |
19 | 23 | import org.ocpsoft.rewrite.context.EvaluationContext; |
20 | 24 | import org.ocpsoft.rewrite.event.Rewrite; |
| 25 | +import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite; |
21 | 26 |
|
22 | 27 | public class JSFValidatorHandler implements AnnotationHandler<JSFValidator> |
23 | 28 | { |
@@ -81,31 +86,55 @@ public JSFRewriteValidator(String validatorId) |
81 | 86 | public boolean validate(Rewrite event, EvaluationContext context, T value) |
82 | 87 | { |
83 | 88 |
|
84 | | - // we need to create the Application ourself using the ApplicationFactory |
85 | | - ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); |
| 89 | + // we will build the FacesContext ourself |
| 90 | + FacesContextFactory factory = |
| 91 | + (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); |
86 | 92 | if (factory == null) { |
87 | | - throw new IllegalArgumentException("Could not find ApplicationFactory"); |
| 93 | + throw new IllegalArgumentException("Could not find FacesContextFactory"); |
88 | 94 | } |
89 | 95 |
|
90 | | - // try to obtain the Application instance from the factory |
91 | | - Application application = factory.getApplication(); |
92 | | - if (application == null) { |
93 | | - throw new IllegalArgumentException("Unable to create Application"); |
94 | | - } |
95 | | - |
96 | | - // obtain the JSF validator |
97 | | - javax.faces.validator.Validator validator = application.createValidator(validatorId); |
98 | | - |
99 | | - // perform validation |
100 | | - try { |
| 96 | + // we need HttpServletRewrite to obtain the HttpServletRequest, HttpServletResponse, etc |
| 97 | + if (event instanceof HttpServletRewrite) { |
| 98 | + HttpServletRewrite httpRewrite = (HttpServletRewrite) event; |
| 99 | + |
| 100 | + // TODO: getting the ServletContext from the request works in Servlet 3.0 only! |
| 101 | + ServletContext servletContext = httpRewrite.getRequest().getServletContext(); |
| 102 | + HttpServletRequest request = httpRewrite.getRequest(); |
| 103 | + HttpServletResponse response = httpRewrite.getResponse(); |
| 104 | + |
| 105 | + // we have to properly release the FacesContext |
| 106 | + FacesContext facesContext = null; |
| 107 | + try { |
| 108 | + |
| 109 | + // build the FacesContext using a fake Lifecycle instance |
| 110 | + facesContext = factory.getFacesContext(servletContext, request, response, new NullLifecycle()); |
| 111 | + if (facesContext == null) { |
| 112 | + throw new IllegalStateException("Could not create FacesContext"); |
| 113 | + } |
| 114 | + |
| 115 | + // obtain the JSF validator |
| 116 | + javax.faces.validator.Validator validator = facesContext.getApplication().createValidator(validatorId); |
| 117 | + |
| 118 | + // perform validation |
| 119 | + try { |
| 120 | + validator.validate(null, new NullComponent(), value); |
| 121 | + return true; |
| 122 | + } |
| 123 | + catch (ValidatorException e) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + finally { |
| 129 | + if (facesContext != null) { |
| 130 | + facesContext.release(); |
| 131 | + } |
| 132 | + } |
101 | 133 |
|
102 | | - validator.validate(null, new NullComponent(), value); |
103 | | - return true; |
104 | | - } |
105 | | - catch (ValidatorException e) { |
106 | | - return false; |
107 | 134 | } |
108 | 135 |
|
| 136 | + throw new IllegalStateException("Unsupported Rewrite event type: " + event.getClass().getName()); |
| 137 | + |
109 | 138 | } |
110 | 139 |
|
111 | 140 | } |
|
0 commit comments