Example usage for com.liferay.portal.kernel.servlet ServletContextUtil getRootPath

List of usage examples for com.liferay.portal.kernel.servlet ServletContextUtil getRootPath

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.servlet ServletContextUtil getRootPath.

Prototype

public static String getRootPath(ServletContext servletContext) throws MalformedURLException 

Source Link

Usage

From source file:com.liferay.rtl.servlet.filters.ComboServletFilter.java

License:Open Source License

@Override
protected void processFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws Exception {

    Set<String> modulePathsSet = new LinkedHashSet<String>();

    Enumeration<String> enu = request.getParameterNames();

    if (ServerDetector.isWebSphere()) {
        Map<String, String[]> parameterMap = HttpUtil.getParameterMap(request.getQueryString());

        enu = Collections.enumeration(parameterMap.keySet());
    }//from  w  w  w.j a  v a 2s  .  c om

    while (enu.hasMoreElements()) {
        String name = enu.nextElement();

        if (_protectedParameters.contains(name)) {
            continue;
        }

        modulePathsSet.add(name);
    }

    if (modulePathsSet.size() == 0) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Modules paths set is empty");

        return;
    }

    String[] modulePaths = modulePathsSet.toArray(new String[modulePathsSet.size()]);

    String firstModulePath = modulePaths[0];

    String extension = FileUtil.getExtension(firstModulePath);

    String minifierType = ParamUtil.getString(request, "minifierType");

    if (Validator.isNull(minifierType)) {
        minifierType = "js";

        if (StringUtil.equalsIgnoreCase(extension, _CSS_EXTENSION)) {
            minifierType = "css";
        }
    }

    if (!minifierType.equals("css") && !minifierType.equals("js")) {
        minifierType = "js";
    }

    String modulePathsString = null;

    byte[][] bytesArray = null;

    if (!PropsValues.COMBO_CHECK_TIMESTAMP) {
        modulePathsString = Arrays.toString(modulePaths);

        if (minifierType.equals("css") && DynamicCSSUtil.isRightToLeft(request)) {

            modulePathsString += ".rtl";
        }

        bytesArray = _bytesArrayPortalCache.get(modulePathsString);
    }

    if (bytesArray == null) {
        String rootPath = ServletContextUtil.getRootPath(_servletContext);

        bytesArray = new byte[modulePaths.length][];

        for (int i = 0; i < modulePaths.length; i++) {
            String modulePath = modulePaths[i];

            if (!validateModuleExtension(modulePath)) {
                response.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);

                return;
            }

            byte[] bytes = new byte[0];

            if (Validator.isNotNull(modulePath)) {
                modulePath = StringUtil.replaceFirst(modulePath, PortalUtil.getPathContext(), StringPool.BLANK);

                URL url = getResourceURL(_servletContext, rootPath, modulePath);

                if (url == null) {
                    response.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);

                    return;
                }

                bytes = getResourceContent(request, response, url, modulePath, minifierType);
            }

            bytesArray[i] = bytes;
        }

        if ((modulePathsString != null) && !PropsValues.COMBO_CHECK_TIMESTAMP) {

            _bytesArrayPortalCache.put(modulePathsString, bytesArray);
        }
    }

    String contentType = ContentTypes.TEXT_JAVASCRIPT;

    if (StringUtil.equalsIgnoreCase(extension, _CSS_EXTENSION)) {
        contentType = ContentTypes.TEXT_CSS;
    }

    response.setContentType(contentType);

    ServletResponseUtil.write(response, bytesArray);
}