Search in sources :

Example 1 with WCMMode

use of com.day.cq.wcm.api.WCMMode in project acs-aem-commons by Adobe-Consulting-Services.

the class WCMViewsFilter method getOrSetFirstWCMMode.

/**
 * Gets or sets and gets the original WCMMode for the Request.
 *
 * @param request the Request
 * @return the original WCMMode for the Request
 */
private WCMMode getOrSetFirstWCMMode(final SlingHttpServletRequest request) {
    WCMMode wcmMode = (WCMMode) request.getAttribute(ATTR_FILTER);
    if (wcmMode == null) {
        wcmMode = WCMMode.fromRequest(request);
        request.setAttribute(ATTR_FILTER, wcmMode);
    }
    return wcmMode;
}
Also used : WCMMode(com.day.cq.wcm.api.WCMMode)

Example 2 with WCMMode

use of com.day.cq.wcm.api.WCMMode in project acs-aem-commons by Adobe-Consulting-Services.

the class WCMViewsFilter method doFilter.

@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    final WCMMode requestMode = this.getOrSetFirstWCMMode(slingRequest);
    final List<String> requestViews = this.getRequestViews(slingRequest);
    final List<String> componentViews = this.getComponentViews(slingRequest);
    if (!this.accepts(slingRequest)) {
        log.trace("WCM Filters does NOT accept [ {} ]", slingRequest.getResource().getPath());
        chain.doFilter(request, response);
    } else if ((CollectionUtils.isEmpty(requestViews) && CollectionUtils.isNotEmpty(componentViews)) || (CollectionUtils.isNotEmpty(requestViews) && CollectionUtils.isEmpty(componentViews)) || (CollectionUtils.isNotEmpty(requestViews) && CollectionUtils.isNotEmpty(componentViews) && !CollectionUtils.containsAny(requestViews, componentViews))) {
        log.trace("WCMView Empty/Not Empty -- Setting WCMMode [ {} ] for [ {} ]", WCMMode.DISABLED.name(), slingRequest.getResource().getPath());
        this.processChain(slingRequest, response, chain, WCMMode.DISABLED, requestMode);
    } else if (CollectionUtils.containsAny(requestViews, componentViews)) {
        log.debug("WCMView Match -- Setting WCMMode [ {} ] for [ {} ]", requestMode.name(), slingRequest.getResource().getPath());
        this.processChain(slingRequest, response, chain, requestMode, requestMode);
    } else {
        chain.doFilter(request, response);
    }
}
Also used : WCMMode(com.day.cq.wcm.api.WCMMode) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 3 with WCMMode

use of com.day.cq.wcm.api.WCMMode in project acs-aem-commons by Adobe-Consulting-Services.

the class SetWCMMode method doStartTag.

/**
 * {@inheritDoc}
 */
@Override
public int doStartTag() throws JspException {
    final WCMMode toSet = WCMMode.valueOf(mode);
    final ServletRequest request = pageContext.getRequest();
    this.oldMode = WCMMode.fromRequest(request);
    toSet.toRequest(request);
    return EVAL_BODY_INCLUDE;
}
Also used : WCMMode(com.day.cq.wcm.api.WCMMode) ServletRequest(javax.servlet.ServletRequest)

Example 4 with WCMMode

use of com.day.cq.wcm.api.WCMMode in project acs-aem-commons by Adobe-Consulting-Services.

the class AemEnvironmentIndicatorFilter method doFilter.

@Override
@SuppressWarnings("squid:S3776")
public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
    if (!(servletRequest instanceof HttpServletRequest) || !(servletResponse instanceof HttpServletResponse)) {
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;
    if (!this.accepts(request)) {
        filterChain.doFilter(request, response);
        return;
    }
    final BufferingResponse capturedResponse = new BufferingResponse(response);
    filterChain.doFilter(request, capturedResponse);
    boolean doInclude = true;
    if (ArrayUtils.isNotEmpty(excludedWCMModes)) {
        // Test for configured WCM modes, where the indicators are not displayed
        WCMMode wcmmode = extractFromRequest(request);
        if (wcmmode != null) {
            for (String m : excludedWCMModes) {
                if (StringUtils.equalsIgnoreCase(wcmmode.name(), m)) {
                    doInclude = false;
                    break;
                }
            }
        } else {
        // No wcmmode could be extracted from the request
        }
    }
    // Get contents
    final String contents = capturedResponse.getContents();
    if (doInclude && contents != null && StringUtils.contains(response.getContentType(), "html")) {
        final int bodyIndex = contents.indexOf("</body>");
        if (bodyIndex != -1) {
            final PrintWriter printWriter = response.getWriter();
            printWriter.write(contents.substring(0, bodyIndex));
            if (StringUtils.isNotBlank(css)) {
                printWriter.write("<style>" + css + " </style>");
                printWriter.write("<div id=\"" + DIV_ID + "\">" + innerHTML + "</div>");
            }
            if (StringUtils.isNotBlank(titlePrefix)) {
                printWriter.printf(TITLE_UPDATE_SCRIPT, titlePrefix);
            }
            printWriter.write(contents.substring(bodyIndex));
            return;
        }
    }
    if (contents != null) {
        response.getWriter().write(contents);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BufferingResponse(com.adobe.acs.commons.util.BufferingResponse) WCMMode(com.day.cq.wcm.api.WCMMode) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Aggregations

WCMMode (com.day.cq.wcm.api.WCMMode)4 BufferingResponse (com.adobe.acs.commons.util.BufferingResponse)1 PrintWriter (java.io.PrintWriter)1 ServletRequest (javax.servlet.ServletRequest)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1