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
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.jackrabbit.webdav.server.AbstractWebdavServlet;
27 import org.apache.jackrabbit.webdav.*;
28 import org.codehaus.plexus.spring.PlexusToSpringUtils;
29 import org.springframework.web.context.WebApplicationContext;
30 import org.springframework.web.context.support.WebApplicationContextUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletException;
36 import javax.servlet.http.HttpServletResponse;
37 import javax.servlet.http.HttpServletRequest;
39 import java.io.IOException;
41 import org.apache.maven.archiva.security.ServletAuthenticator;
42 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
47 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 public class RepositoryServlet
51 extends AbstractWebdavServlet
52 implements ConfigurationListener
54 private Logger log = LoggerFactory.getLogger( RepositoryServlet.class );
56 private ArchivaConfiguration configuration;
58 private Map<String, ManagedRepositoryConfiguration> repositoryMap;
60 private DavLocatorFactory locatorFactory;
62 private DavResourceFactory resourceFactory;
64 private DavSessionProvider sessionProvider;
66 private final Object reloadLock = new Object();
68 public void init( javax.servlet.ServletConfig servletConfig )
69 throws ServletException
71 super.init( servletConfig );
72 initServers( servletConfig );
76 * Service the given request. This method has been overridden and copy/pasted to allow better exception handling and
77 * to support different realms
81 * @throws ServletException
82 * @throws java.io.IOException
85 protected void service( HttpServletRequest request, HttpServletResponse response )
86 throws ServletException, IOException
88 WebdavRequest webdavRequest = new WebdavRequestImpl( request, getLocatorFactory() );
89 // DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
90 int methodCode = DavMethods.getMethodCode( request.getMethod() );
92 DavMethods.isDeltaVMethod( webdavRequest ) &&
93 !( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode );
94 WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
98 // make sure there is a authenticated user
99 if ( !getDavSessionProvider().attachSession( webdavRequest ) )
104 // check matching if=header for lock-token relevant operations
105 DavResource resource =
106 getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
108 if ( !isPreconditionValid( webdavRequest, resource ) )
110 webdavResponse.sendError( DavServletResponse.SC_PRECONDITION_FAILED );
113 if ( !execute( webdavRequest, webdavResponse, methodCode, resource ) )
115 super.service( request, response );
119 catch ( UnauthorizedDavException e )
121 webdavResponse.setHeader( "WWW-Authenticate", getAuthenticateHeaderValue( e.getRepositoryName() ) );
122 webdavResponse.sendError( e.getErrorCode(), e.getStatusPhrase() );
124 catch ( BrowserRedirectException e )
126 response.sendRedirect( e.getLocation() );
128 catch ( DavException e )
130 if ( e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED )
132 final String msg = "Should throw " + UnauthorizedDavException.class.getName();
134 webdavResponse.sendError( e.getErrorCode(), msg );
136 else if ( e.getCause() != null )
138 webdavResponse.sendError( e.getErrorCode(), e.getCause().getMessage() );
142 webdavResponse.sendError( e.getErrorCode(), e.getMessage() );
147 getDavSessionProvider().releaseSession( webdavRequest );
151 public synchronized void initServers( ServletConfig servletConfig )
153 WebApplicationContext wac =
154 WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
157 (ArchivaConfiguration) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaConfiguration.class.getName() ) );
158 configuration.addListener( this );
160 repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap();
162 for ( ManagedRepositoryConfiguration repo : repositoryMap.values() )
164 File repoDir = new File( repo.getLocation() );
166 if ( !repoDir.exists() )
168 if ( !repoDir.mkdirs() )
170 // Skip invalid directories.
171 log( "Unable to create missing directory for " + repo.getLocation() );
178 (DavResourceFactory) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaDavResourceFactory.class ) );
179 locatorFactory = new ArchivaDavLocatorFactory();
181 ServletAuthenticator servletAuth =
182 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
183 HttpAuthenticator httpAuth =
184 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
186 sessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
189 public void configurationEvent( ConfigurationEvent event )
191 if ( event.getType() == ConfigurationEvent.SAVED )
197 private void initRepositories()
199 synchronized ( repositoryMap )
201 repositoryMap.clear();
202 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
205 synchronized ( reloadLock )
207 initServers( getServletConfig() );
211 public synchronized ManagedRepositoryConfiguration getRepository( String prefix )
213 if ( repositoryMap.isEmpty() )
215 repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() );
217 return repositoryMap.get( prefix );
220 ArchivaConfiguration getConfiguration()
222 return configuration;
225 protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource )
227 // check for read or write access to the resource when resource-based permission is implemented
232 public DavSessionProvider getDavSessionProvider()
234 return sessionProvider;
237 public void setDavSessionProvider( final DavSessionProvider davSessionProvider )
239 this.sessionProvider = davSessionProvider;
242 public DavLocatorFactory getLocatorFactory()
244 return locatorFactory;
247 public void setLocatorFactory( final DavLocatorFactory davLocatorFactory )
249 locatorFactory = davLocatorFactory;
252 public DavResourceFactory getResourceFactory()
254 return resourceFactory;
257 public void setResourceFactory( final DavResourceFactory davResourceFactory )
259 resourceFactory = davResourceFactory;
262 public String getAuthenticateHeaderValue()
264 throw new UnsupportedOperationException();
267 public String getAuthenticateHeaderValue( String repository )
269 return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\"";