With JSP elements it has been difficult to change the HTTP response header at runtime. Though it has been possible to set the Content-Type per SiteCalalog entries, it was not possible to set this dynamically at run time. Now with support for Groovy as a language for WCS Elements it is much easier to implement HTTP header manipulating responses.
In this blog I will show three use cases:
- setting of the Content-Type header
- sending a HTTP response status code
- sending a redirect
This article assumes that Co-Resident SatelliteServer is used. The setting of the response Content-Type is likely to work with Remote SatelliteServer, the other two use cases will probably fail in some way or form.
To implement the three use-cases you will need to make this work is to create a Groovy element and make sure that this element is the first element called. This means that you will need to create a SIteEntry and mark this as a Wrapper and have the root CSElement be a Groovy element.
Content-Type
The setting of the Content-Type header is in essence straight forward.
ics.StreamHeader('Content-Type', 'application/json'); ics.StreamEvalBytes('{msg: "HelloWorld"}');
Thought this works nicely in this small example, it might fail if the rendering is more complex. If, for instance, from this element other elements are called to actually render the response. In some cases, on which I will not elaborate further, the servlet engine will complain that it cannot give a PrintWriter to a servlet because earlier on in the request handling a ServletOutputStream was already provided to (another) servlet. The error message might be something like “getOutputStream() has already been called for this response“. The javadoc for the ServletResponse states at the getOutputStream() method: Either this method or getWriter() may be called to write the body, not both. Bear with me while I explain where this error comes from and how to work around it.
The ContentServer servlet had (many years ago) support for sending both text and binary responses. This support has been discontinued though under some conditions it will still work. The decision to stream binary or text content is made based on the Content-Type at the time WCS needs to grab either a Writer or an OutputStream to start streaming to the browser. The decision to stream text content is based on the beginning of the Content-Type header; if it starts with ‘text/’ if will stream text, otherwise binary content. The default Content-Type is text/html. As seen in the example, the Content-Type is changed to ‘application/json’ before any content is streamed. Since ‘application/json’ does not start with ‘text/’ WCS will stream binary content and call getOutputStream(). If later in the request handling a JSP element is used to render and that JSP is (implicitly) asking for a PrintWriter the request might bomb. The work-around is simple: first call setContent-Type with a ‘text/’ argument on the ics object and than call setContent-Type on the ServletResponse object with the Content-Type you want to set.
ics.StreamHeader('Content-Type', 'text/plain'); ics.getIServlet().getServletResponse().setContentType('application/json'); ics.CallElement('my/element/json' ,null)
In this construct the WCS ContentServer servlet is first told to stream text, and then the HTTP header is changed directly on the HttpServletResponse.
Redirect
Redirecting is straight forward, just call sendRedirect on the HttpServletResponse object.
ics.getIServlet().getServletResponse().sendRedirect("http://search.oracle.com/search/search?start=1&search_p_main_operator=all&q=Webcenter+Sites");
Response status code
Also sending a different response status is straight forward:
ics.getIServlet().getServletResponse().sendError(403);
You can also call setStatus() instead of sendError() if you don’t want to invoke the error handler from the web application.This might be useful in cases where you don’t want to display an error page or where the error page is rendered in WCS.
All content listed on this page is the property of Oracle Corp. Redistribution not allowed without written permission