* {@inheritDoc}
*/
@Override
public void buildView(FacesContext context, UIViewRoot view) throws IOException
{
ExternalContext externalContext = context.getExternalContext();
if (context.getPartialViewContext().isPartialRequest())
{
// try to get (or create) a ResponseSwitch and turn off the output
Object origResponse = context.getExternalContext().getResponse();
ResponseSwitch responseSwitch = ExternalContextUtils.getResponseSwitch(origResponse);
if (responseSwitch == null)
{
// no ResponseSwitch installed yet - create one
responseSwitch = ExternalContextUtils.createResponseSwitch(origResponse);
if (responseSwitch != null)
{
// install the ResponseSwitch
context.getExternalContext().setResponse(responseSwitch);
}
}
if (responseSwitch != null)
{
responseSwitch.setEnabled(context, false);
}
}
ServletResponse response = (ServletResponse) externalContext.getResponse();
ServletRequest request = (ServletRequest) externalContext.getRequest();
Locale locale = view.getLocale();
response.setLocale(locale);
Config.set(request, Config.FMT_LOCALE, context.getViewRoot().getLocale());
String viewId = view.getViewId();
ServletViewResponseWrapper wrappedResponse = new ServletViewResponseWrapper((HttpServletResponse) response);
externalContext.setResponse(wrappedResponse);
try
{
externalContext.dispatch(viewId);
}
catch (FacesException e)
{
// try to extract the most likely exceptions here
// and provide a better error message for them
String message = e.getMessage();
// errors related to using facelets-only tags on a JSP page
if (message != null)
{
// does the message contain "f" (prefix f of tags)
// or the related uri http://java.sun.com/jsf/core
if (message.contains("\"f\"")
|| message.contains("\"" + CoreLibrary.Namespace + "\""))
{
// check facelets-only f tags
for (String tag : FACELETS_ONLY_F_TAGS)
{
if (message.contains("\"" + tag + "\""))
{
String exceptionMessage = "The tag f:" + tag +
" is only available on facelets.";
throw new FacesException(exceptionMessage,
new FaceletsOnlyException(exceptionMessage, e.getCause()));
}
}
}
else if (message.contains("\"h\"")
|| message.contains("\"" + HtmlLibrary.Namespace + "\""))
{
// check facelets-only h tags
for (String tag : FACELETS_ONLY_H_TAGS)
{
if (message.contains("\"" + tag + "\""))
{
String exceptionMessage = "The tag h:" + tag +
" is only available on facelets.";
throw new FacesException(exceptionMessage,
new FaceletsOnlyException(exceptionMessage, e.getCause()));
}
}
}
else
{
// check facelets-only namespaces
String namespace = null;
if (message.contains(UILibrary.Namespace))
{
namespace = UILibrary.Namespace;
}
else if (message.contains(CompositeLibrary.NAMESPACE))
{
namespace = CompositeLibrary.NAMESPACE;
}
if (namespace != null)
{
// the message contains a facelets-only namespace
String exceptionMessage = "All tags with namespace " +
namespace + " are only available on facelets.";
throw new FacesException(exceptionMessage,
new FaceletsOnlyException(exceptionMessage, e.getCause()));
}
}
}
// no rule applied to this Exception - rethrow it
throw e;
}
finally
{
externalContext.setResponse(response);
}
boolean errorResponse = wrappedResponse.getStatus() < 200 || wrappedResponse.getStatus() > 299;
if (errorResponse)
{