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.kernel.servlet;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceReference;
020    import com.liferay.registry.ServiceTracker;
021    import com.liferay.registry.ServiceTrackerCustomizer;
022    
023    import java.net.MalformedURLException;
024    import java.net.URL;
025    
026    import java.util.Collection;
027    import java.util.Map;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import javax.servlet.ServletContext;
031    
032    /**
033     * @author Peter Fellwock
034     */
035    public class PortalWebResourcesUtil {
036    
037            public static String getContextPath(String resourceType) {
038                    PortalWebResources portalWebResources = getPortalWebResources(
039                            resourceType);
040    
041                    return portalWebResources.getContextPath();
042            }
043    
044            public static long getLastModified(String resourceType) {
045                    PortalWebResources portalWebResources = getPortalWebResources(
046                            resourceType);
047    
048                    return portalWebResources.getLastModified();
049            }
050    
051            public static String getPathResourceType(String path) {
052                    for (PortalWebResources portalWebResources :
053                                    _instance._getPortalWebResourcesList()) {
054    
055                            if (path.contains(portalWebResources.getContextPath())) {
056                                    return portalWebResources.getResourceType();
057                            }
058                    }
059    
060                    return null;
061            }
062    
063            public static ServletContext getPathServletContext(String path) {
064                    for (PortalWebResources portalWebResources :
065                                    _instance._getPortalWebResourcesList()) {
066    
067                            ServletContext servletContext =
068                                    portalWebResources.getServletContext();
069    
070                            URL url = getResource(servletContext, path);
071    
072                            if (url != null) {
073                                    return servletContext;
074                            }
075                    }
076    
077                    return null;
078            }
079    
080            public static PortalWebResources getPortalWebResources(
081                    String resourceType) {
082    
083                    for (PortalWebResources portalWebResources :
084                                    _instance._getPortalWebResourcesList()) {
085    
086                            if (resourceType.equals(portalWebResources.getResourceType())) {
087                                    return portalWebResources;
088                            }
089                    }
090    
091                    return null;
092            }
093    
094            public static URL getResource(ServletContext servletContext, String path) {
095                    path = stripContextPath(servletContext, path);
096    
097                    try {
098                            URL url = servletContext.getResource(path);
099    
100                            if (url != null) {
101                                    return url;
102                            }
103                    }
104                    catch (MalformedURLException murle) {
105                    }
106    
107                    return null;
108            }
109    
110            public static URL getResource(String path) {
111                    ServletContext servletContext = getPathServletContext(path);
112    
113                    if (servletContext != null) {
114                            return getResource(servletContext, path);
115                    }
116    
117                    return null;
118            }
119    
120            public static ServletContext getServletContext(String resourceType) {
121                    PortalWebResources portalWebResources = getPortalWebResources(
122                            resourceType);
123    
124                    return portalWebResources.getServletContext();
125            }
126    
127            public static boolean hasContextPath(String requestURI) {
128                    for (PortalWebResources portalWebResources :
129                                    _instance._getPortalWebResourcesList()) {
130    
131                            if (requestURI.startsWith(portalWebResources.getContextPath())) {
132                                    return true;
133                            }
134                    }
135    
136                    return false;
137            }
138    
139            public static boolean isAvailable(String path) {
140                    URL url = getResource(path);
141    
142                    if (url != null) {
143                            return true;
144                    }
145    
146                    return false;
147            }
148    
149            public static String stripContextPath(
150                    ServletContext servletContext, String path) {
151    
152                    String contextPath = servletContext.getContextPath();
153    
154                    if (path.startsWith(contextPath)) {
155                            path = path.substring(contextPath.length());
156                    }
157    
158                    return path;
159            }
160    
161            private PortalWebResourcesUtil() {
162                    Registry registry = RegistryUtil.getRegistry();
163    
164                    _serviceTracker = registry.trackServices(
165                            PortalWebResources.class,
166                            new PortalWebResourcesServiceTrackerCustomizer());
167    
168                    _serviceTracker.open();
169            }
170    
171            private Collection<PortalWebResources> _getPortalWebResourcesList() {
172                    return _portalWebResourcesMap.values();
173            }
174    
175            private static final PortalWebResourcesUtil _instance =
176                    new PortalWebResourcesUtil();
177    
178            private final Map<ServiceReference<PortalWebResources>, PortalWebResources>
179                    _portalWebResourcesMap = new ConcurrentHashMap<>();
180            private final ServiceTracker<PortalWebResources, PortalWebResources>
181                    _serviceTracker;
182    
183            private class PortalWebResourcesServiceTrackerCustomizer
184                    implements ServiceTrackerCustomizer
185                            <PortalWebResources, PortalWebResources> {
186    
187                    @Override
188                    public PortalWebResources addingService(
189                            ServiceReference<PortalWebResources> serviceReference) {
190    
191                            Registry registry = RegistryUtil.getRegistry();
192    
193                            PortalWebResources portalWebResources = registry.getService(
194                                    serviceReference);
195    
196                            _portalWebResourcesMap.put(serviceReference, portalWebResources);
197    
198                            return portalWebResources;
199                    }
200    
201                    @Override
202                    public void modifiedService(
203                            ServiceReference<PortalWebResources> serviceReference,
204                            PortalWebResources portalWebResources) {
205                    }
206    
207                    @Override
208                    public void removedService(
209                            ServiceReference<PortalWebResources> serviceReference,
210                            PortalWebResources portalWebResources) {
211    
212                            Registry registry = RegistryUtil.getRegistry();
213    
214                            registry.ungetService(serviceReference);
215    
216                            _portalWebResourcesMap.remove(serviceReference);
217                    }
218    
219            }
220    
221    }