use of jakarta.faces.context.PartialResponseWriter in project myfaces by apache.
the class PartialViewContextImpl method processPartialRendering.
private void processPartialRendering(UIViewRoot viewRoot, PhaseId phaseId) {
// note that we cannot use this.getPartialResponseWriter(), because
// this could cause problems if PartialResponseWriter is wrapped
PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter();
PartialViewContext pvc = context.getPartialViewContext();
ResponseWriter oldWriter = context.getResponseWriter();
boolean inDocument = false;
// response type = text/xml
// no caching and no timeout if possible!
ExternalContext externalContext = context.getExternalContext();
externalContext.setResponseContentType("text/xml");
externalContext.addResponseHeader("Pragma", "no-cache");
externalContext.addResponseHeader("Cache-control", "no-cache");
// under normal circumstances pragma should be enough, IE needs
// a special treatment!
// http://support.microsoft.com/kb/234067
externalContext.addResponseHeader("Expires", "-1");
try {
String currentEncoding = writer.getCharacterEncoding();
writer.writePreamble("<?xml version=\"1.0\" encoding=\"" + (currentEncoding == null ? "UTF-8" : currentEncoding) + "\"?>");
writer.startDocument();
writer.writeAttribute("id", viewRoot.getContainerClientId(context), "id");
inDocument = true;
context.setResponseWriter(writer);
if (isResetValues()) {
viewRoot.resetValues(context, getRenderIds());
}
if (pvc.isRenderAll()) {
processRenderAll(viewRoot, writer);
} else {
Collection<String> renderIds = pvc.getRenderIds();
// Only apply partial visit if we have ids to traverse
if (renderIds != null && !renderIds.isEmpty()) {
// render=@all, so output the body.
if (renderIds.contains(PartialResponseWriter.RENDER_ALL_MARKER)) {
processRenderAll(viewRoot, writer);
} else {
// In JSF 2.3 it was added jakarta.faces.Resource as an update target to add scripts or
// stylesheets inside <head> tag
List<UIComponent> updatedComponents = new ArrayList<>();
RequestViewContext rvc = RequestViewContext.getCurrentInstance(context);
processRenderResource(context, writer, rvc, updatedComponents, "head");
processRenderResource(context, writer, rvc, updatedComponents, "body");
processRenderResource(context, writer, rvc, updatedComponents, "form");
VisitContext visitCtx = getVisitContextFactory().getVisitContext(context, renderIds, MyFacesVisitHints.SET_SKIP_UNRENDERED);
viewRoot.visitTree(visitCtx, new PhaseAwareVisitCallback(context, phaseId, updatedComponents));
}
} else {
List<UIComponent> updatedComponents = new ArrayList<>();
RequestViewContext rvc = RequestViewContext.getCurrentInstance(context);
processRenderResource(context, writer, rvc, updatedComponents, "head");
processRenderResource(context, writer, rvc, updatedComponents, "body");
processRenderResource(context, writer, rvc, updatedComponents, "form");
}
List<String> evalScripts = pvc.getEvalScripts();
if (evalScripts != null && evalScripts.size() > 0) {
for (String script : evalScripts) {
writer.startEval();
writer.write(script);
writer.endEval();
}
}
}
// invoke encodeAll() on every UIViewParameter in the view to
// enable every UIViewParameter to save its value in the state
// just like UIViewRoot.encodeEnd() does on a normal request
// (see MYFACES-2645 for details)
Collection<UIViewParameter> viewParams = ViewMetadata.getViewParameters(viewRoot);
if (!viewParams.isEmpty()) {
for (UIViewParameter param : viewParams) {
param.encodeAll(context);
}
}
// Retrieve the state and apply it if it is not null.
String viewState = context.getApplication().getStateManager().getViewState(context);
if (viewState != null) {
writer.startUpdate(HtmlResponseStateManager.generateUpdateViewStateId(context));
writer.write(viewState);
writer.endUpdate();
} else if (viewRoot.isTransient()) {
writer.startUpdate(HtmlResponseStateManager.generateUpdateViewStateId(context));
writer.write(StateTokenProcessor.STATELESS_TOKEN);
writer.endUpdate();
}
ClientWindow cw = context.getExternalContext().getClientWindow();
if (cw != null) {
writer.startUpdate(HtmlResponseStateManager.generateUpdateClientWindowId(context));
writer.writeText(cw.getId(), null);
writer.endUpdate();
}
} catch (IOException ex) {
if (log.isLoggable(Level.SEVERE)) {
log.log(Level.SEVERE, "", ex);
}
} finally {
try {
if (inDocument) {
writer.endDocument();
}
writer.flush();
} catch (IOException ex) {
if (log.isLoggable(Level.SEVERE)) {
log.log(Level.SEVERE, "", ex);
}
}
context.setResponseWriter(oldWriter);
}
}
use of jakarta.faces.context.PartialResponseWriter in project myfaces by apache.
the class PartialViewContextImpl method getPartialResponseWriter.
@Override
public PartialResponseWriter getPartialResponseWriter() {
assertNotReleased();
if (_partialResponseWriter == null) {
ResponseWriter responseWriter = context.getResponseWriter();
if (responseWriter == null) {
// ResponseWriter from the RenderKit and then wrap if necessary.
try {
RenderKit renderKit = context.getRenderKit();
if (renderKit == null) {
// If the viewRoot was set to null by some reason, or there is no
// renderKitId on that view, this could be still an ajax redirect,
// so we have to try to calculate the renderKitId and return a
// RenderKit instance, to send the response.
String renderKitId = context.getApplication().getViewHandler().calculateRenderKitId(context);
RenderKitFactory rkf = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
renderKit = rkf.getRenderKit(context, renderKitId);
}
responseWriter = renderKit.createResponseWriter(context.getExternalContext().getResponseOutputWriter(), "text/xml", context.getExternalContext().getRequestCharacterEncoding());
} catch (IOException e) {
throw new IllegalStateException("Cannot create Partial Response Writer", e);
}
}
// createResponseWriter, so we should cast here for it and prevent double wrapping.
if (responseWriter instanceof PartialResponseWriter) {
_partialResponseWriter = (PartialResponseWriter) responseWriter;
} else {
_partialResponseWriter = new PartialResponseWriterImpl(responseWriter);
}
}
return _partialResponseWriter;
}
use of jakarta.faces.context.PartialResponseWriter in project myfaces by apache.
the class ServletExternalContextImpl method redirect.
@Override
public void redirect(final String url) throws IOException {
FacesContext facesContext = getCurrentFacesContext();
PartialViewContext partialViewContext = facesContext.getPartialViewContext();
if (partialViewContext.isPartialRequest()) {
PartialResponseWriter writer = partialViewContext.getPartialResponseWriter();
this.setResponseContentType("text/xml");
this.setResponseCharacterEncoding("UTF-8");
this.addResponseHeader("Cache-control", "no-cache");
writer.startDocument();
writer.redirect(url);
writer.endDocument();
facesContext.responseComplete();
} else if (_servletResponse instanceof HttpServletResponse) {
((HttpServletResponse) _servletResponse).sendRedirect(url);
facesContext.responseComplete();
} else {
throw new IllegalArgumentException("Only HttpServletResponse supported");
}
}
use of jakarta.faces.context.PartialResponseWriter in project mojarra by eclipse-ee4j.
the class ExternalContextImpl method redirect.
/**
* @see ExternalContext#redirect(String)
*/
@Override
public void redirect(String requestURI) throws IOException {
FacesContext ctx = FacesContext.getCurrentInstance();
doLastPhaseActions(ctx, true);
if (ctx.getPartialViewContext().isPartialRequest()) {
if (response instanceof HttpServletResponse && ctx.getResponseComplete()) {
throw new IllegalStateException();
}
PartialResponseWriter pwriter;
ResponseWriter writer = ctx.getResponseWriter();
if (writer instanceof PartialResponseWriter) {
pwriter = (PartialResponseWriter) writer;
} else {
pwriter = ctx.getPartialViewContext().getPartialResponseWriter();
}
setResponseContentType(RIConstants.TEXT_XML_CONTENT_TYPE);
setResponseCharacterEncoding("UTF-8");
addResponseHeader("Cache-Control", "no-cache");
// pwriter.writePreamble("<?xml version='1.0' encoding='UTF-8'?>\n");
pwriter.startDocument();
pwriter.redirect(requestURI);
pwriter.endDocument();
} else if (response instanceof HttpServletResponse) {
((HttpServletResponse) response).sendRedirect(requestURI);
} else {
throw new IllegalStateException();
}
ctx.responseComplete();
}
use of jakarta.faces.context.PartialResponseWriter in project mojarra by eclipse-ee4j.
the class PartialViewContextImpl method createPartialResponseWriter.
private PartialResponseWriter createPartialResponseWriter() {
ExternalContext extContext = ctx.getExternalContext();
String encoding = extContext.getRequestCharacterEncoding();
extContext.setResponseCharacterEncoding(encoding);
ResponseWriter responseWriter = null;
Writer out = null;
try {
out = extContext.getResponseOutputWriter();
} catch (IOException ioe) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
}
}
if (out != null) {
UIViewRoot viewRoot = ctx.getViewRoot();
if (viewRoot != null) {
responseWriter = ctx.getRenderKit().createResponseWriter(out, RIConstants.TEXT_XML_CONTENT_TYPE, encoding);
} else {
RenderKitFactory factory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
RenderKit renderKit = factory.getRenderKit(ctx, RenderKitFactory.HTML_BASIC_RENDER_KIT);
responseWriter = renderKit.createResponseWriter(out, RIConstants.TEXT_XML_CONTENT_TYPE, encoding);
}
}
if (responseWriter instanceof PartialResponseWriter) {
return (PartialResponseWriter) responseWriter;
} else {
return new PartialResponseWriter(responseWriter);
}
}
Aggregations