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 require, String use,
040                            String bodyContentString, BodyContent previousBodyContent,
041                            PageContext pageContext)
042                    throws Exception {
043    
044                    String previousBodyContentString = null;
045    
046                    if ((previousBodyContent != null) &&
047                            !(previousBodyContent instanceof BodyContentWrapper)) {
048    
049                            // LPS-22413
050    
051                            previousBodyContentString = previousBodyContent.getString();
052                    }
053    
054                    ScriptTag scriptTag = new ScriptTag();
055    
056                    scriptTag.setPageContext(pageContext);
057                    scriptTag.setPosition(position);
058                    scriptTag.setRequire(require);
059                    scriptTag.setUse(use);
060    
061                    BodyContent bodyContent = pageContext.pushBody();
062    
063                    scriptTag.setBodyContent(bodyContent);
064    
065                    bodyContent.write(bodyContentString);
066    
067                    pageContext.popBody();
068    
069                    scriptTag.doEndTag();
070    
071                    scriptTag.release();
072    
073                    if (previousBodyContentString != null) {
074    
075                            // LPS-22413
076    
077                            previousBodyContent.clear();
078    
079                            previousBodyContent.append(previousBodyContentString);
080                    }
081            }
082    
083            public static void flushScriptData(PageContext pageContext)
084                    throws Exception {
085    
086                    HttpServletRequest request =
087                            (HttpServletRequest)pageContext.getRequest();
088    
089                    ScriptData scriptData = (ScriptData)request.getAttribute(
090                            WebKeys.AUI_SCRIPT_DATA);
091    
092                    if (scriptData == null) {
093                            return;
094                    }
095    
096                    request.removeAttribute(WebKeys.AUI_SCRIPT_DATA);
097    
098                    scriptData.writeTo(request, pageContext.getOut());
099            }
100    
101            @Override
102            public int doEndTag() throws JspException {
103                    HttpServletRequest request =
104                            (HttpServletRequest)pageContext.getRequest();
105    
106                    try {
107                            String portletId = null;
108    
109                            Portlet portlet = (Portlet)request.getAttribute(
110                                    WebKeys.RENDER_PORTLET);
111    
112                            if (portlet != null) {
113                                    portletId = portlet.getPortletId();
114                            }
115    
116                            StringBundler bodyContentSB = getBodyContentAsStringBundler();
117    
118                            String require = getRequire();
119                            String use = getUse();
120    
121                            if ((require != null) && (use != null)) {
122                                    throw new JspException(
123                                            "Attributes \"require\" and \"use\" are both set");
124                            }
125    
126                            if (getSandbox() || (require != null) || (use != null)) {
127                                    StringBundler sb = new StringBundler();
128    
129                                    if ((require == null) && (use == null)) {
130                                            sb.append("(function() {");
131                                    }
132    
133                                    sb.append("var $ = AUI.$;");
134                                    sb.append("var _ = AUI._;");
135                                    sb.append(bodyContentSB);
136    
137                                    if ((require == null) && (use == null)) {
138                                            sb.append("})();");
139                                    }
140    
141                                    bodyContentSB = sb;
142                            }
143    
144                            if (isPositionInLine()) {
145                                    ScriptData scriptData = new ScriptData();
146    
147                                    if (require != null) {
148                                            scriptData.append(
149                                                    portletId, bodyContentSB, require,
150                                                    ScriptData.ModulesType.ES6);
151                                    }
152                                    else {
153                                            scriptData.append(
154                                                    portletId, bodyContentSB, use,
155                                                    ScriptData.ModulesType.AUI);
156                                    }
157    
158                                    String page = getPage();
159    
160                                    if (FileAvailabilityUtil.isAvailable(
161                                                    pageContext.getServletContext(), page)) {
162    
163                                            PortalIncludeUtil.include(pageContext, page);
164                                    }
165                                    else {
166                                            scriptData.writeTo(request, pageContext.getOut());
167                                    }
168                            }
169                            else {
170                                    ScriptData scriptData = (ScriptData)request.getAttribute(
171                                            WebKeys.AUI_SCRIPT_DATA);
172    
173                                    if (scriptData == null) {
174                                            scriptData = new ScriptData();
175    
176                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
177                                    }
178    
179                                    if (require != null) {
180                                            scriptData.append(
181                                                    portletId, bodyContentSB, require,
182                                                    ScriptData.ModulesType.ES6);
183                                    }
184                                    else {
185                                            scriptData.append(
186                                                    portletId, bodyContentSB, use,
187                                                    ScriptData.ModulesType.AUI);
188                                    }
189                            }
190    
191                            return EVAL_PAGE;
192                    }
193                    catch (Exception e) {
194                            throw new JspException(e);
195                    }
196                    finally {
197                            if (!ServerDetector.isResin()) {
198                                    cleanUp();
199                            }
200    
201                            request.removeAttribute(WebKeys.JAVASCRIPT_CONTEXT);
202                    }
203            }
204    
205            @Override
206            public int doStartTag() throws JspException {
207                    HttpServletRequest request =
208                            (HttpServletRequest)pageContext.getRequest();
209    
210                    request.setAttribute(WebKeys.JAVASCRIPT_CONTEXT, Boolean.TRUE);
211    
212                    return super.doStartTag();
213            }
214    
215            @Override
216            protected void cleanUp() {
217                    setPosition(null);
218                    setRequire(null);
219                    setUse(null);
220            }
221    
222    }