001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.portletadmin;
021:
022: import java.io.StringWriter;
023: import java.util.Enumeration;
024: import java.util.HashMap;
025: import java.util.Hashtable;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Set;
029: import java.util.Vector;
030:
031: import javax.portlet.PortletRequest;
032: import javax.portlet.PortletSession;
033:
034: import com.nabhinc.portal.config.DescriptionType;
035: import com.nabhinc.portal.config.DisplayNameType;
036: import com.nabhinc.portal.config.ExpirationCacheType;
037: import com.nabhinc.portal.config.InitParamType;
038: import com.nabhinc.portal.config.KeywordsType;
039: import com.nabhinc.portal.config.MimeTypeType;
040: import com.nabhinc.portal.config.NameType;
041: import com.nabhinc.portal.config.PortletApp;
042: import com.nabhinc.portal.config.PortletCollectionType;
043: import com.nabhinc.portal.config.PortletInfoType;
044: import com.nabhinc.portal.config.PortletModeType;
045: import com.nabhinc.portal.config.PortletNameType;
046: import com.nabhinc.portal.config.PortletPreferencesType;
047: import com.nabhinc.portal.config.PortletType;
048: import com.nabhinc.portal.config.PreferenceType;
049: import com.nabhinc.portal.config.ResourceBundleType;
050: import com.nabhinc.portal.config.RoleLinkType;
051: import com.nabhinc.portal.config.SecurityConstraintType;
052: import com.nabhinc.portal.config.SecurityRoleRefType;
053: import com.nabhinc.portal.config.ShortTitleType;
054: import com.nabhinc.portal.config.SupportedLocaleType;
055: import com.nabhinc.portal.config.SupportsType;
056: import com.nabhinc.portal.config.TitleType;
057: import com.nabhinc.portal.config.UserDataConstraintType;
058: import com.nabhinc.portal.config.ValueType;
059: import com.nabhinc.portal.config.impl.DescriptionTypeImpl;
060: import com.nabhinc.portal.config.impl.DisplayNameTypeImpl;
061: import com.nabhinc.portal.config.impl.ExpirationCacheTypeImpl;
062: import com.nabhinc.portal.config.impl.InitParamTypeImpl;
063: import com.nabhinc.portal.config.impl.KeywordsTypeImpl;
064: import com.nabhinc.portal.config.impl.MimeTypeTypeImpl;
065: import com.nabhinc.portal.config.impl.NameTypeImpl;
066: import com.nabhinc.portal.config.impl.PortletCollectionTypeImpl;
067: import com.nabhinc.portal.config.impl.PortletInfoTypeImpl;
068: import com.nabhinc.portal.config.impl.PortletModeTypeImpl;
069: import com.nabhinc.portal.config.impl.PortletNameTypeImpl;
070: import com.nabhinc.portal.config.impl.PortletPreferencesTypeImpl;
071: import com.nabhinc.portal.config.impl.PortletTypeImpl;
072: import com.nabhinc.portal.config.impl.PreferenceTypeImpl;
073: import com.nabhinc.portal.config.impl.ResourceBundleTypeImpl;
074: import com.nabhinc.portal.config.impl.RoleLinkTypeImpl;
075: import com.nabhinc.portal.config.impl.SecurityConstraintTypeImpl;
076: import com.nabhinc.portal.config.impl.SecurityRoleRefTypeImpl;
077: import com.nabhinc.portal.config.impl.ShortTitleTypeImpl;
078: import com.nabhinc.portal.config.impl.SupportedLocaleTypeImpl;
079: import com.nabhinc.portal.config.impl.SupportsTypeImpl;
080: import com.nabhinc.portal.config.impl.TitleTypeImpl;
081: import com.nabhinc.portal.config.impl.UserDataConstraintTypeImpl;
082: import com.nabhinc.portal.config.impl.ValueTypeImpl;
083: import com.nabhinc.portal.core.PortletConfigInfo;
084:
085: /**
086: *
087: *
088: * @author Padmanabh Dabke<br/>
089: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
090: */
091: public class PortletUtil {
092:
093: public static String getPortletParameter(String paramName,
094: PortletRequest rReq, PortletSession pSession) {
095: String paramValue = rReq.getParameter(paramName);
096: if (paramValue != null) {
097: pSession.setAttribute(paramName, paramValue,
098: PortletSession.PORTLET_SCOPE);
099: } else {
100: paramValue = (String) pSession.getAttribute(paramName,
101: PortletSession.PORTLET_SCOPE);
102: }
103:
104: return paramValue;
105: }
106:
107: /**
108: * Create PortletType of specified portlet class and name.
109: * @param portletName The portlet's name.
110: * @param className The portlet class name.
111: * @return
112: */
113: public static PortletType createPortletType(String portletName,
114: String className) {
115: if (portletName == null || className == null)
116: return null;
117:
118: PortletType pType = new PortletTypeImpl();
119: PortletInfoType pInfoType = new PortletInfoTypeImpl();
120:
121: PortletNameType portletNameType = new PortletNameTypeImpl();
122: portletNameType.setValue(portletName.trim());
123: pType.setPortletName(portletNameType);
124: pType.setPortletClass(className);
125: pType.setPortletInfo(pInfoType);
126:
127: return pType;
128: }
129:
130: /**
131: * Sets portlets Category or keywords
132: * @param keywords A comma separated category keywords.
133: */
134: public static void setKeywords(String keywords, PortletType pType) {
135: if (keywords != null && !"".equals(keywords.trim())) {
136: KeywordsType keywordType = new KeywordsTypeImpl();
137: keywordType.setValue(keywords);
138: pType.getPortletInfo().setKeywords(keywordType);
139: }
140: }
141:
142: /**
143: * Sets portlet title
144: * @param portletTitle
145: * @param pType
146: */
147: public static void setTitle(String portletTitle, PortletType pType) {
148: if (portletTitle != null && !"".equals(portletTitle.trim())) {
149: TitleType titleType = new TitleTypeImpl();
150: titleType.setValue(portletTitle);
151: pType.getPortletInfo().setTitle(titleType);
152: }
153: }
154:
155: /**
156: * Sets portlet short title
157: * @param portletShortTitle
158: * @param pType
159: */
160: public static void setShortTitle(String portletShortTitle,
161: PortletType pType) {
162: if (portletShortTitle != null
163: && !"".equals(portletShortTitle.trim())) {
164: ShortTitleType shortTitleType = new ShortTitleTypeImpl();
165: shortTitleType.setValue(portletShortTitle);
166: pType.getPortletInfo().setShortTitle(shortTitleType);
167: }
168: }
169:
170: /**
171: * Sets resource bundle
172: * @param resourceBundleName
173: * @param pType
174: */
175: public static void setResourceBundle(String resourceBundleName,
176: PortletType pType) {
177: if (resourceBundleName != null
178: || !"".equals(resourceBundleName.trim())) {
179: ResourceBundleType resourceBundleType = new ResourceBundleTypeImpl();
180: resourceBundleType.setValue(resourceBundleName.trim());
181: pType.setResourceBundle(resourceBundleType);
182: }
183: }
184:
185: /**
186: * Sets supported mime types and portlet modes.
187: * Possible supported portlet modes are "edit" and "help".
188: * The mime types can be any mime-types supported by http's accept header,
189: * e.g. text/html, application/vnd.wap.xhtml+xml<br>
190: * Note that this method will set all mime types have the same modes. Use
191: * {@link #setSupports(String[], String, PortletType) setSupports(String[], String, PortletType)}
192: * method instead.
193: *
194: * @param modes Portlet modes. Possible values: "edit" and "help".
195: * @param mimeTypes Mime types supported by HTTP accept header.
196: * @param pType PortletType
197: */
198: public static void setSupports(String[] modes, String[] mimeTypes,
199: PortletType pType) {
200: if (mimeTypes != null) {
201: for (int j = 0; j < mimeTypes.length; j++) {
202: if (mimeTypes[j] == null
203: || "".equals(mimeTypes[j].trim()))
204: continue;
205: setSupports(modes, mimeTypes[j], pType);
206:
207: }
208: }
209: }
210:
211: /**
212: * Sets a supported mime type and portlet modes.
213: * Possible supported portlet modes are "edit" and "help".
214: * The mime type can be any mime-types supported by http's accept header,
215: * e.g. text/html, application/vnd.wap.xhtml+xml<br>
216: * Note that this method will set a mime-type with its supported portlet modes at a time.
217: * @param modes Portlet modes. Possible values: "edit" and "help".
218: * @param mimeType Mime type supported by HTTP accept header.
219: * @param pType PortletType
220: */
221: @SuppressWarnings("unchecked")
222: public static void setSupports(String[] modes, String mimeType,
223: PortletType pType) {
224: if (mimeType != null) {
225: SupportsType supportsType = new SupportsTypeImpl();
226:
227: MimeTypeType mimeTypeType = new MimeTypeTypeImpl();
228: mimeTypeType.setValue(mimeType);
229: supportsType.setMimeType(mimeTypeType);
230:
231: if (modes != null) {
232: for (int i = 0; i < modes.length; i++) {
233: if (modes[i] == null || "".equals(modes[i].trim()))
234: continue;
235: PortletModeType portletModeType = new PortletModeTypeImpl();
236: portletModeType.setValue(modes[i]);
237: supportsType.getPortletMode().add(portletModeType);
238: }
239: }
240:
241: pType.getSupports().add(supportsType);
242: }
243:
244: }
245:
246: /**
247: * Sets init parameters.
248: * @param initParamsMap Init param name/value pair map.
249: * @param pType
250: */
251: @SuppressWarnings("unchecked")
252: public static void setInitParams(Hashtable initParamsMap,
253: PortletType pType) {
254: if (initParamsMap != null) {
255: Enumeration paramNames = initParamsMap.keys();
256: while (paramNames.hasMoreElements()) {
257: String name = (String) paramNames.nextElement();
258: if (name == null || "".equals(name))
259: continue;
260: String value = (String) initParamsMap.get(name);
261: if (value == null || "".equals(value))
262: continue;
263: InitParamType initParamType = new InitParamTypeImpl();
264:
265: NameType nameType = new NameTypeImpl();
266: nameType.setValue(name.trim());
267: ValueType valueType = new ValueTypeImpl();
268: valueType.setValue(value);
269:
270: initParamType.setName(nameType);
271: initParamType.setValue(valueType);
272: pType.getInitParam().add(initParamType);
273:
274: }
275: }
276: }
277:
278: /**
279: * Sets the portlet preferences. A preference can have a list of values (more than one values).
280: * Note read only field is ignored (false by default) using this method.
281: * @param prefMap A table that map a preference name with a list (java.util.List) of preference value.
282: * @param pType
283: */
284: @SuppressWarnings("unchecked")
285: public static void setPreferences(Hashtable prefMap,
286: PortletType pType) {
287: if (prefMap != null) {
288: PortletPreferencesType pPrefType = new PortletPreferencesTypeImpl();
289: Enumeration prefNames = prefMap.keys();
290: while (prefNames.hasMoreElements()) {
291: String name = (String) prefNames.nextElement();
292: if (name == null || "".equals(name))
293: continue;
294: PreferenceType prefType = new PreferenceTypeImpl();
295: NameType nameType = new NameTypeImpl();
296: nameType.setValue(name);
297: prefType.setName(nameType);
298:
299: List valueList = (List) prefMap.get(name);
300: Iterator iter = valueList.iterator();
301: boolean emptyValue = true;
302: while (iter.hasNext()) {
303: String valueStr = (String) iter.next();
304: if (valueStr == null || "".equals(valueStr.trim()))
305: continue;
306: ValueType valueType = new ValueTypeImpl();
307: valueType.setValue(valueStr);
308: prefType.getValue().add(valueType);
309: emptyValue = false;
310: }
311: if (!emptyValue)
312: pPrefType.getPreference().add(prefType);
313: }
314: pType.setPortletPreferences(pPrefType);
315: }
316: }
317:
318: /**
319: * Sets the preferences. Note that the size of the names, values and readonly list
320: * must be the same.
321: * @param prefNames List of Preference names.
322: * @param prefValues List of Preference value vectors.
323: * @param readOnly List of Preference read only flags (String).
324: * @param validatorName The preference validator class name. Optional.
325: * @param pType
326: */
327: @SuppressWarnings("unchecked")
328: public static void setPreferences(List prefNames, List prefValues,
329: List readOnly, String validatorName, PortletType pType) {
330: if (prefNames != null && prefValues != null && readOnly != null) {
331: int listSize = prefNames.size();
332: if (prefValues.size() == listSize
333: && readOnly.size() == listSize) {
334: PortletPreferencesType pPrefType = new PortletPreferencesTypeImpl();
335: boolean isEmpty = true;
336: for (int i = 0; i < listSize; i++) {
337: String prefName = (String) prefNames.get(i);
338: if (prefName == null || "".equals(prefName))
339: continue;
340: isEmpty = false;
341: PreferenceType prefType = new PreferenceTypeImpl();
342: NameType nameType = new NameTypeImpl();
343: nameType.setValue(prefName.trim());
344: prefType.setName(nameType);
345:
346: Vector prefValueVec = (Vector) prefValues.get(i);
347: for (int j = 0; j < prefValueVec.size(); j++) {
348: String prefValue = (String) prefValueVec.get(j);
349: if (prefValue != null && !"".equals(prefValue)) {
350: prefValueVec.add(prefValue);
351: ValueType valueType = new ValueTypeImpl();
352: valueType.setValue(prefValue);
353: prefType.getValue().add(valueType);
354: }
355: }
356:
357: String isReadOnly = (String) readOnly.get(i);
358: prefType.setReadOnly(isReadOnly);
359:
360: pPrefType.getPreference().add(prefType);
361: }
362:
363: if (validatorName != null
364: && !"".equals(validatorName.trim()))
365: pPrefType.setPreferencesValidator(validatorName);
366:
367: if (!isEmpty)
368: pType.setPortletPreferences(pPrefType);
369: }
370: }
371: }
372:
373: /**
374: * Sets Security Role Reference
375: * @param securityRoles A map of Role name (String) and Role Link (String)
376: * @param pType
377: */
378: @SuppressWarnings("unchecked")
379: public static void setSecurityRoleReference(HashMap securityRoles,
380: PortletType pType) {
381: if (securityRoles != null && securityRoles.size() > 0) {
382: Set roleNames = securityRoles.keySet();
383: for (Iterator iter = roleNames.iterator(); iter.hasNext();) {
384: SecurityRoleRefType secRoleRefType = new SecurityRoleRefTypeImpl();
385: String roleName = (String) iter.next();
386: if (roleName == null || "".equals(roleName))
387: continue;
388: RoleLinkType roleLinkType = new RoleLinkTypeImpl();
389: roleLinkType.setValue((String) securityRoles
390: .get(roleName));
391: secRoleRefType.setRoleName(roleName.trim());
392: secRoleRefType.setRoleLink(roleLinkType);
393:
394: pType.getSecurityRoleRef().add(secRoleRefType);
395: }
396: }
397: }
398:
399: /**
400: * Sets descriptions of default language (empty language code).
401: * @param descr The descriptions.
402: * @param pType
403: */
404: @SuppressWarnings("unchecked")
405: public static void setDescription(String descr, PortletType pType) {
406: if (descr != null) {
407: DescriptionType descrType = new DescriptionTypeImpl();
408: descrType.setValue(descr);
409: descrType.setLang("");
410: pType.getDescription().add(descrType);
411: }
412: }
413:
414: /**
415: * Sets descriptions of multiple languages.
416: * @param descrs A map of locale language code (String) and
417: * description (String).
418: * @param pType
419: */
420: @SuppressWarnings("unchecked")
421: public static void setDescription(HashMap descrs, PortletType pType) {
422: if (descrs != null) {
423: Set langs = descrs.keySet();
424: for (Iterator iter = langs.iterator(); iter.hasNext();) {
425: DescriptionType descrType = new DescriptionTypeImpl();
426: String langCode = (String) iter.next();
427: String value = (String) descrs.get(langCode);
428: if (value == null || "".equals(value))
429: continue;
430: descrType.setValue(value);
431: if (langCode != null && !"".equals(langCode.trim()))
432: descrType.setLang(langCode);
433:
434: pType.getDescription().add(descrType);
435: }
436: }
437: }
438:
439: /**
440: * Sets display name of default language (empty language code)
441: * @param displayName
442: * @param pType
443: */
444: @SuppressWarnings("unchecked")
445: public static void setDisplayName(String displayName,
446: PortletType pType) {
447: if (displayName != null) {
448: DisplayNameType displayType = new DisplayNameTypeImpl();
449: displayType.setValue(displayName);
450: displayType.setLang("");
451: pType.getDisplayName().add(displayType);
452: }
453: }
454:
455: /**
456: * Sets display names of multiple languages.
457: * @param displayNameMap A map of locale language codes (String) and
458: * display names (String).
459: * @param pType
460: */
461: @SuppressWarnings("unchecked")
462: public static void setDisplayName(HashMap displayNameMap,
463: PortletType pType) {
464: if (displayNameMap != null) {
465: Set langs = displayNameMap.keySet();
466: for (Iterator iter = langs.iterator(); iter.hasNext();) {
467: DisplayNameType displayNameType = new DisplayNameTypeImpl();
468: String langCode = (String) iter.next();
469: String value = (String) displayNameMap.get(langCode);
470: if (value == null || "".equals(value))
471: continue;
472: displayNameType.setValue(value.trim());
473: if (langCode != null && !"".equals(langCode.trim()))
474: displayNameType.setLang(langCode);
475:
476: pType.getDisplayName().add(displayNameType);
477: }
478: }
479: }
480:
481: /**
482: * Sets the expiration cache.
483: * @param exp
484: * @param pType
485: */
486: public static void setExpirationCache(int exp, PortletType pType) {
487: ExpirationCacheType expCacheType = new ExpirationCacheTypeImpl();
488: expCacheType.setValue(exp);
489: pType.setExpirationCache(expCacheType);
490: }
491:
492: /**
493: * Sets supported locales.
494: * @param locales
495: * @param pType
496: */
497: @SuppressWarnings("unchecked")
498: public static void setSupportedLocales(String[] locales,
499: PortletType pType) {
500: if (locales != null) {
501: for (int j = 0; j < locales.length; j++) {
502: SupportedLocaleType slType = new SupportedLocaleTypeImpl();
503: slType.setValue(locales[j]);
504: pType.getSupportedLocale().add(slType);
505: }
506:
507: }
508: }
509:
510: /**
511: * Returns an array of values of the specified preference name.
512: * @param pType
513: * @param prefName The preference of interest.
514: * @return
515: */
516: public static String[] getPreferenceValues(PortletType pType,
517: String prefName) {
518: String[] values = null;
519: List prefList = pType.getPortletPreferences().getPreference();
520: for (int i = 0; i < prefList.size(); i++) {
521: if (prefName.equals(((PreferenceType) prefList.get(i))
522: .getName().getValue())) {
523: List prefValueList = ((PreferenceType) prefList.get(i))
524: .getValue();
525: values = new String[prefValueList.size()];
526: for (int j = 0; j < prefValueList.size(); j++)
527: values[j] = ((ValueType) prefValueList.get(j))
528: .getValue();
529:
530: }
531: }
532: return values;
533: }
534:
535: /**
536: * Checks if the specified preference is read only.
537: * @param pType
538: * @param prefName
539: * @return true if the preference is read only (not modifiable).
540: */
541: public static boolean isPreferenceReadOnly(PortletType pType,
542: String prefName) {
543: List prefList = pType.getPortletPreferences().getPreference();
544: String readOnly = "false";
545: for (int i = 0; i < prefList.size(); i++) {
546: if (prefName.equals(((PreferenceType) prefList.get(i))
547: .getName().getValue())) {
548: readOnly = ((PreferenceType) prefList.get(i))
549: .getReadOnly();
550: }
551: }
552: return Boolean.getBoolean(readOnly);
553: }
554:
555: /**
556: * Returns a Hashtable that maps preference's name and a list (java.util.List)
557: * of preference values.
558: * @param portletApp
559: * @param portletNames
560: * @param transportGuarantee
561: * @param displayName
562: */
563: @SuppressWarnings("unchecked")
564: public static Hashtable getPreferenceMap(PortletType pType) {
565: Hashtable prefMap = new Hashtable();
566: List prefList = pType.getPortletPreferences().getPreference();
567: for (int i = 0; i < prefList.size(); i++) {
568: prefMap.put(((PreferenceType) prefList.get(i)).getName()
569: .getValue(), ((PreferenceType) prefList.get(i))
570: .getValue());
571: }
572:
573: return prefMap;
574: }
575:
576: /**
577: * Return a map of locale language code and description value.
578: * Default language has empty language code.
579: * @param descrs A map of locale language code (String) and
580: * description (String).
581: * @param pType
582: */
583: @SuppressWarnings("unchecked")
584: public static HashMap getDescriptionMap(PortletType pType) {
585: List descrList = pType.getDescription();
586: HashMap descrMap = new HashMap();
587: for (int i = 0; i < descrList.size(); i++) {
588: DescriptionType descrType = (DescriptionType) descrList
589: .get(i);
590: String lang = descrType.getLang();
591: if (lang == null)
592: lang = "";
593: String descr = descrType.getValue();
594: descrMap.put(lang, descr);
595: }
596: return descrMap;
597: }
598:
599: /**
600: * Returns a map of locale language code(s) and display name(s).
601: * Default language has empty language code.
602: * @param pType
603: * @return
604: */
605: @SuppressWarnings("unchecked")
606: public static HashMap getDisplayNameMap(PortletType pType) {
607: List displayList = pType.getDisplayName();
608: HashMap displayMap = new HashMap();
609: for (int i = 0; i < displayList.size(); i++) {
610: DisplayNameType displayType = (DisplayNameType) displayList
611: .get(i);
612: String lang = displayType.getLang();
613: if (lang == null)
614: lang = "";
615: String descr = displayType.getValue();
616: displayMap.put(lang, descr);
617: }
618: return displayMap;
619: }
620:
621: /*
622: * Returns the first localized display name in the list, if any. Otherwise,
623: * returns the default display name or empty string if none is available.
624: * @param pcInfo
625: * @return
626: */
627: /*
628: public static String getDisplayName (PortletType pType) {
629: String displayName = "";
630: List displayList = pType.getDisplayName();
631: if (displayList.size() > 0) {
632: for (int i=0; i < displayList.size(); i++) {
633: DisplayNameType displayNameType = ((DisplayNameType) displayList.get(i));
634: String lang = displayNameType.getLang();
635: if (!"".equals(lang) || i == displayList.size() -1) {
636: displayName = displayNameType.getValue();
637: break;
638: }
639: }
640: }
641:
642: return displayName;
643: }
644: */
645:
646: /**
647: * Returns a default display name from PortletConfigInfo, if provided. Otherwise,
648: * returns to get a localized display name. If none of them defined, empty
649: * string is returned. The order of localized display name might not be preserved,
650: * if more than one are defined.
651: * @param pcInfo
652: * @return
653: */
654: public static String getDisplayName(PortletConfigInfo pcInfo) {
655: String displayName = pcInfo.displayName;
656: if ((displayName == null || "".equals(displayName.trim()))
657: && pcInfo.localizedDisplayNames.size() > 0) {
658: Set localizedDisplayNames = pcInfo.localizedDisplayNames
659: .keySet();
660: Object localLang = localizedDisplayNames.iterator().next();
661: displayName = (String) pcInfo.localizedDisplayNames
662: .get(localLang);
663: }
664:
665: return displayName == null ? "" : displayName;
666: }
667:
668: /*
669: * Returns the first localized description in the list, if any. Otherwise,
670: * returns the default description or empty string if none is available.
671: * @param pcInfo
672: * @return
673: */
674: /*
675: public static String getDescription (PortletType pType) {
676: String descr = "";
677: List descList = pType.getDescription();
678: if (descList.size() > 0) {
679: for (int i=0; i < descList.size(); i++) {
680: DescriptionType descrType = ((DescriptionType) descList.get(i));
681: String lang = descrType.getLang();
682: if ((lang != null && !"".equals(lang)) || i == descList.size() -1) {
683: descr = descrType.getValue();
684: break;
685: }
686: }
687: }
688:
689: return descr;
690: }
691: */
692: /**
693: * Returns a default description from PortletConfigInfo, if provided.
694: * Otherwise, try to get a localized description.
695: * If none of them is defined, empty
696: * string is returned. The order of localized description might not be preserved,
697: * if more than one are defined.
698: * @param pcInfo
699: * @return
700: */
701: public static String getDescription(PortletConfigInfo pcInfo) {
702: String description = pcInfo.description;
703: if ((description == null || "".equals(description.trim()))
704: && pcInfo.localizedDescriptions.size() > 0) {
705: Set locDescr = pcInfo.localizedDescriptions.keySet();
706: Object localLang = locDescr.iterator().next();
707: description = (String) pcInfo.localizedDescriptions
708: .get(localLang);
709: }
710:
711: return description == null ? "" : description;
712: }
713:
714: /**
715: *
716: * @param portletApp Portlet configuration.
717: * @param portletNames Array of portlet names. Set to <code>null</code> will remove the security constraint.
718: * @param transportGuarantee Transport layer describes the user data constraint for the portlets collection.
719: * Possible values: <code>CONFIDENTIAL</code>, <code>INTEGRAL</code>. Default is <code>CONFIDENTIAL</code>.
720: * @param displayname Name of the security constraint. Default name: Secure Portlets.
721: */
722: @SuppressWarnings("unchecked")
723: public static void setSecurityConstraint(PortletApp portletApp,
724: Set portletNames, String transportGuarantee,
725: String displayName) {
726: final String DEFAULT_DISPLAY_NAME = "Secure Portlets";
727: final String DEFAULT_TRANSPORT_GUARANTEE = "CONFIDENTIAL";
728: transportGuarantee = (transportGuarantee == null ? DEFAULT_TRANSPORT_GUARANTEE
729: : transportGuarantee);
730: boolean isExisted = false;
731:
732: List secList = portletApp.getSecurityConstraint();
733: SecurityConstraintType constraint = null;
734: int i = 0;
735: while (i < secList.size()) {
736: constraint = (SecurityConstraintType) secList.get(i);
737: if (constraint.getUserDataConstraint()
738: .getTransportGuarantee().equals(
739: transportGuarantee.toUpperCase())) {
740: isExisted = true;
741: //remove from the security list
742: secList.remove(i);
743: constraint.getDisplayName().clear();
744: break;
745: }
746: i++;
747: }
748:
749: if (portletNames == null || portletNames.size() == 0) {
750: return;
751: }
752:
753: if (!isExisted) {
754: constraint = new SecurityConstraintTypeImpl();
755: UserDataConstraintType userDataConstraint = new UserDataConstraintTypeImpl();
756: userDataConstraint.setTransportGuarantee(transportGuarantee
757: .toUpperCase());
758: constraint.setUserDataConstraint(userDataConstraint);
759: DisplayNameType displayNameType = new DisplayNameTypeImpl();
760: displayNameType
761: .setValue((displayName == null ? DEFAULT_DISPLAY_NAME
762: : displayName));
763: constraint.getDisplayName().add(displayNameType);
764:
765: PortletCollectionType pColl = new PortletCollectionTypeImpl();
766: Iterator iter = portletNames.iterator();
767: while (iter.hasNext()) {
768: PortletNameType pNameType = new PortletNameTypeImpl();
769: pNameType.setValue((String) iter.next());
770: pColl.getPortletName().add(pNameType);
771: }
772:
773: constraint.setPortletCollection(pColl);
774: secList.add(i, constraint);
775: }
776: }
777:
778: /**
779: * Returns all supported mime types.
780: * @param pType
781: * @return
782: */
783: public static String[] getSupportedMimeTypes(PortletType pType) {
784: List supportList = pType.getSupports();
785: String[] mimeTypes = new String[supportList.size()];
786: for (int i = 0; i < supportList.size(); i++) {
787: mimeTypes[i] = ((SupportsType) supportList.get(i))
788: .getMimeType().getValue().toLowerCase();
789: }
790: return mimeTypes;
791: }
792:
793: /**
794: * Returns a Hashtable that maps a mime-type to an array (of String) of
795: * supported modes.
796: * @param pType
797: * @return
798: */
799: @SuppressWarnings("unchecked")
800: public static Hashtable getSupportsMap(PortletType pType) {
801: Hashtable supportsMap = new Hashtable();
802: List supportList = pType.getSupports();
803:
804: for (int i = 0; i < supportList.size(); i++) {
805: SupportsType sType = (SupportsType) supportList.get(i);
806: String mimeType = sType.getMimeType().getValue()
807: .toLowerCase();
808: List modeList = sType.getPortletMode();
809: String[] modes = new String[modeList.size()];
810: for (int j = 0; j < modeList.size(); j++) {
811: modes[j] = ((PortletModeType) modeList.get(j))
812: .getValue().toLowerCase();
813: }
814: supportsMap.put(mimeType, modes);
815: }
816:
817: return supportsMap;
818: }
819:
820: /**
821: * Sets init parameters.
822: * @param initParamsMap Init param name/value pair map.
823: * @param pType
824: */
825: @SuppressWarnings("unchecked")
826: public static Hashtable getInitParams(PortletType pType) {
827: List initParamList = pType.getInitParam();
828: Hashtable initParams = new Hashtable();
829: for (int i = 0; i < initParamList.size(); i++) {
830: InitParamType initParamType = (InitParamType) initParamList
831: .get(i);
832: String name = initParamType.getName().getValue();
833: String value = initParamType.getValue().getValue();
834: //String descr = ((DescriptionType) initParamType.getDescription().get(0)).getValue();
835: initParams.put(name, value);
836: }
837: return initParams;
838: }
839:
840: @SuppressWarnings("unchecked")
841: public static Hashtable getInitParams(PortletConfigInfo pcInfo) {
842: Enumeration initParamEnum = pcInfo.config
843: .getInitParameterNames();
844: Hashtable initParams = new Hashtable();
845: while (initParamEnum.hasMoreElements()) {
846: String name = (String) initParamEnum.nextElement();
847: initParams.put(name, pcInfo.config.getInitParameter(name));
848: }
849: return initParams;
850: }
851:
852: public static String getErrorStackTrace(Throwable ex) {
853: StackTraceElement[] trace = ex.getStackTrace();
854: StringWriter writer = new StringWriter();
855: for (int i = 0; i < trace.length; i++) {
856: writer.write(trace[i].toString());
857: writer.write("<br/>");
858: }
859: Throwable rootCause = ex.getCause();
860: if (rootCause != null) {
861: writer.write("<br/><b>Root Cause:</b><br/>");
862: writer.write(rootCause.toString());
863: trace = rootCause.getStackTrace();
864: for (int i = 0; i < trace.length; i++) {
865: writer.write(" ");
866: writer.write(trace[i].toString());
867: writer.write("<br/>");
868: }
869: }
870: return writer.toString();
871: }
872:
873: }
|