001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.layoutconfiguration.util.xml;
016    
017    import com.liferay.portal.kernel.portlet.PortletContainerUtil;
018    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
019    import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
020    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.model.PortletConstants;
027    import com.liferay.portal.service.PortletLocalServiceUtil;
028    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.PortletPreferencesFactoryUtil;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Douglas Wong
040     */
041    public class PortletLogic extends RuntimeLogic {
042    
043            public static final String CLOSE_1_TAG = "</runtime-portlet>";
044    
045            public static final String CLOSE_2_TAG = "/>";
046    
047            public static final String OPEN_TAG = "<runtime-portlet";
048    
049            public PortletLogic(
050                    HttpServletRequest request, HttpServletResponse response) {
051    
052                    _request = request;
053                    _response = response;
054            }
055    
056            @Override
057            public String getClose1Tag() {
058                    return CLOSE_1_TAG;
059            }
060    
061            @Override
062            public String getOpenTag() {
063                    return OPEN_TAG;
064            }
065    
066            @Override
067            public String processXML(String xml) throws Exception {
068                    Document document = SAXReaderUtil.read(xml);
069    
070                    Element rootElement = document.getRootElement();
071    
072                    String portletId = rootElement.attributeValue("name");
073                    String instanceId = rootElement.attributeValue("instance");
074                    String queryString = rootElement.attributeValue("queryString");
075    
076                    if (Validator.isNotNull(instanceId)) {
077                            portletId = PortletConstants.assemblePortletId(
078                                    portletId, instanceId);
079                    }
080    
081                    BufferCacheServletResponse bufferCacheServletResponse =
082                            new BufferCacheServletResponse(_response);
083    
084                    HttpServletRequest request = DynamicServletRequest.addQueryString(
085                            _request, queryString);
086    
087                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
088                            WebKeys.THEME_DISPLAY);
089    
090                    Portlet portlet = getPortlet(themeDisplay, portletId);
091    
092                    PortletContainerUtil.render(
093                            request, bufferCacheServletResponse, portlet);
094    
095                    return bufferCacheServletResponse.getString();
096            }
097    
098            /**
099             * @see com.liferay.portal.model.impl.LayoutTypePortletImpl#getStaticPortlets(
100             *      String)
101             */
102            protected Portlet getPortlet(ThemeDisplay themeDisplay, String portletId)
103                    throws Exception {
104    
105                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
106                            themeDisplay.getCompanyId(), portletId);
107    
108                    // See LayoutTypePortletImpl#getStaticPortlets for why we only clone
109                    // non-instanceable portlets
110    
111                    if (PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
112                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, themeDisplay.getPlid(),
113                                    portletId) < 1) {
114    
115                            PortletPreferencesFactoryUtil.getPortletSetup(_request, portletId);
116    
117                            PortletLayoutListener portletLayoutListener =
118                                    portlet.getPortletLayoutListenerInstance();
119    
120                            if (portletLayoutListener != null) {
121                                    portletLayoutListener.onAddToLayout(
122                                            portletId, themeDisplay.getPlid());
123                            }
124                    }
125    
126                    if (!portlet.isInstanceable()) {
127                            portlet = (Portlet)portlet.clone();
128                    }
129    
130                    portlet.setStatic(true);
131    
132                    return portlet;
133            }
134    
135            private HttpServletRequest _request;
136            private HttpServletResponse _response;
137    
138    }