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.taglib;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.taglib.BodyContentWrapper;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.StringBundler;
022    
023    import java.io.IOException;
024    import java.io.Writer;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspException;
028    import javax.servlet.jsp.tagext.BodyContent;
029    import javax.servlet.jsp.tagext.BodyTag;
030    
031    /**
032     * <p>
033     * See https://issues.liferay.com/browse/LPS-13878.
034     * </p>
035     *
036     * @author Shuyang Zhou
037     */
038    public class BaseBodyTagSupport extends TagSupport {
039    
040            @SuppressWarnings("unused")
041            public int doAfterBody() throws JspException {
042                    return SKIP_BODY;
043            }
044    
045            @SuppressWarnings("unused")
046            public void doInitBody() throws JspException {
047            }
048    
049            @Override
050            @SuppressWarnings("unused")
051            public int doStartTag() throws JspException {
052                    return BodyTag.EVAL_BODY_BUFFERED;
053            }
054    
055            public BodyContent getBodyContent() {
056                    return bodyContent;
057            }
058    
059            public StringBundler getBodyContentAsStringBundler() {
060                    if (!(this instanceof BodyTag)) {
061                            throw new RuntimeException(
062                                    getClass().getName() + " must implement " +
063                                            BodyTag.class.getName());
064                    }
065    
066                    BodyContent bodyContent = getBodyContent();
067    
068                    if (bodyContent instanceof BodyContentWrapper) {
069                            BodyContentWrapper bodyContentWrapper =
070                                    (BodyContentWrapper)bodyContent;
071    
072                            return bodyContentWrapper.getStringBundler();
073                    }
074    
075                    if (bodyContent == null) {
076                            return new StringBundler();
077                    }
078    
079                    if (ServerDetector.isTomcat() && _log.isWarnEnabled()) {
080                            _log.warn(
081                                    "BodyContent is not BodyContentWrapper. Check " +
082                                            "JspFactorySwapper.");
083                    }
084    
085                    String bodyContentString = bodyContent.getString();
086    
087                    if (bodyContentString == null) {
088                            return new StringBundler();
089                    }
090    
091                    return new StringBundler(bodyContentString);
092            }
093    
094            public HttpServletRequest getRequest() {
095                    return (HttpServletRequest)pageContext.getRequest();
096            }
097    
098            @Override
099            public void release() {
100                    bodyContent = null;
101    
102                    super.release();
103            }
104    
105            public void setBodyContent(BodyContent bodyContent) {
106                    this.bodyContent = bodyContent;
107            }
108    
109            public void writeBodyContent(Writer writer) throws IOException {
110                    StringBundler sb = getBodyContentAsStringBundler();
111    
112                    sb.writeTo(writer);
113            }
114    
115            protected BodyContent bodyContent;
116    
117            private static final Log _log = LogFactoryUtil.getLog(
118                    BaseBodyTagSupport.class);
119    
120    }