]> source.dussan.org Git - archiva.git/blob
5c03abc1f3c725a0ab324b44448ff5b3575be36c
[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 org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.NetworkProxy;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.NetworkProxyConfiguration;
28 import org.apache.archiva.metadata.model.facets.AuditEvent;
29 import org.apache.archiva.repository.RemoteRepository;
30 import org.apache.archiva.repository.RepositoryException;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.features.RemoteIndexFeature;
33 import org.apache.commons.lang3.StringUtils;
34 import org.springframework.stereotype.Service;
35
36 import javax.inject.Inject;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41  * @author Olivier Lamy
42  * @since 1.4-M1
43  */
44 @Service( "networkProxyAdmin#default" )
45 public class DefaultNetworkProxyAdmin
46     extends AbstractRepositoryAdmin
47     implements NetworkProxyAdmin
48 {
49
50     @Inject
51     RepositoryRegistry repositoryRegistry;
52
53     @Override
54     public List<NetworkProxy> getNetworkProxies()
55         throws RepositoryAdminException
56     {
57         List<NetworkProxy> networkProxies =
58             new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
59         for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
60         {
61             networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
62         }
63         return networkProxies;
64     }
65
66     @Override
67     public NetworkProxy getNetworkProxy( String networkProxyId )
68         throws RepositoryAdminException
69     {
70         for ( NetworkProxy networkProxy : getNetworkProxies() )
71         {
72             if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
73             {
74                 return networkProxy;
75             }
76         }
77
78         return null;
79     }
80
81     @Override
82     public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
83         throws RepositoryAdminException
84     {
85         if ( networkProxy == null )
86         {
87             return;
88         }
89         if ( getNetworkProxy( networkProxy.getId() ) != null )
90         {
91             throw new RepositoryAdminException(
92                 "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
93         }
94         Configuration configuration = getArchivaConfiguration().getConfiguration();
95         configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
96
97         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
98
99         saveConfiguration( configuration );
100     }
101
102     @Override
103     public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
104         throws RepositoryAdminException
105     {
106         if ( networkProxy == null )
107         {
108             return;
109         }
110         if ( getNetworkProxy( networkProxy.getId() ) == null )
111         {
112             throw new RepositoryAdminException(
113                 "cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist" );
114         }
115         Configuration configuration = getArchivaConfiguration().getConfiguration();
116         NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
117         configuration.removeNetworkProxy( networkProxyConfiguration );
118         configuration.addNetworkProxy( networkProxyConfiguration );
119
120         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation );
121
122         saveConfiguration( configuration );
123     }
124
125     @Override
126     public void deleteNetworkProxy( String networkProxyId, AuditInformation auditInformation )
127         throws RepositoryAdminException
128     {
129
130         NetworkProxy networkProxy = getNetworkProxy( networkProxyId );
131         if ( networkProxy == null )
132         {
133             throw new RepositoryAdminException(
134                 "cannot delete NetworkProxy with id " + networkProxyId + " as not exist" );
135         }
136         Configuration configuration = getArchivaConfiguration().getConfiguration();
137         NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration( networkProxy );
138         configuration.removeNetworkProxy( networkProxyConfiguration );
139
140         for ( RemoteRepository repo : repositoryRegistry.getRemoteRepositories()) {
141             if (repo.supportsFeature( RemoteIndexFeature.class )) {
142                 RemoteIndexFeature rif = repo.getFeature( RemoteIndexFeature.class ).get();
143                 if (networkProxyId.equals(rif.getProxyId())) {
144                     rif.setProxyId( null );
145                     try
146                     {
147                         repositoryRegistry.putRepository( repo, configuration);
148                     }
149                     catch ( RepositoryException e )
150                     {
151                         log.error("Could not update repository {}", repo.getId(), e);
152                     }
153                 }
154             }
155         }
156
157         triggerAuditEvent( networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation );
158
159         saveConfiguration( configuration );
160     }
161
162     protected NetworkProxy getNetworkProxy( NetworkProxyConfiguration networkProxyConfiguration )
163     {
164         return networkProxyConfiguration == null
165             ? null
166             : getModelMapper().map( networkProxyConfiguration, NetworkProxy.class );
167     }
168
169     protected NetworkProxyConfiguration getNetworkProxyConfiguration( NetworkProxy networkProxy )
170     {
171         return networkProxy == null
172             ? null
173             : getModelMapper().map( networkProxy, NetworkProxyConfiguration.class );
174     }
175 }