Example usage for com.liferay.portal.kernel.servlet HttpHeaders CACHE_CONTROL_NO_CACHE_VALUE

List of usage examples for com.liferay.portal.kernel.servlet HttpHeaders CACHE_CONTROL_NO_CACHE_VALUE

Introduction

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

Prototype

String CACHE_CONTROL_NO_CACHE_VALUE

To view the source code for com.liferay.portal.kernel.servlet HttpHeaders CACHE_CONTROL_NO_CACHE_VALUE.

Click Source Link

Usage

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

License:Open Source License

protected byte[] getResourceContent(HttpServletRequest request, HttpServletResponse response, URL resourceURL,
        String resourcePath, String minifierType) throws IOException {

    String fileContentKey = resourcePath.concat(StringPool.QUESTION).concat(minifierType);

    FileContentBag fileContentBag = _fileContentBagPortalCache.get(fileContentKey);

    if ((fileContentBag != null) && !PropsValues.COMBO_CHECK_TIMESTAMP) {
        return fileContentBag._fileContent;
    }//from   w ww.  jav  a  2  s  . c om

    URLConnection urlConnection = null;

    if (resourceURL != null) {
        urlConnection = resourceURL.openConnection();
    }

    if ((fileContentBag != null) && PropsValues.COMBO_CHECK_TIMESTAMP) {
        long elapsedTime = System.currentTimeMillis() - fileContentBag._lastModified;

        if ((urlConnection != null) && (elapsedTime <= PropsValues.COMBO_CHECK_TIMESTAMP_INTERVAL)
                && (urlConnection.getLastModified() == fileContentBag._lastModified)) {

            return fileContentBag._fileContent;
        }

        _fileContentBagPortalCache.remove(fileContentKey);
    }

    if (resourceURL == null) {
        fileContentBag = _EMPTY_FILE_CONTENT_BAG;
    } else {
        String stringFileContent = StringUtil.read(urlConnection.getInputStream());

        if (!StringUtil.endsWith(resourcePath, _CSS_MINIFIED_SUFFIX)
                && !StringUtil.endsWith(resourcePath, _JAVASCRIPT_MINIFIED_SUFFIX)) {

            if (minifierType.equals("css")) {
                try {
                    stringFileContent = DynamicCSSUtil.parseSass(_servletContext, request, resourcePath,
                            stringFileContent);
                } catch (Exception e) {
                    _log.error("Unable to parse SASS on CSS " + resourceURL.getPath(), e);

                    if (_log.isDebugEnabled()) {
                        _log.debug(stringFileContent);
                    }

                    response.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
                }

                String baseURL = StringPool.BLANK;

                int index = resourcePath.lastIndexOf(CharPool.SLASH);

                if (index != -1) {
                    baseURL = resourcePath.substring(0, index + 1);
                }

                stringFileContent = AggregateUtil.updateRelativeURLs(stringFileContent, baseURL);

                stringFileContent = MinifierUtil.minifyCss(stringFileContent);
            } else if (minifierType.equals("js")) {
                stringFileContent = MinifierUtil.minifyJavaScript(stringFileContent);
            }
        }

        fileContentBag = new FileContentBag(stringFileContent.getBytes(StringPool.UTF8),
                urlConnection.getLastModified());
    }

    if (PropsValues.COMBO_CHECK_TIMESTAMP) {
        int timeToLive = (int) (PropsValues.COMBO_CHECK_TIMESTAMP_INTERVAL / Time.SECOND);

        _fileContentBagPortalCache.put(fileContentKey, fileContentBag, timeToLive);
    }

    return fileContentBag._fileContent;
}

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   www  . ja  v a 2 s  .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);
}

From source file:com.liferay.rtl.servlet.filters.dynamiccss.DynamicCSSFilter.java

License:Open Source License

