1
22
23 package com.liferay.portal.wsrp;
24
25 import com.caucho.util.LocaleUtil;
26 import com.liferay.portal.kernel.language.LanguageUtil;
27 import com.liferay.portal.kernel.util.JavaConstants;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.model.Portlet;
30 import com.liferay.portal.service.PortletLocalServiceUtil;
31 import com.liferay.portal.wsrp.util.WSRPUtil;
32 import com.liferay.portlet.PortletConfigFactory;
33
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.Map;
38 import java.util.MissingResourceException;
39 import java.util.ResourceBundle;
40 import java.util.Set;
41
42 import javax.portlet.PortletConfig;
43 import javax.portlet.PortletMode;
44
45 import javax.servlet.ServletContext;
46 import javax.servlet.http.HttpServletRequest;
47
48 import oasis.names.tc.wsrp.v1.types.CookieProtocol;
49 import oasis.names.tc.wsrp.v1.types.LocalizedString;
50 import oasis.names.tc.wsrp.v1.types.MarkupType;
51 import oasis.names.tc.wsrp.v1.types.PortletDescription;
52 import oasis.names.tc.wsrp.v1.types.RegistrationContext;
53 import oasis.names.tc.wsrp.v1.types.ServiceDescription;
54 import oasis.names.tc.wsrp.v1.types.UserContext;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.apache.wsrp4j.exception.ErrorCodes;
59 import org.apache.wsrp4j.exception.WSRPException;
60 import org.apache.wsrp4j.producer.provider.DescriptionHandler;
61 import org.apache.wsrp4j.util.WindowStates;
62
63
69 public class DescriptionHandlerImpl implements DescriptionHandler {
70
71 public PortletDescription getPortletDescription(String portletHandle,
72 RegistrationContext regContext, UserContext userContext,
73 String[] desiredLocales) throws WSRPException {
74 long companyId = WSRPUtil.getCompanyId();
75
76 Portlet portlet = null;
77
78 try {
79 portlet = PortletLocalServiceUtil.getPortletById(companyId,
80 portletHandle);
81 }
82 catch (Exception e) {
83 throw new WSRPException(ErrorCodes.LOAD_SERVICEDESCRIPTION_FAILED,
84 e);
85 }
86
87 return _getPortletDescription(portlet, regContext, desiredLocales);
88 }
89
90 public PortletDescription getPortletDescription(String portletHandle)
91 throws WSRPException {
92 return getPortletDescription(portletHandle, null, null, null);
93 }
94
95 public PortletDescription[] getProducerOfferedPortletDescriptions(
96 RegistrationContext regContext, String[] desiredLocales)
97 throws WSRPException {
98 long companyId = WSRPUtil.getCompanyId();
99
100 PortletDescription[] portletDescriptions = null;
101
102 try {
103 List portlets = PortletLocalServiceUtil.getPortlets(companyId);
104 portletDescriptions = new PortletDescription[portlets.size()];
105
106 for (int i = 0; i < portlets.size(); i++) {
107 Portlet portlet = (Portlet) portlets.get(i);
108
109 portletDescriptions[i] = _getPortletDescription(portlet,
110 regContext, desiredLocales);
111 }
112 }
113 catch (Exception e) {
114 throw new WSRPException(ErrorCodes.LOAD_SERVICEDESCRIPTION_FAILED,
115 e);
116 }
117
118 return portletDescriptions;
119 }
120
121 public boolean isRegistrationRequired() throws WSRPException {
122 return false;
123 }
124
125 public ServiceDescription getServiceDescription(
126 RegistrationContext regContext, String[] desiredLocales)
127 throws WSRPException {
128 ServiceDescription serviceDescription = new ServiceDescription();
129 serviceDescription.setRequiresRegistration(isRegistrationRequired());
130
131 CookieProtocol cookieProtocol = CookieProtocol.perGroup;
132 serviceDescription.setRequiresInitCookie(cookieProtocol);
133
134 PortletDescription[] portletDescriptions = getProducerOfferedPortletDescriptions(
135 regContext, desiredLocales);
136
137 serviceDescription.setOfferedPortlets(portletDescriptions);
138
139 return serviceDescription;
140 }
141
142 private PortletDescription _getPortletDescription(Portlet portlet,
143 RegistrationContext regContext, String[] desiredLocales)
144 throws WSRPException {
145 PortletDescription portletDescription = new PortletDescription();
146
147 Set localesSet = portlet.getSupportedLocales();
148 String[] locales = null;
149
150 if (localesSet.size() != 0) {
151 locales = (String[])localesSet.toArray(new String[localesSet.size()]);
152 }
153 else {
154 Locale[] localeObjs = LanguageUtil.getAvailableLocales();
155
156 locales = com.liferay.portal.kernel.util.LocaleUtil.toLanguageIds(
157 localeObjs);
158 }
159
160 portletDescription.setPortletHandle(portlet.getPortletId());
162
163 Map portletModesMap = portlet.getPortletModes();
165 Set mimeTypes = portletModesMap.keySet();
166 MarkupType[] markupTypes = new MarkupType[mimeTypes.size()];
167 Iterator it = mimeTypes.iterator();
168 int i = 0;
169
170 for (i = 0; it.hasNext(); i++) {
171 boolean viewModeFound = false;
172 String mimeType = (String) it.next();
173 markupTypes[i] = new MarkupType();
174
175 markupTypes[i].setMimeType(mimeType);
176
177 Set portletModesSet = (Set) portletModesMap.get(mimeType);
179 String[] portletModes = null;
180
181 if (!portletModesSet.contains(PortletMode.VIEW.toString())) {
183 portletModes = new String[portletModesSet.size() + 1];
184 portletModes[portletModes.length - 1] = WSRPUtil
185 .toWsrpMode(PortletMode.VIEW.toString());
186 }
187 else {
188 portletModes = new String[portletModesSet.size()];
189 }
190
191 Iterator itr = portletModesSet.iterator();
192
193 for (int j = 0; itr.hasNext(); j++) {
194 String mode = (String) itr.next();
195
196 portletModes[j] = WSRPUtil.toWsrpMode(mode);
197 }
198
199 markupTypes[i].setModes(portletModes);
200
201 String[] windowStates = { WindowStates._normal,
203 WindowStates._minimized, WindowStates._maximized };
204 markupTypes[i].setWindowStates(windowStates);
205
206 markupTypes[i].setLocales(locales);
207 }
208
209 if (mimeTypes.size() <= 0) {
211 markupTypes = new MarkupType[1];
212 markupTypes[0] = new MarkupType();
213 markupTypes[0].setMimeType("text/html");
214
215 String[] portletModes = { WSRPUtil.toWsrpMode(PortletMode.VIEW
217 .toString()) };
218 markupTypes[0].setModes(portletModes);
219
220 String[] windowStates = { WindowStates._normal,
222 WindowStates._minimized, WindowStates._maximized };
223 markupTypes[i].setWindowStates(windowStates);
224
225 markupTypes[0].setLocales(locales);
226 }
227
228 portletDescription.setMarkupTypes(markupTypes);
229
230 ServletContext ctx = WSRPUtil.getServletContext();
232
233 PortletConfig portletConfig = PortletConfigFactory.create(portlet,
234 ctx);
235
236 HttpServletRequest req = WSRPUtil.getHttpServletRequest();
238 Locale requestLocale = req.getLocale();
239 String lang = requestLocale.getDisplayLanguage();
240 ResourceBundle resourceBundle = portletConfig
241 .getResourceBundle(requestLocale);
242
243 LocalizedString shortTitle = new LocalizedString();
244 shortTitle.setLang(lang);
245 shortTitle.setValue(_getResourceString(resourceBundle, JavaConstants.JAVAX_PORTLET_SHORT_TITLE, StringPool.BLANK));
246 portletDescription.setShortTitle(shortTitle);
247
248 LocalizedString title = new LocalizedString();
249 title.setLang(lang);
250 title.setValue(_getResourceString(resourceBundle, JavaConstants.JAVAX_PORTLET_TITLE, StringPool.BLANK));
251 portletDescription.setTitle(title);
252
253 portletDescription.setGroupID(portlet.getPortletId());
254
255 return portletDescription;
256 }
257
258 private String _getResourceString(ResourceBundle bundle, String key, String def) {
259 String value = def;
260
261 try {
262 value = bundle.getString(key);
263 }
264 catch (MissingResourceException e){}
265
266 return value;
267 }
268 private Log _log = LogFactory.getLog(ServiceDescription.class);
269
270 }