1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
64   * <a href="DescriptionHandlerImpl.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Michael Young
67   *
68   */
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         // Required
161         portletDescription.setPortletHandle(portlet.getPortletId());
162 
163         // Required
164         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             // Required
178             Set portletModesSet = (Set) portletModesMap.get(mimeType);
179             String[] portletModes = null;
180 
181             // Make sure we have at least VIEW
182             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             // Required
202             String[] windowStates = { WindowStates._normal,
203                     WindowStates._minimized, WindowStates._maximized };
204             markupTypes[i].setWindowStates(windowStates);
205 
206             markupTypes[i].setLocales(locales);
207         }
208 
209         // make sure we have at least one
210         if (mimeTypes.size() <= 0) {
211             markupTypes = new MarkupType[1];
212             markupTypes[0] = new MarkupType();
213             markupTypes[0].setMimeType("text/html");
214 
215             // Required
216             String[] portletModes = { WSRPUtil.toWsrpMode(PortletMode.VIEW
217                     .toString()) };
218             markupTypes[0].setModes(portletModes);
219 
220             // Required
221             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         // get portlet config so we can get localized title
231         ServletContext ctx = WSRPUtil.getServletContext();
232 
233         PortletConfig portletConfig = PortletConfigFactory.create(portlet,
234             ctx);
235 
236         // get requested language
237         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 }