* @param component
* @throws IOException
*/
public static void encodeAreas(FacesContext context,
UIComponent component) throws IOException {
AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
ExternalContext externalContext = context.getExternalContext();
Map<String, Object> requestMap = externalContext.getRequestMap();
Set<String> rendered = ajaxContext.getAjaxRenderedAreas();
StringBuffer senderString = new StringBuffer();
// write special area for list of rendered elements. Client-side
// Java
// Script
// read this structure for update areas of DOM tree.
ResponseWriter out = context.getResponseWriter();
// Create <span> element to keep list rendered aread ( in title
// attribute )
// More right will create special namespace for such
// information,
// but I want to keep simple html ( xhtml ) document - on case
// I have troubles with microsoft XMLHTTP validations.
out
.startElement(AjaxContainerRenderer.AJAX_RESULT_GROUP_TAG,
component);
out.writeAttribute(HTML.NAME_ATTRIBUTE,
AjaxContainerRenderer.AJAX_UPDATE_HEADER, null);
for (Iterator<String> it = rendered.iterator(); it.hasNext();) {
String id = (String) it.next();
// out.startElement(AJAX_RESULT_TAG, component);
// out.writeText(id,null);
// out.endElement(AJAX_RESULT_TAG);
senderString.append(id);
if (it.hasNext()) {
senderString.append(',');
}
}
out.writeAttribute(AjaxContainerRenderer.AJAX_RESULT_GROUP_ATTR,
senderString, null);
out.endElement(AjaxContainerRenderer.AJAX_RESULT_GROUP_TAG);
// For sequences and client-saved states.
out.startElement(AjaxContainerRenderer.AJAX_VIEW_STATE_TAG, component);
out.writeAttribute(HTML.id_ATTRIBUTE,
AjaxContainerRenderer.AJAX_VIEW_STATE_ID, null);
writeState(context);
out.endElement(AjaxContainerRenderer.AJAX_VIEW_STATE_TAG);
// Write rendered flag to html <meta>
out
.startElement(AjaxContainerRenderer.AJAX_RESULT_GROUP_TAG,
component);
out.writeAttribute(HTML.id_ATTRIBUTE,
AjaxContainerRenderer.AJAX_FLAG_HEADER, null);
out.writeAttribute(HTML.NAME_ATTRIBUTE,
AjaxContainerRenderer.AJAX_FLAG_HEADER, null);
out.writeAttribute(AjaxContainerRenderer.AJAX_RESULT_GROUP_ATTR,
"true", null);
out.endElement(AjaxContainerRenderer.AJAX_RESULT_GROUP_TAG);
// set response header with list of rendered ID's
Object response = externalContext.getResponse();
// Use reflection for send responce headers - we can get
// different responces classes
// for different environment ( portal, cocoon etc )
if (response instanceof HttpServletResponse) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
// httpResponse.setHeader(AjaxContainerRenderer.AJAX_UPDATE_HEADER,
// senderString.toString());
httpResponse.setHeader(AjaxContainerRenderer.AJAX_FLAG_HEADER,
"true");
} else {
try {
Method setHeadergMethod = response.getClass()
.getMethod("setHeader",
new Class[] { String.class, String.class });
// setHeadergMethod.invoke(response, new Object[] {
// AjaxContainerRenderer.AJAX_UPDATE_HEADER,
// senderString.toString() });
setHeadergMethod.invoke(response, new Object[] {
AjaxContainerRenderer.AJAX_FLAG_HEADER, "true" });
} catch (Exception e) {
log
.error(Messages
.getMessage(Messages.DETECTING_ENCODING_DISABLED_ERROR));
log.error(Messages.getMessage(
Messages.OBTAIN_RESPONSE_SET_HEADER_ERROR, e));
}
}
Map<String, Object> responseDataMap = ajaxContext.getResponseDataMap();
// Get data serializer instance
AJAXDataSerializer serializer = (AJAXDataSerializer) ServicesUtils
.getServiceInstance(AJAXDataSerializer.SERVICE);
// Put data to JavaScript handlers, inside <span> elements.
for (Iterator<String> dataIterator = responseDataMap.keySet().iterator(); dataIterator
.hasNext();) {
Object dataKey = dataIterator.next();
out.startElement(HTML.SPAN_ELEM, component);
out.writeAttribute(HTML.id_ATTRIBUTE, dataKey, null);
String dataString = serializer.asString(responseDataMap
.get(dataKey));
out.write(dataString);
out.endElement(HTML.SPAN_ELEM);
}
// Include active 'oncomplete' function content :
Object oncomplete = ajaxContext.getOncomplete();
if(null != oncomplete){
out.startElement(HTML.SPAN_ELEM, component);
out.writeAttribute(HTML.id_ATTRIBUTE, ONCOMPLETE_CONTENT_ID, null);
out.writeText(oncomplete,null);
out.endElement(HTML.SPAN_ELEM);