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.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.LayoutTypePortlet;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.util.LinkedHashSet;
029    import java.util.List;
030    import java.util.Set;
031    
032    import javax.portlet.MimeResponse;
033    
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * @author Raymond Aug??
038     */
039    public class PortletJSONUtil {
040    
041            public static void populatePortletJSONObject(
042                            HttpServletRequest request, String portletHTML, Portlet portlet,
043                            JSONObject jsonObject)
044                    throws Exception {
045    
046                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
047                            WebKeys.THEME_DISPLAY);
048    
049                    Set<String> footerCssSet = new LinkedHashSet<String>();
050                    Set<String> footerJavaScriptSet = new LinkedHashSet<String>();
051                    Set<String> headerCssSet = new LinkedHashSet<String>();
052                    Set<String> headerJavaScriptSet = new LinkedHashSet<String>();
053    
054                    boolean portletOnLayout = false;
055    
056                    String rootPortletId = getRootPortletId(portlet);
057                    String portletId = portlet.getPortletId();
058    
059                    LayoutTypePortlet layoutTypePortlet =
060                            themeDisplay.getLayoutTypePortlet();
061    
062                    for (Portlet layoutPortlet : layoutTypePortlet.getAllPortlets()) {
063    
064                            // Check to see if an instance of this portlet is already in the
065                            // layout, but ignore the portlet that was just added
066    
067                            String layoutPortletRootPortletId = getRootPortletId(layoutPortlet);
068    
069                            if (rootPortletId.equals(layoutPortletRootPortletId) &&
070                                    !portletId.equals(layoutPortlet.getPortletId())) {
071    
072                                    portletOnLayout = true;
073    
074                                    break;
075                            }
076                    }
077    
078                    if (!portletOnLayout && portlet.isAjaxable()) {
079                            Portlet rootPortlet = portlet.getRootPortlet();
080    
081                            for (String footerPortalCss : portlet.getFooterPortalCss()) {
082                                    if (!HttpUtil.hasProtocol(footerPortalCss)) {
083                                            footerPortalCss =
084                                                    PortalUtil.getPathContext() + footerPortalCss;
085    
086                                            footerPortalCss = PortalUtil.getStaticResourceURL(
087                                                    request, footerPortalCss, rootPortlet.getTimestamp());
088                                    }
089    
090                                    footerCssSet.add(footerPortalCss);
091                            }
092    
093                            for (String footerPortalJavaScript :
094                                            portlet.getFooterPortalJavaScript()) {
095    
096                                    if (!HttpUtil.hasProtocol(footerPortalJavaScript)) {
097                                            footerPortalJavaScript =
098                                                    PortalUtil.getPathContext() + footerPortalJavaScript;
099    
100                                            footerPortalJavaScript = PortalUtil.getStaticResourceURL(
101                                                    request, footerPortalJavaScript,
102                                                    rootPortlet.getTimestamp());
103                                    }
104    
105                                    footerJavaScriptSet.add(footerPortalJavaScript);
106                            }
107    
108                            for (String footerPortletCss : portlet.getFooterPortletCss()) {
109                                    if (!HttpUtil.hasProtocol(footerPortletCss)) {
110                                            footerPortletCss =
111                                                    portlet.getStaticResourcePath() + footerPortletCss;
112    
113                                            footerPortletCss = PortalUtil.getStaticResourceURL(
114                                                    request, footerPortletCss, rootPortlet.getTimestamp());
115                                    }
116    
117                                    footerCssSet.add(footerPortletCss);
118                            }
119    
120                            for (String footerPortletJavaScript :
121                                            portlet.getFooterPortletJavaScript()) {
122    
123                                    if (!HttpUtil.hasProtocol(footerPortletJavaScript)) {
124                                            footerPortletJavaScript =
125                                                    portlet.getStaticResourcePath() +
126                                                            footerPortletJavaScript;
127    
128                                            footerPortletJavaScript = PortalUtil.getStaticResourceURL(
129                                                    request, footerPortletJavaScript,
130                                                    rootPortlet.getTimestamp());
131                                    }
132    
133                                    footerJavaScriptSet.add(footerPortletJavaScript);
134                            }
135    
136                            for (String headerPortalCss : portlet.getHeaderPortalCss()) {
137                                    if (!HttpUtil.hasProtocol(headerPortalCss)) {
138                                            headerPortalCss =
139                                                    PortalUtil.getPathContext() + headerPortalCss;
140    
141                                            headerPortalCss = PortalUtil.getStaticResourceURL(
142                                                    request, headerPortalCss, rootPortlet.getTimestamp());
143                                    }
144    
145                                    headerCssSet.add(headerPortalCss);
146                            }
147    
148                            for (String headerPortalJavaScript :
149                                            portlet.getHeaderPortalJavaScript()) {
150    
151                                    if (!HttpUtil.hasProtocol(headerPortalJavaScript)) {
152                                            headerPortalJavaScript =
153                                                    PortalUtil.getPathContext() + headerPortalJavaScript;
154    
155                                            headerPortalJavaScript = PortalUtil.getStaticResourceURL(
156                                                    request, headerPortalJavaScript,
157                                                    rootPortlet.getTimestamp());
158                                    }
159    
160                                    headerJavaScriptSet.add(headerPortalJavaScript);
161                            }
162    
163                            for (String headerPortletCss : portlet.getHeaderPortletCss()) {
164                                    if (!HttpUtil.hasProtocol(headerPortletCss)) {
165                                            headerPortletCss =
166                                                    portlet.getStaticResourcePath() + headerPortletCss;
167    
168                                            headerPortletCss = PortalUtil.getStaticResourceURL(
169                                                    request, headerPortletCss, rootPortlet.getTimestamp());
170                                    }
171    
172                                    headerCssSet.add(headerPortletCss);
173                            }
174    
175                            for (String headerPortletJavaScript :
176                                            portlet.getHeaderPortletJavaScript()) {
177    
178                                    if (!HttpUtil.hasProtocol(headerPortletJavaScript)) {
179                                            headerPortletJavaScript =
180                                                    portlet.getStaticResourcePath() +
181                                                            headerPortletJavaScript;
182    
183                                            headerPortletJavaScript = PortalUtil.getStaticResourceURL(
184                                                    request, headerPortletJavaScript,
185                                                    rootPortlet.getTimestamp());
186                                    }
187    
188                                    headerJavaScriptSet.add(headerPortletJavaScript);
189                            }
190                    }
191    
192                    String footerCssPaths = JSONFactoryUtil.serialize(
193                            footerCssSet.toArray(new String[footerCssSet.size()]));
194    
195                    jsonObject.put(
196                            "footerCssPaths", JSONFactoryUtil.createJSONArray(footerCssPaths));
197    
198                    String footerJavaScriptPaths = JSONFactoryUtil.serialize(
199                            footerJavaScriptSet.toArray(
200                                    new String[footerJavaScriptSet.size()]));
201    
202                    jsonObject.put(
203                            "footerJavaScriptPaths",
204                            JSONFactoryUtil.createJSONArray(footerJavaScriptPaths));
205    
206                    String headerCssPaths = JSONFactoryUtil.serialize(
207                            headerCssSet.toArray(new String[headerCssSet.size()]));
208    
209                    jsonObject.put(
210                            "headerCssPaths", JSONFactoryUtil.createJSONArray(headerCssPaths));
211    
212                    String headerJavaScriptPaths = JSONFactoryUtil.serialize(
213                            headerJavaScriptSet.toArray(
214                                    new String[headerJavaScriptSet.size()]));
215    
216                    jsonObject.put(
217                            "headerJavaScriptPaths",
218                            JSONFactoryUtil.createJSONArray(headerJavaScriptPaths));
219    
220                    List<String> markupHeadElements = (List<String>)request.getAttribute(
221                            MimeResponse.MARKUP_HEAD_ELEMENT);
222    
223                    if (markupHeadElements != null) {
224                            jsonObject.put(
225                                    "markupHeadElements",
226                                    StringUtil.merge(markupHeadElements, StringPool.BLANK));
227                    }
228    
229                    jsonObject.put("portletHTML", portletHTML);
230                    jsonObject.put("refresh", !portlet.isAjaxable());
231            }
232    
233            protected static String getRootPortletId(Portlet portlet) {
234    
235                    // Workaround for portlet#getRootPortletId because that does not return
236                    // the proper root portlet ID for OpenSocial and WSRP portlets
237    
238                    Portlet rootPortlet = portlet.getRootPortlet();
239    
240                    return rootPortlet.getPortletId();
241            }
242    
243    }