blob: 6b4d5246c74b10376c3cf072165beb1791c5e785 [file] [log] [blame]
/*
* 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.sling.jcr.davex.impl.servlets;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import javax.jcr.LoginException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
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.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.jackrabbit.server.SessionProvider;
import org.apache.jackrabbit.server.remoting.davex.JcrRemotingServlet;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.auth.core.AuthenticationSupport;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.HttpService;
/**
* DavEx WebDav servlet which acquires a Repository instance via the OSGi
* service registry.
*
*/
@Component(label = "%dav.name", description = "%dav.description")
@Properties( { @Property(name = "alias", value = "/server"),
@Property(name = "init.resource-path-prefix", value = "/server"),
@Property(name = "init.missing-auth-mapping", value = ""),
@Property(name = "service.description", value = "Sling JcrRemoting Servlet"),
@Property(name = "service.vendor", value = "The Apache Software Foundation") })
public class SlingDavExServlet extends JcrRemotingServlet {
private static final String INIT_KEY_PREFIX = "init.";
@Reference
private Repository repository;
@Reference
private HttpService httpService;
@Reference
private AuthenticationSupport authentiator;
@Activate
protected void activate(final ComponentContext ctx)
throws Exception {
final AuthHttpContext context = new AuthHttpContext();
context.setAuthenticationSupport(authentiator);
final String alias = (String)ctx.getProperties().get("alias");
final Dictionary<String, String> initProps = new Hashtable<String, String>();
@SuppressWarnings("unchecked")
final Enumeration<String> keyEnum = ctx.getProperties().keys();
while ( keyEnum.hasMoreElements() ) {
final String key = keyEnum.nextElement();
if ( key.startsWith(INIT_KEY_PREFIX) ) {
final String paramKey = key.substring(INIT_KEY_PREFIX.length());
final Object paramValue = ctx.getProperties().get(key);
if (paramValue != null) {
initProps.put(paramKey, paramValue.toString());
}
}
}
this.httpService.registerServlet(alias, this, initProps, context);
}
@Deactivate
protected void deactivate(final Map<String, Object> props) {
final String alias = (String)props.get("alias");
this.httpService.unregister(alias);
}
@Override
protected Repository getRepository() {
return repository;
}
@Override
protected SessionProvider getSessionProvider() {
return new SessionProvider() {
public Session getSession(final HttpServletRequest req,
final Repository paramRepository,
final String paramString)
throws LoginException, ServletException, RepositoryException {
final ResourceResolver resolver = (ResourceResolver) req.getAttribute(AuthenticationSupport.REQUEST_ATTRIBUTE_RESOLVER);
if ( resolver != null ) {
return resolver.adaptTo(Session.class);
}
return null;
}
public void releaseSession(Session paramSession) {
// nothing to do
}
};
}
}