You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultNetworkProxyAdmin.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package org.apache.archiva.admin.repository.networkproxy;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.AuditInformation;
  21. import org.apache.archiva.admin.model.RepositoryAdminException;
  22. import org.apache.archiva.admin.model.beans.NetworkProxy;
  23. import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
  24. import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
  25. import org.apache.archiva.configuration.model.Configuration;
  26. import org.apache.archiva.configuration.model.NetworkProxyConfiguration;
  27. import org.apache.archiva.metadata.model.facets.AuditEvent;
  28. import org.apache.archiva.repository.RemoteRepository;
  29. import org.apache.archiva.repository.RepositoryException;
  30. import org.apache.archiva.repository.RepositoryRegistry;
  31. import org.apache.archiva.repository.features.RemoteIndexFeature;
  32. import org.apache.commons.lang3.StringUtils;
  33. import org.springframework.stereotype.Service;
  34. import javax.inject.Inject;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. /**
  38. * @author Olivier Lamy
  39. * @since 1.4-M1
  40. */
  41. @Service( "networkProxyAdmin#default" )
  42. public class DefaultNetworkProxyAdmin
  43. extends AbstractRepositoryAdmin
  44. implements NetworkProxyAdmin
  45. {
  46. @Inject
  47. RepositoryRegistry repositoryRegistry;
  48. @Override
  49. public List<NetworkProxy> getNetworkProxies()
  50. throws RepositoryAdminException
  51. {
  52. List<NetworkProxy> networkProxies =
  53. new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
  54. for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
  55. {
  56. networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
  57. }
  58. return networkProxies;
  59. }
  60. @Override
  61. public NetworkProxy getNetworkProxy( String networkProxyId )
  62. throws RepositoryAdminException
  63. {
  64. for ( NetworkProxy networkProxy : getNetworkProxies() )
  65. {
  66. if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
  67. {
  68. return networkProxy;
  69. }
  70. }
  71. return null;
  72. }
  73. @Override
  74. public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
  75. throws RepositoryAdminException
  76. {
  77. if ( networkProxy == null )
  78. {
  79. return;
  80. }
  81. if ( getNetworkProxy( networkProxy.getId() ) != null )
  82. {
  83. throw new RepositoryAdminException(
  84. "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
  85. }
  86. Configuration configuration = getArchivaConfiguration().getConfiguration();
  87. configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
  88. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
  89. saveConfiguration( configuration );
  90. }
  91. @Override
  92. public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
  93. throws RepositoryAdminException
  94. {
  95. if ( networkProxy == null )
  96. {
  97. return;
  98. }
  99. if ( getNetworkProxy( networkProxy.getId() ) == null )
  100. {
  101. throw new RepositoryAdminException(
  102. "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
  103. }
  104. Configuration configuration = getArchivaConfiguration().getConfiguration();
  105. NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
  106. configuration.removeNetworkProxy( networkProxyConfiguration );
  107. configuration.addNetworkProxy( networkProxyConfiguration );
  108. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
  109. saveConfiguration( configuration );
  110. }
  111. @Override
  112. public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
  113. throws RepositoryAdminException
  114. {
  115. NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
  116. if ( networkProxy == null )
  117. {
  118. throw new RepositoryAdminException(
  119. "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
  120. }
  121. for ( RemoteRepository repo : repositoryRegistry.getRemoteRepositories()) {
  122. if (repo.supportsFeature( RemoteIndexFeature.class )) {
  123. RemoteIndexFeature rif = repo.getFeature( RemoteIndexFeature.class );
  124. if (networkProxyId.equals(rif.getProxyId())) {
  125. rif.setProxyId( null );
  126. try
  127. {
  128. repositoryRegistry.putRepository( repo );
  129. }
  130. catch ( RepositoryException e )
  131. {
  132. log.error("Could not update repository {}", repo.getId(), e);
  133. }
  134. }
  135. }
  136. }
  137. Configuration configuration = getArchivaConfiguration().getConfiguration();
  138. NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
  139. configuration.removeNetworkProxy( networkProxyConfiguration );
  140. saveConfiguration( configuration );
  141. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
  142. }
  143. protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
  144. {
  145. return networkProxyConfiguration == null
  146. ? null
  147. : getModelMapper().map( networkProxyConfiguration, NetworkProxy.class );
  148. }
  149. protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
  150. {
  151. return networkProxy == null
  152. ? null
  153. : getModelMapper().map( networkProxy, NetworkProxyConfiguration.class );
  154. }
  155. }