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