]> source.dussan.org Git - archiva.git/commitdiff
use configured applicationUrl for download urls
authorOlivier Lamy <olamy@apache.org>
Fri, 25 May 2012 21:46:38 +0000 (21:46 +0000)
committerOlivier Lamy <olamy@apache.org>
Fri, 25 May 2012 21:46:38 +0000 (21:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1342819 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java

index 089d15060b7727be299cab25eb4d88b601641323..3726b6af50af495a401166e9fd0482cde985f914 100644 (file)
@@ -19,18 +19,20 @@ package org.apache.archiva.rest.services;
  */
 
 import org.apache.archiva.admin.model.AuditInformation;
+import org.apache.archiva.admin.model.RepositoryAdminException;
+import org.apache.archiva.admin.model.admin.ArchivaAdministration;
 import org.apache.archiva.audit.AuditEvent;
 import org.apache.archiva.audit.AuditListener;
 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
+import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
+import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
+import org.apache.archiva.redback.users.User;
+import org.apache.archiva.redback.users.UserManager;
 import org.apache.archiva.security.AccessDeniedException;
 import org.apache.archiva.security.ArchivaSecurityException;
 import org.apache.archiva.security.PrincipalNotFoundException;
 import org.apache.archiva.security.UserRepositories;
 import org.apache.commons.lang.StringUtils;
-import org.apache.archiva.redback.users.User;
-import org.apache.archiva.redback.users.UserManager;
-import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
-import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
@@ -67,6 +69,9 @@ public abstract class AbstractRestService
     @Named( value = "repositorySessionFactory" )
     protected RepositorySessionFactory repositorySessionFactory;
 
+    @Inject
+    protected ArchivaAdministration archivaAdministration;
+
     @Context
     protected HttpServletRequest httpServletRequest;
 
@@ -122,7 +127,13 @@ public abstract class AbstractRestService
     }
 
     protected String getBaseUrl( HttpServletRequest req )
+        throws RepositoryAdminException
     {
+        String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
+        if ( StringUtils.isNotBlank( applicationUrl ) )
+        {
+            return applicationUrl;
+        }
         return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80
             ? ""
             : ":" + req.getServerPort() ) + req.getContextPath();
index 518311d633f7b4cc3ff5fa07204e6e63d5698bc2..417c7b04802f30b9623b0c7f9600b7dd05a62aaa 100644 (file)
@@ -36,6 +36,7 @@ import org.apache.commons.lang.StringUtils;
 import org.springframework.stereotype.Service;
 
 import javax.inject.Inject;
+import javax.ws.rs.core.Response;
 import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Collections;
@@ -199,85 +200,98 @@ public class DefaultManagedRepositoriesService
     }
 
     private String createSnippet( ManagedRepository repo )
