diff options
author | Ciprian Ciubotariu <cheepeero@gmx.net> | 2016-10-04 17:16:50 +0300 |
---|---|---|
committer | Ciprian Ciubotariu <cheepeero@gmx.net> | 2016-10-04 18:12:05 +0300 |
commit | ef0f29235d7d5ebecf3f60accf2c76bcb7c8230b (patch) | |
tree | 341131e752e91571489f5749e667ce6543980d7b /archiva-modules/archiva-web/archiva-webdav | |
parent | 1fd9c951e19a30f7636f4a924cb9e2bf06af2861 (diff) | |
download | archiva-ef0f29235d7d5ebecf3f60accf2c76bcb7c8230b.tar.gz archiva-ef0f29235d7d5ebecf3f60accf2c76bcb7c8230b.zip |
[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.
Diffstat (limited to 'archiva-modules/archiva-web/archiva-webdav')
4 files changed, 82 insertions, 43 deletions
diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java index 93547c0d9..d4958b113 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java @@ -47,7 +47,6 @@ import org.apache.archiva.model.ArchivaRepositoryMetadata; 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 @@ public class ArchivaDavResourceFactory 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 @@ public class ArchivaDavResourceFactory 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; diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedMetadataTestCase.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedMetadataTestCase.java index 58ce852a3..eb7bd3637 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedMetadataTestCase.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedMetadataTestCase.java @@ -62,46 +62,6 @@ public abstract class AbstractRepositoryServletProxiedMetadataTestCase 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 ) { diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java index 68b0535dc..6c87d82ad 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java @@ -75,6 +75,7 @@ import java.net.URL; 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 abstract class AbstractRepositoryServletTestCase } + 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 diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletNoProxyTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletNoProxyTest.java index 004d16d26..cb862676a 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletNoProxyTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletNoProxyTest.java @@ -30,6 +30,9 @@ import org.junit.Test; 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 class RepositoryServletNoProxyTest 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") ); + } + } |