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.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
31 import javax.servlet.http.HttpServletResponse;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.jackrabbit.webdav.DavException;
36 import org.apache.jackrabbit.webdav.DavResource;
37 import org.apache.jackrabbit.webdav.DavResourceFactory;
38 import org.apache.jackrabbit.webdav.DavResourceLocator;
39 import org.apache.jackrabbit.webdav.DavServletRequest;
40 import org.apache.jackrabbit.webdav.DavServletResponse;
41 import org.apache.jackrabbit.webdav.DavSession;
42 import org.apache.jackrabbit.webdav.lock.LockManager;
43 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
44 import org.apache.maven.archiva.common.utils.PathUtil;
45 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
46 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
47 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
48 import org.apache.maven.archiva.model.ArtifactReference;
49 import org.apache.maven.archiva.model.VersionedReference;
50 import org.apache.maven.archiva.policies.ProxyDownloadException;
51 import org.apache.maven.archiva.proxy.RepositoryProxyConnectors;
52 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
53 import org.apache.maven.archiva.repository.RepositoryContentFactory;
54 import org.apache.maven.archiva.repository.RepositoryException;
55 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
56 import org.apache.maven.archiva.repository.audit.AuditEvent;
57 import org.apache.maven.archiva.repository.audit.AuditListener;
58 import org.apache.maven.archiva.repository.audit.Auditable;
59 import org.apache.maven.archiva.repository.content.RepositoryRequest;
60 import org.apache.maven.archiva.repository.layout.LayoutException;
61 import org.apache.maven.archiva.repository.metadata.MetadataTools;
62 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
63 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataMerge;
64 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
65 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
66 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
67 import org.apache.maven.archiva.security.ArchivaXworkUser;
68 import org.apache.maven.archiva.security.ServletAuthenticator;
69 import org.apache.maven.archiva.webdav.util.MimeTypes;
70 import org.apache.maven.archiva.webdav.util.RepositoryPathUtil;
71 import org.apache.maven.archiva.webdav.util.WebdavMethodUtil;
72 import org.apache.maven.model.DistributionManagement;
73 import org.apache.maven.model.Model;
74 import org.apache.maven.model.Relocation;
75 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
76 import org.codehaus.plexus.digest.ChecksumFile;
77 import org.codehaus.plexus.digest.Digester;
78 import org.codehaus.plexus.digest.DigesterException;
79 import org.codehaus.plexus.redback.authentication.AuthenticationException;
80 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
81 import org.codehaus.plexus.redback.authorization.AuthorizationException;
82 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
83 import org.codehaus.plexus.redback.policy.AccountLockedException;
84 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
85 import org.codehaus.plexus.redback.system.SecuritySession;
86 import org.codehaus.plexus.redback.system.SecuritySystemConstants;
87 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
88 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
89 import org.slf4j.Logger;
90 import org.slf4j.LoggerFactory;
92 import com.opensymphony.xwork2.ActionContext;
95 * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
96 * @plexus.component role="org.apache.maven.archiva.webdav.ArchivaDavResourceFactory"
98 public class ArchivaDavResourceFactory
99 implements DavResourceFactory, Auditable
101 private static final String PROXIED_SUFFIX = " (proxied)";
103 private static final String HTTP_PUT_METHOD = "PUT";
105 private Logger log = LoggerFactory.getLogger( ArchivaDavResourceFactory.class );
108 * @plexus.requirement role="org.apache.maven.archiva.repository.audit.AuditListener"
110 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
113 * @plexus.requirement
115 private RepositoryContentFactory repositoryFactory;
118 * @plexus.requirement
120 private RepositoryRequest repositoryRequest;
123 * @plexus.requirement role-hint="default"
125 private RepositoryProxyConnectors connectors;
128 * @plexus.requirement
130 private MetadataTools metadataTools;
133 * @plexus.requirement
135 private MimeTypes mimeTypes;
138 * @plexus.requirement
140 private ArchivaConfiguration archivaConfiguration;
143 * @plexus.requirement
145 private ServletAuthenticator servletAuth;
148 * @plexus.requirement role-hint="basic"
150 private HttpAuthenticator httpAuth;
153 * Lock Manager - use simple implementation from JackRabbit
155 private final LockManager lockManager = new SimpleLockManager();
158 * @plexus.requirement
160 private RepositoryContentConsumers consumers;
163 * @plexus.requirement
165 private ChecksumFile checksum;
168 * @plexus.requirement role-hint="sha1"
170 private Digester digestSha1;
173 * @plexus.requirement role-hint="md5";
175 private Digester digestMd5;
178 * @plexus.requirement
180 private ArchivaXworkUser archivaXworkUser;
182 public DavResource createResource( final DavResourceLocator locator, final DavServletRequest request,
183 final DavServletResponse response )
186 checkLocatorIsInstanceOfRepositoryLocator( locator );
187 ArchivaDavResourceLocator archivaLocator = (ArchivaDavResourceLocator) locator;
189 RepositoryGroupConfiguration repoGroupConfig =
190 archivaConfiguration.getConfiguration().getRepositoryGroupsAsMap().get( archivaLocator.getRepositoryId() );
191 List<String> repositories = new ArrayList<String>();
193 boolean isGet = WebdavMethodUtil.isReadMethod( request.getMethod() );
194 boolean isPut = WebdavMethodUtil.isWriteMethod( request.getMethod() );
196 if ( repoGroupConfig != null )
198 if( WebdavMethodUtil.isWriteMethod( request.getMethod() ) )
200 throw new DavException( HttpServletResponse.SC_METHOD_NOT_ALLOWED,
201 "Write method not allowed for repository groups." );
203 repositories.addAll( repoGroupConfig.getRepositories() );
205 // handle browse requests for virtual repos
206 if ( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ).endsWith( "/" ) )
208 return getResource( request, repositories, archivaLocator );
213 repositories.add( archivaLocator.getRepositoryId() );
216 //MRM-419 - Windows Webdav support. Should not 404 if there is no content.
217 if (StringUtils.isEmpty(archivaLocator.getRepositoryId()))
219 throw new DavException(HttpServletResponse.SC_NO_CONTENT);
222 List<DavResource> availableResources = new ArrayList<DavResource>();
223 List<String> resourcesInAbsolutePath = new ArrayList<String>();
224 DavException e = null;
226 for ( String repositoryId : repositories )
228 ManagedRepositoryContent managedRepository = null;
232 managedRepository = getManagedRepository( repositoryId );
234 catch ( DavException de )
236 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Invalid managed repository <" +
237 repositoryId + ">" );
240 DavResource resource = null;
242 if ( !locator.getResourcePath().startsWith( ArchivaDavResource.HIDDEN_PATH_PREFIX ) )
244 if ( managedRepository != null )
248 if( isAuthorized( request, repositoryId ) )
250 LogicalResource logicalResource =
251 new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
255 resource = doGet( managedRepository, request, archivaLocator, logicalResource );
260 resource = doPut( managedRepository, request, archivaLocator, logicalResource );
264 catch ( DavException de )
270 if( resource == null )
272 e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
276 availableResources.add( resource );
278 String logicalResource = RepositoryPathUtil.getLogicalResource( locator.getResourcePath() );
279 resourcesInAbsolutePath.add( managedRepository.getRepoRoot() + logicalResource );
284 e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Repository does not exist" );
289 if ( availableResources.isEmpty() )
294 String requestedResource = request.getRequestURI();
296 // MRM-872 : merge all available metadata
297 // merge metadata only when requested via the repo group
298 if ( ( repositoryRequest.isMetadata( requestedResource ) || ( requestedResource.endsWith( "metadata.xml.sha1" ) || requestedResource.endsWith( "metadata.xml.md5" ) ) ) &&
299 repoGroupConfig != null )
301 // this should only be at the project level not version level!
302 if( isProjectReference( requestedResource ) )
304 String artifactId = StringUtils.substringBeforeLast( requestedResource.replace( '\\', '/' ), "/" );
305 artifactId = StringUtils.substringAfterLast( artifactId, "/" );
307 ArchivaDavResource res = ( ArchivaDavResource ) availableResources.get( 0 );
308 String filePath = StringUtils.substringBeforeLast( res.getLocalResource().getAbsolutePath().replace( '\\', '/' ), "/" );
309 filePath = filePath + "/maven-metadata-" + repoGroupConfig.getId() + ".xml";
311 // for MRM-872 handle checksums of the merged metadata files
312 if( repositoryRequest.isSupportFile( requestedResource ) )
314 File metadataChecksum = new File( filePath + "."
315 + StringUtils.substringAfterLast( requestedResource, "." ) );
316 if( metadataChecksum.exists() )
318 LogicalResource logicalResource =
319 new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
321 ArchivaDavResource metadataChecksumResource =
322 new ArchivaDavResource( metadataChecksum.getAbsolutePath(), logicalResource.getPath(), null,
323 request.getRemoteAddr(), request.getDavSession(), archivaLocator, this,
324 mimeTypes, auditListeners, consumers, archivaXworkUser );
325 availableResources.add( 0, metadataChecksumResource );
329 { // merge the metadata of all repos under group
330 ArchivaRepositoryMetadata mergedMetadata = new ArchivaRepositoryMetadata();
331 for ( String resourceAbsPath : resourcesInAbsolutePath )
335 File metadataFile = new File( resourceAbsPath );
336 ArchivaRepositoryMetadata repoMetadata = RepositoryMetadataReader.read( metadataFile );
337 mergedMetadata = RepositoryMetadataMerge.merge( mergedMetadata, repoMetadata );
339 catch ( RepositoryMetadataException r )
341 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
342 "Error occurred while reading metadata file." );
348 File resourceFile = writeMergedMetadataToFile( mergedMetadata, filePath );
350 LogicalResource logicalResource =
351 new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
353 ArchivaDavResource metadataResource =
354 new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource.getPath(), null,
355 request.getRemoteAddr(), request.getDavSession(), archivaLocator, this,
356 mimeTypes, auditListeners, consumers, archivaXworkUser );
357 availableResources.add( 0, metadataResource );
359 catch ( RepositoryMetadataException r )
361 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
362 "Error occurred while writing metadata file." );
364 catch ( IOException ie )
366 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
367 "Error occurred while generating checksum files." );
369 catch ( DigesterException de )
371 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
372 "Error occurred while generating checksum files." );
378 DavResource resource = availableResources.get( 0 );
379 setHeaders(response, locator, resource );
381 // compatibility with MRM-440 to ensure browsing the repository works ok
382 if ( resource.isCollection() && !request.getRequestURI().endsWith("/" ) )
384 throw new BrowserRedirectException( resource.getHref() );
386 resource.addLockManager(lockManager);
390 public DavResource createResource( final DavResourceLocator locator, final DavSession davSession )
393 checkLocatorIsInstanceOfRepositoryLocator( locator );
394 ArchivaDavResourceLocator archivaLocator = (ArchivaDavResourceLocator) locator;
396 DavResource resource = null;
397 if ( !locator.getResourcePath().startsWith( ArchivaDavResource.HIDDEN_PATH_PREFIX ) )
399 ManagedRepositoryContent managedRepository = getManagedRepository( archivaLocator.getRepositoryId() );
400 String logicalResource = RepositoryPathUtil.getLogicalResource( locator.getResourcePath() );
401 File resourceFile = new File( managedRepository.getRepoRoot(), logicalResource );
403 new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource,
404 managedRepository.getRepository(), davSession, archivaLocator, this, mimeTypes,
405 auditListeners, consumers, archivaXworkUser );
407 resource.addLockManager(lockManager);
411 private DavResource doGet( ManagedRepositoryContent managedRepository, DavServletRequest request,
412 ArchivaDavResourceLocator locator, LogicalResource logicalResource )
415 File resourceFile = new File( managedRepository.getRepoRoot(), logicalResource.getPath() );
417 //MRM-893, dont send back a file when user intentionally wants a directory
418 if ( locator.getHref( false ).endsWith( "/" ) )
420 if ( ! resourceFile.isDirectory() )
422 //force a resource not found
427 ArchivaDavResource resource =
428 new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource.getPath(),
429 managedRepository.getRepository(), request.getRemoteAddr(),
430 request.getDavSession(), locator, this, mimeTypes, auditListeners, consumers, archivaXworkUser );
432 if ( !resource.isCollection() )
434 boolean previouslyExisted = resourceFile.exists();
436 // At this point the incoming request can either be in default or
437 // legacy layout format.
438 boolean fromProxy = fetchContentFromProxies( managedRepository, request, logicalResource );
442 // Perform an adjustment of the resource to the managed
443 // repository expected path.
444 String localResourcePath =
445 repositoryRequest.toNativePath( logicalResource.getPath(), managedRepository );
446 resourceFile = new File( managedRepository.getRepoRoot(), localResourcePath );
448 catch ( LayoutException e )
450 if ( previouslyExisted )
454 throw new DavException( HttpServletResponse.SC_NOT_FOUND, e );
457 // Attempt to fetch the resource from any defined proxy.
460 String repositoryId = locator.getRepositoryId();
461 String event = ( previouslyExisted ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE ) + PROXIED_SUFFIX;
462 triggerAuditEvent( request.getRemoteAddr(), repositoryId, logicalResource.getPath(), event );
465 if ( !resourceFile.exists() )
472 new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource.getPath(),
473 managedRepository.getRepository(), request.getRemoteAddr(),
474 request.getDavSession(), locator, this, mimeTypes, auditListeners,
475 consumers, archivaXworkUser );
481 private DavResource doPut( ManagedRepositoryContent managedRepository, DavServletRequest request,
482 ArchivaDavResourceLocator locator, LogicalResource logicalResource )
486 * Create parent directories that don't exist when writing a file This actually makes this implementation not
487 * compliant to the WebDAV RFC - but we have enough knowledge about how the collection is being used to do this
488 * reasonably and some versions of Maven's WebDAV don't correctly create the collections themselves.
491 File rootDirectory = new File( managedRepository.getRepoRoot() );
492 File destDir = new File( rootDirectory, logicalResource.getPath() ).getParentFile();
494 if ( request.getMethod().equals(HTTP_PUT_METHOD) && !destDir.exists() )
497 String relPath = PathUtil.getRelative( rootDirectory.getAbsolutePath(), destDir );
498 triggerAuditEvent( request.getRemoteAddr(), logicalResource.getPath(), relPath, AuditEvent.CREATE_DIR );
501 File resourceFile = new File( managedRepository.getRepoRoot(), logicalResource.getPath() );
503 return new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource.getPath(),
504 managedRepository.getRepository(), request.getRemoteAddr(),
505 request.getDavSession(), locator, this, mimeTypes, auditListeners, consumers, archivaXworkUser );
508 private boolean fetchContentFromProxies( ManagedRepositoryContent managedRepository, DavServletRequest request,
509 LogicalResource resource )
512 if ( repositoryRequest.isSupportFile( resource.getPath() ) )
514 File proxiedFile = connectors.fetchFromProxies( managedRepository, resource.getPath() );
516 return ( proxiedFile != null );
519 // Is it a Metadata resource?
520 if ( repositoryRequest.isDefault( resource.getPath() ) && repositoryRequest.isMetadata( resource.getPath() ) )
522 return connectors.fetchMetatadaFromProxies(managedRepository, resource.getPath()) != null;
525 // Not any of the above? Then it's gotta be an artifact reference.
528 // Get the artifact reference in a layout neutral way.
529 ArtifactReference artifact = repositoryRequest.toArtifactReference( resource.getPath() );
531 if ( artifact != null )
533 applyServerSideRelocation( managedRepository, artifact );
535 File proxiedFile = connectors.fetchFromProxies( managedRepository, artifact );
537 resource.setPath( managedRepository.toPath( artifact ) );
539 return ( proxiedFile != null );
542 catch ( LayoutException e )
546 catch ( ProxyDownloadException e )
548 log.error( e.getMessage(), e );
549 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to fetch artifact resource." );
555 * A relocation capable client will request the POM prior to the artifact, and will then read meta-data and do
556 * client side relocation. A simplier client (like maven 1) will only request the artifact and not use the
559 * For such clients, archiva does server-side relocation by reading itself the <relocation> element in
560 * metadatas and serving the expected artifact.
562 protected void applyServerSideRelocation( ManagedRepositoryContent managedRepository, ArtifactReference artifact )
563 throws ProxyDownloadException
565 if ( "pom".equals( artifact.getType() ) )
570 // Build the artifact POM reference
571 ArtifactReference pomReference = new ArtifactReference();
572 pomReference.setGroupId( artifact.getGroupId() );
573 pomReference.setArtifactId( artifact.getArtifactId() );
574 pomReference.setVersion( artifact.getVersion() );
575 pomReference.setType( "pom" );
577 // Get the artifact POM from proxied repositories if needed
578 connectors.fetchFromProxies( managedRepository, pomReference );
580 // Open and read the POM from the managed repo
581 File pom = managedRepository.toFile( pomReference );
590 // MavenXpp3Reader leaves the file open, so we need to close it ourselves.
591 FileReader reader = new FileReader( pom );
592 Model model = new MavenXpp3Reader().read( reader );
595 DistributionManagement dist = model.getDistributionManagement();
598 Relocation relocation = dist.getRelocation();
599 if ( relocation != null )
601 // artifact is relocated : update the repositoryPath
602 if ( relocation.getGroupId() != null )
604 artifact.setGroupId( relocation.getGroupId() );
606 if ( relocation.getArtifactId() != null )
608 artifact.setArtifactId( relocation.getArtifactId() );
610 if ( relocation.getVersion() != null )
612 artifact.setVersion( relocation.getVersion() );
617 catch ( FileNotFoundException e )
619 // Artifact has no POM in repo : ignore
621 catch ( IOException e )
623 // Unable to read POM : ignore.
625 catch ( XmlPullParserException e )
627 // Invalid POM : ignore
632 private void triggerAuditEvent( String remoteIP, String repositoryId, String resource, String action )
634 String activePrincipal = archivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
635 AuditEvent event = new AuditEvent( repositoryId, activePrincipal, resource, action );
636 event.setRemoteIP( remoteIP );
638 for ( AuditListener listener : auditListeners )
640 listener.auditEvent( event );
644 public void addAuditListener( AuditListener listener )
646 this.auditListeners.add( listener );
649 public void clearAuditListeners()
651 this.auditListeners.clear();
654 public void removeAuditListener( AuditListener listener )
656 this.auditListeners.remove( listener );
659 private void setHeaders( DavServletResponse response, DavResourceLocator locator, DavResource resource )
661 // [MRM-503] - Metadata file need Pragma:no-cache response
663 if ( locator.getResourcePath().endsWith( "/maven-metadata.xml" ) )
665 response.addHeader( "Pragma", "no-cache" );
666 response.addHeader( "Cache-Control", "no-cache" );
669 //We need to specify this so connecting wagons can work correctly
670 response.addDateHeader("last-modified", resource.getModificationTime());
672 // TODO: [MRM-524] determine http caching options for other types of files (artifacts, sha1, md5, snapshots)
675 private ManagedRepositoryContent getManagedRepository( String respositoryId )
678 if ( respositoryId != null )
682 return repositoryFactory.getManagedRepositoryContent( respositoryId );
684 catch ( RepositoryNotFoundException e )
686 throw new DavException( HttpServletResponse.SC_NOT_FOUND, e );
688 catch ( RepositoryException e )
690 throw new DavException( HttpServletResponse.SC_NOT_FOUND, e );
696 private void checkLocatorIsInstanceOfRepositoryLocator( DavResourceLocator locator )
699 if ( !( locator instanceof RepositoryLocator ) )
701 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
702 "Locator does not implement RepositoryLocator" );
706 class LogicalResource
710 public LogicalResource( String path )
715 public String getPath()
720 public void setPath( String path )
726 protected boolean isAuthorized( DavServletRequest request, String repositoryId )
731 AuthenticationResult result = httpAuth.getAuthenticationResult( request, null );
732 SecuritySession securitySession = httpAuth.getSecuritySession( request.getSession( true ) );
734 return servletAuth.isAuthenticated( request, result ) &&
735 servletAuth.isAuthorized( request, securitySession, repositoryId,
736 WebdavMethodUtil.isWriteMethod( request.getMethod() ) );
738 catch ( AuthenticationException e )
740 boolean isPut = WebdavMethodUtil.isWriteMethod( request.getMethod() );
742 // safety check for MRM-911
743 String guest = archivaXworkUser.getGuest();
746 if( servletAuth.isAuthorized( guest,
747 ( ( ArchivaDavResourceLocator ) request.getRequestLocator() ).getRepositoryId(), isPut ) )
752 catch ( UnauthorizedException ae )
754 throw new UnauthorizedDavException( repositoryId,
755 "You are not authenticated and authorized to access any repository." );
758 throw new UnauthorizedDavException( repositoryId, "You are not authenticated" );
760 catch ( MustChangePasswordException e )
762 throw new UnauthorizedDavException( repositoryId, "You must change your password." );
764 catch ( AccountLockedException e )
766 throw new UnauthorizedDavException( repositoryId, "User account is locked." );
768 catch ( AuthorizationException e )
770 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
771 "Fatal Authorization Subsystem Error." );
773 catch ( UnauthorizedException e )
775 throw new UnauthorizedDavException( repositoryId, e.getMessage() );
779 private DavResource getResource( DavServletRequest request, List<String> repositories, ArchivaDavResourceLocator locator )
782 List<File> mergedRepositoryContents = new ArrayList<File>();
783 LogicalResource logicalResource =
784 new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
787 // if the current user logged in has permission to any of the repositories, allow user to
788 // browse the repo group but displaying only the repositories which the user has permission to access.
789 // otherwise, prompt for authentication.
791 // put the current session in the session map which will be passed to ArchivaXworkUser
792 Map<String, Object> sessionMap = new HashMap<String, Object>();
793 if( request.getSession().getAttribute( SecuritySystemConstants.SECURITY_SESSION_KEY ) != null )
795 sessionMap.put( SecuritySystemConstants.SECURITY_SESSION_KEY,
796 request.getSession().getAttribute( SecuritySystemConstants.SECURITY_SESSION_KEY ) );
799 String activePrincipal = archivaXworkUser.getActivePrincipal( sessionMap );
800 boolean allow = isAllowedToContinue( request, repositories, activePrincipal );
804 boolean isPut = WebdavMethodUtil.isWriteMethod( request.getMethod() );
806 for( String repository : repositories )
808 // for prompted authentication
809 if( httpAuth.getSecuritySession( request.getSession( true ) ) != null )
813 if( isAuthorized( request, repository ) )
815 getResource( locator, mergedRepositoryContents, logicalResource, repository );
818 catch ( DavException e )
825 // for the current user logged in
828 if( servletAuth.isAuthorized( activePrincipal, repository, isPut ) )
830 getResource( locator, mergedRepositoryContents, logicalResource, repository );
833 catch ( UnauthorizedException e )
842 throw new UnauthorizedDavException( locator.getRepositoryId(), "User not authorized." );
845 ArchivaVirtualDavResource resource =
846 new ArchivaVirtualDavResource( mergedRepositoryContents, logicalResource.getPath(), mimeTypes, locator, this );
848 // compatibility with MRM-440 to ensure browsing the repository group works ok
849 if ( resource.isCollection() && !request.getRequestURI().endsWith("/" ) )
851 throw new BrowserRedirectException( resource.getHref() );
857 private void getResource( ArchivaDavResourceLocator locator, List<File> mergedRepositoryContents,
858 LogicalResource logicalResource, String repository )
861 ManagedRepositoryContent managedRepository = null;
865 managedRepository = getManagedRepository( repository );
867 catch ( DavException de )
869 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Invalid managed repository <" +
873 if ( !locator.getResourcePath().startsWith( ArchivaVirtualDavResource.HIDDEN_PATH_PREFIX ) )
875 if( managedRepository != null )
877 File resourceFile = new File( managedRepository.getRepoRoot(), logicalResource.getPath() );
878 if( resourceFile.exists() )
880 mergedRepositoryContents.add( resourceFile );
887 * Check if the current user is authorized to access any of the repos
890 * @param repositories
891 * @param activePrincipal
894 private boolean isAllowedToContinue( DavServletRequest request, List<String> repositories, String activePrincipal )
896 boolean allow = false;
899 // if securitySession != null, it means that the user was prompted for authentication
900 if( httpAuth.getSecuritySession( request.getSession() ) != null )
902 for( String repository : repositories )
906 if( isAuthorized( request, repository ) )
912 catch( DavException e )
920 boolean isPut = WebdavMethodUtil.isWriteMethod( request.getMethod() );
921 for( String repository : repositories )
925 if( servletAuth.isAuthorized( activePrincipal, repository, isPut ) )
931 catch ( UnauthorizedException e )
941 private File writeMergedMetadataToFile( ArchivaRepositoryMetadata mergedMetadata, String outputFilename )
942 throws RepositoryMetadataException, DigesterException, IOException
944 File outputFile = new File( outputFilename );
945 if( outputFile.exists() )
947 FileUtils.deleteQuietly( outputFile );
950 outputFile.getParentFile().mkdirs();
951 RepositoryMetadataWriter.write( mergedMetadata, outputFile );
953 createChecksumFile( outputFilename, digestSha1 );
954 createChecksumFile( outputFilename, digestMd5 );
959 private void createChecksumFile( String path, Digester digester )
960 throws DigesterException, IOException
962 File checksumFile = new File( path + digester.getFilenameExtension() );
963 if ( !checksumFile.exists() )
965 FileUtils.deleteQuietly( checksumFile );
966 checksum.createChecksum( new File( path ), digester );
968 else if ( !checksumFile.isFile() )
970 log.error( "Checksum file is not a file." );
974 private boolean isProjectReference( String requestedResource )
978 VersionedReference versionRef = metadataTools.toVersionedReference( requestedResource );
981 catch ( RepositoryMetadataException re )
987 public void setServletAuth( ServletAuthenticator servletAuth )
989 this.servletAuth = servletAuth;
992 public void setHttpAuth( HttpAuthenticator httpAuth )
994 this.httpAuth = httpAuth;