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.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    }