+        throws ArchivaRestServiceException
     {
-        StringBuilder snippet = new StringBuilder();
-        snippet.append( "<project>\n" );
-        snippet.append( "  ...\n" );
-        snippet.append( "  <distributionManagement>\n" );
-
-        String distRepoName = "repository";
-        if ( repo.isSnapshots() )
+        try
         {
-            distRepoName = "snapshotRepository";
-        }
+            StringBuilder snippet = new StringBuilder();
+            snippet.append( "<project>\n" );
+            snippet.append( "  ...\n" );
+            snippet.append( "  <distributionManagement>\n" );
 
-        snippet.append( "    <" ).append( distRepoName ).append( ">\n" );
-        snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
-        snippet.append( "      <url>" );
-        snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
-        snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
+            String distRepoName = "repository";
+            if ( repo.isSnapshots() )
+            {
+                distRepoName = "snapshotRepository";
+            }
 
-        if ( !"default".equals( repo.getLayout() ) )
-        {
-            snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>" );
-        }
+            snippet.append( "    <" ).append( distRepoName ).append( ">\n" );
+            snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
+            snippet.append( "      <url>" );
+            snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
+            snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
 
-        snippet.append( "    </" ).append( distRepoName ).append( ">\n" );
-        snippet.append( "  </distributionManagement>\n" );
-        snippet.append( "\n" );
+            if ( !"default".equals( repo.getLayout() ) )
+            {
+                snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>" );
+            }
 
-        snippet.append( "  <repositories>\n" );
-        snippet.append( "    <repository>\n" );
-        snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
-        snippet.append( "      <name>" ).append( repo.getName() ).append( "</name>\n" );
+            snippet.append( "    </" ).append( distRepoName ).append( ">\n" );
+            snippet.append( "  </distributionManagement>\n" );
+            snippet.append( "\n" );
 
-        snippet.append( "      <url>" );
-        snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
-        snippet.append( "/" ).append( repo.getId() ).append( "/" );
+            snippet.append( "  <repositories>\n" );
+            snippet.append( "    <repository>\n" );
+            snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
+            snippet.append( "      <name>" ).append( repo.getName() ).append( "</name>\n" );
 
-        snippet.append( "</url>\n" );
+            snippet.append( "      <url>" );
+            snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
+            snippet.append( "/" ).append( repo.getId() ).append( "/" );
 
-        if ( !"default".equals( repo.getLayout() ) )
-        {
-            snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
-        }
+            snippet.append( "</url>\n" );
 
-        snippet.append( "      <releases>\n" );
-        snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
-        snippet.append( "      </releases>\n" );
-        snippet.append( "      <snapshots>\n" );
-        snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
-        snippet.append( "      </snapshots>\n" );
-        snippet.append( "    </repository>\n" );
-        snippet.append( "  </repositories>\n" );
-        snippet.append( "  <pluginRepositories>\n" );
-        snippet.append( "    <pluginRepository>\n" );
-        snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
-        snippet.append( "      <name>" ).append( repo.getName() ).append( "</name>\n" );
-
-        snippet.append( "      <url>" );
-        snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
-        snippet.append( "/" ).append( repo.getId() ).append( "/" );
-
-        snippet.append( "</url>\n" );
-
-        if ( !"default".equals( repo.getLayout() ) )
-        {
-            snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
-        }
-
-        snippet.append( "      <releases>\n" );
-        snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
-        snippet.append( "      </releases>\n" );
-        snippet.append( "      <snapshots>\n" );
-        snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
-        snippet.append( "      </snapshots>\n" );
-        snippet.append( "    </pluginRepository>\n" );
-        snippet.append( "  </pluginRepositories>\n" );
+            if ( !"default".equals( repo.getLayout() ) )
+            {
+                snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
+            }
 
-        snippet.append( "  ...\n" );
-        snippet.append( "</project>\n" );
+            snippet.append( "      <releases>\n" );
+            snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
+                "</enabled>\n" );
+            snippet.append( "      </releases>\n" );
+            snippet.append( "      <snapshots>\n" );
+            snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
+                "</enabled>\n" );
+            snippet.append( "      </snapshots>\n" );
+            snippet.append( "    </repository>\n" );
+            snippet.append( "  </repositories>\n" );
+            snippet.append( "  <pluginRepositories>\n" );
+            snippet.append( "    <pluginRepository>\n" );
+            snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
+            snippet.append( "      <name>" ).append( repo.getName() ).append( "</name>\n" );
+
+            snippet.append( "      <url>" );
+            snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
+            snippet.append( "/" ).append( repo.getId() ).append( "/" );
+
+            snippet.append( "</url>\n" );
+
+            if ( !"default".equals( repo.getLayout() ) )
+            {
+                snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
+            }
 
-        return StringEscapeUtils.escapeXml( snippet.toString() );
+            snippet.append( "      <releases>\n" );
+            snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
+                "</enabled>\n" );
+            snippet.append( "      </releases>\n" );
+            snippet.append( "      <snapshots>\n" );
+            snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
+                "</enabled>\n" );
+            snippet.append( "      </snapshots>\n" );
+            snippet.append( "    </pluginRepository>\n" );
+            snippet.append( "  </pluginRepositories>\n" );
+
+            snippet.append( "  ...\n" );
+            snippet.append( "</project>\n" );
+
+            return StringEscapeUtils.escapeXml( snippet.toString() );
+        }
+        catch ( RepositoryAdminException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
     }
 }
