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