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.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    import com.liferay.portal.model.Portlet;
029    import com.liferay.portal.model.PortletConstants;
030    import com.liferay.portal.service.PortletLocalServiceUtil;
031    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortletKeys;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.PortletPreferencesFactoryUtil;
036    
037    import java.util.HashMap;
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.filter(
096                                    parameterMap, new HashMap<String, String[]>(),
097                                    new PrefixPredicateFilter("p_p_"));
098                    }
099    
100                    HttpServletRequest request = DynamicServletRequest.addQueryString(
101                            _request, parameterMap, queryString, false);
102    
103                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
104                            WebKeys.THEME_DISPLAY);
105    
106                    Portlet portlet = getPortlet(themeDisplay, portletId);
107    
108                    PortletContainerUtil.render(
109                            request, bufferCacheServletResponse, portlet);
110    
111                    return bufferCacheServletResponse.getString();
112            }
113    
114            /**
115             * @see com.liferay.portal.model.impl.LayoutTypePortletImpl#getStaticPortlets(
116             *      String)
117             */
118            protected Portlet getPortlet(ThemeDisplay themeDisplay, String portletId)
119                    throws Exception {
120    
121                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
122                            themeDisplay.getCompanyId(), portletId);
123    
124                    // See LayoutTypePortletImpl#getStaticPortlets for why we only clone
125                    // non-instanceable portlets
126    
127                    if (PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
128                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, themeDisplay.getPlid(),
129                                    portletId) < 1) {
130    
131                            PortletPreferencesFactoryUtil.getPortletSetup(_request, portletId);
132    
133                            PortletLayoutListener portletLayoutListener =
134                                    portlet.getPortletLayoutListenerInstance();
135    
136                            if (portletLayoutListener != null) {
137                                    portletLayoutListener.onAddToLayout(
138                                            portletId, themeDisplay.getPlid());
139                            }
140                    }
141    
142                    if (!portlet.isInstanceable()) {
143                            portlet = (Portlet)portlet.clone();
144                    }
145    
146                    portlet.setStatic(true);
147    
148                    return portlet;
149            }
150    
151            private final HttpServletRequest _request;
152            private final HttpServletResponse _response;
153    
154    }