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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.audit.AuditEvent;
  26. import org.apache.archiva.configuration.Configuration;
  27. import org.apache.archiva.configuration.NetworkProxyConfiguration;
  28. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.springframework.stereotype.Service;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * @author Olivier Lamy
  35. * @since 1.4-M1
  36. */
  37. @Service( "networkProxyAdmin#default" )
  38. public class DefaultNetworkProxyAdmin
  39. extends AbstractRepositoryAdmin
  40. implements NetworkProxyAdmin
  41. {
  42. @Override
  43. public List<NetworkProxy> getNetworkProxies()
  44. throws RepositoryAdminException
  45. {
  46. List<NetworkProxy> networkProxies =
  47. new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
  48. for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
  49. {
  50. networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
  51. }
  52. return networkProxies;
  53. }
  54. @Override
  55. public NetworkProxy getNetworkProxy( String networkProxyId )
  56. throws RepositoryAdminException
  57. {
  58. for ( NetworkProxy networkProxy : getNetworkProxies() )
  59. {
  60. if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
  61. {
  62. return networkProxy;
  63. }
  64. }
  65. return null;
  66. }
  67. @Override
  68. public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
  69. throws RepositoryAdminException
  70. {
  71. if ( networkProxy == null )
  72. {
  73. return;
  74. }
  75. if ( getNetworkProxy( networkProxy.getId() ) != null )
  76. {
  77. throw new RepositoryAdminException(
  78. "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
  79. }
  80. Configuration configuration = getArchivaConfiguration().getConfiguration();
  81. configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
  82. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
  83. saveConfiguration( configuration );
  84. }
  85. @Override
  86. public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
  87. throws RepositoryAdminException
  88. {
  89. if ( networkProxy == null )
  90. {
  91. return;
  92. }
  93. if ( getNetworkProxy( networkProxy.getId() ) == null )
  94. {
  95. throw new RepositoryAdminException(
  96. "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
  97. }
  98. Configuration configuration = getArchivaConfiguration().getConfiguration();
  99. NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
  100. configuration.removeNetworkProxy( networkProxyConfiguration );
  101. configuration.addNetworkProxy( networkProxyConfiguration );
  102. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
  103. saveConfiguration( configuration );
  104. }
  105. @Override
  106. public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
  107. throws RepositoryAdminException
  108. {
  109. NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
  110. if ( networkProxy == null )
  111. {
  112. throw new RepositoryAdminException(
  113. "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
  114. }
  115. Configuration configuration = getArchivaConfiguration().getConfiguration();
  116. NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
  117. configuration.removeNetworkProxy( networkProxyConfiguration );
  118. for ( RemoteRepositoryConfiguration rrc : configuration.getRemoteRepositories() )
  119. {
  120. if ( StringUtils.equals( rrc.getRemoteDownloadNetworkProxyId(), networkProxyId ) )
  121. {
  122. rrc.setRemoteDownloadNetworkProxyId( null );
  123. }
  124. }
  125. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
  126. saveConfiguration( configuration );
  127. }
  128. protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
  129. {
  130. return networkProxyConfiguration == null
  131. ? null
  132. : getModelMapper().map( networkProxyConfiguration, NetworkProxy.class );
  133. }
  134. protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
  135. {
  136. return networkProxy == null
  137. ? null
  138. : getModelMapper().map( networkProxy, NetworkProxyConfiguration.class );
  139. }
  140. }