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.security.ServletAuthenticator;
30 import org.apache.jackrabbit.webdav.DavException;
31 import org.apache.jackrabbit.webdav.DavLocatorFactory;
32 import org.apache.jackrabbit.webdav.DavMethods;
33 import org.apache.jackrabbit.webdav.DavResource;
34 import org.apache.jackrabbit.webdav.DavResourceFactory;
35 import org.apache.jackrabbit.webdav.DavServletResponse;
36 import org.apache.jackrabbit.webdav.DavSessionProvider;
37 import org.apache.jackrabbit.webdav.WebdavRequest;
38 import org.apache.jackrabbit.webdav.WebdavRequestImpl;
39 import org.apache.jackrabbit.webdav.WebdavResponse;
40 import org.apache.jackrabbit.webdav.WebdavResponseImpl;
41 import org.apache.jackrabbit.webdav.server.AbstractWebdavServlet;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.ConfigurableApplicationContext;
45 import org.springframework.web.context.WebApplicationContext;
46 import org.springframework.web.context.support.WebApplicationContextUtils;
48 import javax.servlet.ServletConfig;
49 import javax.servlet.ServletException;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import java.io.IOException;
53 import java.nio.file.Files;
54 import java.nio.file.Path;
55 import java.nio.file.Paths;
56 import java.util.LinkedHashMap;
58 import java.util.concurrent.locks.ReentrantReadWriteLock;
59 import java.util.stream.Collectors;
64 public class RepositoryServlet
65 extends AbstractWebdavServlet
66 implements ConfigurationListener
68 private Logger log = LoggerFactory.getLogger( RepositoryServlet.class );
70 private ArchivaConfiguration configuration;
72 RepositoryRegistry repositoryRegistry;
74 private Map<String, ManagedRepository> repositoryMap;
76 private DavLocatorFactory locatorFactory;
78 private DavResourceFactory resourceFactory;
80 private DavSessionProvider sessionProvider;
82 protected final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
85 public void init( ServletConfig servletConfig )
86 throws ServletException
88 super.init( servletConfig );
89 initServers( servletConfig );
93 * Service the given request. This method has been overridden and copy/pasted to allow better exception handling and
94 * to support different realms
98 * @throws ServletException
99 * @throws java.io.IOException
102 protected void service( HttpServletRequest request, HttpServletResponse response )
103 throws ServletException, IOException
105 WebdavRequest webdavRequest = new WebdavRequestImpl( request, getLocatorFactory() );
106 // DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
107 int methodCode = DavMethods.getMethodCode( request.getMethod() );
108 boolean noCache = DavMethods.isDeltaVMethod( webdavRequest ) && !( DavMethods.DAV_VERSION_CONTROL == methodCode
109 || DavMethods.DAV_REPORT == methodCode );
110 WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
111 DavResource resource = null;
115 // make sure there is a authenticated user
116 if ( !getDavSessionProvider().attachSession( webdavRequest ) )
121 // check matching if=header for lock-token relevant operations
123 getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
125 if ( !isPreconditionValid( webdavRequest, resource ) )
127 webdavResponse.sendError( DavServletResponse.SC_PRECONDITION_FAILED );
130 if ( !execute( webdavRequest, webdavResponse, methodCode, resource ) )
132 super.service( request, response );
136 catch ( UnauthorizedDavException e )
138 webdavResponse.setHeader( "WWW-Authenticate", getAuthenticateHeaderValue( e.getRepositoryName() ) );
139 webdavResponse.sendError( e.getErrorCode(), e.getStatusPhrase() );
141 catch ( BrowserRedirectException e )
143 response.sendRedirect( e.getLocation() );
145 catch ( DavException e )
147 if ( e.getErrorCode() == HttpServletResponse.SC_UNAUTHORIZED )
149 final String msg = "Should throw " + UnauthorizedDavException.class.getName();
151 webdavResponse.sendError( e.getErrorCode(), msg );
153 else if ( e.getCause() != null )
155 webdavResponse.sendError( e.getErrorCode(), e.getCause().getMessage() );
159 webdavResponse.sendError( e.getErrorCode(), e.getMessage() );
164 getDavSessionProvider().releaseSession( webdavRequest );
168 public void initServers( ServletConfig servletConfig ) {
170 long start = System.currentTimeMillis();
172 WebApplicationContext wac =
173 WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
175 rwLock.writeLock().lock();
177 configuration = wac.getBean("archivaConfiguration#default", ArchivaConfiguration.class);
178 configuration.addListener(this);
180 repositoryRegistry = wac.getBean(RepositoryRegistry.class);
181 repositoryMap = new LinkedHashMap<>();
185 for (ManagedRepository repo : repositoryMap.values()) {
186 Path repoDir = Paths.get(repo.getLocation());
188 if (!Files.exists(repoDir)) {
190 Files.createDirectories(repoDir);
191 } catch (IOException e) {
192 log.info("Unable to create missing directory for {}", repo.getLocation());
198 resourceFactory = wac.getBean("davResourceFactory#archiva", DavResourceFactory.class);
199 locatorFactory = new ArchivaDavLocatorFactory();
201 ServletAuthenticator servletAuth = wac.getBean(ServletAuthenticator.class);
202 HttpAuthenticator httpAuth = wac.getBean("httpAuthenticator#basic", HttpAuthenticator.class);
204 sessionProvider = new ArchivaDavSessionProvider(servletAuth, httpAuth);
206 rwLock.writeLock().unlock();
208 long end = System.currentTimeMillis();
210 log.debug( "initServers done in {} ms", (end - start) );
213 private void fillRepositoryMap() {
214 final Map<String, ManagedRepository> repos = repositoryRegistry.getManagedRepositories().stream().collect(Collectors.toMap(r -> r.getId(), r -> r));
215 rwLock.writeLock().lock();
217 repositoryMap.clear();
218 repositoryMap.putAll(repos);
220 rwLock.writeLock().unlock();
225 public void configurationEvent( ConfigurationEvent event )
227 if ( event.getType() == ConfigurationEvent.SAVED )
233 catch ( RepositoryAdminException e )
235 log.error( e.getMessage(), e );
236 throw new RuntimeException( e.getMessage(), e );
241 private void initRepositories()
242 throws RepositoryAdminException
244 initServers( getServletConfig() );
247 public ManagedRepository getRepository( String prefix )
248 throws RepositoryAdminException
250 rwLock.readLock().lock();
252 if (repositoryMap.isEmpty()) {
253 rwLock.readLock().unlock();
255 rwLock.readLock().lock();
257 return repositoryMap.get(prefix);
259 rwLock.readLock().unlock();
263 ArchivaConfiguration getConfiguration()
265 return configuration;
269 protected boolean isPreconditionValid( final WebdavRequest request, final DavResource davResource )
271 // check for read or write access to the resource when resource-based permission is implemented
277 public DavSessionProvider getDavSessionProvider()
279 return sessionProvider;
283 public void setDavSessionProvider( final DavSessionProvider davSessionProvider )
285 this.sessionProvider = davSessionProvider;
289 public DavLocatorFactory getLocatorFactory()
291 return locatorFactory;
295 public void setLocatorFactory( final DavLocatorFactory davLocatorFactory )
297 locatorFactory = davLocatorFactory;
301 public DavResourceFactory getResourceFactory()
303 return resourceFactory;
307 public void setResourceFactory( final DavResourceFactory davResourceFactory )
309 resourceFactory = davResourceFactory;
313 public String getAuthenticateHeaderValue()
315 throw new UnsupportedOperationException();
318 public String getAuthenticateHeaderValue( String repository )
320 return "Basic realm=\"Repository Archiva Managed " + repository + " Repository\"";
324 public void destroy()
326 rwLock.writeLock().lock();
328 configuration.removeListener(this);
330 resourceFactory = null;
331 configuration = null;
332 locatorFactory = null;
333 sessionProvider = null;
334 repositoryMap.clear();
335 repositoryMap = null;
337 WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
339 if (wac instanceof ConfigurableApplicationContext) {
340 ((ConfigurableApplicationContext) wac).close();
344 rwLock.writeLock().unlock();