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.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 );
107 DavResource resource = null;
111 // make sure there is a authenticated user
112 if ( !getDavSessionProvider().attachSession( webdavRequest ) )
117 // check matching if=header for lock-token relevant operations
119 getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
121 if ( !isPreconditionValid( webdavRequest, resource ) )
123 webdavResponse.sendError( DavServletResponse.SC_PRECONDITION_FAILED );
126 if ( !execute( webdavRequest, webdavResponse, methodCode, resource ) )
128 super.service( request, response );
132 catch ( UnauthorizedDavException e )
134 webdavResponse.setHeader( "WWW-Authenticate", getAuthenticateHeaderValue( e.getRepositoryName() ) );
135 webdavResponse.sendError( e.getErrorCode(), e.getStatusPhrase() );
137 catch ( BrowserRedirectException e )
139 response.sendRedirect( e.getLocation() );
141 catch ( DavException e )
143 if ( e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED )
145 final String msg = "Should throw " + UnauthorizedDavException.class.getName();
147 webdavResponse.sendError( e.getErrorCode(), msg );
149 else if ( e.getCause() != null )
151 webdavResponse.sendError( e.getErrorCode(), e.getCause().getMessage() );
155 webdavResponse.sendError( e.getErrorCode(), e.getMessage() );
160 getDavSessionProvider().releaseSession( webdavRequest );
164 public synchronized void initServers( ServletConfig servletConfig )
166 WebApplicationContext wac =
167 WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
170 (ArchivaConfiguration) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaConfiguration.class.getName() ) );
171 configuration.addListener( this );
173 repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap();
175 for ( ManagedRepositoryConfiguration repo : repositoryMap.values() )
177 File repoDir = new File( repo.getLocation() );
179 if ( !repoDir.exists() )
181 if ( !repoDir.mkdirs() )
183 // Skip invalid directories.
184 log( "Unable to create missing directory for " + repo.getLocation() );
191 (DavResourceFactory) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaDavResourceFactory.class ) );
192 locatorFactory = new ArchivaDavLocatorFactory();
194 ServletAuthenticator servletAuth =
195 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
196 HttpAuthenticator httpAuth =
197 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
199 ArchivaXworkUser archivaXworkUser =
200 (ArchivaXworkUser) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class.getName() ) );
201 sessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth, archivaXworkUser );
204 public void configurationEvent( ConfigurationEvent event )
206 if ( event.getType() == ConfigurationEvent.SAVED )
212 private void initRepositories()
214 synchronized ( repositoryMap )
216 repositoryMap.clear();
217 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
220 synchronized ( reloadLock )
222 initServers( getServletConfig() );
226 public synchronized ManagedRepositoryConfiguration getRepository( String prefix )
228 if ( repositoryMap.isEmpty() )
230 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
232 return repositoryMap.get( prefix );
235 ArchivaConfiguration getConfiguration()
237 return configuration;
240 protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource )
242 // check for read or write access to the resource when resource-based permission is implemented
247 public DavSessionProvider getDavSessionProvider()
249 return sessionProvider;
252 public void setDavSessionProvider( final DavSessionProvider davSessionProvider )
254 this.sessionProvider = davSessionProvider;
257 public DavLocatorFactory getLocatorFactory()
259 return locatorFactory;
262 public void setLocatorFactory( final DavLocatorFactory davLocatorFactory )
264 locatorFactory = davLocatorFactory;
267 public DavResourceFactory getResourceFactory()
269 return resourceFactory;
272 public void setResourceFactory( final DavResourceFactory davResourceFactory )
274 resourceFactory = davResourceFactory;
277 public String getAuthenticateHeaderValue()
279 throw new UnsupportedOperationException();
282 public String getAuthenticateHeaderValue( String repository )
284 return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\"";