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 (_visitedURLs.contains(portletResource)) {
058                                                    continue;
059                                            }
060    
061                                            if (HttpUtil.hasProtocol(portletResource)) {
062                                                    urls.add(portletResource);
063                                            }
064                                            else {
065                                                    sb.append(StringPool.AMPERSAND);
066    
067                                                    if (!portletResourceAccessor.isPortalResource()) {
068                                                            sb.append(portlet.getPortletId());
069                                                            sb.append(StringPool.COLON);
070                                                    }
071    
072                                                    sb.append(HtmlUtil.escapeURL(portletResource));
073    
074                                                    timestamp = Math.max(timestamp, portlet.getTimestamp());
075                                            }
076    
077                                            _visitedURLs.add(portletResource);
078                                    }
079                            }
080                    }
081    
082                    if (sb.length() > 0) {
083                            String url = _urlPrefix + sb.toString();
084    
085                            url = HttpUtil.addParameter(url, "t", timestamp);
086    
087                            urls.add(url);
088                    }
089    
090                    return urls;
091            }
092    
093            public void setPortletResourceAccessors(
094                    PortletResourceAccessor... portletResourceAccessors) {
095    
096                    _portletResourceAccessors = portletResourceAccessors;
097            }
098    
099            public void setPredicateFilter(PredicateFilter<String> predicateFilter) {
100                    _predicateFilter = predicateFilter;
101            }
102    
103            public void setTimestamp(long timestamp) {
104                    _timestamp = timestamp;
105            }
106    
107            public void setURLPrefix(String urlPrefix) {
108                    _urlPrefix = urlPrefix;
109            }
110    
111            public void setVisitedURLs(Set<String> visitedURLs) {
112                    _visitedURLs = visitedURLs;
113            }
114    
115            private static final PortletNameComparator _portletNameComparator =
116                    new PortletNameComparator();
117    
118            private PortletResourceAccessor[] _portletResourceAccessors;
119            private PredicateFilter<String> _predicateFilter = PredicateFilter.ALL;
120            private long _timestamp;
121            private String _urlPrefix;
122            private Set<String> _visitedURLs;
123    
124    }