com.adobe.acs.commons.wcm.notifications.impl.SystemNotificationsImpl Maven / Gradle / Ivy
Show all versions of acs-aem-commons-bundle Show documentation
/*
* ACS AEM Commons
*
* Copyright (C) 2013 - 2023 Adobe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adobe.acs.commons.wcm.notifications.impl;
import static com.day.cq.wcm.api.NameConstants.NT_PAGE;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.AbstractResourceVisitor;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.observation.ExternalResourceChangeListener;
import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChangeListener;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.acs.commons.http.injectors.AbstractHtmlRequestInjector;
import com.adobe.acs.commons.util.CookieUtil;
import com.adobe.acs.commons.wcm.notifications.SystemNotifications;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
@Component(immediate = true)
@Service(value = SystemNotifications.class)
public class SystemNotificationsImpl extends AbstractHtmlRequestInjector implements SystemNotifications, ResourceChangeListener, ExternalResourceChangeListener {
private static final Logger log = LoggerFactory.getLogger(SystemNotificationsImpl.class);
public static final String COOKIE_NAME = "acs-commons-system-notifications";
private static final String PATH_NOTIFICATIONS = "/etc/acs-commons/notifications";
private static final String PN_ON_TIME = "onTime";
private static final String PN_OFF_TIME = "offTime";
private static final String PN_ENABLED = "enabled";
private static final String INJECT_TEXT =
"";
private static final String SERVICE_NAME = "system-notifications";
private static final Map AUTH_INFO;
static {
AUTH_INFO = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_NAME);
}
private AtomicBoolean isFilter = new AtomicBoolean(false);
private ComponentContext osgiComponentContext;
private ServiceRegistration eventHandlerRegistration;
@Reference
private ResourceResolverFactory resourceResolverFactory;
@Reference
private SlingSettingsService slingSettings;
@Override
protected void inject(HttpServletRequest servletRequest, HttpServletResponse servletResponse, PrintWriter printWriter) {
final String jquerySrc = servletRequest.getContextPath()
+ "/etc/clientlibs/granite/jquery.js";
final String notificationsSrc = servletRequest.getContextPath()
+ "/apps/acs-commons/components/utilities/system-notifications/notification/clientlibs.js";
printWriter.println(String.format(INJECT_TEXT, jquerySrc, notificationsSrc));
}
@Override
protected boolean accepts(final ServletRequest servletRequest,
final ServletResponse servletResponse) {
if (!(servletRequest instanceof SlingHttpServletRequest)
|| !(servletResponse instanceof SlingHttpServletResponse)) {
return false;
}
final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest;
if (StringUtils.startsWith(slingRequest.getResource().getPath(), PATH_NOTIFICATIONS)) {
// Do NOT inject on the notifications Authoring pages
return false;
}
final Resource notificationsFolder = slingRequest.getResourceResolver().getResource(PATH_NOTIFICATIONS);
if (notificationsFolder == null || this.getNotifications(slingRequest, notificationsFolder).size() < 1) {
// If no notifications folder or no active notifications; do not inject JS
return false;
}
return super.accepts(servletRequest, servletResponse);
}
@Override
protected int getInjectIndex(String originalContents) {
// Inject immediately before the ending body tag
return StringUtils.indexOf(originalContents, "