]> source.dussan.org Git - archiva.git/blob
b2dcdd087822cfb1195bb2f3d12607295956e218
[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.AuditInformation;
23 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
24 import org.apache.archiva.admin.repository.RepositoryAdminException;
25 import org.apache.archiva.audit.AuditEvent;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
29 import org.springframework.stereotype.Service;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35  * @author Olivier Lamy
36  * @since 1.4
37  */
38 @Service( "networkProxyAdmin#default" )
39 public class DefaultNetworkProxyAdmin
40     extends AbstractRepositoryAdmin
41     implements NetworkProxyAdmin
42 {
43
44     public List<NetworkProxy> getNetworkProxies()
45         throws RepositoryAdminException
46     {
47         List<NetworkProxy> networkProxies = new ArrayList<NetworkProxy>();
48         for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
49         {
50             networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
51         }
52         return networkProxies;
53     }
54
55     public NetworkProxy getNetworkProxy( String networkProxyId )
56         throws RepositoryAdminException
57     {
58         for ( NetworkProxy networkProxy : getNetworkProxies() )
59         {
60             if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
61             {
62                 return networkProxy;
63             }
64         }
65
66         return null;
67     }
68
69     public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
70         throws RepositoryAdminException
71     {
72         if ( networkProxy == null )
73         {
74             return;
75         }
76         if ( getNetworkProxy( networkProxy.getId() ) != null )
77         {
78             throw new RepositoryAdminException(
79                 "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
80         }
81         Configuration configuration = getArchivaConfiguration().getConfiguration();
82         configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
83
84         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
85
86         saveConfiguration( configuration );
87     }
88
89     public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
90         throws RepositoryAdminException
91     {
92         if ( networkProxy == null )
93         {
94             return;
95         }
96         if ( getNetworkProxy( networkProxy.getId() ) == null )
97         {
98             throw new RepositoryAdminException(
99                 "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
100         }
101         Configuration configuration = getArchivaConfiguration().getConfiguration();
102         NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
103         configuration.removeNetworkProxy( networkProxyConfiguration );
104         configuration.addNetworkProxy( networkProxyConfiguration );
105
106         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
107
108         saveConfiguration( configuration );
109     }
110
111     public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
112         throws RepositoryAdminException
113     {
114
115         NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
116         if ( networkProxy == null )
117         {
118             throw new RepositoryAdminException(
119                 "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
120         }
121         Configuration configuration = getArchivaConfiguration().getConfiguration();
122         NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
123         configuration.removeNetworkProxy( networkProxyConfiguration );
124
125         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
126
127         saveConfiguration( configuration );
128     }
129
130     protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
131     {
132         return networkProxyConfiguration == null
133             ? null
134             : new BeanReplicator().replicateBean( networkProxyConfiguration, NetworkProxy.class );
135     }
136
137     protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
138     {
139         return networkProxy == null
140             ? null
141             : new BeanReplicator().replicateBean( networkProxy, NetworkProxyConfiguration.class );
142     }
143 }