org.apache.jackrabbit.server.jcr.JCRWebdavServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackrabbit-jcr-server Show documentation
Show all versions of jackrabbit-jcr-server Show documentation
WebDAV server implementations for JCR
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.jackrabbit.server.jcr;
import org.apache.jackrabbit.commons.webdav.JcrRemotingConstants;
import org.apache.jackrabbit.server.SessionProvider;
import org.apache.jackrabbit.spi.commons.SessionExtensions;
import org.apache.jackrabbit.util.Text;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavMethods;
import org.apache.jackrabbit.webdav.DavSession;
import org.apache.jackrabbit.webdav.DavSessionProvider;
import org.apache.jackrabbit.webdav.WebdavRequest;
import org.apache.jackrabbit.webdav.header.IfHeader;
import org.apache.jackrabbit.webdav.jcr.JcrDavException;
import org.apache.jackrabbit.webdav.jcr.JcrDavSession;
import org.apache.jackrabbit.webdav.util.LinkHeaderFieldParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.LoginException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* JCRWebdavServer
...
*/
public class JCRWebdavServer implements DavSessionProvider {
/** the default logger */
private static Logger log = LoggerFactory.getLogger(JCRWebdavServer.class);
/** the session cache */
private final SessionCache cache;
/** the jcr repository */
private final Repository repository;
/** the provider for the credentials */
private final SessionProvider sessionProvider;
/**
* Creates a new JCRWebdavServer that operates on the given repository.
*
* @param repository
*/
public JCRWebdavServer(Repository repository, SessionProvider sessionProvider) {
this.repository = repository;
this.sessionProvider = sessionProvider;
cache = new SessionCache();
}
/**
* Creates a new JCRWebdavServer that operates on the given repository.
*
* @param repository
* @param concurrencyLevel
*/
public JCRWebdavServer(Repository repository, SessionProvider sessionProvider, int concurrencyLevel) {
this.repository = repository;
this.sessionProvider = sessionProvider;
cache = new SessionCache(concurrencyLevel);
}
//---------------------------------------< DavSessionProvider interface >---
/**
* Acquires a DavSession either from the session cache or creates a new
* one by login to the repository.
* Upon success, the WebdavRequest will reference that session.
*
* @param request
* @throws DavException if no session could be obtained.
* @see DavSessionProvider#attachSession(org.apache.jackrabbit.webdav.WebdavRequest)
*/
public boolean attachSession(WebdavRequest request)
throws DavException {
DavSession session = cache.get(request);
request.setDavSession(session);
return true;
}
/**
* Releases the reference from the request to the session. If no further
* references to the session exist, the session will be removed from the
* cache.
*
* @param request
* @see DavSessionProvider#releaseSession(org.apache.jackrabbit.webdav.WebdavRequest)
*/
public void releaseSession(WebdavRequest request) {
DavSession session = request.getDavSession();
if (session != null) {
session.removeReference(request);
}
// remove the session from the request
request.setDavSession(null);
}
//--------------------------------------------------------------------------
/**
* Private inner class implementing the DavSession
interface.
*/
private class DavSessionImpl extends JcrDavSession {
/**
* Private constructor.
*
* @param session
*/
private DavSessionImpl(Session session) {
super(session);
}
/**
* Add a reference to this DavSession
.
*
* @see DavSession#addReference(Object)
*/
public void addReference(Object reference) {
cache.addReference(this, reference);
}
/**
* Removes the reference from this DavSession
. If no
* more references are present, this DavSession
is removed
* from the internal cache and the underlying session is released by
* calling {@link SessionProvider#releaseSession(javax.jcr.Session)}
*
* @see DavSession#removeReference(Object)
*/
public void removeReference(Object reference) {
cache.removeReference(this, reference);
}
}
/**
* Private inner class providing a cache for referenced session objects.
*/
private class SessionCache {
private static final int CONCURRENCY_LEVEL_DEFAULT = 50;
private static final int INITIAL_CAPACITY = 50;
private static final int INITIAL_CAPACITY_REF_TO_SESSION = 3 * INITIAL_CAPACITY;
private ConcurrentMap> sessionMap;
private ConcurrentMap