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