001    /**
002     * Copyright (c) 2000-2011 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.dynamiccss;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.scripting.ScriptingException;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.SystemProperties;
028    import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.util.WebKeys;
031    import com.liferay.portal.model.PortletConstants;
032    import com.liferay.portal.model.Theme;
033    import com.liferay.portal.scripting.ruby.RubyExecutor;
034    import com.liferay.portal.service.ThemeLocalServiceUtil;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.util.ContentUtil;
038    
039    import java.util.HashMap;
040    import java.util.Map;
041    import java.util.regex.Matcher;
042    import java.util.regex.Pattern;
043    
044    import javax.servlet.http.HttpServletRequest;
045    
046    /**
047     * @author Raymond Augé
048     * @author Sergio Sánchez
049     */
050    public class DynamicCSSUtil {
051    
052            public static void init() {
053                    try {
054                            _rubyScript = ContentUtil.get(
055                                    PortalClassLoaderUtil.getClassLoader(),
056                                    "com/liferay/portal/servlet/filters/dynamiccss/main.rb");
057                    }
058                    catch (Exception e) {
059                            _log.error(e, e);
060                    }
061            }
062    
063            public static String parseSass(
064                            HttpServletRequest request, String cssRealPath, String content)
065                    throws ScriptingException {
066    
067                    String cssThemePath = getCssThemePath(request, cssRealPath);
068    
069                    if (!DynamicCSSFilter.ENABLED || (cssThemePath == null)) {
070                            return content;
071                    }
072    
073                    Map<String, Object> inputObjects = new HashMap<String, Object>();
074    
075                    inputObjects.put("content", content);
076                    inputObjects.put("cssRealPath", cssRealPath);
077                    inputObjects.put("cssThemePath", cssThemePath);
078                    inputObjects.put("sassCachePath", _SASS_DIR);
079    
080                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
081                            new UnsyncByteArrayOutputStream();
082    
083                    UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(
084                            unsyncByteArrayOutputStream);
085    
086                    inputObjects.put("out", unsyncPrintWriter);
087    
088                    _rubyExecutor.eval(null, inputObjects, null, _rubyScript);
089    
090                    unsyncPrintWriter.flush();
091    
092                    String parsedContent = unsyncByteArrayOutputStream.toString();
093    
094                    if (Validator.isNotNull(parsedContent)) {
095                            return parsedContent;
096                    }
097    
098                    return content;
099            }
100    
101            protected static String getCssThemePath(
102                    HttpServletRequest request, String cssRealPath) {
103    
104                    if (request == null) {
105                            return null;
106                    }
107    
108                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
109                            WebKeys.THEME_DISPLAY);
110    
111                    if (themeDisplay != null) {
112                            return themeDisplay.getPathThemeCss();
113                    }
114    
115                    long companyId = PortalUtil.getCompanyId(request);
116    
117                    String themeId = ParamUtil.getString(request, "themeId");
118    
119                    Matcher portalThemeMatcher = _portalThemePattern.matcher(cssRealPath);
120    
121                    if (portalThemeMatcher.find()) {
122                            String themePathId = portalThemeMatcher.group(1);
123    
124                            themePathId = StringUtil.replace(
125                                    themePathId, StringPool.UNDERLINE, StringPool.BLANK);
126    
127                            themeId = PortalUtil.getJsSafePortletId(themePathId);
128                    }
129                    else {
130                            Matcher pluginThemeMatcher = _pluginThemePattern.matcher(
131                                    cssRealPath);
132    
133                            if (pluginThemeMatcher.find()) {
134                                    String themePathId = pluginThemeMatcher.group(1);
135    
136                                    themePathId = StringUtil.replace(
137                                            themePathId, StringPool.UNDERLINE, StringPool.BLANK);
138    
139                                    StringBundler sb = new StringBundler(4);
140    
141                                    sb.append(themePathId);
142                                    sb.append(PortletConstants.WAR_SEPARATOR);
143                                    sb.append(themePathId);
144                                    sb.append("theme");
145    
146                                    themePathId = sb.toString();
147    
148                                    themeId = PortalUtil.getJsSafePortletId(themePathId);
149                            }
150                    }
151    
152                    if (Validator.isNull(themeId)) {
153                            return null;
154                    }
155    
156                    try {
157                            Theme theme = ThemeLocalServiceUtil.getTheme(
158                                    companyId, themeId, false);
159    
160                            String themeStaticResourcePath = theme.getStaticResourcePath();
161    
162                            return themeStaticResourcePath.concat(theme.getCssPath());
163                    }
164                    catch (Exception e) {
165                            _log.error(e, e);
166                    }
167    
168                    return null;
169            }
170    
171            private static final String _SASS_DIR =
172                    SystemProperties.get(SystemProperties.TMP_DIR) + "/liferay/sass";
173    
174            private static Log _log = LogFactoryUtil.getLog(DynamicCSSUtil.class);
175    
176            private static Pattern _pluginThemePattern =
177                    Pattern.compile("\\/([^\\/]+)-theme\\/", Pattern.CASE_INSENSITIVE);
178            private static Pattern _portalThemePattern =
179                    Pattern.compile("themes\\/([^\\/]+)\\/css", Pattern.CASE_INSENSITIVE);
180            private static RubyExecutor _rubyExecutor = new RubyExecutor();
181            private static String _rubyScript;
182    
183    }