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