001package org.apache.archiva.admin.repository.networkproxy; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.admin.model.AuditInformation; 022import org.apache.archiva.admin.model.RepositoryAdminException; 023import org.apache.archiva.admin.model.beans.NetworkProxy; 024import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin; 025import org.apache.archiva.admin.repository.AbstractRepositoryAdmin; 026import org.apache.archiva.configuration.Configuration; 027import org.apache.archiva.configuration.NetworkProxyConfiguration; 028import org.apache.archiva.metadata.model.facets.AuditEvent; 029import org.apache.archiva.repository.RemoteRepository; 030import org.apache.archiva.repository.RepositoryException; 031import org.apache.archiva.repository.RepositoryRegistry; 032import org.apache.archiva.repository.features.RemoteIndexFeature; 033import org.apache.commons.lang3.StringUtils; 034import org.springframework.stereotype.Service; 035 036import javax.inject.Inject; 037import java.util.ArrayList; 038import java.util.List; 039 040/** 041 * @author Olivier Lamy 042 * @since 1.4-M1 043 */ 044@Service( "networkProxyAdmin#default" ) 045public class DefaultNetworkProxyAdmin 046 extends AbstractRepositoryAdmin 047 implements NetworkProxyAdmin 048{ 049 050 @Inject 051 RepositoryRegistry repositoryRegistry; 052 053 @Override 054 public List<NetworkProxy> getNetworkProxies() 055 throws RepositoryAdminException 056 { 057 List<NetworkProxy> networkProxies = 058 new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() ); 059 for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() ) 060 { 061 networkProxies.add( getNetworkProxy( networkProxyConfiguration ) ); 062 } 063 return networkProxies; 064 } 065 066 @Override 067 public NetworkProxy getNetworkProxy( String networkProxyId ) 068 throws RepositoryAdminException 069 { 070 for ( NetworkProxy networkProxy : getNetworkProxies() ) 071 { 072 if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) ) 073 { 074 return networkProxy; 075 } 076 } 077 078 return null; 079 } 080 081 @Override 082 public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation ) 083 throws RepositoryAdminException 084 { 085 if ( networkProxy == null ) 086 { 087 return; 088 } 089 if ( getNetworkProxy( networkProxy.getId() ) != null ) 090 { 091 throw new RepositoryAdminException( 092 "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" ); 093 } 094 Configuration configuration = getArchivaConfiguration().getConfiguration(); 095 configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) ); 096 097 triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation ); 098 099 saveConfiguration( configuration ); 100 } 101 102 @Override 103 public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation ) 104 throws RepositoryAdminException 105 { 106 if ( networkProxy == null ) 107 { 108 return; 109 } 110 if ( getNetworkProxy( networkProxy.getId() ) == null ) 111 { 112 throw new RepositoryAdminException( 113 "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" ); 114 } 115 Configuration configuration = getArchivaConfiguration().getConfiguration(); 116 NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy ); 117 configuration.removeNetworkProxy( networkProxyConfiguration ); 118 configuration.addNetworkProxy( networkProxyConfiguration ); 119 120 triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation ); 121 122 saveConfiguration( configuration ); 123 } 124 125 @Override 126 public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation ) 127 throws RepositoryAdminException 128 { 129 130 NetworkProxy networkProxy = getNetworkProxy( networkProxyId ); 131 if ( networkProxy == null ) 132 { 133 throw new RepositoryAdminException( 134 "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" ); 135 } 136 Configuration configuration = getArchivaConfiguration().getConfiguration(); 137 NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy ); 138 configuration.removeNetworkProxy( networkProxyConfiguration ); 139 140 for ( RemoteRepository repo : repositoryRegistry.getRemoteRepositories()) { 141 if (repo.supportsFeature( RemoteIndexFeature.class )) { 142 RemoteIndexFeature rif = repo.getFeature( RemoteIndexFeature.class ).get(); 143 if (networkProxyId.equals(rif.getProxyId())) { 144 rif.setProxyId( null ); 145 try 146 { 147 repositoryRegistry.putRepository( repo, configuration); 148 } 149 catch ( RepositoryException e ) 150 { 151 log.error("Could not update repository {}", repo.getId(), e); 152 } 153 } 154 } 155 } 156 157 triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation ); 158 159 saveConfiguration( configuration ); 160 } 161 162 protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration ) 163 { 164 return networkProxyConfiguration == null 165 ? null 166 : getModelMapper().map( networkProxyConfiguration, NetworkProxy.class ); 167 } 168 169 protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy ) 170 { 171 return networkProxy == null 172 ? null 173 : getModelMapper().map( networkProxy, NetworkProxyConfiguration.class ); 174 } 175}