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.portal.servlet;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.PredicateFilter;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.util.comparator.PortletNameComparator;
025    import com.liferay.portlet.PortletResourceAccessor;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Set;
030    
031    /**
032     * @author Carlos Sierra Andr??s
033     */
034    public class ComboServletStaticURLGenerator {
035    
036            public List<String> generate(List<Portlet> portlets) {
037                    List<String> urls = new ArrayList<>();
038    
039                    StringBundler sb = new StringBundler();
040    
041                    long timestamp = _timestamp;
042    
043                    portlets = ListUtil.sort(portlets, _portletNameComparator);
044    
045                    for (Portlet portlet : portlets) {
046                            for (PortletResourceAccessor portletResourceAccessor :
047                                            _portletResourceAccessors) {
048    
049                                    List<String> portletResources = portletResourceAccessor.get(
050                                            portlet);
051    
052                                    for (String portletResource : portletResources) {
053                                            if (!_predicateFilter.filter(portletResource)) {
054                                                    continue;
055                                            }
056    
057                                            if (portletResource.endsWith(".css") && _rtl) {
058                                                    int pos = portletResource.lastIndexOf(
059                                                            StringPool.PERIOD);
060    
061                                                    portletResource = portletResource.substring(
062                                                            0, pos) + "_rtl" + portletResource.substring(pos);
063                                            }
064    
065                                            String url = portletResource;
066    
067                                            if (!HttpUtil.hasProtocol(portletResource)) {
068                                                    url = portlet.getContextPath() + portletResource;
069                                            }
070    
071                                            if (_visitedURLs.contains(url)) {
072                                                    continue;
073                                            }
074    
075                                            if (HttpUtil.hasProtocol(portletResource)) {
076                                                    urls.add(portletResource);
077                                            }
078                                            else {
079                                                    sb.append(StringPool.AMPERSAND);
080    
081                                                    if (!portletResourceAccessor.isPortalResource()) {
082                                                            sb.append(portlet.getPortletId());
083                                                            sb.append(StringPool.COLON);
084                                                    }
085    
086                                                    sb.append(HtmlUtil.escapeURL(portletResource));
087    
088                                                    timestamp = Math.max(timestamp, portlet.getTimestamp());
089                                            }
090    
091                                            _visitedURLs.add(url);
092                                    }
093                            }
094                    }
095    
096                    if (sb.length() > 0) {
097                            String url = _urlPrefix + sb.toString();
098    
099                            url = HttpUtil.addParameter(url, "t", timestamp);
100    
101                            urls.add(url);
102                    }
103    
104                    return urls;
105            }
106    
107            public void setPortletResourceAccessors(
108                    PortletResourceAccessor... portletResourceAccessors) {
109    
110                    _portletResourceAccessors = portletResourceAccessors;
111            }
112    
113            public void setPredicateFilter(PredicateFilter<String> predicateFilter) {
114                    _predicateFilter = predicateFilter;
115            }
116    
117            public void setRtl(boolean rtl) {
118                    _rtl = rtl;
119            }
120    
121            public void setTimestamp(long timestamp) {
122                    _timestamp = timestamp;
123            }
124    
125            public void setURLPrefix(String urlPrefix) {
126                    _urlPrefix = urlPrefix;
127            }
128    
129            public void setVisitedURLs(Set<String> visitedURLs) {
130                    _visitedURLs = visitedURLs;
131            }
132    
133            private static final PortletNameComparator _portletNameComparator =
134                    new PortletNameComparator();
135    
136            private PortletResourceAccessor[] _portletResourceAccessors;
137            private PredicateFilter<String> _predicateFilter = PredicateFilter.ALL;
138            private boolean _rtl = false;
139            private long _timestamp;
140            private String _urlPrefix;
141            private Set<String> _visitedURLs;
142    
143    }