001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.aspects.portlet;
023:
024: import org.jboss.portal.common.invocation.InvocationException;
025: import org.jboss.portal.common.markup.MarkupElement;
026: import org.jboss.portal.core.portlet.info.CorePortletInfo;
027: import org.jboss.portal.core.portlet.info.MarkupHeaderInfo;
028: import org.jboss.portal.portlet.container.PortletContainer;
029: import org.jboss.portal.portlet.container.PortletContainerInvoker;
030: import org.jboss.portal.portlet.invocation.PortletInterceptor;
031: import org.jboss.portal.portlet.invocation.PortletInvocation;
032: import org.jboss.portal.portlet.invocation.response.FragmentResponse;
033: import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
034:
035: import java.io.StringWriter;
036: import java.util.Iterator;
037:
038: /**
039: * @author <a href="mailto:mholzner@novell.com">Martin Holzner</a>
040: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
041: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
042: * @version $Revision: 8786 $
043: */
044: public class HeaderInterceptor extends PortletInterceptor {
045:
046: protected Object invoke(PortletInvocation invocation)
047: throws Exception, InvocationException {
048: PortletInvocationResponse response = (PortletInvocationResponse) invocation
049: .invokeNext();
050:
051: // Only affect fragments
052: if (response instanceof FragmentResponse) {
053: FragmentResponse fragment = (FragmentResponse) response;
054:
055: // The buffer that may become non null if we have content to write in it
056: StringWriter buffer = null;
057:
058: // Get existing header which may be null
059: String headerChars = fragment.getHeader();
060:
061: // Add declaratively injected header content here (get it via the component)
062: PortletContainer container = (PortletContainer) invocation
063: .getAttribute(PortletInvocation.INVOCATION_SCOPE,
064: PortletContainerInvoker.PORTLET_CONTAINER);
065: CorePortletInfo info = (CorePortletInfo) container
066: .getInfo();
067: MarkupHeaderInfo headerContentInfo = info.getMarkupHeader();
068: if (headerContentInfo != null) {
069: // Init the buffer
070: buffer = initBuffer(headerChars);
071:
072: // Get the context path
073: String contextPath = (String) invocation
074: .getDispatchedRequest().getAttribute(
075: "javax.servlet.include.context_path");
076:
077: // Render header
078: render(headerContentInfo, buffer, contextPath);
079: }
080:
081: // Add header content coming from the response written by the portlet at runtime
082: String headerContent = (String) invocation.getAttribute(
083: PortletInvocation.RESPONSE_PROPERTIES_SCOPE,
084: "HEADER_CONTENT");
085: if (headerContent != null) {
086: // Init buffer if needed
087: if (buffer == null) {
088: buffer = initBuffer(headerChars);
089: }
090:
091: // Append the content as it is
092: buffer.write(headerContent);
093: }
094:
095: // If the buffer is not empty, use it
096: if (buffer != null) {
097: headerChars = buffer.toString();
098: }
099:
100: //
101: fragment.setHeader(headerChars);
102: }
103:
104: //
105: return response;
106: }
107:
108: private StringWriter initBuffer(String existingBuffer) {
109: if (existingBuffer != null) {
110: StringWriter buffer = new StringWriter(existingBuffer
111: .length() + 100);
112: buffer.write(existingBuffer);
113: return buffer;
114: } else {
115: return new StringWriter(100);
116: }
117: }
118:
119: /**
120: * Render the header content provided by the portlet.
121: *
122: * @param headerContent
123: * @param writer
124: * @param contextPath
125: */
126: protected void render(MarkupHeaderInfo headerContent,
127: StringWriter writer, String contextPath) {
128: for (Iterator i = headerContent.getMarkupElements().iterator(); i
129: .hasNext();) {
130: MarkupElement element = (MarkupElement) i.next();
131: writer.write(element.toString(contextPath));
132: }
133: }
134: }
|