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.aui;
016    
017    import com.liferay.portal.kernel.servlet.taglib.BodyContentWrapper;
018    import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.taglib.FileAvailabilityUtil;
024    import com.liferay.taglib.aui.base.BaseScriptTag;
025    import com.liferay.taglib.util.PortalIncludeUtil;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.JspException;
029    import javax.servlet.jsp.PageContext;
030    import javax.servlet.jsp.tagext.BodyContent;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class ScriptTag extends BaseScriptTag {
037    
038            public static void doTag(
039                            String position, String use, String bodyContentString,
040                            BodyContent previousBodyContent, PageContext pageContext)
041                    throws Exception {
042    
043                    String previousBodyContentString = null;
044    
045                    if ((previousBodyContent != null) &&
046                            !(previousBodyContent instanceof BodyContentWrapper)) {
047    
048                            // LPS-22413
049    
050                            previousBodyContentString = previousBodyContent.getString();
051                    }
052    
053                    ScriptTag scriptTag = new ScriptTag();
054    
055                    scriptTag.setPageContext(pageContext);
056                    scriptTag.setPosition(position);
057                    scriptTag.setUse(use);
058    
059                    BodyContent bodyContent = pageContext.pushBody();
060    
061                    scriptTag.setBodyContent(bodyContent);
062    
063                    bodyContent.write(bodyContentString);
064    
065                    pageContext.popBody();
066    
067                    scriptTag.doEndTag();
068    
069                    scriptTag.release();
070    
071                    if (previousBodyContentString != null) {
072    
073                            // LPS-22413
074    
075                            previousBodyContent.clear();
076    
077                            previousBodyContent.append(previousBodyContentString);
078                    }
079            }
080    
081            public static void flushScriptData(PageContext pageContext)
082                    throws Exception {
083    
084                    HttpServletRequest request =
085                            (HttpServletRequest)pageContext.getRequest();
086    
087                    ScriptData scriptData = (ScriptData)request.getAttribute(
088                            WebKeys.AUI_SCRIPT_DATA);
089    
090                    if (scriptData == null) {
091                            return;
092                    }
093    
094                    request.removeAttribute(WebKeys.AUI_SCRIPT_DATA);
095    
096                    scriptData.writeTo(request, pageContext.getOut());
097            }
098    
099            @Override
100            public int doEndTag() throws JspException {
101                    HttpServletRequest request =
102                            (HttpServletRequest)pageContext.getRequest();
103    
104                    try {
105                            String portletId = null;
106    
107                            Portlet portlet = (Portlet)request.getAttribute(
108                                    WebKeys.RENDER_PORTLET);
109    
110                            if (portlet != null) {
111                                    portletId = portlet.getPortletId();
112                            }
113    
114                            StringBundler bodyContentSB = getBodyContentAsStringBundler();
115    
116                            String use = getUse();
117    
118                            if (getSandbox() || (use != null)) {
119                                    StringBundler sb = new StringBundler();
120    
121                                    if (use == null) {
122                                            sb.append("(function() {");
123                                    }
124    
125                                    sb.append("var $ = AUI.$;");
126                                    sb.append("var _ = AUI._;");
127                                    sb.append(bodyContentSB);
128    
129                                    if (use == null) {
130                                            sb.append("})();");
131                                    }
132    
133                                    bodyContentSB = sb;
134                            }
135    
136                            if (isPositionInLine()) {
137                                    ScriptData scriptData = new ScriptData();
138    
139                                    scriptData.append(portletId, bodyContentSB, use);
140    
141                                    String page = getPage();
142    
143                                    if (FileAvailabilityUtil.isAvailable(
144                                                    pageContext.getServletContext(), page)) {
145    
146                                            PortalIncludeUtil.include(pageContext, page);
147                                    }
148                                    else {
149                                            scriptData.writeTo(request, pageContext.getOut());
150                                    }
151                            }
152                            else {
153                                    ScriptData scriptData = (ScriptData)request.getAttribute(
154                                            WebKeys.AUI_SCRIPT_DATA);
155    
156                                    if (scriptData == null) {
157                                            scriptData = new ScriptData();
158    
159                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
160                                    }
161    
162                                    scriptData.append(portletId, bodyContentSB, use);
163                            }
164    
165                            return EVAL_PAGE;
166                    }
167                    catch (Exception e) {
168                            throw new JspException(e);
169                    }
170                    finally {
171                            if (!ServerDetector.isResin()) {
172                                    cleanUp();
173                            }
174    
175                            request.removeAttribute(WebKeys.JAVASCRIPT_CONTEXT);
176                    }
177            }
178    
179            @Override
180            public int doStartTag() throws JspException {
181                    HttpServletRequest request =
182                            (HttpServletRequest)pageContext.getRequest();
183    
184                    request.setAttribute(WebKeys.JAVASCRIPT_CONTEXT, Boolean.TRUE);
185    
186                    return super.doStartTag();
187            }
188    
189            @Override
190            protected void cleanUp() {
191                    setPosition(null);
192                    setUse(null);
193            }
194    
195    }