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.MediaType;
026: import org.jboss.portal.common.util.Tools;
027: import org.jboss.portal.portlet.OpaqueStateString;
028: import org.jboss.portal.portlet.Portlet;
029: import org.jboss.portal.portlet.PortletInvokerException;
030: import org.jboss.portal.portlet.StateString;
031: import org.jboss.portal.portlet.invocation.PortletInvocation;
032: import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
033: import org.jboss.portal.portlet.spi.PortalContext;
034: import org.jboss.portal.portlet.spi.SecurityContext;
035: import org.jboss.portal.portlet.spi.UserContext;
036: import org.jboss.portal.portlet.spi.WindowContext;
037: import org.jboss.portal.portlet.state.AccessMode;
038: import org.jboss.portal.registration.Registration;
039: import org.jboss.portal.server.PortalConstants;
040: import org.jboss.portal.wsrp.UserContextConverter;
041: import org.jboss.portal.wsrp.WSRPConstants;
042: import org.jboss.portal.wsrp.WSRPExceptionFactory;
043: import org.jboss.portal.wsrp.WSRPUtils;
044: import org.jboss.portal.wsrp.core.InvalidHandleFault;
045: import org.jboss.portal.wsrp.core.InvalidRegistrationFault;
046: import org.jboss.portal.wsrp.core.MarkupParams;
047: import org.jboss.portal.wsrp.core.MarkupType;
048: import org.jboss.portal.wsrp.core.OperationFailedFault;
049: import org.jboss.portal.wsrp.core.PortletContext;
050: import org.jboss.portal.wsrp.core.PortletDescription;
051: import org.jboss.portal.wsrp.core.RegistrationContext;
052: import org.jboss.portal.wsrp.core.RuntimeContext;
053: import org.jboss.portal.wsrp.core.UnsupportedMimeTypeFault;
054: import org.jboss.portal.wsrp.core.UnsupportedModeFault;
055: import org.jboss.portal.wsrp.core.UnsupportedWindowStateFault;
056:
057: import javax.activation.MimeTypeParseException;
058: import java.security.Principal;
059: import java.util.Arrays;
060: import java.util.Collections;
061: import java.util.HashSet;
062: import java.util.List;
063: import java.util.Map;
064: import java.util.Set;
065:
066: /**
067: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
068: * @version $Revision: 8784 $
069: * @since 2.6
070: */
071: public abstract class RequestProcessor {
072: private static final String WINDOW_STATE = "window state";
073: private static final String PORTLET_MODE = "portlet mode";
074:
075: protected PortletInvocation invocation;
076: protected MarkupRequest markupRequest;
077: protected PortletDescription portletDescription;
078: protected Portlet portlet;
079: protected WSRPInstanceContext instanceContext;
080: protected WSRPProducerImpl producer;
081:
082: protected RequestProcessor(WSRPProducerImpl producer) {
083: this .producer = producer;
084: }
085:
086: void prepareInvocation() throws InvalidRegistrationFault,
087: OperationFailedFault, InvalidHandleFault,
088: UnsupportedMimeTypeFault, UnsupportedWindowStateFault,
089: UnsupportedModeFault {
090: Registration registration = producer
091: .getRegistrationOrFailIfInvalid(getRegistrationContext());
092:
093: // get session information and deal with it
094: final RuntimeContext runtimeContext = getRuntimeContext();
095: WSRPExceptionFactory
096: .throwMissingParametersFaultIfValueIsMissing(
097: runtimeContext, "RuntimeContext",
098: getContextName());
099:
100: checkForSessionIDs(runtimeContext);
101:
102: // get markup parameters
103: final MarkupParams params = getMarkupParams();
104: WSRPExceptionFactory
105: .throwMissingParametersFaultIfValueIsMissing(params,
106: "MarkupParams", getContextName());
107:
108: // get portlet handle
109: PortletContext wsrpPC = getPortletContext();
110: WSRPExceptionFactory
111: .throwMissingParametersFaultIfValueIsMissing(wsrpPC,
112: "PortletContext", getContextName());
113: org.jboss.portal.portlet.PortletContext portletContext = WSRPUtils
114: .convertToPortalPortletContext(wsrpPC);
115:
116: // retrieve the portlet
117: try {
118: // calls RegistrationLocal.setRegistration so no need to here
119: portlet = producer.getPortletWith(portletContext,
120: registration);
121: } catch (PortletInvokerException e) {
122: throw WSRPExceptionFactory.throwSOAPFaultException(
123: WSRPExceptionFactory.OPERATION_FAILED,
124: "Could not retrieve portlet '" + portletContext
125: + "'", e);
126: }
127:
128: // get portlet description for the desired portlet...
129: final String[] desiredLocales = params.getLocales();
130: portletDescription = producer.getPortletDescription(portlet,
131: desiredLocales);
132: if (Boolean.TRUE.equals(portletDescription.getUsesMethodGet())) {
133: throw WSRPExceptionFactory
134: .throwSOAPFaultException(
135: WSRPExceptionFactory.OPERATION_FAILED,
136: "Portlets using GET method in forms are not currently supported.",
137: null);
138: }
139: MarkupType[] markupTypes = portletDescription.getMarkupTypes();
140:
141: // based on the markup parameters and portlet description generate the most appropriate markup request
142: markupRequest = createMarkupRequestFrom(markupTypes, params,
143: portlet);
144:
145: // prepare information for invocation
146: StateString navigationalState = createNavigationalState(params
147: .getNavigationalState());
148:
149: final org.jboss.portal.wsrp.core.UserContext wsrpUserContext = getUserContext();
150: checkUserContext(wsrpUserContext);
151:
152: SecurityContext securityContext = createSecurityContext(params,
153: runtimeContext, wsrpUserContext);
154: MarkupInfo streamInfo = createStreamInfo(markupRequest);
155: PortalContext portalContext = createPortalContext(params,
156: markupRequest);
157: UserContext userContext = createUserContext(wsrpUserContext,
158: markupRequest.getLocale(), desiredLocales);
159: instanceContext = createInstanceContext(portletContext,
160: getAccessMode(), runtimeContext.getPortletInstanceKey());
161: WindowContext windowContext = createWindowContext(
162: portletContext.getId(), runtimeContext);
163:
164: // prepare the invocation
165: initInvocation(navigationalState, securityContext, streamInfo,
166: portalContext, userContext, windowContext);
167: }
168:
169: abstract RegistrationContext getRegistrationContext();
170:
171: abstract RuntimeContext getRuntimeContext();
172:
173: abstract MarkupParams getMarkupParams();
174:
175: abstract PortletContext getPortletContext();
176:
177: abstract org.jboss.portal.wsrp.core.UserContext getUserContext();
178:
179: abstract String getContextName();
180:
181: abstract AccessMode getAccessMode();
182:
183: abstract void initInvocation(StateString navigationalState,
184: SecurityContext securityContext, MarkupInfo markupInfo,
185: PortalContext portalContext, UserContext userContext,
186: WindowContext windowContext);
187:
188: abstract Object processResponse(PortletInvocationResponse response);
189:
190: /**
191: * Returns the most appropriate information to base markup generation on based on a Portlet's specified markup types
192: * and a markup request parameters.
193: *
194: * @param markupTypes the Portlet's specified markup types
195: * @param params the markup request parameters
196: * @param portlet
197: * @return a MarkupRequest containing the most appropriate information to base markup generation for this request
198: */
199: private MarkupRequest createMarkupRequestFrom(
200: MarkupType[] markupTypes, MarkupParams params,
201: Portlet portlet) throws UnsupportedMimeTypeFault,
202: UnsupportedModeFault, UnsupportedWindowStateFault {
203: String[] desiredMIMETypes = params.getMimeTypes();
204: MarkupType markupType = null;
205:
206: // Get the MIME type to use
207: for (int i = 0; markupType == null
208: && i < desiredMIMETypes.length; i++) {
209: String desiredMIMEType = desiredMIMETypes[i];
210: for (int j = 0; j < markupTypes.length; j++) {
211: if (desiredMIMEType.equalsIgnoreCase(markupTypes[j]
212: .getMimeType())) {
213: markupType = markupTypes[j];
214: break;
215: }
216: }
217: }
218:
219: // no MIME type was found: error!
220: if (markupType == null) {
221: throw WSRPExceptionFactory.throwSOAPFaultException(
222: WSRPExceptionFactory.UNSUPPORTED_MIME_TYPE,
223: "None of the specified MIME types are supported by portlet '"
224: + portlet.getContext().getId() + "'", null);
225: }
226:
227: // get the mode
228: String mode;
229: try {
230: mode = getMatchingOrFailFrom(markupType.getModes(), params
231: .getMode(), PORTLET_MODE);
232: } catch (IllegalArgumentException e) {
233: throw WSRPExceptionFactory.throwSOAPFaultException(
234: WSRPExceptionFactory.UNSUPPORTED_MODE,
235: "Unsupported mode '" + params.getMode() + "'", e);
236: }
237:
238: // get the window state
239: String windowState;
240: try {
241: windowState = getMatchingOrFailFrom(markupType
242: .getWindowStates(), params.getWindowState(),
243: WINDOW_STATE);
244: } catch (IllegalArgumentException e) {
245: throw WSRPExceptionFactory.throwSOAPFaultException(
246: WSRPExceptionFactory.UNSUPPORTED_WINDOW_STATE,
247: "Unsupported window state '" + params.getMode()
248: + "'", e);
249: }
250:
251: // get the character set
252: String characterSet = getMatchingOrDefaultFrom(new String[0],
253: params.getMarkupCharacterSets(),
254: WSRPConstants.DEFAULT_CHARACTER_SET);
255:
256: return new MarkupRequest(markupType, mode, windowState,
257: characterSet, portlet);
258: }
259:
260: /**
261: * Retrieves the desired value from the set of possible values if such value exists or throw an
262: * <code>IllegalArgumentException</code>.
263: *
264: * @param possibleValues the set of supported values
265: * @param desired the desired value
266: * @param valueType a name identifying the type of the desired value (for error reporting purpose)
267: * @return the desired value
268: * @throws IllegalArgumentException if the desired value is not found in the set of possible values
269: */
270: private String getMatchingOrFailFrom(String[] possibleValues,
271: String desired, String valueType)
272: throws IllegalArgumentException {
273: List possibleList = Arrays.asList(possibleValues);
274: if (possibleList.contains(desired)) {
275: return desired;
276: }
277: throw new IllegalArgumentException(desired
278: + " is not a supported " + valueType);
279: }
280:
281: /**
282: * Retrieves the best matching value from a set of possible values based on an ordered set of preferred values or the
283: * given default value if no matching value is found.
284: *
285: * @param possibleValues the set of possible values
286: * @param preferredValues the ordered (according to user preferences) set of preferred values
287: * @param defaultValue the default value to be used if no match can be found
288: * @return the first match in the set of possible values from the ordered set of preferred values or the default
289: * value if no such value can be found
290: */
291: private String getMatchingOrDefaultFrom(String[] possibleValues,
292: String[] preferredValues, String defaultValue) {
293: if (preferredValues != null && possibleValues != null) {
294: List possibleList = Arrays.asList(possibleValues);
295:
296: for (int i = 0; i < preferredValues.length; i++) {
297: if (possibleList.contains(preferredValues[i])) {
298: return preferredValues[i];
299: }
300: }
301: }
302:
303: return defaultValue;
304: }
305:
306: private void checkUserContext(
307: org.jboss.portal.wsrp.core.UserContext wsrpUserContext) {
308: if (wsrpUserContext != null) {
309: WSRPExceptionFactory
310: .throwMissingParametersFaultIfValueIsMissing(
311: wsrpUserContext.getUserContextKey(),
312: "User Context Key", "UserContext");
313: }
314: }
315:
316: private void checkForSessionIDs(RuntimeContext runtimeContext) {
317: if (runtimeContext.getSessionID() != null) {
318: MarkupHandler.throwOperationFaultOnSessionOperation();
319: }
320: }
321:
322: protected StateString createNavigationalState(
323: String navigationalState) {
324: if (navigationalState == null) {
325: return null;
326: } else {
327: return new OpaqueStateString(navigationalState);
328: }
329: }
330:
331: private WSRPInstanceContext createInstanceContext(
332: org.jboss.portal.portlet.PortletContext portletContext,
333: final AccessMode accessMode, String instanceId) {
334: return new WSRPInstanceContext(portletContext, accessMode,
335: instanceId);
336: }
337:
338: private WindowContext createWindowContext(
339: final String portletHandle,
340: final RuntimeContext runtimeContext) {
341: return new WindowContext() {
342: public String getId() {
343: String prefix = runtimeContext.getNamespacePrefix();
344: if (prefix != null && prefix.length() > 0) {
345: return prefix;
346: } else {
347: // No provided namespace prefix for portlet, using portlet handle instead
348: return portletHandle;
349: }
350: }
351: };
352: }
353:
354: private UserContext createUserContext(
355: final org.jboss.portal.wsrp.core.UserContext userContext,
356: String preferredLocale, final String[] supportedLocales) {
357: // todo: investigate ways to cache this information?
358: // fix-me: should getInformations be put in the request attribute PortletRequest.USER_INFO?
359: return UserContextConverter.createPortalUserContextFrom(
360: userContext, supportedLocales, preferredLocale);
361: }
362:
363: private PortalContext createPortalContext(
364: final MarkupParams params, final MarkupRequest markupRequest) {
365: return new PortalContext() {
366:
367: public String getInfo() {
368: return PortalConstants.VERSION.toString();
369: }
370:
371: public Set getWindowStates() {
372: String[] validNewWindowStates = params
373: .getValidNewWindowStates();
374: if (validNewWindowStates != null) {
375: Set states = new HashSet(
376: validNewWindowStates.length);
377: for (int i = 0; i < validNewWindowStates.length; i++) {
378: String state = validNewWindowStates[i];
379: states
380: .add(WSRPUtils
381: .getJSR168WindowStateFromWSRPName(state));
382: }
383: return states;
384: }
385: return markupRequest.getSupportedWindowStates();
386: }
387:
388: public Set getModes() {
389: String[] validNewModes = params.getValidNewModes();
390: if (validNewModes != null) {
391: Set modes = new HashSet(validNewModes.length);
392: for (int i = 0; i < validNewModes.length; i++) {
393: String mode = validNewModes[i];
394: modes
395: .add(WSRPUtils
396: .getJSR168PortletModeFromWSRPName(mode));
397: }
398: return modes;
399: }
400: return markupRequest.getSupportedModes();
401: }
402:
403: public Map getProperties() {
404: return Collections.EMPTY_MAP;
405: }
406: };
407: }
408:
409: private MarkupInfo createStreamInfo(MarkupRequest markupRequest) {
410: MarkupInfo markupInfo;
411: try {
412: markupInfo = new MarkupInfo(MediaType
413: .parseMimeType(markupRequest.getMimeType()),
414: markupRequest.getCharacterSet());
415: } catch (MimeTypeParseException e) {
416: throw WSRPExceptionFactory.throwSOAPFaultException(
417: WSRPExceptionFactory.UNSUPPORTED_MIME_TYPE, e
418: .getLocalizedMessage(), e);
419: }
420: return markupInfo;
421: }
422:
423: // fix-me: check that the correct semantics is used.
424: private SecurityContext createSecurityContext(
425: final MarkupParams params,
426: final RuntimeContext runtimeContext,
427: final org.jboss.portal.wsrp.core.UserContext wsrpUserContext) {
428: return new SecurityContext() {
429: public boolean isSecure() {
430: return params.isSecureClientCommunication();
431: }
432:
433: public String getAuthType() {
434: return runtimeContext.getUserAuthentication();
435: }
436:
437: public String getRemoteUser() {
438: if (wsrpUserContext != null) {
439: return wsrpUserContext.getUserContextKey();
440: }
441: return null;
442: }
443:
444: public Principal getUserPrincipal() {
445: return null;
446: }
447:
448: public boolean isUserInRole(String roleName) {
449: return wsrpUserContext != null
450: && Tools.isContainedIn(roleName,
451: wsrpUserContext.getUserCategories());
452: }
453:
454: public boolean isAuthenticated() {
455: return wsrpUserContext != null;
456: }
457: };
458: }
459:
460: public PortletInvocation getInvocation() {
461: return invocation;
462: }
463:
464: public void setInvocation(PortletInvocation invocation) {
465: this.invocation = invocation;
466: }
467: }
|