This project has retired. For details please refer to its Attic page.
DefaultProxyConnectorAdmin xref
View Javadoc
1   package org.apache.archiva.admin.repository.proxyconnector;
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.ProxyConnector;
24  import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25  import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
26  import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorOrderComparator;
27  import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
28  import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
29  import org.apache.archiva.configuration.Configuration;
30  import org.apache.archiva.configuration.ProxyConnectorConfiguration;
31  import org.apache.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
32  import org.apache.archiva.metadata.model.facets.AuditEvent;
33  import org.apache.commons.collections.CollectionUtils;
34  import org.apache.commons.lang.StringUtils;
35  import org.springframework.stereotype.Service;
36  
37  import javax.inject.Inject;
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.HashMap;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * @author Olivier Lamy
47   * @since 1.4-M1
48   */
49  @Service ( "proxyConnectorAdmin#default" )
50  public class DefaultProxyConnectorAdmin
51      extends AbstractRepositoryAdmin
52      implements ProxyConnectorAdmin
53  {
54  
55      @Inject
56      private ManagedRepositoryAdmin managedRepositoryAdmin;
57  
58      @Inject
59      private RemoteRepositoryAdmin remoteRepositoryAdmin;
60  
61      @Override
62      public List<ProxyConnector> getProxyConnectors()
63          throws RepositoryAdminException
64      {
65          List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
66              getArchivaConfiguration().getConfiguration().getProxyConnectors();
67          List<ProxyConnector> proxyConnectors = new ArrayList<>( proxyConnectorConfigurations.size() );
68          for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
69          {
70              proxyConnectors.add( getProxyConnector( configuration ) );
71          }
72          Collections.sort( proxyConnectors, ProxyConnectorOrderComparator.getInstance() );
73          return proxyConnectors;
74      }
75  
76      @Override
77      public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
78          throws RepositoryAdminException
79      {
80          for ( ProxyConnector proxyConnector : getProxyConnectors() )
81          {
82              if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
83                  targetRepoId, proxyConnector.getTargetRepoId() ) )
84              {
85                  return proxyConnector;
86              }
87          }
88          return null;
89      }
90  
91      @Override
92      public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
93          throws RepositoryAdminException
94      {
95          if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
96          {
97              throw new RepositoryAdminException(
98                  "Unable to add proxy connector, as one already exists with source repository id ["
99                      + proxyConnector.getSourceRepoId() + "] and target repository id ["
100                     + proxyConnector.getTargetRepoId() + "]." );
101         }
102 
103         validateProxyConnector( proxyConnector );
104 
105         proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
106         proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
107 
108         Configuration configuration = getArchivaConfiguration().getConfiguration();
109 
110         ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
111         configuration.addProxyConnector( proxyConnectorConfiguration );
112         saveConfiguration( configuration );
113         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
114                            AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
115         return Boolean.TRUE;
116 
117     }
118 
119     // FIXME take care of proxyConnectorRules !
120     @Override
121     public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
122         throws RepositoryAdminException
123     {
124         Configuration configuration = getArchivaConfiguration().getConfiguration();
125         ProxyConnectorConfiguration proxyConnectorConfiguration =
126             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
127         if ( proxyConnectorConfiguration == null )
128         {
129             throw new RepositoryAdminException(
130                 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
131                     + proxyConnector.getTargetRepoId() );
132         }
133         configuration.removeProxyConnector( proxyConnectorConfiguration );
134         saveConfiguration( configuration );
135         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
136                            AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
137         return Boolean.TRUE;
138     }
139 
140     // FIXME care take of proxyConnectorRules !
141     @Override
142     public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
143         throws RepositoryAdminException
144     {
145         Configuration configuration = getArchivaConfiguration().getConfiguration();
146         ProxyConnectorConfiguration proxyConnectorConfiguration =
147             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
148         configuration.removeProxyConnector( proxyConnectorConfiguration );
149         configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
150         saveConfiguration( configuration );
151         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
152                            AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
153         return Boolean.TRUE;
154     }
155 
156     protected List<String> unescapePatterns( List<String> patterns )
157     {
158         if ( patterns != null )
159         {
160             List<String> rawPatterns = new ArrayList<>( patterns.size() );
161             for ( String pattern : patterns )
162             {
163                 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
164             }
165             return rawPatterns;
166         }
167 
168         return Collections.emptyList();
169     }
170 
171     @Override
172     public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
173         throws RepositoryAdminException
174     {
175         Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<>();
176 
177         Iterator<ProxyConnector> it = getProxyConnectors().iterator();
178         while ( it.hasNext() )
179         {
180             ProxyConnector proxyConfig = it.next();
181             String key = proxyConfig.getSourceRepoId();
182 
183             List<ProxyConnector> connectors = proxyConnectorMap.get( key );
184             if ( connectors == null )
185             {
186                 connectors = new ArrayList<>( 1 );
187                 proxyConnectorMap.put( key, connectors );
188             }
189 
190             connectors.add( proxyConfig );
191 
192             Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
193         }
194 
195         return proxyConnectorMap;
196     }
197 
198     private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
199                                                             Configuration configuration )
200     {
201         if ( StringUtils.isBlank( sourceId ) )
202         {
203             return null;
204         }
205 
206         if ( StringUtils.isBlank( targetId ) )
207         {
208             return null;
209         }
210 
211         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
212         return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
213     }
214 
215     protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
216     {
217         return proxyConnector == null
218             ? null
219             : getModelMapper().map( proxyConnector, ProxyConnectorConfiguration.class );
220     }
221 
222     protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
223     {
224         return proxyConnectorConfiguration == null
225             ? null
226             : getModelMapper().map( proxyConnectorConfiguration, ProxyConnector.class );
227     }
228 
229     protected void validateProxyConnector( ProxyConnector proxyConnector )
230         throws RepositoryAdminException
231     {
232         // validate source a Managed target a Remote
233         if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
234         {
235             throw new RepositoryAdminException(
236                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
237                     + " is not a ManagedRepository" );
238         }
239         if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
240         {
241             throw new RepositoryAdminException(
242                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
243                     + " is not a RemoteRepository" );
244         }
245 
246         // FIXME validate NetworkProxyConfiguration too when available
247     }
248 }