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.security.ArchivaXworkUser;
48 import org.apache.maven.archiva.security.ServletAuthenticator;
49 import org.codehaus.plexus.spring.PlexusToSpringUtils;
50 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
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;
61 public class RepositoryServlet
62 extends AbstractWebdavServlet
63 implements ConfigurationListener
65 private Logger log = LoggerFactory.getLogger( RepositoryServlet.class );
67 private ArchivaConfiguration configuration;
69 private Map<String, ManagedRepositoryConfiguration> repositoryMap;
71 private DavLocatorFactory locatorFactory;
73 private DavResourceFactory resourceFactory;
75 private DavSessionProvider sessionProvider;
77 private final Object reloadLock = new Object();
79 public void init( javax.servlet.ServletConfig servletConfig )
80 throws ServletException
82 super.init( servletConfig );
83 initServers( servletConfig );
87 * Service the given request. This method has been overridden and copy/pasted to allow better exception handling and
88 * to support different realms
92 * @throws ServletException
93 * @throws java.io.IOException
96 protected void service( HttpServletRequest request, HttpServletResponse response )
97 throws ServletException, IOException
99 WebdavRequest webdavRequest = new WebdavRequestImpl( request, getLocatorFactory() );
100 // DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
101 int methodCode = DavMethods.getMethodCode( request.getMethod() );
103 DavMethods.isDeltaVMethod( webdavRequest ) &&
104 !( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode );
105 WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
106 DavResource resource = null;
110 // make sure there is a authenticated user
111 if ( !getDavSessionProvider().attachSession( webdavRequest ) )
116 // check matching if=header for lock-token relevant operations
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 ArchivaXworkUser archivaXworkUser =
199 (ArchivaXworkUser) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class.getName() ) );
200 sessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth, archivaXworkUser );
203 public void configurationEvent( ConfigurationEvent event )
205 if ( event.getType() == ConfigurationEvent.SAVED )
211 private void initRepositories()
213 synchronized ( repositoryMap )
215 repositoryMap.clear();
216 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
219 synchronized ( reloadLock )
221 initServers( getServletConfig() );
225 public synchronized ManagedRepositoryConfiguration getRepository( String prefix )
227 if ( repositoryMap.isEmpty() )
229 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
231 return repositoryMap.get( prefix );
234 ArchivaConfiguration getConfiguration()
236 return configuration;
239 protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource )
241 // check for read or write access to the resource when resource-based permission is implemented
246 public DavSessionProvider getDavSessionProvider()
248 return sessionProvider;
251 public void setDavSessionProvider( final DavSessionProvider davSessionProvider )
253 this.sessionProvider = davSessionProvider;
256 public DavLocatorFactory getLocatorFactory()
258 return locatorFactory;
261 public void setLocatorFactory( final DavLocatorFactory davLocatorFactory )
263 locatorFactory = davLocatorFactory;
266 public DavResourceFactory getResourceFactory()
268 return resourceFactory;
271 public void setResourceFactory( final DavResourceFactory davResourceFactory )
273 resourceFactory = davResourceFactory;
276 public String getAuthenticateHeaderValue()
278 throw new UnsupportedOperationException();
281 public String getAuthenticateHeaderValue( String repository )
283 return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\"";