/projects/exoportal-v1.0.2/services/wsrp/impl/src/java/org/exoplatform/services/wsrp/producer/impl/JSR168ContainerProxyImpl.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 362 lines · 310 code · 34 blank · 18 comment · 36 complexity · 512fb86da76d25fb56417bfaf39b27d5 MD5 · raw file

  1. /*
  2. * Copyright 2001-2003 The eXo platform SARL All rights reserved.
  3. * Please look at license.txt in info directory for more license detail.
  4. *
  5. * Created on 16 janv. 2004
  6. */
  7. package org.exoplatform.services.wsrp.producer.impl;
  8. import java.util.Collection;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Locale;
  13. import java.util.Map;
  14. import java.util.MissingResourceException;
  15. import java.util.Set;
  16. import javax.portlet.WindowState;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.apache.commons.logging.Log;
  19. import org.exoplatform.Constants;
  20. import org.exoplatform.services.log.LogService;
  21. import org.exoplatform.services.portletcontainer.*;
  22. import org.exoplatform.services.portletcontainer.pci.*;
  23. import org.exoplatform.services.portletcontainer.pci.model.*;
  24. import org.exoplatform.services.wsrp.WSRPConstants;
  25. import org.exoplatform.services.wsrp.exceptions.Faults;
  26. import org.exoplatform.services.wsrp.exceptions.WSRPException;
  27. import org.exoplatform.services.wsrp.producer.PortletContainerProxy;
  28. import org.exoplatform.services.wsrp.producer.impl.helpers.WSRPHttpServletRequest;
  29. import org.exoplatform.services.wsrp.producer.impl.helpers.WSRPHttpServletResponse;
  30. import org.exoplatform.services.wsrp.type.LocalizedString;
  31. import org.exoplatform.services.wsrp.type.MarkupType;
  32. import org.exoplatform.services.wsrp.type.PortletDescription;
  33. import org.exoplatform.services.wsrp.type.Property;
  34. import org.exoplatform.services.wsrp.type.PropertyList;
  35. import org.exoplatform.services.wsrp.type.ResourceList;
  36. import org.exoplatform.services.wsrp.utils.Utils;
  37. /**
  38. * @author Mestrallet Benjamin
  39. * benjmestrallet@users.sourceforge.net
  40. */
  41. public class JSR168ContainerProxyImpl implements PortletContainerProxy {
  42. private Log log;
  43. private PortletContainerService service_;
  44. private WSRPConfiguration conf;
  45. private WSRPPortletPreferencesPersister persister;
  46. public JSR168ContainerProxyImpl(PortletContainerService service,
  47. WSRPConfiguration conf,
  48. LogService logService) {
  49. service_ = service;
  50. this.log = logService.getLog("org.exoplatform.services.wsrp");
  51. this.conf = conf;
  52. this.persister = WSRPPortletPreferencesPersister.getInstance();
  53. }
  54. public boolean isPortletOffered(String portletHandle) {
  55. String[] key = StringUtils.split(portletHandle, Constants.PORTLET_HANDLE_ENCODER);
  56. if (service_.getAllPortletMetaData().get(key[0] + Constants.PORTLET_HANDLE_ENCODER + key[1]) != null) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. public ResourceList getResourceList(String[] desiredLocales) {
  62. //TODO discover what a resource is
  63. return new ResourceList();
  64. }
  65. public PortletDescription getPortletDesciption(String portletHandle,
  66. String[] desiredLocales) {
  67. String[] k = StringUtils.split(portletHandle, Constants.PORTLET_META_DATA_ENCODER);
  68. String portletApplicationName = k[0];
  69. String portletName = k[1];
  70. log.debug("get description of portlet in application : " + portletApplicationName);
  71. log.debug("get description of portlet : " + portletName);
  72. Map portletMetaDatas = service_.getAllPortletMetaData();
  73. PortletData portlet = (PortletData) portletMetaDatas.get(k[0] + Constants.PORTLET_META_DATA_ENCODER + k[1]);
  74. PortletDescription pD = new PortletDescription();
  75. //delegation to JSR 168 specs
  76. pD.setPortletHandle(portletHandle);
  77. pD.setOnlySecure(new Boolean(portlet.isSecure()));
  78. pD.setDefaultMarkupSecure(new Boolean(portlet.isSecure()));
  79. List portletDescriptions = portlet.getDescription();
  80. if (!(portletDescriptions == null || portletDescriptions.size() == 0)) {
  81. pD.setDescription(getDescription(portletDescriptions, desiredLocales));
  82. }
  83. List portletDisplayNames = portlet.getDisplayName();
  84. if (!(portletDisplayNames == null || portletDisplayNames.size() == 0)) {
  85. pD.setDisplayName(getDisplayName(portletDisplayNames, desiredLocales));
  86. }
  87. pD.setGroupID(portletApplicationName);
  88. pD.setKeywords(getKeyWords(portletApplicationName, portletName, desiredLocales));
  89. pD.setMarkupTypes(setMarkupTypes(portlet.getSupports(),
  90. service_.getWindowStates(portletApplicationName),
  91. desiredLocales));
  92. pD.setShortTitle(getShortTitle(portletApplicationName, portletName, desiredLocales));
  93. pD.setTitle(getTitle(portletApplicationName, portletName, desiredLocales));
  94. pD.setUserProfileItems(getUserProfileItems(portlet.getUserAttributes()));
  95. //WSRP specific issues
  96. pD.setHasUserSpecificState(new Boolean(conf.isHasUserSpecificState()));
  97. pD.setDoesUrlTemplateProcessing(new Boolean(conf.isDoesUrlTemplateProcessing()));
  98. pD.setTemplatesStoredInSession(new Boolean(conf.isTemplatesStoredInSession()));
  99. pD.setUserContextStoredInSession(new Boolean(conf.isUserContextStoredInSession()));
  100. pD.setUsesMethodGet(new Boolean(conf.isUsesMethodGet()));
  101. //pD.setUserCategories(null);
  102. return pD;
  103. }
  104. public void setPortletProperties(String portletHandle,
  105. String owner,
  106. PropertyList propertyList)
  107. throws WSRPException {
  108. // key[0] = application name , key[1] portlet name
  109. log.debug("portlet handle to split in setPortletProperties : " + portletHandle);
  110. String[] key = StringUtils.split(portletHandle, Constants.PORTLET_META_DATA_ENCODER);
  111. //mapping WSRP / JSR 168 : a property is a preference type
  112. Property[] properties = propertyList.getProperties();
  113. Map propertiesMap = new HashMap();
  114. for (int i = 0; i < properties.length; i++) {
  115. Property property = properties[i];
  116. //Locale locale = new Locale(property.getLang());//No mapping available in JSR 168
  117. String preferenceName = property.getName();
  118. String preferenceValue = property.getStringValue();
  119. propertiesMap.put(preferenceName, preferenceValue);
  120. }
  121. Input input = new Input();
  122. ExoWindowID windowID = new ExoWindowID();
  123. windowID.setOwner(owner);
  124. windowID.setPortletApplicationName(key[0]);
  125. windowID.setPortletName(key[1]);
  126. windowID.setUniqueID(key[2]);
  127. input.setWindowID(windowID);
  128. input.setPortletPreferencesPersister(persister);
  129. try {
  130. service_.setPortletPreference(input, propertiesMap);
  131. } catch (Exception e) {
  132. log.error("error while storing preferences", e);
  133. throw new WSRPException(Faults.OPERATION_FAILED_FAULT);
  134. }
  135. }
  136. public Map getPortletProperties(String portletHandle, String owner)
  137. throws WSRPException {
  138. // key[0] = application name , key[1] portlet name
  139. String[] key = StringUtils.split(portletHandle, Constants.PORTLET_META_DATA_ENCODER);
  140. try {
  141. Input input = new Input();
  142. ExoWindowID windowID = new ExoWindowID();
  143. windowID.setOwner(owner);
  144. windowID.setPortletApplicationName(key[0]);
  145. windowID.setPortletName(key[1]);
  146. windowID.setUniqueID(key[2]);
  147. input.setWindowID(windowID);
  148. input.setPortletPreferencesPersister(persister);
  149. return service_.getPortletPreference(input);
  150. } catch (Exception e) {
  151. throw new WSRPException(Faults.OPERATION_FAILED_FAULT);
  152. }
  153. }
  154. public Map getAllPortletMetaData() {
  155. return service_.getAllPortletMetaData();
  156. }
  157. public Collection getWindowStates(String s) {
  158. return service_.getWindowStates(s);
  159. }
  160. public Collection getSupportedWindowStates() {
  161. return service_.getSupportedWindowStates();
  162. }
  163. public RenderOutput render(WSRPHttpServletRequest request, WSRPHttpServletResponse response, RenderInput input)
  164. throws WSRPException {
  165. try {
  166. return service_.render(request, response, input);
  167. } catch (PortletContainerException e) {
  168. throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
  169. }
  170. }
  171. public ActionOutput processAction(WSRPHttpServletRequest request, WSRPHttpServletResponse response, ActionInput input)
  172. throws WSRPException {
  173. try {
  174. ActionOutput out = service_.processAction(request, response, input);
  175. Map propertiesMap = out.getProperties();
  176. Set set = propertiesMap.keySet();
  177. for (Iterator iterator = set.iterator(); iterator.hasNext();) {
  178. String key = (String) iterator.next();
  179. if (key.startsWith(PortletContainerConstants.EXCEPTION)) {
  180. log.error("Error body : " + propertiesMap.get(key));
  181. throw new WSRPException(Faults.PORTLET_STATE_CHANGE_REQUIRED_FAULT);
  182. }
  183. }
  184. return out;
  185. } catch (PortletContainerException e) {
  186. throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
  187. }
  188. }
  189. public Collection getSupportedPortletModesWithDescriptions() {
  190. return service_.getSupportedPortletModesWithDescriptions();
  191. }
  192. public Collection getSupportedWindowStatesWithDescriptions() {
  193. return service_.getSupportedWindowStatesWithDescriptions();
  194. }
  195. private LocalizedString getDescription(List list, String[] desiredLocales) {
  196. for (int i = 0; i < desiredLocales.length; i++) {
  197. String desiredLocale = desiredLocales[i];
  198. for (Iterator iter = list.iterator(); iter.hasNext();) {
  199. Description desc = (Description) iter.next();
  200. if (desc.getLang().equalsIgnoreCase(desiredLocale)) {
  201. return Utils.getLocalizedString(desc.getDescription(), desiredLocale);
  202. }
  203. }
  204. }
  205. return null;
  206. }
  207. private LocalizedString getDisplayName(List list, String[] desiredLocales) {
  208. for (int i = 0; i < desiredLocales.length; i++) {
  209. String desiredLocale = desiredLocales[i];
  210. for (Iterator iter = list.iterator(); iter.hasNext();) {
  211. DisplayName displayName = (DisplayName) iter.next();
  212. if (displayName.getLang().equalsIgnoreCase(desiredLocale)) {
  213. return Utils.getLocalizedString(displayName.getDisplayName(), desiredLocale);
  214. }
  215. }
  216. }
  217. return null;
  218. }
  219. private LocalizedString[] getKeyWords(String portletAppName,
  220. String portletName,
  221. String[] desiredLocales) {
  222. for (int i = 0; i < desiredLocales.length; i++) {
  223. String desiredLocale = desiredLocales[i];
  224. java.util.ResourceBundle resourceBundle = getBundle(portletAppName, portletName,
  225. new Locale(desiredLocale));
  226. if (resourceBundle.getLocale().getLanguage().equalsIgnoreCase(desiredLocale)
  227. || i == desiredLocales.length - 1) {
  228. try {
  229. String keyWords = resourceBundle.getString(PortletData.KEYWORDS);
  230. String[] a = StringUtils.split(keyWords, ",");
  231. LocalizedString[] b = new LocalizedString[a.length];
  232. for (int j = 0; j < a.length; j++) {
  233. b[j] = Utils.getLocalizedString(a[j], desiredLocale);
  234. }
  235. return b;
  236. } catch (MissingResourceException ex){
  237. log.debug("No keyword defined for the portlet " + portletAppName + "/" + portletName);
  238. return null;
  239. }
  240. }
  241. }
  242. return null;
  243. }
  244. private MarkupType[] setMarkupTypes(List list, Collection windowStates, String[] locales) {
  245. MarkupType[] array = new MarkupType[list.size()];
  246. int i = 0;
  247. MarkupType mT = null;
  248. for (Iterator iter = list.iterator(); iter.hasNext(); i++) {
  249. Supports element = (Supports) iter.next();
  250. mT = new MarkupType();
  251. mT.setMimeType(element.getMimeType());
  252. List modes = element.getPortletMode();
  253. String[] modesInArray = new String[modes.size()];
  254. int j = 0;
  255. for (Iterator iterator = modes.iterator(); iterator.hasNext(); j++) {
  256. String pM = (String) iterator.next();
  257. modesInArray[j] = WSRPConstants.WSRP_PREFIX + pM.toString();
  258. }
  259. mT.setModes(modesInArray);
  260. j = 0;
  261. String[] windowStatesInArray = new String[windowStates.size()];
  262. for (Iterator iterator = windowStates.iterator(); iterator.hasNext(); j++) {
  263. WindowState wS = (WindowState) iterator.next();
  264. windowStatesInArray[j] = WSRPConstants.WSRP_PREFIX + wS.toString();
  265. }
  266. mT.setWindowStates(windowStatesInArray);
  267. mT.setLocales(locales);
  268. array[i] = mT;
  269. }
  270. return array;
  271. }
  272. private LocalizedString getTitle(String portletAppName,
  273. String portletName,
  274. String[] desiredLocales) {
  275. for (int i = 0; i < desiredLocales.length; i++) {
  276. String desiredLocale = desiredLocales[i];
  277. java.util.ResourceBundle resourceBundle = getBundle(portletAppName, portletName,
  278. new Locale(desiredLocale));
  279. if (resourceBundle.getLocale().getLanguage().equalsIgnoreCase(desiredLocale)
  280. || i == desiredLocales.length - 1) {
  281. return Utils.getLocalizedString(resourceBundle.getString(PortletData.PORTLET_TITLE),
  282. desiredLocale);
  283. }
  284. }
  285. return null;
  286. }
  287. private LocalizedString getShortTitle(String portletAppName,
  288. String portletName,
  289. String[] desiredLocales) {
  290. for (int i = 0; i < desiredLocales.length; i++) {
  291. String desiredLocale = desiredLocales[i];
  292. java.util.ResourceBundle resourceBundle = getBundle(portletAppName, portletName,
  293. new Locale(desiredLocale));
  294. if (resourceBundle.getLocale().getLanguage().equalsIgnoreCase(desiredLocale)
  295. || i == desiredLocales.length - 1) {
  296. try{
  297. return Utils.getLocalizedString(resourceBundle.getString(PortletData.PORTLET_SHORT_TITLE),
  298. desiredLocale);
  299. } catch (MissingResourceException ex){
  300. log.debug("No short title defined for the portlet " + portletAppName + "/" + portletName);
  301. return null;
  302. }
  303. }
  304. }
  305. return null;
  306. }
  307. private String[] getUserProfileItems(List userAttributes) {
  308. String[] toReturnArray = new String[userAttributes.size()];
  309. int i = 0;
  310. for (Iterator iter = userAttributes.iterator(); iter.hasNext(); i++) {
  311. UserAttribute userAttr = (UserAttribute) iter.next();
  312. toReturnArray[i] = userAttr.getName();
  313. }
  314. return toReturnArray;
  315. }
  316. private java.util.ResourceBundle getBundle(String portletAppName, String portletName, Locale locale) {
  317. try {
  318. WSRPHttpServletRequest request = new WSRPHttpServletRequest(null);
  319. WSRPHttpServletResponse response = new WSRPHttpServletResponse();
  320. return service_.getBundle(request, response,
  321. portletAppName, portletName,
  322. locale);
  323. } catch (PortletContainerException e) {
  324. e.printStackTrace();
  325. }
  326. return null;
  327. }
  328. }