package org.argeo.cms.internal.kernel;
-import static org.argeo.cms.auth.AuthConstants.LOGIN_CONTEXT_USER;
-
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import org.apache.jackrabbit.server.remoting.davex.JcrRemotingServlet;
import org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet;
import org.argeo.cms.CmsException;
-import org.argeo.cms.auth.AuthConstants;
import org.argeo.cms.auth.HttpRequestCallback;
import org.argeo.cms.auth.HttpRequestCallbackHandler;
-import org.argeo.jcr.ArgeoJcrConstants;
import org.argeo.jcr.JcrUtils;
+import org.argeo.node.NodeConstants;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.useradmin.Authorization;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
/**
* Intercepts and enriches http access, mainly focusing on security and
* transactionality.
*/
-class DataHttp implements KernelConstants, ArgeoJcrConstants {
+class DataHttp implements KernelConstants {
private final static Log log = LogFactory.getLog(DataHttp.class);
// private final static String ATTR_AUTH = "auth";
private final static String HEADER_AUTHORIZATION = "Authorization";
private final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
+ private final static String DEFAULT_PROTECTED_HANDLERS = "/org/argeo/cms/internal/kernel/protectedHandlers.xml";
+
+ private final BundleContext bc;
private final HttpService httpService;
+ private final ServiceTracker<Repository, Repository> repositories;
// FIXME Make it more unique
private String httpAuthRealm = "Argeo";
private OpenInViewSessionProvider sessionProvider;
DataHttp(HttpService httpService) {
- this.httpService = httpService;
+ this.bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
sessionProvider = new OpenInViewSessionProvider();
- // registerRepositoryServlets(ALIAS_NODE, node);
+ this.httpService = httpService;
+ repositories = new ServiceTracker<>(bc, Repository.class, new RepositoriesStc());
+ repositories.open();
}
public void destroy() {
- // unregisterRepositoryServlets(ALIAS_NODE);
+ repositories.close();
}
void registerRepositoryServlets(String alias, Repository repository) {
registerWebdavServlet(alias, repository, false);
registerRemotingServlet(alias, repository, true);
registerRemotingServlet(alias, repository, false);
+ if (log.isDebugEnabled())
+ log.debug("Registered servlets for repository '" + alias + "'");
} catch (Exception e) {
- throw new CmsException("Could not register servlets for repository " + alias, e);
+ throw new CmsException("Could not register servlets for repository '" + alias + "'", e);
}
}
void unregisterRepositoryServlets(String alias) {
- // FIXME unregister servlets
+ try {
+ httpService.unregister(webdavPath(alias, true));
+ httpService.unregister(webdavPath(alias, false));
+ httpService.unregister(remotingPath(alias, true));
+ httpService.unregister(remotingPath(alias, false));
+ if (log.isDebugEnabled())
+ log.debug("Unregistered servlets for repository '" + alias + "'");
+ } catch (Exception e) {
+ log.error("Could not unregister servlets for repository '" + alias + "'", e);
+ }
}
void registerWebdavServlet(String alias, Repository repository, boolean anonymous)
throws NamespaceException, ServletException {
WebdavServlet webdavServlet = new WebdavServlet(repository, sessionProvider);
- String pathPrefix = anonymous ? WEBDAV_PUBLIC : WEBDAV_PRIVATE;
- String path = pathPrefix + "/" + alias;
+ String path = webdavPath(alias, anonymous);
Properties ip = new Properties();
ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_CONFIG, WEBDAV_CONFIG);
ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, path);
void registerRemotingServlet(String alias, Repository repository, boolean anonymous)
throws NamespaceException, ServletException {
- String pathPrefix = anonymous ? REMOTING_PUBLIC : REMOTING_PRIVATE;
RemotingServlet remotingServlet = new RemotingServlet(repository, sessionProvider);
- String path = pathPrefix + "/" + alias;
+ String path = remotingPath(alias, anonymous);
Properties ip = new Properties();
ip.setProperty(JcrRemotingServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, path);
// Looks like a bug in Jackrabbit remoting init
ip.setProperty(RemotingServlet.INIT_PARAM_HOME, KernelUtils.getOsgiInstanceDir() + "/tmp/jackrabbit");
ip.setProperty(RemotingServlet.INIT_PARAM_TMP_DIRECTORY, "remoting");
- // in order to avoid annoying warning.
- ip.setProperty(RemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG, "");
+ ip.setProperty(RemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG, DEFAULT_PROTECTED_HANDLERS);
httpService.registerServlet(path, remotingServlet, ip, new DataHttpContext(anonymous));
}
+ private String webdavPath(String alias, boolean anonymous) {
+ String pathPrefix = anonymous ? WEBDAV_PUBLIC : WEBDAV_PRIVATE;
+ return pathPrefix + "/" + alias;
+ }
+
+ private String remotingPath(String alias, boolean anonymous) {
+ String pathPrefix = anonymous ? REMOTING_PUBLIC : REMOTING_PRIVATE;
+ return pathPrefix + "/" + alias;
+ }
+
private Subject subjectFromRequest(HttpServletRequest request) {
Authorization authorization = (Authorization) request.getAttribute(HttpContext.AUTHORIZATION);
if (authorization == null)
throw new CmsException("Not authenticated");
try {
- LoginContext lc = new LoginContext(AuthConstants.LOGIN_CONTEXT_USER,
+ LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
new HttpRequestCallbackHandler(request));
lc.login();
return lc.getSubject();
}
}
+ private class RepositoriesStc implements ServiceTrackerCustomizer<Repository, Repository> {
+
+ @Override
+ public Repository addingService(ServiceReference<Repository> reference) {
+ Repository repository = bc.getService(reference);
+ Object jcrRepoAlias = reference.getProperty(NodeConstants.JCR_REPOSITORY_ALIAS);
+ if (jcrRepoAlias != null) {
+ String alias = jcrRepoAlias.toString();
+ registerRepositoryServlets(alias, repository);
+ }
+ return repository;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference<Repository> reference, Repository service) {
+ }
+
+ @Override
+ public void removedService(ServiceReference<Repository> reference, Repository service) {
+ Object jcrRepoAlias = reference.getProperty(NodeConstants.JCR_REPOSITORY_ALIAS);
+ if (jcrRepoAlias != null) {
+ String alias = jcrRepoAlias.toString();
+ unregisterRepositoryServlets(alias);
+ }
+ }
+ }
+
private class DataHttpContext implements HttpContext {
private final boolean anonymous;
if (anonymous) {
Subject subject = KernelUtils.anonymousLogin();
Authorization authorization = subject.getPrivateCredentials(Authorization.class).iterator().next();
- request.setAttribute(REMOTE_USER, AuthConstants.ROLE_ANONYMOUS);
+ request.setAttribute(REMOTE_USER, NodeConstants.ROLE_ANONYMOUS);
request.setAttribute(AUTHORIZATION, authorization);
return true;
}
if (log.isTraceEnabled())
KernelUtils.logRequestHeaders(log, request);
try {
- new LoginContext(LOGIN_CONTEXT_USER, new HttpRequestCallbackHandler(request)).login();
+ new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, new HttpRequestCallbackHandler(request)).login();
return true;
} catch (CredentialNotFoundException e) {
CallbackHandler token = basicAuth(request);
if (token != null) {
try {
- LoginContext lc = new LoginContext(LOGIN_CONTEXT_USER, token);
+ LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, token);
lc.login();
// Note: this is impossible to reliably clear the
// authorization header when access from a browser.
@Override
public URL getResource(String name) {
- return Activator.getBundleContext().getBundle().getResource(name);
+ return KernelUtils.getBundleContext(DataHttp.class).getBundle().getResource(name);
}
@Override