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