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.taglib;
016    
017    import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
018    import com.liferay.portal.kernel.servlet.ServletContextPool;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.util.PortalUtil;
022    
023    import java.net.URL;
024    
025    import java.security.AccessController;
026    import java.security.PrivilegedExceptionAction;
027    
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    import javax.servlet.ServletContext;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class FileAvailabilityUtil {
037    
038            public static void clearAvailabilities() {
039                    String servletContextName = PortalUtil.getServletContextName();
040    
041                    ServletContext servletContext = ServletContextPool.get(
042                            servletContextName);
043    
044                    Map<String, Boolean> availabilities =
045                            (Map<String, Boolean>)servletContext.getAttribute(
046                                    FileAvailabilityUtil.class.getName());
047    
048                    if (availabilities != null) {
049                            servletContext.removeAttribute(
050                                    FileAvailabilityUtil.class.getName());
051                    }
052            }
053    
054            public static boolean isAvailable(
055                    ServletContext servletContext, String path) {
056    
057                    if (Validator.isNull(path)) {
058                            return false;
059                    }
060    
061                    if (path.charAt(0) != CharPool.SLASH) {
062                            return true;
063                    }
064    
065                    Map<String, Boolean> availabilities = _getAvailabilities(
066                            servletContext);
067    
068                    Boolean available = availabilities.get(path);
069    
070                    if (available != null) {
071                            return available;
072                    }
073    
074                    URL url = null;
075    
076                    try {
077                            url = AccessController.doPrivileged(
078                                    new ResourcePrivilegedExceptionAction(servletContext, path));
079                    }
080                    catch (Exception e) {
081                    }
082    
083                    if ((url == null) && !PortalWebResourcesUtil.isAvailable(path)) {
084                            available = Boolean.FALSE;
085                    }
086                    else {
087                            available = Boolean.TRUE;
088                    }
089    
090                    availabilities.put(path, available);
091    
092                    return available;
093            }
094    
095            private static Map<String, Boolean> _getAvailabilities(
096                    ServletContext servletContext) {
097    
098                    Map<String, Boolean> availabilities =
099                            (Map<String, Boolean>)servletContext.getAttribute(
100                                    FileAvailabilityUtil.class.getName());
101    
102                    if (availabilities == null) {
103                            availabilities = new ConcurrentHashMap<>();
104    
105                            servletContext.setAttribute(
106                                    FileAvailabilityUtil.class.getName(), availabilities);
107                    }
108    
109                    return availabilities;
110            }
111    
112            private static class ResourcePrivilegedExceptionAction
113                    implements PrivilegedExceptionAction<URL> {
114    
115                    public ResourcePrivilegedExceptionAction(
116                            ServletContext servletContext, String path) {
117    
118                            _servletContext = servletContext;
119                            _path = path;
120                    }
121    
122                    @Override
123                    public URL run() throws Exception {
124                            return _servletContext.getResource(_path);
125                    }
126    
127                    private final String _path;
128                    private final ServletContext _servletContext;
129    
130            }
131    
132    }