KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > producer > impl > JSR168ContainerProxyImpl


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
9 import java.util.Collection JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.MissingResourceException JavaDoc;
16 import java.util.Set JavaDoc;
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 /**
43  * @author Mestrallet Benjamin
44  * benjmestrallet@users.sourceforge.net
45  */

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 JavaDoc portletHandle) {
63     String JavaDoc[] 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 JavaDoc[] desiredLocales) {
71     //TODO discover what a resource is
72
return new ResourceList();
73   }
74
75   public PortletDescription getPortletDesciption(String JavaDoc portletHandle,
76                                                  String JavaDoc[] desiredLocales) {
77     String JavaDoc[] k = StringUtils.split(portletHandle, Constants.PORTLET_META_DATA_ENCODER);
78     String JavaDoc portletApplicationName = k[0];
79     String JavaDoc portletName = k[1];
80     log.debug("get description of portlet in application : " + portletApplicationName);
81     log.debug("get description of portlet : " + portletName);
82
83     Map JavaDoc 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     //delegation to JSR 168 specs
88
pD.setPortletHandle(portletHandle);
89     pD.setOnlySecure(new Boolean JavaDoc(portlet.isSecure()));
90     pD.setDefaultMarkupSecure(new Boolean JavaDoc(portlet.isSecure()));
91     List JavaDoc portletDescriptions = portlet.getDescription();
92     if (!(portletDescriptions == null || portletDescriptions.size() == 0)) {
93       pD.setDescription(getDescription(portletDescriptions, desiredLocales));
94     }
95     List JavaDoc 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     //WSRP specific issues
110
pD.setHasUserSpecificState(new Boolean JavaDoc(conf.isHasUserSpecificState()));
111     pD.setDoesUrlTemplateProcessing(new Boolean JavaDoc(conf.isDoesUrlTemplateProcessing()));
112     pD.setTemplatesStoredInSession(new Boolean JavaDoc(conf.isTemplatesStoredInSession()));
113     pD.setUserContextStoredInSession(new Boolean JavaDoc(conf.isUserContextStoredInSession()));
114     pD.setUsesMethodGet(new Boolean JavaDoc(conf.isUsesMethodGet()));
115     //pD.setUserCategories(null);
116

117     return pD;
118   }
119
120
121   public void setPortletProperties(String JavaDoc portletHandle,
122                                    String JavaDoc owner,
123                                    PropertyList propertyList)
124       throws WSRPException {
125     // key[0] = application name , key[1] portlet name
126
log.debug("portlet handle to split in setPortletProperties : " + portletHandle);
127     String JavaDoc[] key = StringUtils.split(portletHandle, Constants.PORTLET_META_DATA_ENCODER);
128     //mapping WSRP / JSR 168 : a property is a preference type
129
Property[] properties = propertyList.getProperties();
130     Map JavaDoc propertiesMap = new HashMap JavaDoc();
131     for (int i = 0; i < properties.length; i++) {
132       Property property = properties[i];
133       //Locale locale = new Locale(property.getLang());//No mapping available in JSR 168
134
String JavaDoc preferenceName = property.getName();
135       String JavaDoc 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 JavaDoc e) {
149       log.error("error while storing preferences", e);
150       throw new WSRPException(Faults.OPERATION_FAILED_FAULT);
151     }
152
153   }
154
155   public Map JavaDoc getPortletProperties(String JavaDoc portletHandle, String JavaDoc owner)
156       throws WSRPException {
157     // key[0] = application name , key[1] portlet name
158
String JavaDoc[] 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 JavaDoc e) {
170       throw new WSRPException(Faults.OPERATION_FAILED_FAULT);
171     }
172   }
173
174   public Map JavaDoc getAllPortletMetaData() {
175     return service_.getAllPortletMetaData();
176   }
177
178   public Collection JavaDoc getWindowStates(String JavaDoc s) {
179     return service_.getWindowStates(s);
180   }
181
182   public Collection JavaDoc 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 JavaDoc propertiesMap = out.getProperties();
200       Set JavaDoc set = propertiesMap.keySet();
201       for (Iterator JavaDoc iterator = set.iterator(); iterator.hasNext();) {
202         String JavaDoc key = (String JavaDoc) 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 JavaDoc getSupportedPortletModesWithDescriptions() {
215     return service_.getSupportedPortletModesWithDescriptions();
216   }
217
218   public Collection JavaDoc getSupportedWindowStatesWithDescriptions() {
219     return service_.getSupportedWindowStatesWithDescriptions();
220   }
221
222   private LocalizedString getDescription(List JavaDoc list, String JavaDoc[] desiredLocales) {
223     for (int i = 0; i < desiredLocales.length; i++) {
224       String JavaDoc desiredLocale = desiredLocales[i];
225       for (Iterator JavaDoc 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 JavaDoc list, String JavaDoc[] desiredLocales) {
236     for (int i = 0; i < desiredLocales.length; i++) {
237       String JavaDoc desiredLocale = desiredLocales[i];
238       for (Iterator JavaDoc 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 JavaDoc portletAppName,
249                                         String JavaDoc portletName,
250                                         String JavaDoc[] desiredLocales) {
251     for (int i = 0; i < desiredLocales.length; i++) {
252       String JavaDoc desiredLocale = desiredLocales[i];
253       java.util.ResourceBundle JavaDoc resourceBundle = getBundle(portletAppName, portletName,
254           new Locale JavaDoc(desiredLocale));
255       if (resourceBundle.getLocale().getLanguage().equalsIgnoreCase(desiredLocale)
256           || i == desiredLocales.length - 1) {
257         try {
258           String JavaDoc keyWords = resourceBundle.getString(PortletData.KEYWORDS);
259           String JavaDoc[] 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 JavaDoc 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 JavaDoc list, Collection JavaDoc windowStates, String JavaDoc[] locales) {
275     MarkupType[] array = new MarkupType[list.size()];
276     int i = 0;
277     MarkupType mT = null;
278     for (Iterator JavaDoc iter = list.iterator(); iter.hasNext(); i++) {
279       Supports element = (Supports) iter.next();
280       mT = new MarkupType();
281       mT.setMimeType(element.getMimeType());
282       List JavaDoc modes = element.getPortletMode();
283       String JavaDoc[] modesInArray = new String JavaDoc[modes.size()];
284       int j = 0;
285       for (Iterator JavaDoc iterator = modes.iterator(); iterator.hasNext(); j++) {
286         String JavaDoc pM = (String JavaDoc) iterator.next();
287         modesInArray[j] = WSRPConstants.WSRP_PREFIX + pM.toString();
288       }
289       mT.setModes(modesInArray);
290       j = 0;
291       String JavaDoc[] windowStatesInArray = new String JavaDoc[windowStates.size()];
292       for (Iterator JavaDoc 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 JavaDoc portletAppName,
304                                    String JavaDoc portletName,
305                                    String JavaDoc[] desiredLocales) {
306     for (int i = 0; i < desiredLocales.length; i++) {
307       String JavaDoc desiredLocale = desiredLocales[i];
308       java.util.ResourceBundle JavaDoc resourceBundle = getBundle(portletAppName, portletName,
309           new Locale JavaDoc(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 JavaDoc portletAppName,
320                                         String JavaDoc portletName,
321                                         String JavaDoc[] desiredLocales) {
322     for (int i = 0; i < desiredLocales.length; i++) {
323       String JavaDoc desiredLocale = desiredLocales[i];
324       java.util.ResourceBundle JavaDoc resourceBundle = getBundle(portletAppName, portletName,
325           new Locale JavaDoc(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 JavaDoc 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 JavaDoc[] getUserProfileItems(List JavaDoc userAttributes) {
341     String JavaDoc[] toReturnArray = new String JavaDoc[userAttributes.size()];
342     int i = 0;
343     for (Iterator JavaDoc 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 JavaDoc getBundle(String JavaDoc portletAppName, String JavaDoc portletName, Locale JavaDoc 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