001    /**
002     * Copyright (c) 2000-2013 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.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 Raymond Aug??
033     * @author Eduardo Lundgren
034     */
035    public class ServletAggregateContext extends BaseAggregateContext {
036    
037            public ServletAggregateContext(
038                            ServletContext servletContext, String resourcePath)
039                    throws IOException {
040    
041                    _servletContext = servletContext;
042    
043                    String rootPath = ServletContextUtil.getRootPath(_servletContext);
044    
045                    int pos = resourcePath.lastIndexOf(StringPool.SLASH);
046    
047                    if (pos > 0) {
048                            resourcePath = resourcePath.substring(0, resourcePath.length() - 1);
049                    }
050    
051                    pos = resourcePath.lastIndexOf(StringPool.SLASH);
052    
053                    resourcePath = resourcePath.substring(0, pos);
054    
055                    pos = resourcePath.lastIndexOf(rootPath);
056    
057                    if (pos == 0) {
058                            resourcePath = resourcePath.substring(rootPath.length());
059                    }
060    
061                    pushPath(resourcePath);
062            }
063    
064            @Override
065            public String getContent(String path) {
066                    try {
067                            String fullPath = getFullPath(StringPool.BLANK);
068    
069                            URL resourceURL = null;
070    
071                            if (Validator.isUrl(path)) {
072                                    resourceURL = new URL(path);
073                            }
074                            else {
075                                    resourceURL = _servletContext.getResource(
076                                            fullPath.concat(path));
077                            }
078    
079                            if (resourceURL == null) {
080                                    return null;
081                            }
082    
083                            URLConnection urlConnection = resourceURL.openConnection();
084    
085                            return StringUtil.read(urlConnection.getInputStream());
086                    }
087                    catch (IOException ioe) {
088                            _log.error(ioe, ioe);
089                    }
090    
091                    return null;
092            }
093    
094            private static Log _log = LogFactoryUtil.getLog(
095                    ServletAggregateContext.class);
096    
097            private ServletContext _servletContext;
098    
099    }