001    /**
002     * Copyright (c) 2000-2011 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.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    }