[MRM-595]

applied patch submitted by nicolas de loof

-change on archiva-webapp ProxiedDavServer to fecth from proxied prior to computing the resource File
-testcase to demonstrate the issue and the Fix.


git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@597318 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2007-11-22 07:38:01 +00:00
parent 177a4c2765
commit 750d651eee
2 changed files with 166 additions and 69 deletions

View File

@ -19,6 +19,18 @@ package org.apache.maven.archiva.web.repository;
* under the License. * under the License.
*/ */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.maven.archiva.common.utils.PathUtil; import org.apache.maven.archiva.common.utils.PathUtil;
import org.apache.maven.archiva.model.ArtifactReference; import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.model.ProjectReference; import org.apache.maven.archiva.model.ProjectReference;
@ -49,18 +61,6 @@ import org.codehaus.plexus.webdav.DavServerListener;
import org.codehaus.plexus.webdav.servlet.DavServerRequest; import org.codehaus.plexus.webdav.servlet.DavServerRequest;
import org.codehaus.plexus.webdav.util.WebdavMethodUtil; import org.codehaus.plexus.webdav.util.WebdavMethodUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
/** /**
* ProxiedDavServer * ProxiedDavServer
* *
@ -166,8 +166,9 @@ public class ProxiedDavServer
{ {
String requestURL = request.getRequest().getRequestURL().toString(); String requestURL = request.getRequest().getRequestURL().toString();
// [MRM-440] - If webdav URL lacks a trailing /, navigating to all links in the listing return 404. // [MRM-440] - If webdav URL lacks a trailing /, navigating to
if( !requestURL.endsWith( "/" ) ) // all links in the listing return 404.
if ( !requestURL.endsWith( "/" ) )
{ {
String redirectToLocation = requestURL + "/"; String redirectToLocation = requestURL + "/";
response.sendRedirect( redirectToLocation ); response.sendRedirect( redirectToLocation );
@ -181,22 +182,30 @@ public class ProxiedDavServer
return; return;
} }
// At this point the incoming request can either be in default or legacy layout format. // At this point the incoming request can either be in default or
// legacy layout format.
try try
{ {
// Perform an adjustment of the resource to the managed repository expected path. boolean fromProxy = fetchContentFromProxies( request, resource );
resource = repositoryRequest.toNativePath( request.getLogicalResource(), managedRepository );
// Perform an adjustment of the resource to the managed
// repository expected path.
resource =
repositoryRequest
.toNativePath( request.getLogicalResource(), managedRepository );
resourceFile = new File( managedRepository.getRepoRoot(), resource ); resourceFile = new File( managedRepository.getRepoRoot(), resource );
// Adjust the pathInfo resource to be in the format that the dav server impl expects. // Adjust the pathInfo resource to be in the format that the dav
request.getRequest().setPathInfo( resource ); // server impl expects.
request.setLogicalResource( resource );
boolean previouslyExisted = resourceFile.exists(); boolean previouslyExisted = resourceFile.exists();
// Attempt to fetch the resource from any defined proxy. // Attempt to fetch the resource from any defined proxy.
if( fetchContentFromProxies( request, resource ) ) if ( fromProxy )
{ {
processAuditEvents( request, resource, previouslyExisted, resourceFile, " (proxied)" ); processAuditEvents( request, resource, previouslyExisted, resourceFile,
" (proxied)" );
} }
} }
catch ( LayoutException e ) catch ( LayoutException e )
@ -210,14 +219,16 @@ public class ProxiedDavServer
if ( resourceFile.exists() ) if ( resourceFile.exists() )
{ {
// [MRM-503] - Metadata file need Pragma:no-cache response header. // [MRM-503] - Metadata file need Pragma:no-cache response
// header.
if ( request.getLogicalResource().endsWith( "/maven-metadata.xml" ) ) if ( request.getLogicalResource().endsWith( "/maven-metadata.xml" ) )
{ {
response.addHeader( "Pragma", "no-cache" ); response.addHeader( "Pragma", "no-cache" );
response.addHeader( "Cache-Control", "no-cache" ); response.addHeader( "Cache-Control", "no-cache" );
} }
// TODO: [MRM-524] determine http caching options for other types of files (artifacts, sha1, md5, snapshots) // TODO: [MRM-524] determine http caching options for other
// types of files (artifacts, sha1, md5, snapshots)
davServer.process( request, response ); davServer.process( request, response );
} }
@ -229,22 +240,24 @@ public class ProxiedDavServer
if ( isPut ) if ( isPut )
{ {
/* Create parent directories that don't exist when writing a file /*
* Create parent directories that don't exist when writing a file
* This actually makes this implementation not compliant to the * This actually makes this implementation not compliant to the
* WebDAV RFC - but we have enough knowledge * WebDAV RFC - but we have enough knowledge about how the
* about how the collection is being used to do this reasonably and * collection is being used to do this reasonably and some versions
* some versions of Maven's WebDAV don't * of Maven's WebDAV don't correctly create the collections
* correctly create the collections themselves. * themselves.
*/ */
File rootDirectory = getRootDirectory(); File rootDirectory = getRootDirectory();
if ( rootDirectory != null ) if ( rootDirectory != null )
{ {
File destDir = new File( rootDirectory, resource ).getParentFile(); File destDir = new File( rootDirectory, resource ).getParentFile();
if( !destDir.exists() ) if ( !destDir.exists() )
{ {
destDir.mkdirs(); destDir.mkdirs();
String relPath = PathUtil.getRelative( rootDirectory.getAbsolutePath(), destDir ); String relPath =
PathUtil.getRelative( rootDirectory.getAbsolutePath(), destDir );
triggerAuditEvent( request, relPath, AuditEvent.CREATE_DIR ); triggerAuditEvent( request, relPath, AuditEvent.CREATE_DIR );
} }
} }
@ -263,7 +276,8 @@ public class ProxiedDavServer
} }
} }
private void respondResourceMissing( DavServerRequest request, HttpServletResponse response, Throwable t ) private void respondResourceMissing( DavServerRequest request, HttpServletResponse response,
Throwable t )
{ {
response.setStatus( HttpServletResponse.SC_NOT_FOUND ); response.setStatus( HttpServletResponse.SC_NOT_FOUND );
@ -341,8 +355,9 @@ public class ProxiedDavServer
File proxiedFile = connectors.fetchFromProxies( managedRepository, artifact ); File proxiedFile = connectors.fetchFromProxies( managedRepository, artifact );
// Set the path to the resource using managed repository specific layout format. // Set the path to the resource using managed repository
request.getRequest().setPathInfo( managedRepository.toPath( artifact ) ); // specific layout format.
request.setLogicalResource( managedRepository.toPath( artifact ) );
return ( proxiedFile != null ); return ( proxiedFile != null );
} }
} }
@ -434,7 +449,7 @@ public class ProxiedDavServer
// Open and read the POM from the managed repo // Open and read the POM from the managed repo
File pom = managedRepository.toFile( pomReference ); File pom = managedRepository.toFile( pomReference );
if( !pom.exists() ) if ( !pom.exists() )
{ {
return; return;
} }
@ -515,10 +530,10 @@ public class ProxiedDavServer
return managedRepository; return managedRepository;
} }
private void processAuditEvents( DavServerRequest request, String resource, boolean previouslyExisted, private void processAuditEvents( DavServerRequest request, String resource,
File resourceFile, String suffix ) boolean previouslyExisted, File resourceFile, String suffix )
{ {
if( suffix == null ) if ( suffix == null )
{ {
suffix = ""; suffix = "";
} }
@ -570,7 +585,8 @@ public class ProxiedDavServer
private void triggerAuditEvent( DavServerRequest request, String resource, String action ) private void triggerAuditEvent( DavServerRequest request, String resource, String action )
{ {
triggerAuditEvent( archivaUser.getActivePrincipal(), getRemoteIP( request ), resource, action ); triggerAuditEvent( archivaUser.getActivePrincipal(), getRemoteIP( request ), resource,
action );
} }
private String getRemoteIP( DavServerRequest request ) private String getRemoteIP( DavServerRequest request )

View File

@ -0,0 +1,81 @@
package org.apache.maven.archiva.web.repository;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import org.apache.maven.archiva.policies.ReleasesPolicy;
import java.io.File;
/**
* RepositoryServlet Tests, Proxied, Get of Release Artifacts, with varying policy settings.
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id: RepositoryServletProxiedReleasePolicyTest.java 590908 2007-11-01 06:21:26Z joakime $
*/
public class RepositoryServletProxiedRelocatedTest
extends AbstractRepositoryServletProxiedTestCase
{
public void testGetProxiedReleaseArtifactPolicyOncePass()
throws Exception
{
// --- Setup
setupCentralRemoteRepo();
setupCleanInternalRepo();
String resourcePath = "org/apache/archiva/test/1.0/test-1.0.jar";
String expectedRemoteContents = "archiva-test-1.0|jar-remote-contents";
populateRepo( remoteCentral, resourcePath, expectedRemoteContents );
resourcePath = "archiva/test/1.0/test-1.0.pom";
String pom = "<project>" +
"<modelVersion>4.0.0</modelVersion>" +
"<groupId>archiva</groupId>" +
"<artifactId>test</artifactId>" +
"<version>1.0</version>" +
"<distributionManagement>" +
"<relocation>" +
"<groupId>org.apache.archiva</groupId>" +
"</relocation>" +
"</distributionManagement>" +
"</project>";
populateRepo( remoteCentral, resourcePath, pom );
resourcePath = "archiva/jars/test-1.0.jar";
setupReleaseConnector( REPOID_INTERNAL, remoteCentral, ReleasesPolicy.ONCE );
saveConfiguration();
// --- Execution
// process the response code later, not via an exception.
HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
WebResponse response = sc.getResponse( request );
// --- Verification
assertResponseOK( response );
assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
}
}