This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduced QueryStringContributor component interface
... the idea is to allow nexus plugins contribute additional query string paramaters. Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
- Loading branch information
1 parent
3350061
commit b419100
Showing
4 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...-proxy/src/main/java/org/sonatype/nexus/proxy/storage/remote/http/QueryStringBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,65 @@ | |||
/** | |||
* Sonatype Nexus (TM) Open Source Version | |||
* Copyright (c) 2007-2012 Sonatype, Inc. | |||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | |||
* | |||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | |||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | |||
* | |||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | |||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | |||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | |||
*/ | |||
package org.sonatype.nexus.proxy.storage.remote.http; | |||
|
|||
import java.util.List; | |||
|
|||
import javax.inject.Inject; | |||
import javax.inject.Named; | |||
import javax.inject.Singleton; | |||
|
|||
import org.codehaus.plexus.util.StringUtils; | |||
import org.sonatype.nexus.proxy.repository.ProxyRepository; | |||
import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext; | |||
|
|||
@Named | |||
@Singleton | |||
public class QueryStringBuilder | |||
{ | |||
private final List<QueryStringContributor> queryParameterContributors; | |||
|
|||
@Inject | |||
public QueryStringBuilder( List<QueryStringContributor> queryParameterContributors ) | |||
{ | |||
this.queryParameterContributors = queryParameterContributors; | |||
} | |||
|
|||
public String getQueryString( RemoteStorageContext ctx, ProxyRepository repository ) | |||
{ | |||
final StringBuilder result = new StringBuilder(); | |||
|
|||
final String configuredQueryString = ctx.getRemoteConnectionSettings().getQueryString(); | |||
|
|||
if ( StringUtils.isNotBlank( configuredQueryString ) ) | |||
{ | |||
result.append( configuredQueryString ); | |||
} | |||
|
|||
for ( QueryStringContributor contributor : queryParameterContributors ) | |||
{ | |||
String contributedQueryString = contributor.getQueryString( ctx, repository ); | |||
if ( StringUtils.isNotBlank( contributedQueryString ) ) | |||
{ | |||
if ( StringUtils.isNotBlank( result.toString() ) ) | |||
{ | |||
result.append( '&' ); | |||
} | |||
result.append( contributedQueryString ); | |||
} | |||
} | |||
|
|||
final String resultStr = result.toString(); | |||
|
|||
return StringUtils.isNotBlank( resultStr ) ? resultStr : null; | |||
} | |||
} |
32 changes: 32 additions & 0 deletions
32
...xy/src/main/java/org/sonatype/nexus/proxy/storage/remote/http/QueryStringContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
/** | |||
* Sonatype Nexus (TM) Open Source Version | |||
* Copyright (c) 2007-2012 Sonatype, Inc. | |||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | |||
* | |||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | |||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | |||
* | |||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | |||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | |||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | |||
*/ | |||
package org.sonatype.nexus.proxy.storage.remote.http; | |||
|
|||
import org.sonatype.nexus.proxy.repository.ProxyRepository; | |||
import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext; | |||
|
|||
/** | |||
* Classes implementing this interface are injected into the {@link QueryStringBuilder} and asked for a custom string to | |||
* append to the query string. The string will be prepended with either ``&'' or ``?'' as necessary. | |||
* | |||
* @since 2.2 | |||
*/ | |||
public interface QueryStringContributor | |||
{ | |||
/** | |||
* @param ctx The remote storage settings. | |||
* @param repository The proxy repository | |||
* @return The string to append to query string | |||
*/ | |||
public String getQueryString( RemoteStorageContext ctx, ProxyRepository repository ); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...xy/src/test/java/org/sonatype/nexus/proxy/storage/remote/http/QueryStringBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,110 @@ | |||
/** | |||
* Sonatype Nexus (TM) Open Source Version | |||
* Copyright (c) 2007-2012 Sonatype, Inc. | |||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | |||
* | |||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | |||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | |||
* | |||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | |||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | |||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | |||
*/ | |||
package org.sonatype.nexus.proxy.storage.remote.http; | |||
|
|||
import java.util.Arrays; | |||
import java.util.Collections; | |||
|
|||
import org.junit.Assert; | |||
import org.junit.Test; | |||
import org.sonatype.nexus.proxy.repository.DefaultRemoteConnectionSettings; | |||
import org.sonatype.nexus.proxy.repository.ProxyRepository; | |||
import org.sonatype.nexus.proxy.storage.remote.DefaultRemoteStorageContext; | |||
import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext; | |||
|
|||
public class QueryStringBuilderTest | |||
{ | |||
@Test | |||
public void testNoQueryString() | |||
{ | |||
QueryStringBuilder subject = new QueryStringBuilder( Collections.<QueryStringContributor> emptyList() ); | |||
Assert.assertNull( subject.getQueryString( getRemoteStorageContext( null ), null ) ); | |||
} | |||
|
|||
@Test | |||
public void testConfiguredStringOnly() | |||
{ | |||
QueryStringBuilder subject = new QueryStringBuilder( Collections.<QueryStringContributor> emptyList() ); | |||
|
|||
String configuredString = "configuredString"; | |||
|
|||
Assert.assertEquals( configuredString, | |||
subject.getQueryString( getRemoteStorageContext( configuredString ), null ) ); | |||
} | |||
|
|||
@Test | |||
public void testOneContributor() | |||
{ | |||
final String contributedString = "contributedString"; | |||
QueryStringContributor contributor = new QueryStringContributor() | |||
{ | |||
@Override | |||
public String getQueryString( RemoteStorageContext ctx, ProxyRepository repository ) | |||
{ | |||
return contributedString; | |||
} | |||
}; | |||
QueryStringBuilder subject = new QueryStringBuilder( Collections.singletonList( contributor ) ); | |||
Assert.assertEquals( contributedString, subject.getQueryString( getRemoteStorageContext( null ), null ) ); | |||
} | |||
|
|||
@Test | |||
public void testTwoContributor() | |||
{ | |||
QueryStringContributor contributor1 = new QueryStringContributor() | |||
{ | |||
@Override | |||
public String getQueryString( RemoteStorageContext ctx, ProxyRepository repository ) | |||
{ | |||
return "contributedString1"; | |||
} | |||
}; | |||
QueryStringContributor contributor2 = new QueryStringContributor() | |||
{ | |||
@Override | |||
public String getQueryString( RemoteStorageContext ctx, ProxyRepository repository ) | |||
{ | |||
return "contributedString2"; | |||
} | |||
}; | |||
|
|||
QueryStringBuilder subject = new QueryStringBuilder( Arrays.asList( contributor1, contributor2 ) ); | |||
Assert.assertEquals( "contributedString1&contributedString2", | |||
subject.getQueryString( getRemoteStorageContext( null ), null ) ); | |||
} | |||
|
|||
@Test | |||
public void testConfiguredStringAndOneContributor() | |||
{ | |||
QueryStringContributor contributor = new QueryStringContributor() | |||
{ | |||
@Override | |||
public String getQueryString( RemoteStorageContext ctx, ProxyRepository repository ) | |||
{ | |||
return "contributedString"; | |||
} | |||
}; | |||
QueryStringBuilder subject = new QueryStringBuilder( Collections.singletonList( contributor ) ); | |||
Assert.assertEquals( "configuredString&contributedString", | |||
subject.getQueryString( getRemoteStorageContext( "configuredString" ), null ) ); | |||
} | |||
|
|||
private DefaultRemoteStorageContext getRemoteStorageContext( String configuredString ) | |||
{ | |||
DefaultRemoteConnectionSettings settings = new DefaultRemoteConnectionSettings(); | |||
settings.setQueryString( configuredString ); | |||
DefaultRemoteStorageContext ctx = new DefaultRemoteStorageContext( null ); | |||
ctx.setRemoteConnectionSettings( settings ); | |||
return ctx; | |||
} | |||
} |