1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.wsrp;
21  
22  import com.caucho.util.LocaleUtil;
23  import com.liferay.portal.kernel.language.LanguageUtil;
24  import com.liferay.portal.kernel.util.JavaConstants;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.portal.service.PortletLocalServiceUtil;
28  import com.liferay.portal.wsrp.util.WSRPUtil;
29  import com.liferay.portlet.PortletConfigFactory;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.Map;
35  import java.util.MissingResourceException;
36  import java.util.ResourceBundle;
37  import java.util.Set;
38  
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletMode;
41  
42  import javax.servlet.ServletContext;
43  import javax.servlet.http.HttpServletRequest;
44  
45  import oasis.names.tc.wsrp.v1.types.CookieProtocol;
46  import oasis.names.tc.wsrp.v1.types.LocalizedString;
47  import oasis.names.tc.wsrp.v1.types.MarkupType;
48  import oasis.names.tc.wsrp.v1.types.PortletDescription;
49  import oasis.names.tc.wsrp.v1.types.RegistrationContext;
50  import oasis.names.tc.wsrp.v1.types.ServiceDescription;
51  import oasis.names.tc.wsrp.v1.types.UserContext;
52  
53  import org.apache.commons.logging.Log;
54  import org.apache.commons.logging.LogFactory;
55  import org.apache.wsrp4j.exception.ErrorCodes;
56  import org.apache.wsrp4j.exception.WSRPException;
57  import org.apache.wsrp4j.producer.provider.DescriptionHandler;
58  import org.apache.wsrp4j.util.WindowStates;
59  
60  /**
61   * <a href="DescriptionHandlerImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Michael Young
64   *
65   */
66  public class DescriptionHandlerImpl implements DescriptionHandler {
67  
68      public PortletDescription getPortletDescription(String portletHandle,
69              RegistrationContext regContext, UserContext userContext,
70              String[] desiredLocales) throws WSRPException {
71          long companyId = WSRPUtil.getCompanyId();
72  
73          Portlet portlet = null;
74  
75          try {
76              portlet = PortletLocalServiceUtil.getPortletById(companyId,
77                      portletHandle);
78          }
79          catch (Exception e) {
80              throw new WSRPException(ErrorCodes.LOAD_SERVICEDESCRIPTION_FAILED,
81                      e);
82          }
83  
84          return _getPortletDescription(portlet, regContext, desiredLocales);
85      }
86  
87      public PortletDescription getPortletDescription(String portletHandle)
88              throws WSRPException {
89          return getPortletDescription(portletHandle, null, null, null);
90      }
91  
92      public PortletDescription[] getProducerOfferedPortletDescriptions(
93              RegistrationContext regContext, String[] desiredLocales)
94              throws WSRPException {
95          long companyId = WSRPUtil.getCompanyId();
96  
97          PortletDescription[] portletDescriptions = null;
98  
99          try {
100             List portlets = PortletLocalServiceUtil.getPortlets(companyId);
101             portletDescriptions = new PortletDescription[portlets.size()];
102 
103             for (int i = 0; i < portlets.size(); i++) {
104                 Portlet portlet = (Portlet) portlets.get(i);
105 
106                 portletDescriptions[i] = _getPortletDescription(portlet,
107                         regContext, desiredLocales);
108             }
109         }
110         catch (Exception e) {
111             throw new WSRPException(ErrorCodes.LOAD_SERVICEDESCRIPTION_FAILED,
112                     e);
113         }
114 
115         return portletDescriptions;
116     }
117 
118     public boolean isRegistrationRequired() throws WSRPException {
119         return false;
120     }
121 
122     public ServiceDescription getServiceDescription(
123             RegistrationContext regContext, String[] desiredLocales)
124             throws WSRPException {
125         ServiceDescription serviceDescription = new ServiceDescription();
126         serviceDescription.setRequiresRegistration(isRegistrationRequired());
127 
128         CookieProtocol cookieProtocol = CookieProtocol.perGroup;
129         serviceDescription.setRequiresInitCookie(cookieProtocol);
130 
131         PortletDescription[] portletDescriptions = getProducerOfferedPortletDescriptions(
132                 regContext, desiredLocales);
133 
134         serviceDescription.setOfferedPortlets(portletDescriptions);
135 
136         return serviceDescription;
137     }
138 
139     private PortletDescription _getPortletDescription(Portlet portlet,
140             RegistrationContext regContext, String[] desiredLocales)
141             throws WSRPException {
142         PortletDescription portletDescription = new PortletDescription();
143 
144         Set localesSet = portlet.getSupportedLocales();
145         String[] locales = null;
146         
147         if (localesSet.size() != 0) {
148             locales = (String[])localesSet.toArray(new String[localesSet.size()]);
149         }
150         else {
151             Locale[] localeObjs = LanguageUtil.getAvailableLocales();
152             
153             locales = com.liferay.portal.kernel.util.LocaleUtil.toLanguageIds(
154                 localeObjs);
155         }
156         
157         // Required
158         portletDescription.setPortletHandle(portlet.getPortletId());
159 
160         // Required
161         Map portletModesMap = portlet.getPortletModes();
162         Set mimeTypes = portletModesMap.keySet();
163         MarkupType[] markupTypes = new MarkupType[mimeTypes.size()];
164         Iterator it = mimeTypes.iterator();
165         int i = 0;
166 
167         for (i = 0; it.hasNext(); i++) {
168             boolean viewModeFound = false;
169             String mimeType = (String) it.next();
170             markupTypes[i] = new MarkupType();
171 
172             markupTypes[i].setMimeType(mimeType);
173 
174             // Required
175             Set portletModesSet = (Set) portletModesMap.get(mimeType);
176             String[] portletModes = null;
177 
178             // Make sure we have at least VIEW
179             if (!portletModesSet.contains(PortletMode.VIEW.toString())) {
180                 portletModes = new String[portletModesSet.size() + 1];
181                 portletModes[portletModes.length - 1] = WSRPUtil
182                         .toWsrpMode(PortletMode.VIEW.toString());
183             }
184             else {
185                 portletModes = new String[portletModesSet.size()];
186             }
187 
188             Iterator itr = portletModesSet.iterator();
189 
190             for (int j = 0; itr.hasNext(); j++) {
191                 String mode = (String) itr.next();
192 
193                 portletModes[j] = WSRPUtil.toWsrpMode(mode);
194             }
195 
196             markupTypes[i].setModes(portletModes);
197 
198             // Required
199             String[] windowStates = { WindowStates._normal,
200                     WindowStates._minimized, WindowStates._maximized };
201             markupTypes[i].setWindowStates(windowStates);
202 
203             markupTypes[i].setLocales(locales);
204         }
205 
206         // make sure we have at least one
207         if (mimeTypes.size() <= 0) {
208             markupTypes = new MarkupType[1];
209             markupTypes[0] = new MarkupType();
210             markupTypes[0].setMimeType("text/html");
211 
212             // Required
213             String[] portletModes = { WSRPUtil.toWsrpMode(PortletMode.VIEW
214                     .toString()) };
215             markupTypes[0].setModes(portletModes);
216 
217             // Required
218             String[] windowStates = { WindowStates._normal,
219                     WindowStates._minimized, WindowStates._maximized };
220             markupTypes[i].setWindowStates(windowStates);
221 
222             markupTypes[0].setLocales(locales);
223         }
224 
225         portletDescription.setMarkupTypes(markupTypes);
226 
227         // get portlet config so we can get localized title
228         ServletContext ctx = WSRPUtil.getServletContext();
229 
230         PortletConfig portletConfig = PortletConfigFactory.create(portlet,
231             ctx);
232 
233         // get requested language
234         HttpServletRequest req = WSRPUtil.getHttpServletRequest();
235         Locale requestLocale = req.getLocale();
236         String lang = requestLocale.getDisplayLanguage();
237         ResourceBundle resourceBundle = portletConfig
238                 .getResourceBundle(requestLocale);
239 
240         LocalizedString shortTitle = new LocalizedString();
241         shortTitle.setLang(lang);
242         shortTitle.setValue(_getResourceString(resourceBundle, JavaConstants.JAVAX_PORTLET_SHORT_TITLE, StringPool.BLANK));
243         portletDescription.setShortTitle(shortTitle);
244 
245         LocalizedString title = new LocalizedString();
246         title.setLang(lang);
247         title.setValue(_getResourceString(resourceBundle, JavaConstants.JAVAX_PORTLET_TITLE, StringPool.BLANK));
248         portletDescription.setTitle(title);
249 
250         portletDescription.setGroupID(portlet.getPortletId());
251 
252         return portletDescription;
253     }
254 
255     private String _getResourceString(ResourceBundle bundle, String key, String def) {
256         String value = def;
257 
258         try {
259             value = bundle.getString(key);
260         }
261         catch (MissingResourceException e){}
262 
263         return value;
264     }
265     private Log _log = LogFactory.getLog(ServiceDescription.class);
266 
267 }