1 package org.apache.maven.archiva.webdav;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.jackrabbit.webdav.DavSessionProvider;
23 import org.apache.jackrabbit.webdav.WebdavRequest;
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.DavServletRequest;
26 import org.apache.maven.archiva.webdav.util.WebdavMethodUtil;
27 import org.apache.maven.archiva.webdav.util.RepositoryPathUtil;
28 import org.apache.maven.archiva.security.ServletAuthenticator;
29 import org.codehaus.plexus.redback.authentication.AuthenticationException;
30 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
31 import org.codehaus.plexus.redback.policy.AccountLockedException;
32 import org.codehaus.plexus.redback.authorization.AuthorizationException;
33 import org.codehaus.plexus.spring.PlexusToSpringUtils;
34 import org.springframework.web.context.WebApplicationContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import javax.servlet.http.HttpServletResponse;
41 * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
43 public class ArchivaDavSessionProvider implements DavSessionProvider
45 private Logger log = LoggerFactory.getLogger(ArchivaDavSessionProvider.class);
47 private ServletAuthenticator servletAuth;
49 public ArchivaDavSessionProvider(WebApplicationContext applicationContext)
51 servletAuth = (ServletAuthenticator) applicationContext.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
54 public boolean attachSession(WebdavRequest request) throws DavException
56 final String repositoryId = RepositoryPathUtil.getRepositoryName(removeContextPath(request));
60 return servletAuth.isAuthenticated(request, repositoryId) &&
61 servletAuth.isAuthorized(request, repositoryId, WebdavMethodUtil.isWriteMethod( request.getMethod() ) );
63 catch ( AuthenticationException e )
65 log.error( "Cannot authenticate user.", e );
66 throw new UnauthorizedDavException(repositoryId, "You are not authenticated");
68 catch ( MustChangePasswordException e )
70 log.error( "User must change password." );
71 throw new UnauthorizedDavException(repositoryId, "You must change your password.");
73 catch ( AccountLockedException e )
75 log.error( "User account is locked." );
76 throw new UnauthorizedDavException(repositoryId, "User account is locked.");
78 catch ( AuthorizationException e )
80 log.error( "Fatal Authorization Subsystem Error." );
81 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Fatal Authorization Subsystem Error." );
85 public void releaseSession(WebdavRequest webdavRequest)
90 private String removeContextPath(final DavServletRequest request)
92 String path = request.getRequestURI();
93 String ctx = request.getContextPath();
94 if (path.startsWith(ctx)) {
95 path = path.substring(ctx.length());