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
23 import java.io.IOException;
26 import javax.servlet.ServletConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.apache.jackrabbit.webdav.DavException;
32 import org.apache.jackrabbit.webdav.DavLocatorFactory;
33 import org.apache.jackrabbit.webdav.DavMethods;
34 import org.apache.jackrabbit.webdav.DavResource;
35 import org.apache.jackrabbit.webdav.DavResourceFactory;
36 import org.apache.jackrabbit.webdav.DavServletResponse;
37 import org.apache.jackrabbit.webdav.DavSessionProvider;
38 import org.apache.jackrabbit.webdav.WebdavRequest;
39 import org.apache.jackrabbit.webdav.WebdavRequestImpl;
40 import org.apache.jackrabbit.webdav.WebdavResponse;
41 import org.apache.jackrabbit.webdav.WebdavResponseImpl;
42 import org.apache.jackrabbit.webdav.server.AbstractWebdavServlet;
43 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
44 import org.apache.maven.archiva.configuration.ConfigurationEvent;
45 import org.apache.maven.archiva.configuration.ConfigurationListener;
46 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
47 import org.apache.maven.archiva.repository.audit.AuditEvent;
48 import org.apache.maven.archiva.security.ServletAuthenticator;
49 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
50 import org.codehaus.plexus.spring.PlexusToSpringUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.web.context.WebApplicationContext;
54 import org.springframework.web.context.support.WebApplicationContextUtils;
59 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
62 public class RepositoryServlet
63 extends AbstractWebdavServlet
64 implements ConfigurationListener
66 private Logger log = LoggerFactory.getLogger( RepositoryServlet.class );
68 private ArchivaConfiguration configuration;
70 private Map<String, ManagedRepositoryConfiguration> repositoryMap;
72 private DavLocatorFactory locatorFactory;
74 private DavResourceFactory resourceFactory;
76 private DavSessionProvider sessionProvider;
78 private final Object reloadLock = new Object();
80 public void init( javax.servlet.ServletConfig servletConfig )
81 throws ServletException
83 super.init( servletConfig );
84 initServers( servletConfig );
88 * Service the given request. This method has been overridden and copy/pasted to allow better exception handling and
89 * to support different realms
93 * @throws ServletException
94 * @throws java.io.IOException
97 protected void service( HttpServletRequest request, HttpServletResponse response )
98 throws ServletException, IOException
100 WebdavRequest webdavRequest = new WebdavRequestImpl( request, getLocatorFactory() );
101 // DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
102 int methodCode = DavMethods.getMethodCode( request.getMethod() );
104 DavMethods.isDeltaVMethod( webdavRequest ) &&
105 !( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode );
106 WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
110 // make sure there is a authenticated user
111 if ( !getDavSessionProvider().attachSession( webdavRequest ) )
116 // check matching if=header for lock-token relevant operations
117 DavResource resource =
118 getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
120 if ( !isPreconditionValid( webdavRequest, resource ) )
122 webdavResponse.sendError( DavServletResponse.SC_PRECONDITION_FAILED );
125 if ( !execute( webdavRequest, webdavResponse, methodCode, resource ) )
127 super.service( request, response );
131 catch ( UnauthorizedDavException e )
133 webdavResponse.setHeader( "WWW-Authenticate", getAuthenticateHeaderValue( e.getRepositoryName() ) );
134 webdavResponse.sendError( e.getErrorCode(), e.getStatusPhrase() );
136 catch ( BrowserRedirectException e )
138 response.sendRedirect( e.getLocation() );
140 catch ( DavException e )
142 if ( e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED )
144 final String msg = "Should throw " + UnauthorizedDavException.class.getName();
146 webdavResponse.sendError( e.getErrorCode(), msg );
148 else if ( e.getCause() != null )
150 webdavResponse.sendError( e.getErrorCode(), e.getCause().getMessage() );
154 webdavResponse.sendError( e.getErrorCode(), e.getMessage() );
159 getDavSessionProvider().releaseSession( webdavRequest );
163 public synchronized void initServers( ServletConfig servletConfig )
165 WebApplicationContext wac =
166 WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
169 (ArchivaConfiguration) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaConfiguration.class.getName() ) );
170 configuration.addListener( this );
172 repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap();
174 for ( ManagedRepositoryConfiguration repo : repositoryMap.values() )
176 File repoDir = new File( repo.getLocation() );
178 if ( !repoDir.exists() )
180 if ( !repoDir.mkdirs() )
182 // Skip invalid directories.
183 log( "Unable to create missing directory for " + repo.getLocation() );
190 (DavResourceFactory) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaDavResourceFactory.class ) );
191 locatorFactory = new ArchivaDavLocatorFactory();
193 ServletAuthenticator servletAuth =
194 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
195 HttpAuthenticator httpAuth =
196 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
198 sessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
201 public void configurationEvent( ConfigurationEvent event )
203 if ( event.getType() == ConfigurationEvent.SAVED )
209 private void initRepositories()
211 synchronized ( repositoryMap )
213 repositoryMap.clear();
214 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
217 synchronized ( reloadLock )
219 initServers( getServletConfig() );
223 public synchronized ManagedRepositoryConfiguration getRepository( String prefix )
225 if ( repositoryMap.isEmpty() )
227 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
229 return repositoryMap.get( prefix );
232 ArchivaConfiguration getConfiguration()
234 return configuration;
237 protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource )
239 // check for read or write access to the resource when resource-based permission is implemented
244 public DavSessionProvider getDavSessionProvider()
246 return sessionProvider;
249 public void setDavSessionProvider( final DavSessionProvider davSessionProvider )
251 this.sessionProvider = davSessionProvider;
254 public DavLocatorFactory getLocatorFactory()
256 return locatorFactory;
259 public void setLocatorFactory( final DavLocatorFactory davLocatorFactory )
261 locatorFactory = davLocatorFactory;
264 public DavResourceFactory getResourceFactory()
266 return resourceFactory;
269 public void setResourceFactory( final DavResourceFactory davResourceFactory )
271 resourceFactory = davResourceFactory;
274 public String getAuthenticateHeaderValue()
276 throw new UnsupportedOperationException();
279 public String getAuthenticateHeaderValue( String repository )
281 return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\"";