001    package 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    
021    import net.sf.beanlib.provider.replicator.BeanReplicator;
022    import org.apache.archiva.admin.model.AuditInformation;
023    import org.apache.archiva.admin.model.RepositoryAdminException;
024    import org.apache.archiva.admin.model.beans.NetworkProxy;
025    import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
026    import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
027    import org.apache.archiva.audit.AuditEvent;
028    import org.apache.archiva.configuration.Configuration;
029    import org.apache.archiva.configuration.NetworkProxyConfiguration;
030    import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
031    import org.apache.commons.lang.StringUtils;
032    import org.springframework.stereotype.Service;
033    
034    import java.util.ArrayList;
035    import java.util.List;
036    
037    /**
038     * @author Olivier Lamy
039     * @since 1.4-M1
040     */
041    @Service( "networkProxyAdmin#default" )
042    public class DefaultNetworkProxyAdmin
043        extends AbstractRepositoryAdmin
044        implements NetworkProxyAdmin
045    {
046    
047        public List<NetworkProxy> getNetworkProxies()
048            throws RepositoryAdminException
049        {
050            List<NetworkProxy> networkProxies =
051                new ArrayList<NetworkProxy>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
052            for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
053            {
054                networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
055            }
056            return networkProxies;
057        }
058    
059        public NetworkProxy getNetworkProxy( String networkProxyId )
060            throws RepositoryAdminException
061        {
062            for ( NetworkProxy networkProxy : getNetworkProxies() )
063            {
064                if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
065                {
066                    return networkProxy;
067                }
068            }
069    
070            return null;
071        }
072    
073        public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
074            throws RepositoryAdminException
075        {
076            if ( networkProxy == null )
077            {
078                return;
079            }
080            if ( getNetworkProxy( networkProxy.getId() ) != null )
081            {
082                throw new RepositoryAdminException(
083                    "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
084            }
085            Configuration configuration = getArchivaConfiguration().getConfiguration();
086            configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
087    
088            triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
089    
090            saveConfiguration( configuration );
091        }
092    
093        public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
094            throws RepositoryAdminException
095        {
096            if ( networkProxy == null )
097            {
098                return;
099            }
100            if ( getNetworkProxy( networkProxy.getId() ) == null )
101            {
102                throw new RepositoryAdminException(
103                    "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
104            }
105            Configuration configuration = getArchivaConfiguration().getConfiguration();
106            NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
107            configuration.removeNetworkProxy( networkProxyConfiguration );
108            configuration.addNetworkProxy( networkProxyConfiguration );
109    
110            triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
111    
112            saveConfiguration( configuration );
113        }
114    
115        public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
116            throws RepositoryAdminException
117        {
118    
119            NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
120            if ( networkProxy == null )
121            {
122                throw new RepositoryAdminException(
123                    "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
124            }
125            Configuration configuration = getArchivaConfiguration().getConfiguration();
126            NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
127            configuration.removeNetworkProxy( networkProxyConfiguration );
128    
129            for ( RemoteRepositoryConfiguration rrc : configuration.getRemoteRepositories() )
130            {
131                if ( StringUtils.equals( rrc.getRemoteDownloadNetworkProxyId(), networkProxyId ) )
132                {
133                    rrc.setRemoteDownloadNetworkProxyId( null );
134                }
135            }
136    
137            triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
138    
139            saveConfiguration( configuration );
140        }
141    
142        protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
143        {
144            return networkProxyConfiguration == null
145                ? null
146                : new BeanReplicator().replicateBean( networkProxyConfiguration, NetworkProxy.class );
147        }
148    
149        protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
150        {
151            return networkProxy == null
152                ? null
153                : new BeanReplicator().replicateBean( networkProxy, NetworkProxyConfiguration.class );
154        }
155    }