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.taglib.aui;
016    
017    import com.liferay.portal.kernel.servlet.BodyContentWrapper;
018    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
019    import com.liferay.portal.kernel.servlet.taglib.FileAvailabilityUtil;
020    import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
021    import com.liferay.portal.kernel.util.ServerDetector;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.taglib.aui.base.BaseScriptTag;
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 (isPositionInLine()) {
119                                    ScriptData scriptData = new ScriptData();
120    
121                                    scriptData.append(portletId, bodyContentSB, use);
122    
123                                    String page = getPage();
124    
125                                    if (FileAvailabilityUtil.isAvailable(
126                                                    pageContext.getServletContext(), page)) {
127    
128                                            PortalIncludeUtil.include(pageContext, page);
129                                    }
130                                    else {
131                                            scriptData.writeTo(request, pageContext.getOut());
132                                    }
133                            }
134                            else {
135                                    ScriptData scriptData = (ScriptData)request.getAttribute(
136                                            WebKeys.AUI_SCRIPT_DATA);
137    
138                                    if (scriptData == null) {
139                                            scriptData = new ScriptData();
140    
141                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
142                                    }
143    
144                                    scriptData.append(portletId, bodyContentSB, use);
145                            }
146    
147                            return EVAL_PAGE;
148                    }
149                    catch (Exception e) {
150                            throw new JspException(e);
151                    }
152                    finally {
153                            if (!ServerDetector.isResin()) {
154                                    cleanUp();
155                            }
156    
157                            request.removeAttribute(WebKeys.JAVASCRIPT_CONTEXT);
158                    }
159            }
160    
161            @Override
162            public int doStartTag() throws JspException {
163                    HttpServletRequest request =
164                            (HttpServletRequest)pageContext.getRequest();
165    
166                    request.setAttribute(WebKeys.JAVASCRIPT_CONTEXT, Boolean.TRUE);
167    
168                    return super.doStartTag();
169            }
170    
171            @Override
172            protected void cleanUp() {
173                    setPosition(null);
174                    setUse(null);
175            }
176    
177    }