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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. public List<NetworkProxy> getNetworkProxies()
  43. throws RepositoryAdminException
  44. {
  45. List<NetworkProxy> networkProxies =
  46. new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
  47. for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
  48. {
  49. networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
  50. }
  51. return networkProxies;
  52. }
  53. public NetworkProxy getNetworkProxy( String networkProxyId )
  54. throws RepositoryAdminException
  55. {
  56. for ( NetworkProxy networkProxy : getNetworkProxies() )
  57. {
  58. if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
  59. {
  60. return networkProxy;
  61. }
  62. }
  63. return null;
  64. }
  65. public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
  66. throws RepositoryAdminException
  67. {
  68. if ( networkProxy == null )
  69. {
  70. return;
  71. }
  72. if ( getNetworkProxy( networkProxy.getId() ) != null )
  73. {
  74. throw new RepositoryAdminException(
  75. "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
  76. }
  77. Configuration configuration = getArchivaConfiguration().getConfiguration();
  78. configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
  79. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
  80. saveConfiguration( configuration );
  81. }
  82. public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
  83. throws RepositoryAdminException
  84. {
  85. if ( networkProxy == null )
  86. {
  87. return;
  88. }
  89. if ( getNetworkProxy( networkProxy.getId() ) == null )
  90. {
  91. throw new RepositoryAdminException(
  92. "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
  93. }
  94. Configuration configuration = getArchivaConfiguration().getConfiguration();
  95. NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
  96. configuration.removeNetworkProxy( networkProxyConfiguration );
  97. configuration.addNetworkProxy( networkProxyConfiguration );
  98. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
  99. saveConfiguration( configuration );
  100. }
  101. public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
  102. throws RepositoryAdminException
  103. {
  104. NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
  105. if ( networkProxy == null )
  106. {
  107. throw new RepositoryAdminException(
  108. "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
  109. }
  110. Configuration configuration = getArchivaConfiguration().getConfiguration();
  111. NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
  112. configuration.removeNetworkProxy( networkProxyConfiguration );
  113. for ( RemoteRepositoryConfiguration rrc : configuration.getRemoteRepositories() )
  114. {
  115. if ( StringUtils.equals( rrc.getRemoteDownloadNetworkProxyId(), networkProxyId ) )
  116. {
  117. rrc.setRemoteDownloadNetworkProxyId( null );
  118. }
  119. }
  120. triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
  121. saveConfiguration( configuration );
  122. }
  123. protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
  124. {
  125. return networkProxyConfiguration == null
  126. ? null
  127. : getModelMapper().map( networkProxyConfiguration, NetworkProxy.class );
  128. }
  129. protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
  130. {
  131. return networkProxy == null
  132. ? null
  133. : getModelMapper().map( networkProxy, NetworkProxyConfiguration.class );
  134. }
  135. }