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;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.common.i18n.LocaleFormat;
027: import org.jboss.portal.common.util.ConversionException;
028: import org.jboss.portal.common.util.ParameterValidation;
029: import org.jboss.portal.portlet.ActionURL;
030: import org.jboss.portal.portlet.PortletContext;
031: import org.jboss.portal.portlet.PortletURL;
032: import org.jboss.portal.portlet.RenderURL;
033: import org.jboss.portal.portlet.state.AccessMode;
034: import org.jboss.portal.wsrp.core.InteractionParams;
035: import org.jboss.portal.wsrp.core.MarkupParams;
036: import org.jboss.portal.wsrp.core.ModelDescription;
037: import org.jboss.portal.wsrp.core.NamedString;
038: import org.jboss.portal.wsrp.core.PropertyDescription;
039: import org.jboss.portal.wsrp.core.StateChange;
040: import org.jboss.portal.wsrp.registration.LocalizedString;
041: import org.jboss.portal.wsrp.registration.RegistrationPropertyDescription;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.Locale;
048: import java.util.Map;
049:
050: /**
051: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
052: * @version $Revision: 9360 $
053: * @since 2.4
054: */
055: public class WSRPUtils {
056: private final static Map JSR168_WSRP_WINDOW_STATES = new HashMap(7);
057: private final static Map WSRP_JSR168_WINDOW_STATES = new HashMap(7);
058: private final static Map JSR168_WSRP_MODES = new HashMap(7);
059: private final static Map WSRP_JSR168_MODES = new HashMap(7);
060: private static final String SET_OF_LOCALES = "set of Locales";
061: private static final String MODE = "Mode";
062: private static final String WSRP_MODE_NAME = "wsrp portlet name";
063: private static final String WSRP_WINDOW_STATE_NAME = "wsrp window state name";
064: private static final String WINDOW_STATE = "WindowState";
065:
066: static {
067: JSR168_WSRP_WINDOW_STATES.put(WindowState.MAXIMIZED.toString(),
068: WSRPConstants.MAXIMIZED_WINDOW_STATE);
069: JSR168_WSRP_WINDOW_STATES.put(WindowState.MINIMIZED.toString(),
070: WSRPConstants.MINIMIZED_WINDOW_STATE);
071: JSR168_WSRP_WINDOW_STATES.put(WindowState.NORMAL.toString(),
072: WSRPConstants.NORMAL_WINDOW_STATE);
073:
074: JSR168_WSRP_MODES.put(Mode.EDIT.toString(),
075: WSRPConstants.EDIT_MODE);
076: JSR168_WSRP_MODES.put(Mode.HELP.toString(),
077: WSRPConstants.HELP_MODE);
078: JSR168_WSRP_MODES.put(Mode.VIEW.toString(),
079: WSRPConstants.VIEW_MODE);
080:
081: WSRP_JSR168_WINDOW_STATES.put(
082: WSRPConstants.MAXIMIZED_WINDOW_STATE,
083: WindowState.MAXIMIZED);
084: WSRP_JSR168_WINDOW_STATES.put(
085: WSRPConstants.MINIMIZED_WINDOW_STATE,
086: WindowState.MINIMIZED);
087: WSRP_JSR168_WINDOW_STATES.put(
088: WSRPConstants.NORMAL_WINDOW_STATE, WindowState.NORMAL);
089:
090: WSRP_JSR168_MODES.put(WSRPConstants.EDIT_MODE, Mode.EDIT);
091: WSRP_JSR168_MODES.put(WSRPConstants.HELP_MODE, Mode.HELP);
092: WSRP_JSR168_MODES.put(WSRPConstants.VIEW_MODE, Mode.VIEW);
093: }
094:
095: private WSRPUtils() {
096: }
097:
098: public static WindowState getJSR168WindowStateFromWSRPName(
099: String wsrpWindowStateName) {
100: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
101: wsrpWindowStateName, WSRP_WINDOW_STATE_NAME, null);
102: WindowState windowState = (WindowState) WSRP_JSR168_WINDOW_STATES
103: .get(wsrpWindowStateName);
104: return (windowState == null) ? WindowState
105: .create(wsrpWindowStateName) : windowState;
106: }
107:
108: public static String convertJSR168WindowStateNameToWSRPName(
109: String jsr168WindowStateName) {
110: if (jsr168WindowStateName == null) {
111: return WSRPConstants.NORMAL_WINDOW_STATE;
112: }
113:
114: // todo: how should we deal with solo?
115: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
116: jsr168WindowStateName, WSRP_WINDOW_STATE_NAME, null);
117: String wsrpName = (String) JSR168_WSRP_WINDOW_STATES
118: .get(jsr168WindowStateName);
119: return (wsrpName == null) ? jsr168WindowStateName : wsrpName;
120: }
121:
122: public static String getWSRPNameFromJSR168WindowState(
123: WindowState windowState) {
124: ParameterValidation.throwIllegalArgExceptionIfNull(windowState,
125: WINDOW_STATE);
126: return convertJSR168WindowStateNameToWSRPName(windowState
127: .toString());
128: }
129:
130: public static Mode getJSR168PortletModeFromWSRPName(
131: String wsrpPortletModeName) {
132: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
133: wsrpPortletModeName, WSRP_MODE_NAME, null);
134: Mode mode = (Mode) WSRP_JSR168_MODES.get(wsrpPortletModeName);
135: return (mode == null) ? Mode.create(wsrpPortletModeName) : mode;
136: }
137:
138: public static String convertJSR168PortletModeNameToWSRPName(
139: String jsr168PortletModeName) {
140: if (jsr168PortletModeName == null) {
141: return WSRPConstants.VIEW_MODE;
142: }
143:
144: // todo: how should we deal with preview?
145: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
146: jsr168PortletModeName, WSRP_MODE_NAME, null);
147: String wsrpName = (String) JSR168_WSRP_MODES
148: .get(jsr168PortletModeName);
149: return (wsrpName == null) ? jsr168PortletModeName : wsrpName;
150: }
151:
152: public static String getWSRPNameFromJSR168PortletMode(
153: Mode portletMode) {
154: ParameterValidation.throwIllegalArgExceptionIfNull(portletMode,
155: MODE);
156: return convertJSR168PortletModeNameToWSRPName(portletMode
157: .toString());
158: }
159:
160: public static AccessMode getAccessModeFromStateChange(
161: StateChange stateChange) {
162: if (StateChange.readOnly.equals(stateChange)) {
163: return AccessMode.READ_ONLY;
164: }
165: if (StateChange.cloneBeforeWrite.equals(stateChange)) {
166: return AccessMode.CLONE_BEFORE_WRITE;
167: }
168: if (StateChange.readWrite.equals(stateChange)) {
169: return AccessMode.READ_WRITE;
170: }
171: throw new IllegalArgumentException("Unsupported StateChange: "
172: + stateChange);
173: }
174:
175: public static StateChange getStateChangeFromAccessMode(
176: AccessMode accessMode) {
177: if (AccessMode.READ_ONLY.equals(accessMode)) {
178: return StateChange.readOnly;
179: }
180: if (AccessMode.READ_WRITE.equals(accessMode)) {
181: return StateChange.readWrite;
182: }
183: if (AccessMode.CLONE_BEFORE_WRITE.equals(accessMode)) {
184: return StateChange.cloneBeforeWrite;
185: }
186: throw new IllegalArgumentException("Unsupported AccessMode: "
187: + accessMode);
188: }
189:
190: public static String getWSRPURLTypeFrom(PortletURL url) {
191: if (url instanceof WSRPPortletURL) {
192: return ((WSRPPortletURL) url).getURLType();
193: }
194:
195: if (url instanceof RenderURL) {
196: return WSRPRewritingConstants.URL_TYPE_RENDER;
197: }
198:
199: if (url instanceof ActionURL) {
200: return WSRPRewritingConstants.URL_TYPE_BLOCKING_ACTION;
201: }
202:
203: throw new IllegalArgumentException("Unrecognized URL type.");
204: }
205:
206: public static String convertRequestAuthTypeToWSRPAuthType(
207: String authType) {
208: if (authType == null) {
209: return WSRPConstants.NONE_USER_AUTHENTICATION;
210: }
211: if (HttpServletRequest.CLIENT_CERT_AUTH.equals(authType)) {
212: return WSRPConstants.CERTIFICATE_USER_AUTHENTICATION;
213: }
214: if (HttpServletRequest.BASIC_AUTH.equalsIgnoreCase(authType)
215: || HttpServletRequest.FORM_AUTH.equals(authType)) {
216: return WSRPConstants.PASSWORD_USER_AUTHENTICATION; // is this correct?
217: }
218: return authType;
219: }
220:
221: public static String[] convertLocalesToRFC3066LanguageTagArray(
222: List localesOrderedByPreference) {
223: ParameterValidation.throwIllegalArgExceptionIfNull(
224: localesOrderedByPreference, SET_OF_LOCALES);
225:
226: String[] desiredLocales = new String[localesOrderedByPreference
227: .size()];
228: int i = 0;
229: for (Iterator iterator = localesOrderedByPreference.iterator(); iterator
230: .hasNext(); i++) {
231: Locale locale = (Locale) iterator.next();
232: desiredLocales[i] = toString(locale);
233: }
234: return desiredLocales;
235: }
236:
237: public static PortletContext convertToPortalPortletContext(
238: org.jboss.portal.wsrp.core.PortletContext portletContext) {
239: ParameterValidation.throwIllegalArgExceptionIfNull(
240: portletContext, "PortletContext");
241: String handle = portletContext.getPortletHandle();
242: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
243: handle, "portlet handle", "PortletContext");
244:
245: PortletContext context;
246: byte[] state = portletContext.getPortletState();
247: context = PortletContext.createPortletContext(handle, state);
248:
249: return context;
250: }
251:
252: /**
253: * @param portletContext
254: * @return Since 2.6
255: */
256: public static org.jboss.portal.wsrp.core.PortletContext convertToWSRPPortletContext(
257: PortletContext portletContext) {
258: ParameterValidation.throwIllegalArgExceptionIfNull(
259: portletContext, "PortletContext");
260: String id = portletContext.getId();
261: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id,
262: "portlet id", "PortletContext");
263:
264: org.jboss.portal.wsrp.core.PortletContext result = WSRPTypeFactory
265: .createPortletContext(id);
266: result.setPortletState(portletContext.getState());
267: return result;
268: }
269:
270: /**
271: * @param registrationInfo
272: * @return
273: * @since 2.6
274: */
275: public static ModelDescription convertRegistrationPropertiesToModelDescription(
276: Map registrationInfo) {
277: ParameterValidation.throwIllegalArgExceptionIfNull(
278: registrationInfo, "registration info");
279: if (registrationInfo.isEmpty()) {
280: return WSRPTypeFactory.createModelDescription(null);
281: }
282:
283: int size = registrationInfo.size();
284: PropertyDescription[] propertyDescriptions = new PropertyDescription[size];
285: int i = 0;
286: for (Object o : registrationInfo.values()) {
287: RegistrationPropertyDescription property = (RegistrationPropertyDescription) o;
288: propertyDescriptions[i++] = convertToPropertyDescription(property);
289: }
290:
291: return WSRPTypeFactory
292: .createModelDescription(propertyDescriptions);
293: }
294:
295: /**
296: * @param propertyDescription
297: * @return
298: * @since 2.6
299: */
300: public static PropertyDescription convertToPropertyDescription(
301: RegistrationPropertyDescription propertyDescription) {
302: ParameterValidation.throwIllegalArgExceptionIfNull(
303: propertyDescription, "RegistrationPropertyDescription");
304: PropertyDescription propDesc = WSRPTypeFactory
305: .createPropertyDescription(propertyDescription
306: .getName().toString(), propertyDescription
307: .getType());
308:
309: // todo: deal with languages properly!!
310: LocalizedString hint = propertyDescription.getHint();
311: if (hint != null) {
312: propDesc.setHint(convertToWSRPLocalizedString(hint));
313: }
314: LocalizedString label = propertyDescription.getLabel();
315: if (label != null) {
316: propDesc.setLabel(convertToWSRPLocalizedString(label));
317: }
318: return propDesc;
319: }
320:
321: /**
322: * @param propertyDescription
323: * @return
324: * @since 2.6
325: */
326: public static RegistrationPropertyDescription convertToRegistrationPropertyDescription(
327: PropertyDescription propertyDescription) {
328: ParameterValidation.throwIllegalArgExceptionIfNull(
329: propertyDescription, "PropertyDescription");
330: RegistrationPropertyDescription desc = new RegistrationPropertyDescription(
331: propertyDescription.getName(), propertyDescription
332: .getType());
333: desc.setLabel(getLocalizedStringOrNull(propertyDescription
334: .getLabel()));
335: desc.setHint(getLocalizedStringOrNull(propertyDescription
336: .getHint()));
337:
338: return desc;
339: }
340:
341: private static LocalizedString getLocalizedStringOrNull(
342: org.jboss.portal.wsrp.core.LocalizedString wsrpLocalizedString) {
343: if (wsrpLocalizedString == null) {
344: return null;
345: } else {
346: return convertToLocalizedString(wsrpLocalizedString);
347: }
348: }
349:
350: /**
351: * @param wsrpLocalizedString
352: * @return
353: * @since 2.6
354: */
355: public static LocalizedString convertToLocalizedString(
356: org.jboss.portal.wsrp.core.LocalizedString wsrpLocalizedString) {
357: ParameterValidation.throwIllegalArgExceptionIfNull(
358: wsrpLocalizedString, "WSRP LocalizedString");
359: String lang = wsrpLocalizedString.getLang();
360: Locale locale;
361: if (lang == null) {
362: locale = Locale.getDefault();
363: } else {
364: locale = getLocale(lang);
365: }
366:
367: LocalizedString localizedString = new LocalizedString(
368: wsrpLocalizedString.getValue(), locale);
369: localizedString.setResourceName(wsrpLocalizedString
370: .getResourceName());
371: return localizedString;
372: }
373:
374: /**
375: * @param regLocalizedString
376: * @return
377: * @since 2.6
378: */
379: public static org.jboss.portal.wsrp.core.LocalizedString convertToWSRPLocalizedString(
380: LocalizedString regLocalizedString) {
381: ParameterValidation.throwIllegalArgExceptionIfNull(
382: regLocalizedString, "LocalizedString");
383: return WSRPTypeFactory.createLocalizedString(
384: toString(regLocalizedString.getLocale()),
385: regLocalizedString.getResourceName(),
386: regLocalizedString.getValue());
387: }
388:
389: /**
390: * Creates a WSRP LocalizedString based on the best (according to the specified desired locales) value of the given
391: * LocalizedString.
392: *
393: * @param localizedString the LocalizedString from which a localized value is to be extracted
394: * @param desiredLocales the supported locales orderd by user preference, if no desiredLocales are given, the
395: * default local will be used.
396: * @return the best (according to the specified desired locales) value for the given LocalizedString or
397: * <code>null</code> if no such value can be found.
398: * @since 2.6
399: */
400: public static org.jboss.portal.wsrp.core.LocalizedString convertToWSRPLocalizedString(
401: org.jboss.portal.common.i18n.LocalizedString localizedString,
402: String[] desiredLocales) {
403: if (localizedString == null) {
404: return null;
405: }
406:
407: if (desiredLocales == null || desiredLocales.length == 0) {
408: desiredLocales = new String[] { toString(Locale
409: .getDefault()) };
410: }
411:
412: org.jboss.portal.common.i18n.LocalizedString.Value bestMapping = localizedString
413: .getPreferredOrBestLocalizedMappingFor(desiredLocales);
414: if (bestMapping != null) {
415: Locale locale = bestMapping.getLocale();
416: String value = bestMapping.getString();
417: String language = toString(locale);
418: return WSRPTypeFactory.createLocalizedString(language,
419: null, value);
420: }
421: return null;
422: }
423:
424: public static Locale getLocale(String lang)
425: throws IllegalArgumentException {
426: try {
427: return LocaleFormat.RFC3066_LANGUAGE_TAG.getLocale(lang);
428: } catch (ConversionException e) {
429: // Previous behavior on using ConversionException was like that
430: IllegalArgumentException iae = new IllegalArgumentException();
431: iae.initCause(e);
432: throw iae;
433: }
434: }
435:
436: public static String toString(Locale locale)
437: throws IllegalArgumentException {
438: try {
439: return LocaleFormat.RFC3066_LANGUAGE_TAG.toString(locale);
440: } catch (ConversionException e) {
441: // Previous behavior on using ConversionException was like that
442: IllegalArgumentException iae = new IllegalArgumentException();
443: iae.initCause(e);
444: throw iae;
445: }
446: }
447:
448: /**
449: * Debugging method.
450: *
451: * @param params
452: * @return
453: * @since 2.6
454: */
455: public static String toString(MarkupParams params) {
456: if (params != null) {
457: StringBuffer sb = new StringBuffer("MarkupParams");
458: if (params.isSecureClientCommunication()) {
459: sb.append("(secure)");
460: }
461: sb.append("[M=").append(params.getMode()).append("][WS=")
462: .append(params.getWindowState()).append("]")
463: .append("[NS=").append(
464: params.getNavigationalState()).append("]");
465: return sb.toString();
466: }
467: return null;
468: }
469:
470: /**
471: * Debugging method
472: *
473: * @param interactionParams
474: * @return
475: * @since 2.6
476: */
477: public static String toString(InteractionParams interactionParams) {
478: if (interactionParams != null) {
479: StringBuffer sb = new StringBuffer("InteractionParams");
480: sb.append("[IS=").append(
481: interactionParams.getInteractionState())
482: .append("]").append("[StateChange=").append(
483: interactionParams.getPortletStateChange()
484: .getValue()).append("]");
485: NamedString[] formParams = interactionParams
486: .getFormParameters();
487: if (formParams != null) {
488: sb.append("\n\tForm params:\n");
489: for (int i = 0; i < formParams.length; i++) {
490: NamedString formParam = formParams[i];
491: sb.append("\t\t").append(formParam.getName())
492: .append("='").append(formParam.getValue())
493: .append("'\n");
494: }
495: }
496: return sb.toString();
497: }
498: return null;
499: }
500: }
|