Expand my Community achievements bar.

SOLVED

when ReplicationContentFilter actually gets trigerred

Avatar

Level 4

1. i want some properties and nodes get filtered out when i click replicate prgramatically.

2. my actual requirement is when i click quick publish, I want some properties with values /conf/.... not to get published.

Below is my actual code. 

 

Filter for /bin/replicate

package com.macnicagwi.core.filters;

 

import org.apache.jackrabbit.api.security.user.Group;

import org.apache.jackrabbit.api.security.user.User;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.apache.sling.servlets.annotations.SlingServletFilter;

import org.apache.sling.servlets.annotations.SlingServletFilterScope;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import com.day.cq.replication.ReplicationAction;

import com.day.cq.replication.ReplicationActionType;

import com.day.cq.replication.ReplicationContentFilter;

import com.day.cq.replication.ReplicationContentFilterFactory;

import com.day.cq.replication.ReplicationException;

import com.day.cq.replication.Replicator;

 

import javax.jcr.RepositoryException;

import javax.jcr.Session;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import java.io.IOException;

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

 

@component

@SlingServletFilter(

scope = {SlingServletFilterScope.REQUEST},

pattern = "/bin/replicate",

methods = {"POST"}

)

public class replicationFilter implements Filter {

private static final Logger logger = LoggerFactory.getLogger(replicationFilter.class);

 

@reference

ResourceResolverFactory factory;

 

SlingHttpServletRequest slingRequest;

 

@reference Replicator replicator;

 

@Override

public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,

ServletException {

 

final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

slingRequest = (SlingHttpServletRequest) request;

String servletPath = slingRequest.getRequestPathInfo().getResourcePath();

logger.info("reached the path: {}",servletPath);

 

try {

if(isquickPublish(request) && isPublisher()) {

String[] paths = slingRequest.getParameterValues("path");

Session session = slingRequest.getResourceResolver().adaptTo(Session.class);

ReplicationContentFilterFactory replicationFilterFacotry = new ReplicationContentFilterFactoryImpl();

for(String path : paths) {

ReplicationAction replicationAction = new ReplicationAction(ReplicationActionType.ACTIVATE, path);

ReplicationContentFilter filter = replicationFilterFacotry.createFilter(replicationAction);

replicator.replicate(session, ReplicationActionType.ACTIVATE, path);

}

}

} catch (RepositoryException | ReplicationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 

chain.doFilter(request, response);

}

 

private boolean isPublisher() throws RepositoryException {

// TODO Auto-generated method stub

// ResourceResolver resourceResolver = MacnicaCoreUtils.getResourceResolver(factory, "macnicagwi-wcm-system");

ResourceResolver resourceResolver = slingRequest.getResourceResolver();

String id = resourceResolver.getUserID();

User currentUser = resourceResolver.adaptTo(User.class);

logger.info("user name is: {}",currentUser.getID());

Iterator Group groupsIt = currentUser.declaredMemberOf();

while (groupsIt.hasNext()) {

Group group = groupsIt.next();

logger.info("group is: {}",group.toString());

String groupId = group.getID();

String groupName = group.getPrincipal().getName();

if(groupName.contains("approver")) {

logger.info("user is an approver");

return true;

}

logger.info("User belongs to this group {}", groupId);

}

return false;

}

 

private boolean isquickPublish(ServletRequest request) {

// TODO Auto-generated method stub

if(request.getParameter("model")==null && request.getParameter("absoluteTime")==null) {

return true;

}

return false;

}

}

 

ReolicationContentFilterImpl

 

package com.macnicagwi.core.filters;

 

import java.util.List;

 

import javax.jcr.Node;

import javax.jcr.Property;

import javax.jcr.RepositoryException;

 

import org.osgi.service.component.annotations.Component;

 

import com.day.cq.replication.ReplicationContentFilter;

 

@component(service = ReplicationContentFilter.class)

public class ReplicationContentFilterImpl implements ReplicationContentFilter {

 

@Override

public boolean accepts(Node node) {

// TODO Auto-generated method stub

try {

return !node.getPath().startsWith("/conf");

} catch (RepositoryException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return true;

}

 

@Override

public boolean accepts(Property property) {

// TODO Auto-generated method stub

try {

return !property.getValue().getString().startsWith("/conf");

} catch (IllegalStateException | RepositoryException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return true;

}

 

@Override

public boolean allowsDescent(Node node) {

// TODO Auto-generated method stub

return accepts(node);

}

 

@Override

public List<String> getFilteredPaths() {

// TODO Auto-generated method stub

return null;

}

 

}

 

ReplicationContentFilterFactoryImpl

package com.macnicagwi.core.filters;
 
import org.osgi.service.component.annotations.Component;
 
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationContentFilter;
import com.day.cq.replication.ReplicationContentFilterFactory;
 
@component(service=ReplicationContentFilterFactory.class,factory = "replication")
public class ReplicationContentFilterFactoryImpl implements ReplicationContentFilterFactory {
 
public ReplicationContentFilter createFilter(ReplicationAction action) {
// TODO Auto-generated method stub
ReplicationContentFilter filter = new ReplicationContentFilterImpl();
return filter;
}
 
}

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

remove factory parameter in @component . it will work.

View solution in original post

3 Replies

Avatar

Community Advisor

You are creating the filter and but, it is not used in replicate method.

 

try {
if(isquickPublish(request) && isPublisher()) {
String[] paths = slingRequest.getParameterValues("path");
Session session = slingRequest.getResourceResolver().adaptTo(Session.class);
ReplicationContentFilterFactory replicationFilterFactory = new ReplicationContentFilterFactoryImpl();
for(String path : paths) {
ReplicationAction replicationAction = new ReplicationAction(ReplicationActionType.ACTIVATE, path);
ReplicationContentFilter filter = replicationFilterFactory.createFilter(replicationAction);
replicator.replicate(session, ReplicationActionType.ACTIVATE, path, filter);
}
}
} catch (RepositoryException | ReplicationException e) {}

Avatar

Correct answer by
Level 4

remove factory parameter in @component . it will work.