1   /**
2    * Copyright (c) 2000-2007 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.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  /**
62   * <a href="DescriptionHandlerImpl.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Michael Young
65   *
66   */
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         // Required
150         portletDescription.setPortletHandle(portlet.getPortletId());
151 
152         // Required
153         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             // Required
167             Set portletModesSet = (Set) portletModesMap.get(mimeType);
168             String[] portletModes = null;
169 
170             // Make sure we have at least VIEW
171             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             // Required
191             String[] windowStates = { WindowStates._normal,
192                     WindowStates._minimized, WindowStates._maximized };
193             markupTypes[i].setWindowStates(windowStates);
194 
195             markupTypes[i].setLocales(locales);
196         }
197 
198         // make sure we have at least one
199         if (mimeTypes.size() <= 0) {
200             markupTypes = new MarkupType[1];
201             markupTypes[0] = new MarkupType();
202             markupTypes[0].setMimeType("text/html");
203 
204             // Required
205             String[] portletModes = { WSRPUtil.toWsrpMode(PortletMode.VIEW
206                     .toString()) };
207             markupTypes[0].setModes(portletModes);
208 
209             // Required
210             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         // get portlet config so we can get localized title
220         ServletContext ctx = WSRPUtil.getServletContext();
221 
222         PortletConfig portletConfig = PortletConfigFactory.create(portlet,
223             ctx);
224 
225         // get requested language
226         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 }