index e1d6bc6138564b730af179ccc04195d8d548e6dc..4a25b5f87b4229137b5876bee82050d184ce73e3 100644 (file)
@@ -133,9 +133,6 @@ public class DefaultRepositoriesService
     @Inject
     private RepositoryContentFactory repositoryFactory;
 
-    @Inject
-    private ArchivaAdministration archivaAdministration;
-
     @Inject
     @Named( value = "archivaTaskScheduler#repository" )
     private ArchivaTaskScheduler scheduler;
index 863e76c1212fe1a8d908a9749a5d44fd4d9ece5c..47f4e8c979b1e34e87b8ad15946e294df03e1bb7 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.archiva.rest.services;
  */
 
 import net.sf.beanlib.provider.replicator.BeanReplicator;
+import org.apache.archiva.admin.model.RepositoryAdminException;
 import org.apache.archiva.indexer.search.RepositorySearch;
 import org.apache.archiva.indexer.search.RepositorySearchException;
 import org.apache.archiva.indexer.search.SearchFields;
@@ -38,6 +39,7 @@ import org.apache.commons.lang.StringUtils;
 import org.springframework.stereotype.Service;
 
 import javax.inject.Inject;
+import javax.ws.rs.core.Response;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -202,6 +204,7 @@ public class DefaultSearchService
     // internal
     //-------------------------------------
     protected List<Artifact> getArtifacts( SearchResults searchResults )
+        throws ArchivaRestServiceException
     {
 
         if ( searchResults == null || searchResults.isEmpty() )
@@ -240,42 +243,51 @@ public class DefaultSearchService
      * @return
      */
     private String getArtifactUrl( Artifact artifact, String version )
+        throws ArchivaRestServiceException
     {
-
-        if ( httpServletRequest == null )
-        {
-            return null;
-        }
-        if ( StringUtils.isEmpty( artifact.getUrl() ) )
+        try
         {
-            return null;
-        }
-        StringBuilder sb = new StringBuilder( getBaseUrl( httpServletRequest ) );
 
-        sb.append( "/repository" );
+            if ( httpServletRequest == null )
+            {
+                return null;
+            }
+            if ( StringUtils.isEmpty( artifact.getUrl() ) )
+            {
+                return null;
+            }
+            StringBuilder sb = new StringBuilder( getBaseUrl( httpServletRequest ) );
 
-        sb.append( '/' ).append( artifact.getContext() );
+            sb.append( "/repository" );
 
-        sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
-        sb.append( '/' ).append( artifact.getArtifactId() );
-        sb.append( '/' ).append( artifact.getVersion() );
-        sb.append( '/' ).append( artifact.getArtifactId() );
-        sb.append( '-' ).append( artifact.getVersion() );
-        if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
-        {
-            sb.append( '-' ).append( artifact.getClassifier() );
-        }
-        // maven-plugin packaging is a jar
-        if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
-        {
-            sb.append( "jar" );
+            sb.append( '/' ).append( artifact.getContext() );
+
+            sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
+            sb.append( '/' ).append( artifact.getArtifactId() );
+            sb.append( '/' ).append( artifact.getVersion() );
+            sb.append( '/' ).append( artifact.getArtifactId() );
+            sb.append( '-' ).append( artifact.getVersion() );
+            if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
+            {
+                sb.append( '-' ).append( artifact.getClassifier() );
+            }
+            // maven-plugin packaging is a jar
+            if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
+            {
+                sb.append( "jar" );
+            }
+            else
+            {
+                sb.append( '.' ).append( artifact.getPackaging() );
+            }
+
+            return sb.toString();
         }
-        else
+        catch ( RepositoryAdminException e )
         {
-            sb.append( '.' ).append( artifact.getPackaging() );
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
         }
-
-        return sb.toString();
     }