1 package org.apache.archiva.admin.repository.networkproxy;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.NetworkProxy;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.NetworkProxyConfiguration;
28 import org.apache.archiva.metadata.model.facets.AuditEvent;
29 import org.apache.archiva.repository.RemoteRepository;
30 import org.apache.archiva.repository.RepositoryException;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.features.RemoteIndexFeature;
33 import org.apache.commons.lang3.StringUtils;
34 import org.springframework.stereotype.Service;
36 import javax.inject.Inject;
37 import java.util.ArrayList;
38 import java.util.List;
41 * @author Olivier Lamy
44 @Service( "networkProxyAdmin#default" )
45 public class DefaultNetworkProxyAdmin
46 extends AbstractRepositoryAdmin
47 implements NetworkProxyAdmin
51 RepositoryRegistry repositoryRegistry;
54 public List<NetworkProxy> getNetworkProxies()
55 throws RepositoryAdminException
57 List<NetworkProxy> networkProxies =
58 new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
59 for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
61 networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
63 return networkProxies;
67 public NetworkProxy getNetworkProxy( String networkProxyId )
68 throws RepositoryAdminException
70 for ( NetworkProxy networkProxy : getNetworkProxies() )
72 if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
82 public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
83 throws RepositoryAdminException
85 if ( networkProxy == null )
89 if ( getNetworkProxy( networkProxy.getId() ) != null )
91 throw new RepositoryAdminException(
92 "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
94 Configuration configuration = getArchivaConfiguration().getConfiguration();
95 configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
97 triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
99 saveConfiguration( configuration );
103 public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
104 throws RepositoryAdminException
106 if ( networkProxy == null )
110 if ( getNetworkProxy( networkProxy.getId() ) == null )
112 throw new RepositoryAdminException(
113 "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
115 Configuration configuration = getArchivaConfiguration().getConfiguration();
116 NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
117 configuration.removeNetworkProxy( networkProxyConfiguration );
118 configuration.addNetworkProxy( networkProxyConfiguration );
120 triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
122 saveConfiguration( configuration );
126 public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
127 throws RepositoryAdminException
130 NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
131 if ( networkProxy == null )
133 throw new RepositoryAdminException(
134 "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
136 Configuration configuration = getArchivaConfiguration().getConfiguration();
137 NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
138 configuration.removeNetworkProxy( networkProxyConfiguration );
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 );
147 repositoryRegistry.putRepository( repo, configuration);
149 catch ( RepositoryException e )
151 log.error("Could not update repository {}", repo.getId(), e);
157 triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
159 saveConfiguration( configuration );
162 protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
164 return networkProxyConfiguration == null
166 : getModelMapper().map( networkProxyConfiguration, NetworkProxy.class );
169 protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
171 return networkProxy == null
173 : getModelMapper().map( networkProxy, NetworkProxyConfiguration.class );