Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
introduced QueryStringContributor component interface
Browse files Browse the repository at this point in the history
... the idea is to allow nexus plugins contribute additional
query string paramaters.

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
  • Loading branch information
ifedorenko committed Sep 14, 2012
1 parent 3350061 commit b419100
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 2 deletions.
@@ -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;
}
}
@@ -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 );
}
Expand Up @@ -56,6 +56,7 @@
import org.sonatype.nexus.proxy.storage.remote.RemoteItemNotFoundException; import org.sonatype.nexus.proxy.storage.remote.RemoteItemNotFoundException;
import org.sonatype.nexus.proxy.storage.remote.RemoteRepositoryStorage; import org.sonatype.nexus.proxy.storage.remote.RemoteRepositoryStorage;
import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext; import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext;
import org.sonatype.nexus.proxy.storage.remote.http.QueryStringBuilder;
import org.sonatype.nexus.proxy.utils.UserAgentBuilder; import org.sonatype.nexus.proxy.utils.UserAgentBuilder;


/** /**
Expand Down Expand Up @@ -99,16 +100,19 @@ public class HttpClientRemoteStorage
*/ */
private static final boolean CAN_WRITE = true; private static final boolean CAN_WRITE = true;


private QueryStringBuilder queryStringBuilder;

// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Constructors // Constructors
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------


@Inject @Inject
HttpClientRemoteStorage( final UserAgentBuilder userAgentBuilder, HttpClientRemoteStorage( final UserAgentBuilder userAgentBuilder,
final ApplicationStatusSource applicationStatusSource, final ApplicationStatusSource applicationStatusSource,
final MimeSupport mimeSupport ) final MimeSupport mimeSupport, QueryStringBuilder queryStringBuilder)
{ {
super( userAgentBuilder, applicationStatusSource, mimeSupport ); super( userAgentBuilder, applicationStatusSource, mimeSupport );
this.queryStringBuilder = queryStringBuilder;
} }


// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
Expand Down Expand Up @@ -573,7 +577,8 @@ private URL appendQueryString( final URL url,
{ {
final RemoteStorageContext ctx = getRemoteStorageContext( repository ); final RemoteStorageContext ctx = getRemoteStorageContext( repository );


final String queryString = ctx.getRemoteConnectionSettings().getQueryString(); String queryString = queryStringBuilder.getQueryString( ctx, repository );

if ( StringUtils.isNotBlank( queryString ) ) if ( StringUtils.isNotBlank( queryString ) )
{ {
try try
Expand Down
@@ -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;
}
}

0 comments on commit b419100

Please sign in to comment.