1 package org.apache.maven.archiva.web.repository;
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.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ConfigurationNames;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.security.ArchivaRoleConstants;
26 import org.codehaus.plexus.redback.authentication.AuthenticationException;
27 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
28 import org.codehaus.plexus.redback.authorization.AuthorizationException;
29 import org.codehaus.plexus.redback.authorization.AuthorizationResult;
30 import org.codehaus.plexus.redback.policy.AccountLockedException;
31 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
32 import org.codehaus.plexus.redback.system.SecuritySession;
33 import org.codehaus.plexus.redback.system.SecuritySystem;
34 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
35 import org.codehaus.plexus.registry.Registry;
36 import org.codehaus.plexus.registry.RegistryListener;
37 import org.codehaus.plexus.webdav.DavServerComponent;
38 import org.codehaus.plexus.webdav.DavServerException;
39 import org.codehaus.plexus.webdav.servlet.DavServerRequest;
40 import org.codehaus.plexus.webdav.servlet.multiplexed.MultiplexedWebDavServlet;
41 import org.codehaus.plexus.webdav.util.WebdavMethodUtil;
43 import javax.servlet.ServletConfig;
44 import javax.servlet.ServletException;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import java.io.IOException;
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.List;
57 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
60 public class RepositoryServlet
61 extends MultiplexedWebDavServlet
62 implements RegistryListener
67 private SecuritySystem securitySystem;
70 * @plexus.requirement role-hint="basic"
72 private HttpAuthenticator httpAuth;
77 private AuditLog audit;
82 private ArchivaConfiguration configuration;
84 private Map<String, ManagedRepositoryConfiguration> repositoryMap =
85 new HashMap<String, ManagedRepositoryConfiguration>();
87 public void initComponents()
88 throws ServletException
90 super.initComponents();
92 securitySystem = (SecuritySystem) lookup( SecuritySystem.ROLE );
93 httpAuth = (HttpAuthenticator) lookup( HttpAuthenticator.ROLE, "basic" );
94 audit = (AuditLog) lookup( AuditLog.ROLE );
96 configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName() );
97 configuration.addChangeListener( this );
99 updateRepositoryMap();
102 public void initServers( ServletConfig servletConfig )
103 throws DavServerException
105 List repositories = configuration.getConfiguration().getManagedRepositories();
106 Iterator itrepos = repositories.iterator();
107 while ( itrepos.hasNext() )
109 ManagedRepositoryConfiguration repo = (ManagedRepositoryConfiguration) itrepos.next();
111 File repoDir = new File( repo.getLocation() );
113 if ( !repoDir.exists() )
115 if ( !repoDir.mkdirs() )
117 // Skip invalid directories.
118 log( "Unable to create missing directory for " + repo.getLocation() );
123 DavServerComponent server = createServer( repo.getId(), repoDir, servletConfig );
125 server.addListener( audit );
129 public ManagedRepositoryConfiguration getRepository( DavServerRequest request )
131 // TODO: use sync wrapper instead?
132 synchronized ( this.repositoryMap )
134 return repositoryMap.get( request.getPrefix() );
138 public String getRepositoryName( DavServerRequest request )
140 ManagedRepositoryConfiguration repoConfig = getRepository( request );
141 if ( repoConfig == null )
146 return repoConfig.getName();
149 private void updateRepositoryMap()
151 synchronized ( this.repositoryMap )
153 this.repositoryMap.clear();
154 this.repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
158 public boolean isAuthenticated( DavServerRequest davRequest, HttpServletResponse response )
159 throws ServletException, IOException
161 HttpServletRequest request = davRequest.getRequest();
163 // Authentication Tests.
166 AuthenticationResult result = httpAuth.getAuthenticationResult( request, response );
168 if ( result != null && !result.isAuthenticated() )
170 // Must Authenticate.
171 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
172 new AuthenticationException( "User Credentials Invalid" ) );
176 catch ( AuthenticationException e )
178 log( "Fatal Http Authentication Error.", e );
179 throw new ServletException( "Fatal Http Authentication Error.", e );
181 catch ( AccountLockedException e )
183 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
184 new AuthenticationException( "User account is locked" ) );
186 catch ( MustChangePasswordException e )
188 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
189 new AuthenticationException( "You must change your password." ) );
195 public boolean isAuthorized( DavServerRequest davRequest, HttpServletResponse response )
196 throws ServletException, IOException
198 // Authorization Tests.
199 HttpServletRequest request = davRequest.getRequest();
201 boolean isWriteRequest = WebdavMethodUtil.isWriteMethod( request.getMethod() );
203 SecuritySession securitySession = httpAuth.getSecuritySession();
206 String permission = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
208 if ( isWriteRequest )
210 permission = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
213 AuthorizationResult authzResult = securitySystem.authorize( securitySession, permission, davRequest
216 if ( !authzResult.isAuthorized() )
218 if ( authzResult.getException() != null )
220 log( "Authorization Denied [ip=" + request.getRemoteAddr() + ",isWriteRequest=" + isWriteRequest +
221 ",permission=" + permission + ",repo=" + davRequest.getPrefix() + "] : " +
222 authzResult.getException().getMessage() );
225 // Issue HTTP Challenge.
226 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
227 new AuthenticationException( "Authorization Denied." ) );
231 catch ( AuthorizationException e )
233 throw new ServletException( "Fatal Authorization Subsystem Error." );
239 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
244 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
246 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
248 // Attempt to reduce the number of times we refresh the repository map.
249 if ( propertyName.endsWith( ".id" ) || propertyName.endsWith( ".url" ) )
251 synchronized ( this.repositoryMap )
253 updateRepositoryMap();
255 getDavManager().removeAllServers();
259 initServers( getServletConfig() );
261 catch ( DavServerException e )
263 log( "Error restarting WebDAV server after configuration change - service disabled: " +