use of com.adobe.acs.commons.util.BufferingResponse in project acs-aem-commons by Adobe-Consulting-Services.
the class CQIncludePropertyNamespaceServlet method doGet.
// @formatter:on
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
if (!this.accepts(request)) {
response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
response.getWriter().write(new JSONObject().toString());
}
/* Servlet accepts this request */
RequestUtil.setRequestAttribute(request, REQ_ATTR, true);
final String namespace = URLDecoder.decode(PathInfoUtil.getSelector(request, NAME_PROPERTY_SELECTOR_INDEX), "UTF-8");
final RequestDispatcherOptions options = new RequestDispatcherOptions();
options.setReplaceSelectors(AEM_CQ_INCLUDE_SELECTORS);
final BufferingResponse bufferingResponse = new BufferingResponse(response);
request.getRequestDispatcher(request.getResource(), options).forward(request, bufferingResponse);
try {
final JSONObject json = new JSONObject(bufferingResponse.getContents());
final PropertyNamespaceUpdater propertyNamespaceUpdater = new PropertyNamespaceUpdater(namespace);
propertyNamespaceUpdater.accept(json);
response.getWriter().write(json.toString());
} catch (JSONException e) {
log.error("Error composing the cqinclude JSON representation of the widget overlay for [ {} ]", request.getRequestURI(), e);
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(new JSONObject().toString());
}
}
use of com.adobe.acs.commons.util.BufferingResponse in project acs-aem-commons by Adobe-Consulting-Services.
the class QuicklyFilter method doFilter.
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) servletRequest;
final HttpServletResponse response = (HttpServletResponse) servletResponse;
if (!accepts(request)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
final BufferingResponse capturedResponse = new BufferingResponse(response);
filterChain.doFilter(request, capturedResponse);
// Get contents
final String contents = capturedResponse.getContents();
if (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));
printWriter.write(appHTML);
printWriter.write(contents.substring(bodyIndex));
return;
}
}
if (contents != null) {
response.getWriter().write(contents);
}
}
use of com.adobe.acs.commons.util.BufferingResponse in project acs-aem-commons by Adobe-Consulting-Services.
the class AbstractHtmlRequestInjector method doFilter.
@Override
public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
if (!this.accepts(servletRequest, servletResponse)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
// We know these are HTTP Servlet Requests since accepts passed
final HttpServletRequest request = (HttpServletRequest) servletRequest;
final HttpServletResponse response = (HttpServletResponse) servletResponse;
// Prepare to capture the original response
final BufferingResponse originalResponse = new BufferingResponse(response);
// Process and capture the original response
filterChain.doFilter(request, originalResponse);
// Get contents
final String originalContents = originalResponse.getContents();
if (originalContents != null && StringUtils.contains(response.getContentType(), "html")) {
final int injectionIndex = getInjectIndex(originalContents);
if (injectionIndex != -1) {
final PrintWriter printWriter = response.getWriter();
// Write all content up to the injection index
printWriter.write(originalContents.substring(0, injectionIndex));
// Inject the contents; Pass the request/response - consumer can use as needed
inject(request, response, printWriter);
// Write all content after the injection index
printWriter.write(originalContents.substring(injectionIndex));
return;
}
}
if (originalContents != null) {
response.getWriter().write(originalContents);
}
}
use of com.adobe.acs.commons.util.BufferingResponse 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);
}
}
Aggregations