1 package org.apache.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.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ConfigurationEvent;
25 import org.apache.archiva.configuration.ConfigurationListener;
26 import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.RepositoryRegistry;
29 import org.apache.archiva.repository.storage.StorageAsset;
30 import org.apache.archiva.security.ServletAuthenticator;
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.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.context.ConfigurableApplicationContext;
46 import org.springframework.web.context.WebApplicationContext;
47 import org.springframework.web.context.support.WebApplicationContextUtils;
49 import javax.servlet.ServletConfig;
50 import javax.servlet.ServletException;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import java.io.IOException;
54 import java.util.LinkedHashMap;
56 import java.util.concurrent.locks.ReentrantReadWriteLock;
57 import java.util.stream.Collectors;
62 public class RepositoryServlet
63 extends AbstractWebdavServlet
64 implements ConfigurationListener
66 private Logger log = LoggerFactory.getLogger( RepositoryServlet.class );
68 private ArchivaConfiguration configuration;
70 RepositoryRegistry repositoryRegistry;
72 private Map<String, ManagedRepository> repositoryMap;
74 private DavLocatorFactory locatorFactory;
76 private DavResourceFactory resourceFactory;
78 private DavSessionProvider sessionProvider;
80 protected final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
83 public void init( ServletConfig servletConfig )
84 throws ServletException
86 super.init( servletConfig );
87 initServers( servletConfig );
91 * Service the given request. This method has been overridden and copy/pasted to allow better exception handling and
92 * to support different realms
96 * @throws ServletException
97 * @throws java.io.IOException
100 protected void service( HttpServletRequest request, HttpServletResponse response )
101 throws ServletException, IOException
103 WebdavRequest webdavRequest = new WebdavRequestImpl( request, getLocatorFactory() );
104 // DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
105 int methodCode = DavMethods.getMethodCode( request.getMethod() );
106 boolean noCache = DavMethods.isDeltaVMethod( webdavRequest ) && !( DavMethods.DAV_VERSION_CONTROL == methodCode
107 || DavMethods.DAV_REPORT == methodCode );
108 WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
109 DavResource resource = null;
113 // make sure there is a authenticated user
114 if ( !getDavSessionProvider().attachSession( webdavRequest ) )
119 // check matching if=header for lock-token relevant operations
121 getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
123 if ( !isPreconditionValid( webdavRequest, resource ) )
125 webdavResponse.sendError( DavServletResponse.SC_PRECONDITION_FAILED );
128 if ( !execute( webdavRequest, webdavResponse, methodCode, resource ) )
130 super.service( request, response );
134 catch ( UnauthorizedDavException e )
136 webdavResponse.setHeader( "WWW-Authenticate", getAuthenticateHeaderValue( e.getRepositoryName() ) );
137 webdavResponse.sendError( e.getErrorCode(), e.getStatusPhrase() );
139 catch ( BrowserRedirectException e )
141 response.sendRedirect( e.getLocation() );
143 catch ( DavException e )
145 if ( e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED )
147 final String msg = "Should throw " + UnauthorizedDavException.class.getName();
149 webdavResponse.sendError( e.getErrorCode(), msg );
151 else if ( e.getCause() != null )
153 webdavResponse.sendError( e.getErrorCode(), e.getCause().getMessage() );
157 webdavResponse.sendError( e.getErrorCode(), e.getMessage() );
162 getDavSessionProvider().releaseSession( webdavRequest );
166 public void initServers( ServletConfig servletConfig ) {
168 long start = System.currentTimeMillis();
170 WebApplicationContext wac =
171 WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
173 rwLock.writeLock().lock();
175 configuration = wac.getBean("archivaConfiguration#default", ArchivaConfiguration.class);
176 configuration.addListener(this);
178 repositoryRegistry = wac.getBean(RepositoryRegistry.class);
179 repositoryMap = new LinkedHashMap<>();
183 for (ManagedRepository repo : repositoryMap.values()) {
184 StorageAsset repoDir = repo.getAsset("");
186 if (!repoDir.exists()) {
189 } catch (IOException e) {
190 log.info("Unable to create missing directory for {}", repo.getLocation());
196 resourceFactory = wac.getBean("davResourceFactory#archiva", DavResourceFactory.class);
197 locatorFactory = new ArchivaDavLocatorFactory();
199 ServletAuthenticator servletAuth = wac.getBean(ServletAuthenticator.class);
200 HttpAuthenticator httpAuth = wac.getBean("httpAuthenticator#basic", HttpAuthenticator.class);
202 sessionProvider = new ArchivaDavSessionProvider(servletAuth, httpAuth);
204 rwLock.writeLock().unlock();
206 long end = System.currentTimeMillis();
208 log.debug( "initServers done in {} ms", (end - start) );
211 private void fillRepositoryMap() {
212 final Map<String, ManagedRepository> repos = repositoryRegistry.getManagedRepositories().stream().collect(Collectors.toMap(r -> r.getId(), r -> r));
213 rwLock.writeLock().lock();
215 repositoryMap.clear();
216 repositoryMap.putAll(repos);
218 rwLock.writeLock().unlock();
223 public void configurationEvent( ConfigurationEvent event )
225 if ( event.getType() == ConfigurationEvent.SAVED )
231 catch ( RepositoryAdminException e )
233 log.error( e.getMessage(), e );
234 throw new RuntimeException( e.getMessage(), e );
239 private void initRepositories()
240 throws RepositoryAdminException
242 initServers( getServletConfig() );
245 public ManagedRepository getRepository( String prefix )
246 throws RepositoryAdminException
248 rwLock.readLock().lock();
250 if (repositoryMap.isEmpty()) {
251 rwLock.readLock().unlock();
253 rwLock.readLock().lock();
255 return repositoryMap.get(prefix);
257 rwLock.readLock().unlock();
261 ArchivaConfiguration getConfiguration()
263 return configuration;
267 protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource )
269 // check for read or write access to the resource when resource-based permission is implemented
275 public DavSessionProvider getDavSessionProvider()
277 return sessionProvider;
281 public void setDavSessionProvider( final DavSessionProvider davSessionProvider )
283 this.sessionProvider = davSessionProvider;
287 public DavLocatorFactory getLocatorFactory()
289 return locatorFactory;
293 public void setLocatorFactory( final DavLocatorFactory davLocatorFactory )
295 locatorFactory = davLocatorFactory;
299 public DavResourceFactory getResourceFactory()
301 return resourceFactory;
305 public void setResourceFactory( final DavResourceFactory davResourceFactory )
307 resourceFactory = davResourceFactory;
311 public String getAuthenticateHeaderValue()
313 throw new UnsupportedOperationException();
316 public String getAuthenticateHeaderValue( String repository )
318 return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\"";
322 public void destroy()
324 rwLock.writeLock().lock();
326 configuration.removeListener(this);
328 resourceFactory = null;
329 configuration = null;
330 locatorFactory = null;
331 sessionProvider = null;
332 repositoryMap.clear();
333 repositoryMap = null;
335 WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
337 if (wac instanceof ConfigurableApplicationContext) {
338 ((ConfigurableApplicationContext) wac).close();
342 rwLock.writeLock().unlock();