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.filters.aggregate;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.io.IOException;
025    
026    import java.net.URL;
027    import java.net.URLConnection;
028    
029    import javax.servlet.ServletContext;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class ServletPaths {
035    
036            public static String getParentPath(String resourcePath) {
037                    if (Validator.isNull(resourcePath)) {
038                            throw new IllegalArgumentException("Resource path is null");
039                    }
040    
041                    if (resourcePath.charAt(resourcePath.length() - 1) == CharPool.SLASH) {
042                            resourcePath = resourcePath.substring(0, resourcePath.length() - 1);
043                    }
044    
045                    int pos = resourcePath.lastIndexOf(CharPool.SLASH);
046    
047                    if (pos != -1) {
048                            resourcePath = resourcePath.substring(0, pos);
049                    }
050    
051                    return resourcePath;
052            }
053    
054            public ServletPaths(ServletContext servletContext, String resourcePath) {
055                    if (servletContext == null) {
056                            throw new NullPointerException("Servlet context is null");
057                    }
058    
059                    if (Validator.isNull(resourcePath)) {
060                            throw new IllegalArgumentException("Resource path is null");
061                    }
062    
063                    _servletContext = servletContext;
064                    _resourcePath = resourcePath;
065            }
066    
067            public ServletPaths down(String path) {
068                    String normalizedPath = _normalizePath(path);
069    
070                    if (normalizedPath.isEmpty()) {
071                            return this;
072                    }
073    
074                    return new ServletPaths(
075                            _resourcePath.concat(normalizedPath), _servletContext);
076            }
077    
078            public String getContent() {
079                    try {
080                            URL resourceURL = _servletContext.getResource(_resourcePath);
081    
082                            if (resourceURL == null) {
083                                    return null;
084                            }
085    
086                            URLConnection urlConnection = resourceURL.openConnection();
087    
088                            return StringUtil.read(urlConnection.getInputStream());
089                    }
090                    catch (IOException ioe) {
091                            _log.error(ioe, ioe);
092                    }
093    
094                    return null;
095            }
096    
097            public String getResourcePath() {
098                    return _resourcePath;
099            }
100    
101            private ServletPaths(String resourcePath, ServletContext servletContext) {
102                    _resourcePath = resourcePath;
103                    _servletContext = servletContext;
104            }
105    
106            private String _normalizePath(String path) {
107                    if (Validator.isNull(path) || StringPool.SLASH.equals(path)) {
108                            return StringPool.BLANK;
109                    }
110    
111                    if (path.charAt(path.length() - 1) == CharPool.SLASH) {
112                            path = path.substring(0, path.length() - 1);
113                    }
114    
115                    if ((path.charAt(0) != CharPool.SLASH) &&
116                            (_resourcePath.charAt(_resourcePath.length() - 1) !=
117                                    CharPool.SLASH)) {
118    
119                            path = StringPool.SLASH.concat(path);
120                    }
121    
122                    return path;
123            }
124    
125            private static final Log _log = LogFactoryUtil.getLog(ServletPaths.class);
126    
127            private final String _resourcePath;
128            private final ServletContext _servletContext;
129    
130    }