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