is the default implementation class for ViewHandler.
| Method from com.sun.faces.application.ViewHandlerImpl Detail: |
public Locale calculateLocale(FacesContext context) {
if (context == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context");
throw new NullPointerException(message);
}
Locale result = null;
// determine the locales that are acceptable to the client based on the
// Accept-Language header and the find the best match among the
// supported locales specified by the client.
Iterator< Locale > locales = context.getExternalContext().getRequestLocales();
while (locales.hasNext()) {
Locale perf = locales.next();
result = findMatch(context, perf);
if (result != null) {
break;
}
}
// no match is found.
if (result == null) {
if (context.getApplication().getDefaultLocale() == null) {
result = Locale.getDefault();
} else {
result = context.getApplication().getDefaultLocale();
}
}
return result;
}
|
public String calculateRenderKitId(FacesContext context) {
if (context == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context");
throw new NullPointerException(message);
}
Map< String,String > requestParamMap = context.getExternalContext()
.getRequestParameterMap();
String result = requestParamMap.get(
ResponseStateManager.RENDER_KIT_ID_PARAM);
if (result == null) {
if (null ==
(result = context.getApplication().getDefaultRenderKitId())) {
result = RenderKitFactory.HTML_BASIC_RENDER_KIT;
}
}
return result;
}
|
public UIViewRoot createView(FacesContext context,
String viewId) {
if (context == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context");
throw new NullPointerException(message);
}
UIViewRoot result = (UIViewRoot)
context.getApplication().createComponent(UIViewRoot.COMPONENT_TYPE);
if (viewId != null) {
String mapping = Util.getFacesMapping(context);
if (mapping != null) {
if (!Util.isPrefixMapped(mapping)) {
viewId = convertViewId(context, viewId);
} else {
viewId = normalizeRequestURI(viewId, mapping);
if (viewId.equals(mapping)) {
// The request was to the FacesServlet only - no
// path info
// on some containers this causes a recursion in the
// RequestDispatcher and the request appears to hang.
// If this is detected, return status 404
send404Error(context);
}
}
}
result.setViewId(viewId);
}
Locale locale = null;
String renderKitId = null;
// use the locale from the previous view if is was one which will be
// the case if this is called from NavigationHandler. There wouldn't be
// one for the initial case.
if (context.getViewRoot() != null) {
locale = context.getViewRoot().getLocale();
renderKitId = context.getViewRoot().getRenderKitId();
}
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Created new view for " + viewId);
}
// PENDING(): not sure if we should set the RenderKitId here.
// The UIViewRoot ctor sets the renderKitId to the default
// one.
// if there was no locale from the previous view, calculate the locale
// for this view.
if (locale == null) {
locale =
context.getApplication().getViewHandler().calculateLocale(
context);
if (logger.isLoggable(Level.FINE)) {
logger.fine("Locale for this view as determined by calculateLocale "
+ locale.toString());
}
} else {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Using locale from previous view "
+ locale.toString());
}
}
if (renderKitId == null) {
renderKitId =
context.getApplication().getViewHandler().calculateRenderKitId(
context);
if (logger.isLoggable(Level.FINE)) {
logger.fine(
"RenderKitId for this view as determined by calculateRenderKitId "
+ renderKitId);
}
} else {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Using renderKitId from previous view "
+ renderKitId);
}
}
result.setLocale(locale);
result.setRenderKitId(renderKitId);
return result;
}
|
protected Locale findMatch(FacesContext context,
Locale pref) {
Locale result = null;
Iterator< Locale > it = context.getApplication().getSupportedLocales();
while (it.hasNext()) {
Locale supportedLocale = it.next();
if (pref.equals(supportedLocale)) {
// exact match
result = supportedLocale;
break;
} else {
// Make sure the preferred locale doesn't have country
// set, when doing a language match, For ex., if the
// preferred locale is "en-US", if one of supported
// locales is "en-UK", even though its language matches
// that of the preferred locale, we must ignore it.
if (pref.getLanguage().equals(supportedLocale.getLanguage()) &&
supportedLocale.getCountry().length() == 0) {
result = supportedLocale;
}
}
}
// if it's not in the supported locales,
if (null == result) {
Locale defaultLocale = context.getApplication().getDefaultLocale();
if (defaultLocale != null) {
if ( pref.equals(defaultLocale)) {
// exact match
result = defaultLocale;
} else {
// Make sure the preferred locale doesn't have country
// set, when doing a language match, For ex., if the
// preferred locale is "en-US", if one of supported
// locales is "en-UK", even though its language matches
// that of the preferred locale, we must ignore it.
if (pref.getLanguage().equals(defaultLocale.getLanguage()) &&
defaultLocale.getCountry().length() == 0) {
result = defaultLocale;
}
}
}
}
return result;
}
Attempts to find a matching locale based on pref and
list of supported locales, using the matching algorithm
as described in JSTL 8.3.2. |
public String getActionURL(FacesContext context,
String viewId) {
if (context == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context");
throw new NullPointerException(message);
}
if (viewId == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "viewId");
throw new NullPointerException(message);
}
if (viewId.charAt(0) != '/") {
String message =
MessageUtils.getExceptionMessageString(
MessageUtils.ILLEGAL_VIEW_ID_ID,
viewId);
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, "jsf.illegal_view_id_error", viewId);
}
throw new IllegalArgumentException(message);
}
// Acquire the context path, which we will prefix on all results
ExternalContext extContext = context.getExternalContext();
String contextPath = extContext.getRequestContextPath();
// Acquire the mapping used to execute this request (if any)
String mapping = Util.getFacesMapping(context);
// If no mapping can be identified, just return a server-relative path
if (mapping == null) {
return (contextPath + viewId);
}
// Deal with prefix mapping
if (Util.isPrefixMapped(mapping)) {
if (mapping.equals("/*")) {
return (contextPath + viewId);
} else {
return (contextPath + mapping + viewId);
}
}
// Deal with extension mapping
int period = viewId.lastIndexOf('.");
if (period < 0) {
return (contextPath + viewId + mapping);
} else if (!viewId.endsWith(mapping)) {
return (contextPath + viewId.substring(0, period) + mapping);
} else {
return (contextPath + viewId);
}
}
|
public String getResourceURL(FacesContext context,
String path) {
ExternalContext extContext = context.getExternalContext();
if (path.startsWith("/")) {
return (extContext.getRequestContextPath() + path);
} else {
return path;
}
}
|
public void initView(FacesContext context) throws FacesException {
if (context.getExternalContext().getRequestCharacterEncoding() == null) {
super.initView(context);
}
}
|
public void renderView(FacesContext context,
UIViewRoot viewToRender) throws IOException, FacesException {
// suppress rendering if "rendered" property on the component is
// false
if (!viewToRender.isRendered()) {
return;
}
ExternalContext extContext = context.getExternalContext();
ServletRequest request = (ServletRequest) extContext.getRequest();
ServletResponse response = (ServletResponse) extContext.getResponse();
try {
if (executePageToBuildView(context, viewToRender)) {
response.flushBuffer();
ApplicationAssociate associate = getAssociate(context);
if (associate != null) {
associate.responseRendered();
}
return;
}
} catch (IOException e) {
throw new FacesException(e);
}
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Completed building view for : \n" +
viewToRender.getViewId());
}
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "+=+=+=+=+=+= Printout for " + viewToRender.getViewId() + " about to render.");
DebugUtil.printTree(viewToRender, logger, Level.FINEST);
}
// set up the ResponseWriter
RenderKitFactory renderFactory = (RenderKitFactory)
FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
RenderKit renderKit =
renderFactory.getRenderKit(context, viewToRender.getRenderKitId());
ResponseWriter oldWriter = context.getResponseWriter();
if (bufSize == -1) {
WebConfiguration webConfig =
WebConfiguration
.getInstance(context.getExternalContext());
try {
bufSize = Integer
.parseInt(webConfig.getOptionValue(
WebContextInitParameter.ResponseBufferSize));
} catch (NumberFormatException nfe) {
bufSize = Integer
.parseInt(WebContextInitParameter.ResponseBufferSize.getDefaultValue());
}
}
WriteBehindStateWriter stateWriter =
new WriteBehindStateWriter(response.getWriter(),
context,
bufSize);
ResponseWriter newWriter;
if (null != oldWriter) {
newWriter = oldWriter.cloneWithWriter(stateWriter);
} else {
newWriter = renderKit.createResponseWriter(stateWriter,
null,
request.getCharacterEncoding());
}
context.setResponseWriter(newWriter);
newWriter.startDocument();
doRenderView(context, viewToRender);
newWriter.endDocument();
// replace markers in the body content and write it to response.
// flush directly to the response
if (stateWriter.stateWritten()) {
stateWriter.flushToWriter();
}
// clear the ThreadLocal reference.
stateWriter.release();
if (null != oldWriter) {
context.setResponseWriter(oldWriter);
}
// write any AFTER_VIEW_CONTENT to the response
// side effect: AFTER_VIEW_CONTENT removed
ViewHandlerResponseWrapper wrapper = (ViewHandlerResponseWrapper)
RequestStateManager.remove(context, RequestStateManager.AFTER_VIEW_CONTENT);
if (null != wrapper) {
wrapper.flushToWriter(response.getWriter(),
response.getCharacterEncoding());
}
response.flushBuffer();
}
|
public UIViewRoot restoreView(FacesContext context,
String viewId) {
if (context == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context");
throw new NullPointerException(message);
}
ExternalContext extContext = context.getExternalContext();
String mapping = Util.getFacesMapping(context);
UIViewRoot viewRoot = null;
if (mapping != null) {
if (!Util.isPrefixMapped(mapping)) {
viewId = convertViewId(context, viewId);
} else {
viewId = normalizeRequestURI(viewId, mapping);
}
}
// maping could be null if a non-faces request triggered
// this response.
if (extContext.getRequestPathInfo() == null && mapping != null &&
Util.isPrefixMapped(mapping)) {
// this was probably an initial request
// send them off to the root of the web application
try {
context.responseComplete();
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Response Complete for" + viewId);
}
extContext.redirect(extContext.getRequestContextPath());
} catch (IOException ioe) {
throw new FacesException(ioe);
}
} else {
// this is necessary to allow decorated impls.
ViewHandler outerViewHandler =
context.getApplication().getViewHandler();
String renderKitId =
outerViewHandler.calculateRenderKitId(context);
viewRoot = Util.getStateManager(context).restoreView(context,
viewId,
renderKitId);
}
return viewRoot;
}
|
public void writeState(FacesContext context) throws IOException {
if (context == null) {
String message = MessageUtils.getExceptionMessageString
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context");
throw new NullPointerException(message);
}
if (logger.isLoggable(Level.FINE)) {
logger.fine("Begin writing marker for viewId " +
context.getViewRoot().getViewId());
}
WriteBehindStateWriter writer = WriteBehindStateWriter.getCurrentInstance();
if (writer != null) {
writer.writingState();
}
context.getResponseWriter().write(RIConstants.SAVESTATE_FIELD_MARKER);
if (logger.isLoggable(Level.FINE)) {
logger.fine("End writing marker for viewId " +
context.getViewRoot().getViewId());
}
}
|