]> source.dussan.org Git - archiva.git/blob
897bd5c7a9b011253441c0905136953f58dda91a
[archiva.git] /
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
21 import net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.model.AuditInformation;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
25 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
26 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
27 import org.apache.archiva.audit.AuditEvent;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.NetworkProxyConfiguration;
30 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
31 import org.apache.commons.lang.StringUtils;
32 import org.springframework.stereotype.Service;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38  * @author Olivier Lamy
39  * @since 1.4-M1
40  */
41 @Service( "networkProxyAdmin#default" )
42 public class DefaultNetworkProxyAdmin
43     extends AbstractRepositoryAdmin
44     implements NetworkProxyAdmin
45 {
46
47     public List<NetworkProxy> getNetworkProxies()
48         throws RepositoryAdminException
49     {
50         List<NetworkProxy> networkProxies =
51             new ArrayList<NetworkProxy>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
52         for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
53         {
54             networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
55         }
56         return networkProxies;
57     }
58
59     public NetworkProxy getNetworkProxy( String networkProxyId )
60         throws RepositoryAdminException
61     {
62         for ( NetworkProxy networkProxy : getNetworkProxies() )
63         {
64             if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
65             {
66                 return networkProxy;
67             }
68         }
69
70         return null;
71     }
72
73     public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
74         throws RepositoryAdminException
75     {
76         if ( networkProxy == null )
77         {
78             return;
79         }
80         if ( getNetworkProxy( networkProxy.getId() ) != null )
81         {
82             throw new RepositoryAdminException(
83                 "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
84         }
85         Configuration configuration = getArchivaConfiguration().getConfiguration();
86         configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
87
88         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
89
90         saveConfiguration( configuration );
91     }
92
93     public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
94         throws RepositoryAdminException
95     {
96         if ( networkProxy == null )
97         {
98             return;
99         }
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 }