001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.FileUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.io.IOException;
023    
024    /**
025     * @author Raymond Aug??
026     * @author Eduardo Lundgren
027     */
028    public class FileAggregateContext extends BaseAggregateContext {
029    
030            public FileAggregateContext(String docrootPath, String resourcePath) {
031                    int pos = resourcePath.lastIndexOf(StringPool.SLASH);
032    
033                    if (pos > -1) {
034                            resourcePath = resourcePath.substring(0, pos + 1);
035                    }
036    
037                    pushPath(docrootPath);
038                    pushPath(resourcePath);
039            }
040    
041            @Override
042            public String getContent(String path) {
043                    try {
044                            pushPath(path);
045    
046                            String fullPath = getFullPath(StringPool.BLANK);
047    
048                            popPath();
049    
050                            return FileUtil.read(fullPath);
051                    }
052                    catch (IOException ioe) {
053                            _log.error(ioe, ioe);
054                    }
055    
056                    return null;
057            }
058    
059            @Override
060            public String getResourcePath(String path) {
061                    String docrootPath = shiftPath();
062    
063                    String fullPath = getFullPath(StringPool.BLANK);
064    
065                    unshiftPath(docrootPath);
066    
067                    return fullPath.concat(path);
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(FileAggregateContext.class);
071    
072    }