001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2007, 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.wsrp.producer;
023:
024: import org.jboss.portal.common.util.MarkupInfo;
025: import org.jboss.portal.common.util.Tools;
026: import org.jboss.portal.portlet.StateString;
027: import org.jboss.portal.portlet.impl.jsr168.PortletUtils;
028: import org.jboss.portal.portlet.invocation.RenderInvocation;
029: import org.jboss.portal.portlet.invocation.response.FragmentResponse;
030: import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
031: import org.jboss.portal.portlet.spi.PortalContext;
032: import org.jboss.portal.portlet.spi.SecurityContext;
033: import org.jboss.portal.portlet.spi.UserContext;
034: import org.jboss.portal.portlet.spi.WindowContext;
035: import org.jboss.portal.portlet.state.AccessMode;
036: import org.jboss.portal.wsrp.WSRPConstants;
037: import org.jboss.portal.wsrp.WSRPRewritingConstants;
038: import org.jboss.portal.wsrp.WSRPTypeFactory;
039: import org.jboss.portal.wsrp.WSRPUtils;
040: import org.jboss.portal.wsrp.core.GetMarkup;
041: import org.jboss.portal.wsrp.core.InvalidHandleFault;
042: import org.jboss.portal.wsrp.core.InvalidRegistrationFault;
043: import org.jboss.portal.wsrp.core.MarkupContext;
044: import org.jboss.portal.wsrp.core.MarkupParams;
045: import org.jboss.portal.wsrp.core.MarkupResponse;
046: import org.jboss.portal.wsrp.core.MissingParametersFault;
047: import org.jboss.portal.wsrp.core.OperationFailedFault;
048: import org.jboss.portal.wsrp.core.PortletContext;
049: import org.jboss.portal.wsrp.core.RegistrationContext;
050: import org.jboss.portal.wsrp.core.RuntimeContext;
051: import org.jboss.portal.wsrp.core.UnsupportedMimeTypeFault;
052: import org.jboss.portal.wsrp.core.UnsupportedModeFault;
053: import org.jboss.portal.wsrp.core.UnsupportedWindowStateFault;
054:
055: /**
056: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
057: * @version $Revision: 8784 $
058: * @since 2.6
059: */
060: public class RenderRequestProcessor extends RequestProcessor {
061: private static final String EMPTY = "";
062: private final GetMarkup getMarkup;
063:
064: public RenderRequestProcessor(WSRPProducerImpl producer,
065: GetMarkup getMarkup) throws UnsupportedMimeTypeFault,
066: UnsupportedWindowStateFault, InvalidHandleFault,
067: UnsupportedModeFault, MissingParametersFault,
068: InvalidRegistrationFault, OperationFailedFault {
069: super (producer);
070: this .getMarkup = getMarkup;
071: prepareInvocation();
072: }
073:
074: RegistrationContext getRegistrationContext() {
075: return getMarkup.getRegistrationContext();
076: }
077:
078: RuntimeContext getRuntimeContext() {
079: return getMarkup.getRuntimeContext();
080: }
081:
082: MarkupParams getMarkupParams() {
083: return getMarkup.getMarkupParams();
084: }
085:
086: PortletContext getPortletContext() {
087: return getMarkup.getPortletContext();
088: }
089:
090: org.jboss.portal.wsrp.core.UserContext getUserContext() {
091: return getMarkup.getUserContext();
092: }
093:
094: String getContextName() {
095: return MarkupHandler.GET_MARKUP;
096: }
097:
098: AccessMode getAccessMode() {
099: return AccessMode.READ_ONLY;
100: }
101:
102: void initInvocation(StateString navigationalState,
103: SecurityContext securityContext, MarkupInfo markupInfo,
104: PortalContext portalContext, UserContext userContext,
105: WindowContext windowContext) {
106: WSRPRenderContext renderContext = new WSRPRenderContext(
107: navigationalState, securityContext, markupInfo,
108: portalContext, userContext, instanceContext,
109: windowContext, WSRPUtils
110: .getJSR168PortletModeFromWSRPName(markupRequest
111: .getMode()), WSRPUtils
112: .getJSR168WindowStateFromWSRPName(markupRequest
113: .getWindowState()));
114: setInvocation(new RenderInvocation(renderContext));
115: renderContext.contextualize(invocation);
116:
117: invocation.setTarget(portlet.getContext());
118: }
119:
120: Object processResponse(PortletInvocationResponse response) {
121: FragmentResponse fragment = (FragmentResponse) response;
122: String markupString = null;
123: byte[] markupBinary = null;
124: switch (fragment.getType()) {
125: case FragmentResponse.TYPE_CHARS:
126: markupString = processFragmentString(fragment.getChars()
127: .toString());
128: break;
129: case FragmentResponse.TYPE_BYTES:
130: markupBinary = fragment.getBytes().toByteArray(); // fix-me: might need to convert to Base64?
131: break;
132: case FragmentResponse.TYPE_EMPTY:
133: markupString = EMPTY;
134: break;
135: }
136:
137: MarkupContext markupContext;
138: if (markupString != null) {
139: markupContext = WSRPTypeFactory.createMarkupContext(
140: markupRequest.getMimeType(), markupString);
141: } else {
142: markupContext = WSRPTypeFactory.createMarkupContext(
143: markupRequest.getMimeTypeWithCharset(),
144: markupBinary);
145: }
146: markupContext.setLocale(markupRequest.getLocale());
147: markupContext.setRequiresUrlRewriting(Boolean.TRUE);
148: markupContext.setPreferredTitle(portletDescription.getTitle()
149: .getValue());
150:
151: // cache information
152: int expires = fragment.getExpirationSecs();
153: // only create a CacheControl if expiration time is not 0
154: if (expires != 0) {
155: // if expires is negative, replace by -1 to make sure
156: if (expires < 0) {
157: expires = -1;
158: }
159:
160: markupContext.setCacheControl(WSRPTypeFactory
161: .createCacheControl(expires,
162: WSRPConstants.CACHE_PER_USER));
163: }
164:
165: return new MarkupResponse(markupContext, null, null);
166: }
167:
168: /**
169: * Process String returned from RenderResult to add rewriting token if necessary, replacing namespaces by the WSRP
170: * rewrite token. fix-me: need to check for producer rewriting
171: *
172: * @param renderString the String to be processed for rewriting marking
173: * @return a String processed to add rewriting tokens as necessary
174: */
175: private String processFragmentString(String renderString) {
176: return Tools.replaceAllInstancesOfBoundedString(renderString,
177: PortletUtils.PREFIX, PortletUtils.SUFFIX,
178: WSRPRewritingConstants.WSRP_REWRITE_TOKEN);
179: }
180: }
|