[MRM-1928] Use applicationUrl setting in HTTP redirects

Make use of webapp application URL setting when constructing the HTTP
Location header within redirect responses, which allows clients to
follow them when Archiva runs behind HTTP reverse proxies.
This commit is contained in:
Ciprian Ciubotariu 2016-10-04 17:16:50 +03:00
parent 1fd9c951e1
commit ef0f29235d
4 changed files with 82 additions and 43 deletions

View File

@ -47,7 +47,6 @@
import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.policies.ProxyDownloadException;
import org.apache.archiva.proxy.model.RepositoryProxyConnectors;
import org.apache.archiva.proxy.model.ProxyFetchResult;
import org.apache.archiva.redback.authentication.AuthenticationException;
import org.apache.archiva.redback.authentication.AuthenticationResult;
import org.apache.archiva.redback.authorization.AuthorizationException;
@ -577,8 +576,7 @@ private String evaluatePathWithVersion( ArchivaDavResourceLocator archivaLocator
String path = e.getPath();
log.debug( "Relocation to {}", path );
throw new BrowserRedirectException( contextPath + ( StringUtils.startsWith( path, "/" ) ? "" : "/" ) + path,
e.getRelocationType() );
throw new BrowserRedirectException( addHrefPrefix( contextPath, path ), e.getRelocationType() );
}
catch ( XMLException e )
{
@ -935,6 +933,16 @@ private ArchivaDavResourceLocator checkLocatorIsInstanceOfRepositoryLocator( Dav
return archivaLocator;
}
private String addHrefPrefix( String contextPath, String path ) {
String prefix = archivaConfiguration.getConfiguration().getWebapp().getUi().getApplicationUrl();
if (prefix == null || prefix.isEmpty()) {
prefix = contextPath;
}
return prefix + ( StringUtils.startsWith( path, "/" ) ? "" :
( StringUtils.endsWith( prefix, "/" ) ? "" : "/" ) )
+ path;
}
private static class LogicalResource
{
private String path;

View File

@ -62,46 +62,6 @@ protected String requestMetadataOK( String path )
return response.getContentAsString();
}
protected String createVersionMetadata( String groupId, String artifactId, String version )
{
return createVersionMetadata( groupId, artifactId, version, null, null, null );
}
protected String createVersionMetadata( String groupId, String artifactId, String version, String timestamp,
String buildNumber, String lastUpdated )
{
StringBuilder buf = new StringBuilder();
buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
buf.append( " <version>" ).append( version ).append( "</version>\n" );
boolean hasSnapshot = StringUtils.isNotBlank( timestamp ) || StringUtils.isNotBlank( buildNumber );
boolean hasLastUpdated = StringUtils.isNotBlank( lastUpdated );
if ( hasSnapshot || hasLastUpdated )
{
buf.append( " <versioning>\n" );
if ( hasSnapshot )
{
buf.append( " <snapshot>\n" );
buf.append( " <buildNumber>" ).append( buildNumber ).append( "</buildNumber>\n" );
buf.append( " <timestamp>" ).append( timestamp ).append( "</timestamp>\n" );
buf.append( " </snapshot>\n" );
}
if ( hasLastUpdated )
{
buf.append( " <lastUpdated>" ).append( lastUpdated ).append( "</lastUpdated>\n" );
}
buf.append( " </versioning>\n" );
}
buf.append( "</metadata>" );
return buf.toString();
}
protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
String[] versions )
{

View File

@ -75,6 +75,7 @@
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
/**
* AbstractRepositoryServletTestCase
@ -180,6 +181,36 @@ public ServletContext getServletContext()
}
protected String createVersionMetadata(String groupId, String artifactId, String version) {
return createVersionMetadata(groupId, artifactId, version, null, null, null);
}
protected String createVersionMetadata(String groupId, String artifactId, String version, String timestamp, String buildNumber, String lastUpdated) {
StringBuilder buf = new StringBuilder();
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
buf.append("<metadata>\n");
buf.append(" <groupId>").append(groupId).append("</groupId>\n");
buf.append(" <artifactId>").append(artifactId).append("</artifactId>\n");
buf.append(" <version>").append(version).append("</version>\n");
boolean hasSnapshot = StringUtils.isNotBlank(timestamp) || StringUtils.isNotBlank(buildNumber);
boolean hasLastUpdated = StringUtils.isNotBlank(lastUpdated);
if (hasSnapshot || hasLastUpdated) {
buf.append(" <versioning>\n");
if (hasSnapshot) {
buf.append(" <snapshot>\n");
buf.append(" <buildNumber>").append(buildNumber).append("</buildNumber>\n");
buf.append(" <timestamp>").append(timestamp).append("</timestamp>\n");
buf.append(" </snapshot>\n");
}
if (hasLastUpdated) {
buf.append(" <lastUpdated>").append(lastUpdated).append("</lastUpdated>\n");
}
buf.append(" </versioning>\n");
}
buf.append("</metadata>");
return buf.toString();
}
public static class TestWebapplicationContext
implements WebApplicationContext

View File

@ -30,6 +30,9 @@
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
/**
* RepositoryServletTest
@ -534,4 +537,41 @@ public void testGetNoProxyDistributionLegacyLayoutManagedLegacy()
assertResponseNotFound( response );
}
@Test
public void testGetNoProxySnapshotRedirectToTimestampedSnapshot()
throws Exception
{
String commonsLangQuery = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-SNAPSHOT.jar";
String commonsLangMetadata = "commons-lang/commons-lang/2.1-SNAPSHOT/maven-metadata.xml";
String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-20050821.023400-1.jar";
String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";
archivaConfiguration.getConfiguration().getWebapp().getUi().setApplicationUrl("http://localhost");
File artifactFile = new File( repoRootInternal, commonsLangJar );
artifactFile.getParentFile().mkdirs();
FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
File metadataFile = new File( repoRootInternal, commonsLangMetadata );
metadataFile.getParentFile().mkdirs();
FileUtils.writeStringToFile( metadataFile, createVersionMetadata("commons-lang", "commons-lang",
"2.1-SNAPSHOT", "20050821.023400", "1", "20050821.023400"));
WebRequest webRequest = new GetMethodWebRequest(
"http://localhost/repository/internal/" + commonsLangQuery );
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI( webRequest.getUrl().getPath() );
request.addHeader( "User-Agent", "Apache Archiva unit test" );
request.setMethod( webRequest.getHttpMethod().name() );
final MockHttpServletResponse response = execute( request );
assertEquals( HttpServletResponse.SC_MOVED_TEMPORARILY,
response.getStatus() );
assertEquals( "http://localhost/repository/internal/" + commonsLangJar,
response.getHeader("Location") );
}
}