This project has retired. For details please refer to its Attic page.
DefaultNetworkProxyAdmin xref
View Javadoc
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.configuration.RemoteRepositoryConfiguration;
29  import org.apache.archiva.metadata.model.facets.AuditEvent;
30  import org.apache.commons.lang.StringUtils;
31  import org.springframework.stereotype.Service;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * @author Olivier Lamy
38   * @since 1.4-M1
39   */
40  @Service( "networkProxyAdmin#default" )
41  public class DefaultNetworkProxyAdmin
42      extends AbstractRepositoryAdmin
43      implements NetworkProxyAdmin
44  {
45  
46      @Override
47      public List<NetworkProxy> getNetworkProxies()
48          throws RepositoryAdminException
49      {
50          List<NetworkProxy> networkProxies =
51              new ArrayList<>( getArchivaConfiguration().getConfiguration().getNetworkProxies().size() );
52          for ( NetworkProxyConfiguration networkProxyConfiguration : getArchivaConfiguration().getConfiguration().getNetworkProxies() )
53          {
54              networkProxies.add( getNetworkProxy( networkProxyConfiguration ) );
55          }
56          return networkProxies;
57      }
58  
59      @Override
60      public NetworkProxy getNetworkProxy( String networkProxyId )
61          throws RepositoryAdminException
62      {
63          for ( NetworkProxy networkProxy : getNetworkProxies() )
64          {
65              if ( StringUtils.equals( networkProxyId, networkProxy.getId() ) )
66              {
67                  return networkProxy;
68              }
69          }
70  
71          return null;
72      }
73  
74      @Override
75      public void addNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
76          throws RepositoryAdminException
77      {
78          if ( networkProxy == null )
79          {
80              return;
81          }
82          if ( getNetworkProxy( networkProxy.getId() ) != null )
83          {
84              throw new RepositoryAdminException(
85                  "cannot add NetworkProxy with id " + networkProxy.getId() + " already exist" );
86          }
87          Configuration configuration = getArchivaConfiguration().getConfiguration();
88          configuration.addNetworkProxy( getNetworkProxyConfiguration( networkProxy ) );
89  
90          triggerAuditEvent( networkProxy.getId(), null, AuditEvent.ADD_NETWORK_PROXY, auditInformation );
91  
92          saveConfiguration( configuration );
93      }
94  
95      @Override
96      public void updateNetworkProxy( NetworkProxy networkProxy, AuditInformation auditInformation )
97          throws RepositoryAdminException
98      {
99          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 }