001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.layoutconfiguration.util.xml;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    import com.liferay.portal.kernel.xml.Document;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.SAXReaderUtil;
021    import com.liferay.portal.model.PortletConstants;
022    import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
023    
024    import javax.portlet.RenderRequest;
025    import javax.portlet.RenderResponse;
026    
027    import javax.servlet.ServletContext;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortletLogic extends RuntimeLogic {
035    
036            public static final String OPEN_TAG = "<runtime-portlet";
037    
038            public static final String CLOSE_1_TAG = "</runtime-portlet>";
039    
040            public static final String CLOSE_2_TAG = "/>";
041    
042            public PortletLogic(
043                    ServletContext servletContext, HttpServletRequest request,
044                    HttpServletResponse response, RenderRequest renderRequest,
045                    RenderResponse renderResponse) {
046    
047                    _servletContext = servletContext;
048                    _request = request;
049                    _response = response;
050                    _renderRequest = renderRequest;
051                    _renderResponse = renderResponse;
052            }
053    
054            public String getOpenTag() {
055                    return OPEN_TAG;
056            }
057    
058            public String getClose1Tag() {
059                    return CLOSE_1_TAG;
060            }
061    
062            public String processXML(String xml) throws Exception {
063                    Document doc = SAXReaderUtil.read(xml);
064    
065                    Element root = doc.getRootElement();
066    
067                    String rootPortletId = root.attributeValue("name");
068                    String instanceId = root.attributeValue("instance");
069                    String queryString = root.attributeValue("queryString");
070    
071                    String portletId = rootPortletId;
072    
073                    if (Validator.isNotNull(instanceId)) {
074                            portletId += PortletConstants.INSTANCE_SEPARATOR + instanceId;
075                    }
076    
077                    return RuntimePortletUtil.processPortlet(
078                            _servletContext, _request, _response, _renderRequest,
079                            _renderResponse, portletId, queryString, false);
080            }
081    
082            private ServletContext _servletContext;
083            private HttpServletRequest _request;
084            private HttpServletResponse _response;
085            private RenderRequest _renderRequest;
086            private RenderResponse _renderResponse;
087    
088    }