1 package org.apache.maven.archiva.web.repository;
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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.model.ArchivaRepository;
28 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
29 import org.apache.maven.archiva.model.ArtifactReference;
30 import org.apache.maven.archiva.model.ProjectReference;
31 import org.apache.maven.archiva.model.VersionedReference;
32 import org.apache.maven.archiva.proxy.ProxyConnector;
33 import org.apache.maven.archiva.proxy.ProxyException;
34 import org.apache.maven.archiva.proxy.RepositoryProxyConnectors;
35 import org.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
36 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
37 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
38 import org.apache.maven.archiva.repository.layout.LayoutException;
39 import org.apache.maven.wagon.ResourceDoesNotExistException;
40 import org.apache.maven.wagon.proxy.ProxyInfo;
41 import org.codehaus.plexus.webdav.AbstractDavServerComponent;
42 import org.codehaus.plexus.webdav.DavServerComponent;
43 import org.codehaus.plexus.webdav.DavServerException;
44 import org.codehaus.plexus.webdav.servlet.DavServerRequest;
45 import org.codehaus.plexus.webdav.util.WebdavMethodUtil;
48 import java.io.IOException;
49 import java.lang.reflect.Proxy;
50 import java.util.ArrayList;
51 import java.util.Iterator;
52 import java.util.List;
54 import javax.servlet.ServletConfig;
55 import javax.servlet.ServletException;
56 import javax.servlet.http.HttpServletResponse;
61 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
63 * @plexus.component role="org.codehaus.plexus.webdav.DavServerComponent"
65 * instantiation-strategy="per-lookup"
67 public class ProxiedDavServer
68 extends AbstractDavServerComponent
71 * @plexus.requirement role-hint="simple"
73 private DavServerComponent davServer;
78 private ArchivaConfiguration archivaConfiguration;
81 * @plexus.requirement role-hint="default"
83 private RepositoryProxyConnectors connectors;
88 private BidirectionalRepositoryLayoutFactory layoutFactory;
90 private BidirectionalRepositoryLayout layout;
92 private RepositoryConfiguration repositoryConfiguration;
94 private ArchivaRepository managedRepository;
96 private List/*<ArtifactRepository>*/proxiedRepositories;
98 public String getPrefix()
100 return davServer.getPrefix();
103 public File getRootDirectory()
105 return davServer.getRootDirectory();
108 public void setPrefix( String prefix )
110 davServer.setPrefix( prefix );
113 public void setRootDirectory( File rootDirectory )
115 davServer.setRootDirectory( rootDirectory );
118 public void init( ServletConfig servletConfig )
119 throws DavServerException
121 davServer.init( servletConfig );
123 proxiedRepositories = new ArrayList();
125 Configuration config = archivaConfiguration.getConfiguration();
127 repositoryConfiguration = config.findRepositoryById( getPrefix() );
129 managedRepository = ArchivaConfigurationAdaptor.toArchivaRepository( repositoryConfiguration );
133 layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
135 catch ( LayoutException e )
137 throw new DavServerException( "Unable to initialize dav server: " + e.getMessage(), e );
141 public void process( DavServerRequest request, HttpServletResponse response )
142 throws DavServerException, ServletException, IOException
144 if ( WebdavMethodUtil.isReadMethod( request.getRequest().getMethod() ) )
146 if ( !hasResource( request.getLogicalResource() ) )
148 fetchContentFromProxies( request );
152 davServer.process( request, response );
155 private void fetchContentFromProxies( DavServerRequest request )
156 throws ServletException
158 String resource = request.getLogicalResource();
160 if( resource.endsWith( ".sha1" ) ||
161 resource.endsWith( ".md5") )
163 // Checksums are fetched with artifact / metadata.
169 ProjectReference project;
170 VersionedReference versioned;
171 ArtifactReference artifact;
173 artifact = layout.toArtifactReference( resource );
174 if( artifact != null )
176 connectors.fetchFromProxies( managedRepository, artifact );
180 versioned = layout.toVersionedReference( resource );
181 if( versioned != null )
183 connectors.fetchFromProxies( managedRepository, versioned );
187 project = layout.toProjectReference( resource );
188 if( project != null )
190 connectors.fetchFromProxies( managedRepository, project );
194 catch ( ResourceDoesNotExistException e )
196 // return an HTTP 404 instead of HTTP 500 error.
199 catch ( ProxyException e )
201 throw new ServletException( "Unable to fetch resource.", e );
205 public RepositoryConfiguration getRepositoryConfiguration()
207 return repositoryConfiguration;