Browse files

Add logic to build the baseUrl from the System properties if running …

…in the bundle

This assumes http, which is bad, but, this makes getting up and running easier
  • Loading branch information...
1 parent 0834934 commit b42aceca5f0abe9ef2db686fa706733a605a277d @bdemers bdemers committed Jul 8, 2011
View
27 ...i/src/main/java/org/sonatype/nexus/rest/repositories/RestContentRepositoryURLBuilder.java
@@ -16,6 +16,8 @@
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
/**
* Return the REST mounted URLS for Nexus.
@@ -74,6 +76,31 @@ else if ( Request.getCurrent() != null )
baseURL = globalRestApiSettings.getBaseUrl();
}
+ // if still null try to figure out the URL from the system properties (only works for the bundle)
+ //TODO: this could be problematic, consider removing this
+ if ( StringUtils.isEmpty( baseURL ) )
+ {
+ this.log.info( "Base URL not set, this can be set in Administration -> Server -> Application Server Settings" );
+ try
+ {
+ InetAddress local = InetAddress.getLocalHost();
+ String hostname = local.getHostName();
+
+ Integer port = Integer.getInteger( "plexus.application-port" );
+ String contextPath = System.getProperty( "plexus.webapp-context-path" );
+
+ // assume http?
+ if( port != null && contextPath != null )
+ {
+ baseURL = new StringBuffer( "http://" ).append( hostname ).append( ":" ).append( port ).append( contextPath ).toString();
+ }
+ }
+ catch ( UnknownHostException e )
+ {
+ this.log.debug( "Failed to find name" ,e);
+ }
+ }
+
// if all else fails?
if ( StringUtils.isEmpty( baseURL ) )
{
View
55 ...c/test/java/org/sonatype/nexus/rest/repositories/RestContentRepositoryURLBuilderTest.java
@@ -18,6 +18,7 @@
import org.sonatype.nexus.proxy.repository.GroupRepository;
import org.sonatype.nexus.proxy.repository.Repository;
+import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
@@ -112,7 +113,6 @@ public void testForceBaseUrlByIdGlobalDisabled()
public void testNoRequestBaseURLNotSet()
throws Exception
{
-
Mockito.doReturn( true ).when( globalRestApiSettings ).isEnabled();
Mockito.doReturn( false ).when( globalRestApiSettings ).isForceBaseUrl();
Mockito.doReturn( null ).when( globalRestApiSettings ).getBaseUrl();
@@ -152,16 +152,17 @@ public void testBaseUrlGlobalRequest()
try
{
- Request request = new Request();
- request.setRootRef( new Reference( restletBaseURL ) );
- Response response = new Response( request );
- Response.setCurrent( response );
+ Request request = new Request();
+ request.setRootRef( new Reference( restletBaseURL ) );
+ Response response = new Response( request );
+ Response.setCurrent( response );
- RestContentRepositoryURLBuilder urlFinder =
- new RestContentRepositoryURLBuilder( repositoryRegistry, repositoryTypeRegistry, globalRestApiSettings );
+ RestContentRepositoryURLBuilder urlFinder =
+ new RestContentRepositoryURLBuilder( repositoryRegistry, repositoryTypeRegistry,
+ globalRestApiSettings );
- Assert.assertEquals( restletBaseURL + "content/" + MOCK_PATH_PREFIX + "/" + MOCK_REPO_ID,
- urlFinder.getRepositoryUrl( MOCK_REPO_ID ) );
+ Assert.assertEquals( restletBaseURL + "content/" + MOCK_PATH_PREFIX + "/" + MOCK_REPO_ID,
+ urlFinder.getRepositoryUrl( MOCK_REPO_ID ) );
}
finally
{
@@ -180,7 +181,8 @@ public void testNotFound()
}
@Test
- public void testUnknownRepositoryType() throws NoSuchRepositoryException
+ public void testUnknownRepositoryType()
+ throws NoSuchRepositoryException
{
Mockito.doReturn( true ).when( globalRestApiSettings ).isEnabled();
@@ -195,4 +197,37 @@ public void testUnknownRepositoryType() throws NoSuchRepositoryException
}
+ @Test
+ public void testUsingSystemProperties()
+ throws Exception
+ {
+ Mockito.doReturn( true ).when( globalRestApiSettings ).isEnabled();
+ Mockito.doReturn( false ).when( globalRestApiSettings ).isForceBaseUrl();
+ Mockito.doReturn( null ).when( globalRestApiSettings ).getBaseUrl();
+
+ String hostname = InetAddress.getLocalHost().getHostName();
+ Integer port = 1234;
+ String contextPath = "/context-path";
+
+ try
+ {
+ System.setProperty( "plexus.application-port", port.toString() );
+ System.setProperty( "plexus.webapp-context-path", contextPath );
+
+ RestContentRepositoryURLBuilder urlFinder =
+ new RestContentRepositoryURLBuilder( repositoryRegistry, repositoryTypeRegistry,
+ globalRestApiSettings );
+
+ Assert.assertEquals(
+ "http://" + hostname + ":" + port + contextPath + "/content/" + MOCK_PATH_PREFIX + "/" + MOCK_REPO_ID,
+ urlFinder.getRepositoryUrl( MOCK_REPO_ID ) );
+ }
+ finally
+ {
+ System.clearProperty( "plexus.application-port" );
+ System.clearProperty( "plexus.webapp-context-path" );
+ }
+ }
+
+
}

0 comments on commit b42acec

Please sign in to comment.