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.portlet;
016    
017    import com.liferay.portal.kernel.util.Http;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.model.Portlet;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.PortalUtil;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Set;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Carlos Sierra Andr??s
032     */
033    public class PortletResourceStaticURLGenerator {
034    
035            public List<String> generate(
036                    Portlet portlet, PortletResourceAccessor ... portletResourceAccessors) {
037    
038                    List<String> urls = new ArrayList<>();
039    
040                    for (PortletResourceAccessor portletResourceAccessor :
041                                    portletResourceAccessors) {
042    
043                            String contextPath = null;
044    
045                            if (portletResourceAccessor.isPortalResource()) {
046                                    contextPath = PortalUtil.getPathContext();
047                            }
048                            else {
049                                    contextPath = portlet.getContextPath();
050                            }
051    
052                            List<String> portletResources = portletResourceAccessor.get(
053                                    portlet);
054    
055                            for (String portletResource : portletResources) {
056                                    if (!HttpUtil.hasProtocol(portletResource)) {
057                                            Portlet rootPortlet = portlet.getRootPortlet();
058    
059                                            portletResource = PortalUtil.getStaticResourceURL(
060                                                    _request, contextPath + portletResource,
061                                                    rootPortlet.getTimestamp());
062                                    }
063    
064                                    if (!portletResource.contains(Http.PROTOCOL_DELIMITER)) {
065                                            String cdnBaseURL = _themeDisplay.getCDNBaseURL();
066    
067                                            portletResource = cdnBaseURL.concat(portletResource);
068                                    }
069    
070                                    if (!_visitedURLs.contains(portletResource)) {
071                                            urls.add(portletResource);
072    
073                                            _visitedURLs.add(portletResource);
074                                    }
075                            }
076                    }
077    
078                    return urls;
079            }
080    
081            public void setRequest(HttpServletRequest request) {
082                    _request = request;
083                    _themeDisplay = (ThemeDisplay)request.getAttribute(
084                            WebKeys.THEME_DISPLAY);
085            }
086    
087            public void setVisitedURLs(Set<String> visitedURLs) {
088                    _visitedURLs = visitedURLs;
089            }
090    
091            private HttpServletRequest _request;
092            private ThemeDisplay _themeDisplay;
093            private Set<String> _visitedURLs;
094    
095    }