View Javadoc
1   package org.eclipse.aether.internal.impl.filter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.eclipse.aether.repository.RemoteRepository;
27  import org.eclipse.aether.spi.connector.ArtifactDownload;
28  import org.eclipse.aether.spi.connector.ArtifactUpload;
29  import org.eclipse.aether.spi.connector.MetadataDownload;
30  import org.eclipse.aether.spi.connector.MetadataUpload;
31  import org.eclipse.aether.spi.connector.RepositoryConnector;
32  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
33  import org.eclipse.aether.transfer.ArtifactNotFoundException;
34  import org.eclipse.aether.transfer.MetadataNotFoundException;
35  
36  import static java.util.Objects.requireNonNull;
37  
38  /**
39   * A filtering connector that filter transfers using remote repository filter and delegates to another connector.
40   *
41   * @since 1.9.0
42   */
43  public final class FilteringRepositoryConnector
44          implements RepositoryConnector
45  {
46      private final RemoteRepository remoteRepository;
47  
48      private final RepositoryConnector delegate;
49  
50      private final RemoteRepositoryFilter remoteRepositoryFilter;
51  
52      public FilteringRepositoryConnector( RemoteRepository remoteRepository,
53                                           RepositoryConnector delegate,
54                                           RemoteRepositoryFilter remoteRepositoryFilter )
55      {
56          this.remoteRepository = requireNonNull( remoteRepository );
57          this.delegate = requireNonNull( delegate );
58          this.remoteRepositoryFilter = requireNonNull( remoteRepositoryFilter );
59      }
60  
61      @Override
62      public void close()
63      {
64          delegate.close();
65      }
66  
67      @Override
68      public void get( Collection<? extends ArtifactDownload> artifactDownloads,
69                       Collection<? extends MetadataDownload> metadataDownloads )
70      {
71          List<ArtifactDownload> filteredArtifactDownloads = null;
72          if ( artifactDownloads != null && !artifactDownloads.isEmpty() )
73          {
74              filteredArtifactDownloads = new ArrayList<>( artifactDownloads.size() );
75              for ( ArtifactDownload artifactDownload : artifactDownloads )
76              {
77                  RemoteRepositoryFilter.Result result = remoteRepositoryFilter.acceptArtifact(
78                          remoteRepository, artifactDownload.getArtifact() );
79                  if ( result.isAccepted() )
80                  {
81                      filteredArtifactDownloads.add( artifactDownload );
82                  }
83                  else
84                  {
85                      artifactDownload.setException( new ArtifactNotFoundException( artifactDownload.getArtifact(),
86                              remoteRepository, result.reasoning() ) );
87                  }
88              }
89          }
90          List<MetadataDownload> filteredMetadataDownloads = null;
91          if ( metadataDownloads != null && !metadataDownloads.isEmpty() )
92          {
93              filteredMetadataDownloads = new ArrayList<>( metadataDownloads.size() );
94              for ( MetadataDownload metadataDownload : metadataDownloads )
95              {
96                  RemoteRepositoryFilter.Result result = remoteRepositoryFilter.acceptMetadata(
97                          remoteRepository, metadataDownload.getMetadata() );
98                  if ( result.isAccepted() )
99                  {
100                     filteredMetadataDownloads.add( metadataDownload );
101                 }
102                 else
103                 {
104                     metadataDownload.setException( new MetadataNotFoundException( metadataDownload.getMetadata(),
105                             remoteRepository, result.reasoning() ) );
106                 }
107             }
108         }
109         delegate.get( filteredArtifactDownloads, filteredMetadataDownloads );
110     }
111 
112     @Override
113     public void put( Collection<? extends ArtifactUpload> artifactUploads,
114                      Collection<? extends MetadataUpload> metadataUploads )
115     {
116         delegate.put( artifactUploads, metadataUploads );
117     }
118 
119     @Override
120     public String toString()
121     {
122         return "filtered(" + delegate.toString() + ")";
123     }
124 }