protected Object getDynamicContent(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws Exception {

    String requestURI = request.getRequestURI();

    String requestPath = requestURI;

    String contextPath = request.getContextPath();

    if (!contextPath.equals(StringPool.SLASH)) {
        requestPath = requestPath.substring(contextPath.length());
    }//w ww  .  j  a  va2 s  .co m

    URL resourceURL = _servletContext.getResource(requestPath);

    if (resourceURL == null) {
        return null;
    }

    URLConnection urlConnection = resourceURL.openConnection();

    String cacheCommonFileName = getCacheFileName(request);

    File cacheContentTypeFile = new File(_tempDir, cacheCommonFileName + "_E_CONTENT_TYPE");
    File cacheDataFile = new File(_tempDir, cacheCommonFileName + "_E_DATA");

    if (cacheDataFile.exists() && (cacheDataFile.lastModified() >= urlConnection.getLastModified())) {

        if (cacheContentTypeFile.exists()) {
            String contentType = FileUtil.read(cacheContentTypeFile);

            response.setContentType(contentType);
        }

        return cacheDataFile;
    }

    String dynamicContent = null;

    String content = null;

    try {
        if (requestPath.endsWith(_CSS_EXTENSION)) {
            if (_log.isInfoEnabled()) {
                _log.info("Parsing SASS on CSS " + requestPath);
            }

            content = StringUtil.read(urlConnection.getInputStream());

            dynamicContent = DynamicCSSUtil.parseSass(_servletContext, request, requestPath, content);

            response.setContentType(ContentTypes.TEXT_CSS);

            FileUtil.write(cacheContentTypeFile, ContentTypes.TEXT_CSS);
        } else if (requestPath.endsWith(_JSP_EXTENSION)) {
            if (_log.isInfoEnabled()) {
                _log.info("Parsing SASS on JSP or servlet " + requestPath);
            }

            BufferCacheServletResponse bufferCacheServletResponse = new BufferCacheServletResponse(response);

            processFilter(DynamicCSSFilter.class, request, bufferCacheServletResponse, filterChain);

            bufferCacheServletResponse.finishResponse();

            content = bufferCacheServletResponse.getString();

            dynamicContent = DynamicCSSUtil.parseSass(_servletContext, request, requestPath, content);

            FileUtil.write(cacheContentTypeFile, bufferCacheServletResponse.getContentType());
        } else {
            return null;
        }
    } catch (Exception e) {
        _log.error("Unable to parse SASS on CSS " + requestPath, e);

        if (_log.isDebugEnabled()) {
            _log.debug(content);
        }

        response.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
    }

    if (dynamicContent != null) {
        FileUtil.write(cacheDataFile, dynamicContent);
    } else {
        dynamicContent = content;
    }

    return dynamicContent;
}

From source file:com.liferay.wsrp.consumer.portlet.ConsumerPortlet.java

License:Open Source License

protected void processResourceResponse(ResourceRequest resourceRequest, ResourceResponse resourceResponse,
        WSRPConsumerManager wsrpConsumerManager, ServiceHolder serviceHolder,
        oasis.names.tc.wsrp.v2.types.ResourceResponse wsrpResourceResponse) throws Exception {

    PortletSession portletSession = resourceRequest.getPortletSession();

    PortletContext portletContext = wsrpResourceResponse.getPortletContext();

    if (portletContext != null) {
        portletSession.setAttribute(WebKeys.PORTLET_CONTEXT, portletContext);
    }/*from  w w w . j  a  v a  2s.c  o  m*/

    SessionContext sessionContext = wsrpResourceResponse.getSessionContext();

    updateSessionContext(portletSession, serviceHolder, sessionContext);

    ResourceContext resourceContext = wsrpResourceResponse.getResourceContext();

    CacheControl cacheControl = resourceContext.getCacheControl();

    if (cacheControl != null) {
        if (cacheControl.getExpires() == 0) {
            resourceResponse.setProperty(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
        } else if (cacheControl.getExpires() > 0) {
            resourceResponse.setProperty(HttpHeaders.CACHE_CONTROL, "max-age=" + cacheControl.getExpires());
        } else {
            resourceResponse.setProperty(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_DEFAULT_VALUE);
        }
    }

    NamedString[] clientAttributes = resourceContext.getClientAttributes();

    if (clientAttributes != null) {
        for (NamedString clientAttribute : clientAttributes) {
            String name = clientAttribute.getName();
            String value = clientAttribute.getValue();

            if (StringUtil.equalsIgnoreCase(name, HttpHeaders.CONTENT_DISPOSITION)) {

                resourceResponse.setProperty(HttpHeaders.CONTENT_DISPOSITION, value);

                break;
            }
        }
    }

    processMimeResponse(resourceRequest, resourceResponse, resourceContext);
}

From source file:com.liferay.wsrp.servlet.ProxyServlet.java

License:Open Source License

protected void proxyURL(HttpServletRequest request, HttpServletResponse response, URL url) throws Exception {

    URLConnection urlConnection = url.openConnection();

    urlConnection.setIfModifiedSince(request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE));

    HttpSession session = request.getSession();

    String cookie = (String) session.getAttribute(WebKeys.COOKIE);

    if (Validator.isNotNull(cookie)) {
        urlConnection.setRequestProperty(HttpHeaders.COOKIE, cookie);
    }/* ww w .  j a  v  a 2  s . c  om*/

    boolean useCaches = true;

    Enumeration<String> enumeration = request.getHeaderNames();

    while (enumeration.hasMoreElements()) {
        String headerName = enumeration.nextElement();

        if (StringUtil.equalsIgnoreCase(headerName, HttpHeaders.COOKIE)
                || StringUtil.equalsIgnoreCase(headerName, HttpHeaders.IF_MODIFIED_SINCE)) {

            continue;
        }

        String headerValue = request.getHeader(headerName);

        if (Validator.isNotNull(headerValue)) {
            if (StringUtil.equalsIgnoreCase(headerName, HttpHeaders.CACHE_CONTROL)
                    && headerValue.contains(HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE)) {

                useCaches = false;
            }

            urlConnection.setRequestProperty(headerName, headerValue);
        }
    }

    urlConnection.setUseCaches(useCaches);

    urlConnection.connect();

    response.setContentLength(urlConnection.getContentLength());
    response.setContentType(urlConnection.getContentType());

    Map<String, List<String>> headers = urlConnection.getHeaderFields();

    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String headerName = entry.getKey();

        if (Validator.isNotNull(headerName) && !response.containsHeader(headerName)) {

            response.setHeader(headerName, urlConnection.getHeaderField(headerName));
        }
    }

    if (urlConnection instanceof HttpURLConnection) {
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;

        response.setStatus(httpURLConnection.getResponseCode());
    }

    ServletResponseUtil.write(response, urlConnection.getInputStream());
}

