001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.wsrp;
022:
023: import com.liferay.portal.kernel.util.JavaConstants;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.model.Portlet;
026: import com.liferay.portal.service.PortletLocalServiceUtil;
027: import com.liferay.portal.wsrp.util.WSRPUtil;
028: import com.liferay.portlet.PortletConfigFactory;
029:
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Locale;
033: import java.util.Map;
034: import java.util.MissingResourceException;
035: import java.util.ResourceBundle;
036: import java.util.Set;
037:
038: import javax.portlet.PortletConfig;
039: import javax.portlet.PortletMode;
040:
041: import javax.servlet.ServletContext;
042: import javax.servlet.http.HttpServletRequest;
043:
044: import oasis.names.tc.wsrp.v1.types.CookieProtocol;
045: import oasis.names.tc.wsrp.v1.types.LocalizedString;
046: import oasis.names.tc.wsrp.v1.types.MarkupType;
047: import oasis.names.tc.wsrp.v1.types.PortletDescription;
048: import oasis.names.tc.wsrp.v1.types.RegistrationContext;
049: import oasis.names.tc.wsrp.v1.types.ServiceDescription;
050: import oasis.names.tc.wsrp.v1.types.UserContext;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054: import org.apache.wsrp4j.exception.ErrorCodes;
055: import org.apache.wsrp4j.exception.WSRPException;
056: import org.apache.wsrp4j.producer.provider.DescriptionHandler;
057: import org.apache.wsrp4j.util.WindowStates;
058:
059: /**
060: * <a href="DescriptionHandlerImpl.java.html"><b><i>View Source</i></b></a>
061: *
062: * @author Michael Young
063: *
064: */
065: public class DescriptionHandlerImpl implements DescriptionHandler {
066:
067: public PortletDescription getPortletDescription(
068: String portletHandle, RegistrationContext regContext,
069: UserContext userContext, String[] desiredLocales)
070: throws WSRPException {
071: long companyId = WSRPUtil.getCompanyId();
072:
073: Portlet portlet = null;
074:
075: try {
076: portlet = PortletLocalServiceUtil.getPortletById(companyId,
077: portletHandle);
078: } catch (Exception e) {
079: throw new WSRPException(
080: ErrorCodes.LOAD_SERVICEDESCRIPTION_FAILED, e);
081: }
082:
083: return _getPortletDescription(portlet, regContext,
084: desiredLocales);
085: }
086:
087: public PortletDescription getPortletDescription(String portletHandle)
088: throws WSRPException {
089: return getPortletDescription(portletHandle, null, null, null);
090: }
091:
092: public PortletDescription[] getProducerOfferedPortletDescriptions(
093: RegistrationContext regContext, String[] desiredLocales)
094: throws WSRPException {
095: long companyId = WSRPUtil.getCompanyId();
096:
097: PortletDescription[] portletDescriptions = null;
098:
099: try {
100: List portlets = PortletLocalServiceUtil
101: .getPortlets(companyId);
102: portletDescriptions = new PortletDescription[portlets
103: .size()];
104:
105: for (int i = 0; i < portlets.size(); i++) {
106: Portlet portlet = (Portlet) portlets.get(i);
107:
108: portletDescriptions[i] = _getPortletDescription(
109: portlet, regContext, desiredLocales);
110: }
111: } catch (Exception e) {
112: throw new WSRPException(
113: ErrorCodes.LOAD_SERVICEDESCRIPTION_FAILED, 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
128: .setRequiresRegistration(isRegistrationRequired());
129:
130: CookieProtocol cookieProtocol = CookieProtocol.perGroup;
131: serviceDescription.setRequiresInitCookie(cookieProtocol);
132:
133: PortletDescription[] portletDescriptions = getProducerOfferedPortletDescriptions(
134: regContext, desiredLocales);
135:
136: serviceDescription.setOfferedPortlets(portletDescriptions);
137:
138: return serviceDescription;
139: }
140:
141: private PortletDescription _getPortletDescription(Portlet portlet,
142: RegistrationContext regContext, String[] desiredLocales)
143: throws WSRPException {
144: PortletDescription portletDescription = new PortletDescription();
145:
146: Set localesSet = portlet.getSupportedLocales();
147: String[] locales = (String[]) localesSet
148: .toArray(new String[localesSet.size()]);
149:
150: // Required
151: portletDescription.setPortletHandle(portlet.getPortletId());
152:
153: // Required
154: Map portletModesMap = portlet.getPortletModes();
155: Set mimeTypes = portletModesMap.keySet();
156: MarkupType[] markupTypes = new MarkupType[mimeTypes.size()];
157: Iterator it = mimeTypes.iterator();
158: int i = 0;
159:
160: for (i = 0; it.hasNext(); i++) {
161: boolean viewModeFound = false;
162: String mimeType = (String) it.next();
163: markupTypes[i] = new MarkupType();
164:
165: markupTypes[i].setMimeType(mimeType);
166:
167: // Required
168: Set portletModesSet = (Set) portletModesMap.get(mimeType);
169: String[] portletModes = null;
170:
171: // Make sure we have at least VIEW
172: if (!portletModesSet.contains(PortletMode.VIEW.toString())) {
173: portletModes = new String[portletModesSet.size() + 1];
174: portletModes[portletModes.length - 1] = WSRPUtil
175: .toWsrpMode(PortletMode.VIEW.toString());
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
206: .toWsrpMode(PortletMode.VIEW.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(
223: portlet, 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,
235: JavaConstants.JAVAX_PORTLET_SHORT_TITLE,
236: StringPool.BLANK));
237: portletDescription.setShortTitle(shortTitle);
238:
239: LocalizedString title = new LocalizedString();
240: title.setLang(lang);
241: title.setValue(_getResourceString(resourceBundle,
242: JavaConstants.JAVAX_PORTLET_TITLE, StringPool.BLANK));
243: portletDescription.setTitle(title);
244:
245: portletDescription.setGroupID(portlet.getPortletId());
246:
247: return portletDescription;
248: }
249:
250: private String _getResourceString(ResourceBundle bundle,
251: String key, String def) {
252: String value = def;
253:
254: try {
255: value = bundle.getString(key);
256: } catch (MissingResourceException e) {
257: }
258:
259: return value;
260: }
261:
262: private Log _log = LogFactory.getLog(ServiceDescription.class);
263:
264: }
|