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