001
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.Validator;
023 import com.liferay.portal.kernel.xml.Document;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portal.kernel.xml.SAXReaderUtil;
026 import com.liferay.portal.model.Portlet;
027 import com.liferay.portal.model.PortletConstants;
028 import com.liferay.portal.service.PortletLocalServiceUtil;
029 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
030 import com.liferay.portal.theme.ThemeDisplay;
031 import com.liferay.portal.util.PortletKeys;
032 import com.liferay.portal.util.WebKeys;
033 import com.liferay.portlet.PortletPreferencesFactoryUtil;
034
035 import javax.servlet.http.HttpServletRequest;
036 import javax.servlet.http.HttpServletResponse;
037
038
042 public class PortletLogic extends RuntimeLogic {
043
044 public static final String CLOSE_1_TAG = "</runtime-portlet>";
045
046 public static final String CLOSE_2_TAG = "/>";
047
048 public static final String OPEN_TAG = "<runtime-portlet";
049
050 public PortletLogic(
051 HttpServletRequest request, HttpServletResponse response) {
052
053 _request = request;
054 _response = response;
055 }
056
057 @Override
058 public String getClose1Tag() {
059 return CLOSE_1_TAG;
060 }
061
062 @Override
063 public String getOpenTag() {
064 return OPEN_TAG;
065 }
066
067 @Override
068 public String processXML(String xml) throws Exception {
069 Document document = SAXReaderUtil.read(xml);
070
071 Element rootElement = document.getRootElement();
072
073 String portletId = rootElement.attributeValue("name");
074 String instanceId = rootElement.attributeValue("instance");
075 String queryString = rootElement.attributeValue("queryString");
076
077 if (Validator.isNotNull(instanceId)) {
078 portletId = PortletConstants.assemblePortletId(
079 portletId, instanceId);
080 }
081
082 BufferCacheServletResponse bufferCacheServletResponse =
083 new BufferCacheServletResponse(_response);
084
085 queryString = PortletParameterUtil.addNamespace(portletId, queryString);
086
087 HttpServletRequest request = DynamicServletRequest.addQueryString(
088 _request, queryString);
089
090 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
091 WebKeys.THEME_DISPLAY);
092
093 Portlet portlet = getPortlet(themeDisplay, portletId);
094
095 PortletContainerUtil.render(
096 request, bufferCacheServletResponse, portlet);
097
098 return bufferCacheServletResponse.getString();
099 }
100
101
105 protected Portlet getPortlet(ThemeDisplay themeDisplay, String portletId)
106 throws Exception {
107
108 Portlet portlet = PortletLocalServiceUtil.getPortletById(
109 themeDisplay.getCompanyId(), portletId);
110
111
112
113
114 if (PortletPreferencesLocalServiceUtil.getPortletPreferencesCount(
115 PortletKeys.PREFS_OWNER_TYPE_LAYOUT, themeDisplay.getPlid(),
116 portletId) < 1) {
117
118 PortletPreferencesFactoryUtil.getPortletSetup(_request, portletId);
119
120 PortletLayoutListener portletLayoutListener =
121 portlet.getPortletLayoutListenerInstance();
122
123 if (portletLayoutListener != null) {
124 portletLayoutListener.onAddToLayout(
125 portletId, themeDisplay.getPlid());
126 }
127 }
128
129 if (!portlet.isInstanceable()) {
130 portlet = (Portlet)portlet.clone();
131 }
132
133 portlet.setStatic(true);
134
135 return portlet;
136 }
137
138 private HttpServletRequest _request;
139 private HttpServletResponse _response;
140
141 }