Skip to content

Commit f533f45

Browse files
committed
Use custom FacesContext for validation and conversion
1 parent fe6b99f commit f533f45

2 files changed

Lines changed: 51 additions & 72 deletions

File tree

‎annotations/src/main/java/org/ocpsoft/prettyfaces/annotation/handlers/JSFConverterHandler.java‎

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.lang.reflect.Field;
55

66
import javax.faces.FactoryFinder;
7-
import javax.faces.application.Application;
8-
import javax.faces.application.ApplicationFactory;
97
import javax.faces.context.FacesContext;
108
import javax.faces.context.FacesContextFactory;
119
import javax.faces.convert.ConverterException;
@@ -56,7 +54,7 @@ public void process(ClassContext classContext, AnnotatedElement element, JSFConv
5654

5755
// create the converter and attach it to the binding
5856
String converterId = annotation.converterId();
59-
CustomFacesContextJSFRewriteConverter converter = new CustomFacesContextJSFRewriteConverter(converterId);
57+
JSFRewriteConverter converter = new JSFRewriteConverter(converterId);
6058
bindingBuilder.convertedBy(converter);
6159

6260
if (log.isTraceEnabled()) {
@@ -69,11 +67,6 @@ public void process(ClassContext classContext, AnnotatedElement element, JSFConv
6967

7068
}
7169

72-
/**
73-
* Implementation of {@link Converter} that uses the supplied JSF converter for conversion
74-
*
75-
* @author Christian Kaltepoth
76-
*/
7770
private static class JSFRewriteConverter<T> implements Converter<T>
7871
{
7972

@@ -84,49 +77,6 @@ public JSFRewriteConverter(String converterId)
8477
this.converterId = converterId;
8578
}
8679

87-
@Override
88-
@SuppressWarnings("unchecked")
89-
public T convert(Rewrite event, EvaluationContext context, Object value)
90-
{
91-
92-
// we need to create the Application ourself using the ApplicationFactory
93-
ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
94-
if (factory == null) {
95-
throw new IllegalArgumentException("Could not find ApplicationFactory");
96-
}
97-
98-
// try to obtain the Application instance from the factory
99-
Application application = factory.getApplication();
100-
if (application == null) {
101-
throw new IllegalArgumentException("Unable to create Application");
102-
}
103-
104-
// obtain the JSF converter
105-
javax.faces.convert.Converter converter = application.createConverter(converterId);
106-
107-
// run conversion
108-
String valueAsString = value != null ? value.toString() : null;
109-
try {
110-
return (T) converter.getAsObject(null, new NullComponent(), valueAsString);
111-
}
112-
catch (ConverterException e) {
113-
return null;
114-
}
115-
116-
}
117-
118-
}
119-
120-
private static class CustomFacesContextJSFRewriteConverter<T> implements Converter<T>
121-
{
122-
123-
private final String converterId;
124-
125-
public CustomFacesContextJSFRewriteConverter(String converterId)
126-
{
127-
this.converterId = converterId;
128-
}
129-
13080
@Override
13181
@SuppressWarnings("unchecked")
13282
public T convert(Rewrite event, EvaluationContext context, Object value)

‎annotations/src/main/java/org/ocpsoft/prettyfaces/annotation/handlers/JSFValidatorHandler.java‎

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
import java.lang.reflect.Field;
55

66
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;
99
import javax.faces.validator.ValidatorException;
10+
import javax.servlet.ServletContext;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
1013

1114
import org.ocpsoft.logging.Logger;
1215
import org.ocpsoft.prettyfaces.annotation.JSFValidator;
1316
import org.ocpsoft.prettyfaces.core.util.NullComponent;
17+
import org.ocpsoft.prettyfaces.core.util.NullLifecycle;
1418
import org.ocpsoft.rewrite.annotation.api.ClassContext;
1519
import org.ocpsoft.rewrite.annotation.api.FieldContext;
1620
import org.ocpsoft.rewrite.annotation.spi.AnnotationHandler;
1721
import org.ocpsoft.rewrite.bind.BindingBuilder;
1822
import org.ocpsoft.rewrite.bind.Validator;
1923
import org.ocpsoft.rewrite.context.EvaluationContext;
2024
import org.ocpsoft.rewrite.event.Rewrite;
25+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
2126

2227
public class JSFValidatorHandler implements AnnotationHandler<JSFValidator>
2328
{
@@ -81,31 +86,55 @@ public JSFRewriteValidator(String validatorId)
8186
public boolean validate(Rewrite event, EvaluationContext context, T value)
8287
{
8388

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);
8692
if (factory == null) {
87-
throw new IllegalArgumentException("Could not find ApplicationFactory");
93+
throw new IllegalArgumentException("Could not find FacesContextFactory");
8894
}
8995

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+
}
101133

102-
validator.validate(null, new NullComponent(), value);
103-
return true;
104-
}
105-
catch (ValidatorException e) {
106-
return false;
107134
}
108135

136+
throw new IllegalStateException("Unsupported Rewrite event type: " + event.getClass().getName());
137+
109138
}
110139

111140
}

0 commit comments

Comments
 (0)