001: package org.wings.portlet.filter;
002:
003: import java.io.IOException;
004:
005: import javax.portlet.RenderRequest;
006: import javax.portlet.RenderResponse;
007: import javax.servlet.Filter;
008: import javax.servlet.FilterChain;
009: import javax.servlet.FilterConfig;
010: import javax.servlet.ServletException;
011: import javax.servlet.ServletRequest;
012: import javax.servlet.ServletResponse;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.portlet.Const;
018:
019: /**
020: * @author <a href="mailto:marc.musch@mercatis.com">Marc Musch</a>
021: */
022: public class WingS2PortletResponseFilter implements Filter {
023:
024: private final transient static Log log = LogFactory
025: .getLog(WingS2PortletResponseFilter.class);
026:
027: public void init(FilterConfig config) throws ServletException {
028: }
029:
030: /**
031: * Filters the response from the wings servlet. replaces the relative urls
032: * with absolut urls using the contextPath from the renderRequest and
033: * the servlet URL, both given through the request from the portlet.
034: */
035: public void doFilter(ServletRequest request,
036: ServletResponse response, FilterChain filterChain)
037: throws IOException, ServletException {
038:
039: log.debug("start doFilter()");
040:
041: // create response wrapper to get the response content
042: final WingS2PortletResponseWrapper wrapper = new WingS2PortletResponseWrapper(
043: (HttpServletResponse) response);
044:
045: // do the filter chain
046: filterChain.doFilter(request, wrapper);
047:
048: // get the response stream as string
049: String responseContent = wrapper.toString();
050:
051: // get the renderRequest and the renderResponse out of the request
052: RenderRequest renderRequest = (RenderRequest) request
053: .getAttribute(Const.REQUEST_ATTR_RENDER_REQUEST);
054: RenderResponse renderResponse = (RenderResponse) request
055: .getAttribute(Const.REQUEST_ATTR_RENDER_RESPONSE);
056:
057: // get the contextPath from the request and encode it with the response encoder
058: String contextPathEncoded = "";
059: if (renderRequest != null) {
060: String contextPath = renderRequest.getContextPath();
061: if (renderResponse != null) {
062: contextPathEncoded = renderResponse
063: .encodeURL(contextPath);
064: } else {
065: log
066: .error("the renderResponse from the response attribute "
067: + Const.REQUEST_ATTR_RENDER_RESPONSE
068: + " is null");
069: }
070: } else {
071: log.error("the renderRequest from the request attribute "
072: + Const.REQUEST_ATTR_RENDER_REQUEST + " is null");
073: }
074:
075: // get the path to the wing servlet from the request
076: String wingSServletURL = "";
077: wingSServletURL = (String) request
078: .getAttribute(Const.REQUEST_ATTR_WINGS_SERVLET_URL);
079: if (wingSServletURL == null) {
080: log
081: .error("the wingsServletURL from the request attribute "
082: + Const.REQUEST_ATTR_WINGS_SERVLET_URL
083: + " is null");
084: }
085:
086: // replace relative urls
087: responseContent = responseContent.replace("-org/wings/",
088: contextPathEncoded + wingSServletURL + "-org/wings/");
089: responseContent = responseContent.replace("../dwr/",
090: contextPathEncoded + "/dwr/");
091:
092: log.debug("response filtered");
093:
094: // write the response stream
095: response.setContentLength(responseContent.length());
096: response.getOutputStream().print(responseContent);
097:
098: log.debug("end doFilter()");
099:
100: }
101:
102: public void destroy() {
103: }
104:
105: }
|