001    /**
002     * Copyright (c) 2000-2013 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     * @author Douglas Wong
034     */
035    public class PortletLogic extends RuntimeLogic {
036    
037            public static final String CLOSE_1_TAG = "</runtime-portlet>";
038    
039            public static final String CLOSE_2_TAG = "/>";
040    
041            public static final String OPEN_TAG = "<runtime-portlet";
042    
043            public PortletLogic(
044                    ServletContext servletContext, HttpServletRequest request,
045                    HttpServletResponse response, RenderRequest renderRequest,
046                    RenderResponse renderResponse) {
047    
048                    _servletContext = servletContext;
049                    _request = request;
050                    _response = response;
051                    _renderRequest = renderRequest;
052                    _renderResponse = renderResponse;
053            }
054    
055            @Override
056            public String getClose1Tag() {
057                    return CLOSE_1_TAG;
058            }
059    
060            @Override
061            public String getOpenTag() {
062                    return OPEN_TAG;
063            }
064    
065            @Override
066            public String processXML(String xml) throws Exception {
067                    Document doc = SAXReaderUtil.read(xml);
068    
069                    Element root = doc.getRootElement();
070    
071                    String rootPortletId = root.attributeValue("name");
072                    String instanceId = root.attributeValue("instance");
073                    String queryString = root.attributeValue("queryString");
074    
075                    String portletId = rootPortletId;
076    
077                    if (Validator.isNotNull(instanceId)) {
078                            portletId += PortletConstants.INSTANCE_SEPARATOR + instanceId;
079                    }
080    
081                    return RuntimePortletUtil.processPortlet(
082                            _servletContext, _request, _response, _renderRequest,
083                            _renderResponse, portletId, queryString, false);
084            }
085    
086            private RenderRequest _renderRequest;
087            private RenderResponse _renderResponse;
088            private HttpServletRequest _request;
089            private HttpServletResponse _response;
090            private ServletContext _servletContext;
091    
092    }