Example usage for com.liferay.portal.kernel.util FileUtil appendSuffix

List of usage examples for com.liferay.portal.kernel.util FileUtil appendSuffix

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil appendSuffix.

Prototype

public static String appendSuffix(String fileName, String suffix) 

Source Link

Usage

From source file:com.liferay.frontend.css.rtl.servlet.internal.RTLServlet.java

License:Open Source License

protected URL getResourceURL(HttpServletRequest request) throws IOException {

    String path = URLDecoder.decode(RequestDispatcherUtil.getEffectivePath(request), StringPool.UTF8);

    URL url = _servletContextHelper.getResource(path);

    if (url == null) {
        return null;
    }//from  w  ww  . ja v  a2s .  c o m

    String languageId = request.getParameter("languageId");

    if ((languageId == null) || !PortalUtil.isRightToLeft(request)) {
        if (_log.isDebugEnabled()) {
            _log.debug("Skip because specified language " + languageId + " is not right to left");
        }

        return url;
    }

    String rtlPath = FileUtil.appendSuffix(path, "_rtl");

    URL rtlURL = _servletContextHelper.getResource(rtlPath);

    if (rtlURL != null) {
        return rtlURL;
    }

    File dataFile = _bundle.getDataFile(rtlPath);

    if (dataFile.exists() && (dataFile.lastModified() > url.openConnection().getLastModified())) {

        URI uri = dataFile.toURI();

        return uri.toURL();
    }

    CSSRTLConverter cssRTLConverter = new CSSRTLConverter(false);

    String rtl = cssRTLConverter.process(StringUtil.read(url.openStream()));

    InputStream inputStream = new ByteArrayInputStream(rtl.getBytes(StringPool.UTF8));

    OutputStream outputStream = null;

    try {
        dataFile.getParentFile().mkdirs();

        dataFile.createNewFile();

        outputStream = new FileOutputStream(dataFile);

        StreamUtil.transfer(inputStream, outputStream, false);
    } catch (IOException ioe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to cache RTL CSS", ioe);
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }

    inputStream.reset();

    URI uri = dataFile.toURI();

    return uri.toURL();
}