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.ConfigurationEvent;
24 import org.apache.maven.archiva.configuration.ConfigurationListener;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.security.ArchivaRoleConstants;
27 import org.codehaus.plexus.redback.authentication.AuthenticationException;
28 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
29 import org.codehaus.plexus.redback.authorization.AuthorizationException;
30 import org.codehaus.plexus.redback.authorization.AuthorizationResult;
31 import org.codehaus.plexus.redback.policy.AccountLockedException;
32 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
33 import org.codehaus.plexus.redback.system.SecuritySession;
34 import org.codehaus.plexus.redback.system.SecuritySystem;
35 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
36 import org.codehaus.plexus.webdav.DavServerComponent;
37 import org.codehaus.plexus.webdav.DavServerException;
38 import org.codehaus.plexus.webdav.DavServerManager;
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;
44 import java.io.IOException;
47 import javax.servlet.ServletConfig;
48 import javax.servlet.ServletException;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
55 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
58 public class RepositoryServlet
59 extends MultiplexedWebDavServlet
60 implements ConfigurationListener
62 private SecuritySystem securitySystem;
64 private HttpAuthenticator httpAuth;
66 private AuditLog audit;
68 private ArchivaConfiguration configuration;
70 private Map<String, ManagedRepositoryConfiguration> repositoryMap;
72 private ArchivaMimeTypeLoader mimeTypeLoader;
74 public synchronized void initComponents()
75 throws ServletException
77 super.initComponents();
79 mimeTypeLoader = (ArchivaMimeTypeLoader) lookup( ArchivaMimeTypeLoader.class.getName() );
81 securitySystem = (SecuritySystem) lookup( SecuritySystem.ROLE );
82 httpAuth = (HttpAuthenticator) lookup( HttpAuthenticator.ROLE, "basic" );
83 audit = (AuditLog) lookup( AuditLog.ROLE );
85 configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName() );
86 configuration.addListener( this );
88 repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap();
91 public synchronized void initServers( ServletConfig servletConfig )
92 throws DavServerException
94 for ( ManagedRepositoryConfiguration repo : repositoryMap.values() )
96 File repoDir = new File( repo.getLocation() );
98 if ( !repoDir.exists() )
100 if ( !repoDir.mkdirs() )
102 // Skip invalid directories.
103 log( "Unable to create missing directory for " + repo.getLocation() );
108 DavServerComponent server = createServer( repo.getId(), repoDir, servletConfig );
110 server.setUseIndexHtml( true );
111 server.addListener( audit );
116 public void destroy()
120 release( securitySystem );
122 catch ( ServletException e )
124 log( "Unable to release SecuritySystem : " + e.getMessage(), e );
130 catch ( ServletException e )
132 log( "Unable to release HttpAuth : " + e.getMessage(), e );
138 catch ( ServletException e )
140 log( "Unable to release AuditLog : " + e.getMessage(), e );
144 release( configuration );
146 catch ( ServletException e )
148 log( "Unable to release ArchivaConfiguration : " + e.getMessage(), e );
152 release( mimeTypeLoader );
154 catch ( ServletException e )
156 log( "Unable to release ArchivaMimeTypeLoader : " + e.getMessage(), e );
163 protected void service( HttpServletRequest httpRequest, HttpServletResponse httpResponse )
164 throws ServletException, IOException
166 // Wrap the incoming request to adjust paths and whatnot.
167 super.service( new PolicingServletRequest( httpRequest ), httpResponse );
170 public synchronized ManagedRepositoryConfiguration getRepository( String prefix )
172 if ( repositoryMap.isEmpty() )
174 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
176 return repositoryMap.get( prefix );
179 private String getRepositoryName( DavServerRequest request )
181 ManagedRepositoryConfiguration repoConfig = getRepository( request.getPrefix() );
182 if ( repoConfig == null )
187 return repoConfig.getName();
190 public boolean isAuthenticated( DavServerRequest davRequest, HttpServletResponse response )
191 throws ServletException, IOException
193 HttpServletRequest request = davRequest.getRequest();
195 // Authentication Tests.
198 AuthenticationResult result = httpAuth.getAuthenticationResult( request, response );
200 if ( result != null && !result.isAuthenticated() )
202 // Must Authenticate.
203 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
204 new AuthenticationException( "User Credentials Invalid" ) );
208 catch ( AuthenticationException e )
210 log( "Fatal Http Authentication Error.", e );
211 throw new ServletException( "Fatal Http Authentication Error.", e );
213 catch ( AccountLockedException e )
215 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
216 new AuthenticationException( "User account is locked" ) );
218 catch ( MustChangePasswordException e )
220 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
221 new AuthenticationException( "You must change your password." ) );
227 public boolean isAuthorized( DavServerRequest davRequest, HttpServletResponse response )
228 throws ServletException, IOException
230 // Authorization Tests.
231 HttpServletRequest request = davRequest.getRequest();
233 boolean isWriteRequest = WebdavMethodUtil.isWriteMethod( request.getMethod() );
235 SecuritySession securitySession = httpAuth.getSecuritySession();
238 String permission = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
240 if ( isWriteRequest )
242 permission = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
245 AuthorizationResult authzResult =
246 securitySystem.authorize( securitySession, permission, davRequest.getPrefix() );
248 if ( !authzResult.isAuthorized() )
250 if ( authzResult.getException() != null )
252 log( "Authorization Denied [ip=" + request.getRemoteAddr() + ",isWriteRequest=" + isWriteRequest +
253 ",permission=" + permission + ",repo=" + davRequest.getPrefix() + "] : " +
254 authzResult.getException().getMessage() );
257 // Issue HTTP Challenge.
258 httpAuth.challenge( request, response, "Repository " + getRepositoryName( davRequest ),
259 new AuthenticationException( "Authorization Denied." ) );
263 catch ( AuthorizationException e )
265 throw new ServletException( "Fatal Authorization Subsystem Error." );
271 public void configurationEvent( ConfigurationEvent event )
273 if( event.getType() == ConfigurationEvent.SAVED )
279 private void initRepositories()
281 synchronized ( repositoryMap )
283 repositoryMap.clear();
284 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
287 DavServerManager davManager = getDavManager();
289 synchronized ( davManager )
291 // Clear out the old servers.
292 davManager.removeAllServers();
294 // Create new servers.
297 initServers( getServletConfig() );
299 catch ( DavServerException e )
301 log( "Unable to init servers: " + e.getMessage(), e );