001    /**
002     * Copyright (c) 2000-present 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.portal.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.portlet.PortletParameterUtil;
020    import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
021    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.PrefixPredicateFilter;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.kernel.xml.SAXReaderUtil;
029    import com.liferay.portal.model.Portlet;
030    import com.liferay.portal.model.PortletConstants;
031    import com.liferay.portal.service.PortletLocalServiceUtil;
032    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortletKeys;
035    import com.liferay.portlet.PortletPreferencesFactoryUtil;
036    
037    import java.util.Map;
038    
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Douglas Wong
045     */
046    public class PortletLogic extends RuntimeLogic {
047    
048            public static final String CLOSE_1_TAG = "</runtime-portlet>";
049    
050            public static final String CLOSE_2_TAG = "/>";
051    
052            public static final String OPEN_TAG = "<runtime-portlet";
053    
054            public PortletLogic(
055                    HttpServletRequest request, HttpServletResponse response) {
056    
057                    _request = request;
058                    _response = response;
059            }
060    
061            @Override
062            public String getClose1Tag() {
063                    return CLOSE_1_TAG;
064            }
065    
066            @Override
067            public String getOpenTag() {
068                    return OPEN_TAG;
069            }
070    
071            @Override
072            public String processXML(String xml) throws Exception {
073                    Document document = SAXReaderUtil.read(xml);
074    
075                    Element rootElement = document.getRootElement();
076    
077                    String portletId = rootElement.attributeValue("name");
078                    String instanceId = rootElement.attributeValue("instance");
079                    String queryString = rootElement.attributeValue("queryString");
080    
081                    if (Validator.isNotNull(instanceId)) {
082                            portletId = PortletConstants.assemblePortletId(
083                                    portletId, instanceId);
084                    }
085    
086                    BufferCacheServletResponse bufferCacheServletResponse =
087                            new BufferCacheServletResponse(_response);
088    
089                    queryString = PortletParameterUtil.addNamespace(portletId, queryString);
090    
091                    Map<String, String[]> parameterMap = _request.getParameterMap();
092    
093                    if (!portletId.equals(_request.getParameter("p_p_id"))) {
094                            parameterMap = MapUtil.filterByKeys(
095                                    parameterMap, new PrefixPredicateFilter("p_p_"));
096                    }
097    
098                    HttpServletRequest request = DynamicServletRequest.addQueryString(
099                            _request, parameterMap, queryString, false);
100    
101                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
102                            WebKeys.THEME_DISPLAY);
103    
104                    Portlet portlet = getPortlet(themeDisplay, portletId);
105    
106                    PortletContainerUtil.render(
107                            request, bufferCacheServletResponse, portlet);
108    
109                    return bufferCacheServletResponse.getString();
110            }
111    
112            /**
113             * @see com.liferay.portal.model.impl.LayoutTypePortletImpl#getStaticPortlets(
114             *      String)
115             */
116            protected Portlet getPortlet(ThemeDisplay themeDisplay, String portletId)
117                    throws Exception {
118    
119                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
120                            themeDisplay.getCompanyId(), portletId);
121    
122                    // See LayoutTypePortletImpl#getStaticPortlets for why we only clone
123                    // non-instanceable portlets
124    
125                    if (PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
126                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, themeDisplay.getPlid(),
127                                    portletId) < 1) {
128    
129                            PortletPreferencesFactoryUtil.getPortletSetup(_request, portletId);
130    
131                            PortletLayoutListener portletLayoutListener =
132                                    portlet.getPortletLayoutListenerInstance();
133    
134                            if (portletLayoutListener != null) {
135                                    portletLayoutListener.onAddToLayout(
136                                            portletId, themeDisplay.getPlid());
137                            }
138                    }
139    
140                    if (!portlet.isInstanceable()) {
141                            portlet = (Portlet)portlet.clone();
142                    }
143    
144                    portlet.setStatic(true);
145    
146                    return portlet;
147            }
148    
149            private final HttpServletRequest _request;
150            private final HttpServletResponse _response;
151    
152    }