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