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.wsrp.consumer;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.common.util.ContentInfo;
026: import org.jboss.portal.common.util.ParameterValidation;
027: import org.jboss.portal.portlet.PortletInvokerException;
028: import org.jboss.portal.portlet.PortletParametersStateString;
029: import org.jboss.portal.portlet.StateString;
030: import org.jboss.portal.portlet.invocation.PortletInvocation;
031: import org.jboss.portal.portlet.spi.PortletInvocationContext;
032: import org.jboss.portal.portlet.spi.SecurityContext;
033: import org.jboss.portal.portlet.spi.UserContext;
034: import org.jboss.portal.wsrp.WSRPConstants;
035: import org.jboss.portal.wsrp.WSRPTypeFactory;
036: import org.jboss.portal.wsrp.WSRPUtils;
037: import org.jboss.portal.wsrp.core.MarkupParams;
038: import org.jboss.portal.wsrp.core.PortletContext;
039: import org.jboss.portal.wsrp.core.RuntimeContext;
040:
041: /**
042: * Extracts basic required elements for all invocation requests.
043: *
044: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
045: * @version $Revision: 8784 $
046: * @since 2.4
047: */
048: class RequestPrecursor {
049: private final static Logger log = Logger
050: .getLogger(RequestPrecursor.class);
051:
052: private PortletContext portletContext;
053: RuntimeContext runtimeContext;
054: MarkupParams markupParams;
055: private static final String PORTLET_HANDLE = "portlet handle";
056: private static final String SECURITY_CONTEXT = "security context";
057: private static final String USER_CONTEXT = "user context";
058: private static final String INVOCATION_CONTEXT = "invocation context";
059: private static final String STREAM_INFO = "stream info in invocation context";
060: private static final String USER_AGENT = "User-Agent";
061:
062: public RequestPrecursor(WSRPConsumerImpl wsrpConsumer,
063: PortletInvocation invocation)
064: throws PortletInvokerException {
065: // retrieve handle
066: portletContext = WSRPUtils
067: .convertToWSRPPortletContext(WSRPConsumerImpl
068: .getPortletContext(invocation));
069: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
070: getPortletHandle(), PORTLET_HANDLE, null);
071: log.debug("About to invoke on portlet: " + getPortletHandle());
072:
073: // create runtime context
074: SecurityContext securityContext = invocation
075: .getSecurityContext();
076: ParameterValidation.throwIllegalArgExceptionIfNull(
077: securityContext, SECURITY_CONTEXT);
078: String authType = WSRPUtils
079: .convertRequestAuthTypeToWSRPAuthType(securityContext
080: .getAuthType());
081: runtimeContext = WSRPTypeFactory.createRuntimeContext(authType);
082:
083: // set the session id if needed
084: wsrpConsumer.getSessionHandler().setSessionIdIfNeeded(
085: invocation, runtimeContext, getPortletHandle());
086:
087: wsrpConsumer.setTemplatesIfNeeded(invocation, runtimeContext);
088:
089: // create markup params
090: UserContext userContext = invocation.getUserContext();
091: ParameterValidation.throwIllegalArgExceptionIfNull(userContext,
092: USER_CONTEXT);
093: PortletInvocationContext context = invocation
094: .getPortletContext();
095: ParameterValidation.throwIllegalArgExceptionIfNull(context,
096: INVOCATION_CONTEXT);
097: ContentInfo streamInfo = context.getMarkupInfo();
098: ParameterValidation.throwIllegalArgExceptionIfNull(streamInfo,
099: STREAM_INFO);
100:
101: String mode;
102: try {
103: mode = WSRPUtils.getWSRPNameFromJSR168PortletMode(context
104: .getMode());
105: } catch (Exception e) {
106: log.debug("Mode was null in context.");
107: mode = WSRPConstants.VIEW_MODE;
108: }
109:
110: String windowState;
111: try {
112: windowState = WSRPUtils
113: .getWSRPNameFromJSR168WindowState(context
114: .getWindowState());
115: } catch (Exception e) {
116: log.debug("WindowState was null in context.");
117: windowState = WSRPConstants.NORMAL_WINDOW_STATE;
118: }
119:
120: markupParams = WSRPTypeFactory
121: .createMarkupParams(
122: securityContext.isSecure(),
123: WSRPUtils
124: .convertLocalesToRFC3066LanguageTagArray(userContext
125: .getLocales()),
126: new String[] { streamInfo.getContentType()
127: .toString() }, mode, windowState);
128: String userAgent = WSRPConsumerImpl.getHttpRequest(invocation)
129: .getHeader(USER_AGENT);
130: markupParams.setClientData(WSRPTypeFactory
131: .createClientData(userAgent));
132: StateString navigationalState = context.getNavigationalState();
133: if (navigationalState != null) {
134: String state = navigationalState.getStringValue();
135: if (!PortletParametersStateString.JBPNS_PREFIX
136: .equals(state)) // fix-me: see JBPORTAL-900
137: {
138: markupParams.setNavigationalState(state);
139: }
140: }
141:
142: log.debug(WSRPUtils.toString(markupParams));
143: }
144:
145: public String getPortletHandle() {
146: return portletContext.getPortletHandle();
147: }
148:
149: public PortletContext getPortletContext() {
150: return portletContext;
151: }
152: }
|