From source file:com.rivetlogic.portlet.AnnouncerTools.java

License:Open Source License

public static void returnJSONObject(PortletResponse response, JSONObject jsonObj) {
    HttpServletResponse servletResponse = PortalUtil.getHttpServletResponse(response);
    servletResponse.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
    servletResponse.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
    servletResponse.setHeader(HttpHeaders.EXPIRES, String.valueOf(AnnouncerPortlet.LR_EMPTY_VALUE));
    PrintWriter pw;/*from  w  w w.  j  a va2  s. c  o  m*/
    try {
        pw = servletResponse.getWriter();
        pw.write(jsonObj.toString());
        pw.close();
    } catch (IOException e) {
        LOG.error("Error while returning json", e);
    }
}

From source file:com.rivetlogic.skype.util.SkypeUtil.java

License:Open Source License

public static void returnJSONObject(PortletResponse response, JSONObject jsonObj) {
    HttpServletResponse servletResponse = PortalUtil.getHttpServletResponse(response);
    servletResponse.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
    servletResponse.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
    servletResponse.setHeader(HttpHeaders.EXPIRES, String.valueOf(Constants.UNDEFINED_ID));
    PrintWriter pw;//  ww w.ja  va2s .c o  m
    try {
        pw = servletResponse.getWriter();
        pw.write(jsonObj.toString());
        pw.close();
    } catch (IOException e) {
        LOG.error("Error while returning json", e);
    }
}