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