jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anup Chatterjee <anup.c...@gmail.com>
Subject EventListener on Webdav
Date Thu, 15 May 2008 18:50:24 GMT

Hi,

I had asked this question earlier on how to set an event Listener for the
WebDav we found a way to do it by overriding the WebdavServlet with a custom
provider that has the eventLisner added to the session. 

I am not sure if this is the right way. 
I'm open for comments on this implementation or suggestions on if there is
any better way to implement it.

Thanks,
Anup

---
import java.util.Arrays;

import javax.jcr.Credentials;
import javax.jcr.LoginException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.ObservationManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jackrabbit.server.BasicCredentialsProvider;
import org.apache.jackrabbit.server.CredentialsProvider;
import org.apache.jackrabbit.server.SessionProvider;
import org.springmodules.jcr.EventListenerDefinition;

/**
 * This Class implements a default session provider uses a credentials
provider.
 */
public class CustomSessionProviderImpl implements SessionProvider {

    private static final Log log =
LogFactory.getLog(CustomSessionProviderImpl.class);
    private EventListenerDefinition eventListeners[] = new
EventListenerDefinition[] {};
    /**
     * Creates a new SessionProvider
     * 
     */
    public CustomSessionProviderImpl() {
    }

    /**
     * {@inheritDoc }
     */
    public Session getSession(HttpServletRequest request, Repository
repository, String workspace)
            throws LoginException, RepositoryException, ServletException {
        CredentialsProvider cp = new
BasicCredentialsProvider("anonymous:anonymous");
        Credentials creds = cp.getCredentials(request);
        Session jcrSession = null;
        if (creds == null) {
            jcrSession = repository.login(workspace);
        } else {
            jcrSession = repository.login(creds, workspace);
        }
        return addListeners(jcrSession);
    }

    /**
     * {@inheritDoc }
     */
    public void releaseSession(Session session) {
        session.logout();
    }

    /**
     * @param eventListenerDefinitions The eventListenerDefinitions to set.
     */
    public void setEventListeners(EventListenerDefinition[]
eventListenerDefinitions) {
        this.eventListeners = eventListenerDefinitions;
    }

    /**
     * Hook for adding listeners to the newly returned session. We have to
treat exceptions manually and can't reply on
     * the template.
     * 
     * @param session JCR session
     * @return the listened session
     */
    protected Session addListeners(Session session) throws
RepositoryException {
        if (eventListeners != null && eventListeners.length > 0) {
            Workspace ws = session.getWorkspace();
            ObservationManager manager = ws.getObservationManager();
            if (log.isDebugEnabled())
                log.debug("adding listeners " +
Arrays.asList(eventListeners).toString() + " for session " + session);

            for (int i = 0; i < eventListeners.length; i++) {
                manager.addEventListener(eventListeners[i].getListener(),
eventListeners[i].getEventTypes(),
                        eventListeners[i].getAbsPath(),
eventListeners[i].isDeep(), eventListeners[i].getUuid(),
                        eventListeners[i].getNodeTypeName(),
eventListeners[i].isNoLocal());
            }
        }
        return session;
    }

}

import javax.servlet.ServletException;

import org.apache.jackrabbit.j2ee.SimpleWebdavServlet;
import org.apache.jackrabbit.server.SessionProvider;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class CustomWebdavServlet extends SimpleWebdavServlet {
    /**
     * Init this servlet
     *
     * @throws ServletException
     */
    public void init() throws ServletException {
        super.init();
        
        setSessionProvider((SessionProvider)
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()).getBean("customSessionProvider"));

    }
}
-- 
View this message in context: http://www.nabble.com/EventListener-on-Webdav-tp17259936p17259936.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.


Mime
View raw message