View Javadoc
1   package org.eclipse.aether.internal.impl;
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.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.Arrays;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import javax.inject.Named;
30  import javax.inject.Singleton;
31  
32  import org.eclipse.aether.RepositorySystemSession;
33  import org.eclipse.aether.artifact.Artifact;
34  import org.eclipse.aether.metadata.Metadata;
35  import org.eclipse.aether.repository.RemoteRepository;
36  import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
37  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
38  import org.eclipse.aether.transfer.NoRepositoryLayoutException;
39  import org.eclipse.aether.util.ConfigUtils;
40  
41  /**
42   * Provides a Maven-2 repository layout for repositories with content type {@code "default"}.
43   */
44  @Singleton
45  @Named( "maven2" )
46  public final class Maven2RepositoryLayoutFactory
47      implements RepositoryLayoutFactory
48  {
49  
50      static final String CONFIG_PROP_SIGNATURE_CHECKSUMS = "aether.checksums.forSignature";
51      static final String CONFIG_PROP_CHECKSUMS_ALGORITHMS = "aether.checksums.algorithms";
52  
53      static final String DEFAULT_CHECKSUMS_ALGORITHMS = "SHA-1,MD5";
54  
55      private float priority;
56  
57      public float getPriority()
58      {
59          return priority;
60      }
61  
62      /**
63       * Sets the priority of this component.
64       *
65       * @param priority The priority.
66       * @return This component for chaining, never {@code null}.
67       */
68      public Maven2RepositoryLayoutFactory setPriority( float priority )
69      {
70          this.priority = priority;
71          return this;
72      }
73  
74      public RepositoryLayout newInstance( RepositorySystemSession session, RemoteRepository repository )
75          throws NoRepositoryLayoutException
76      {
77          if ( !"default".equals( repository.getContentType() ) )
78          {
79              throw new NoRepositoryLayoutException( repository );
80          }
81          boolean forSignature = ConfigUtils.getBoolean( session, false, CONFIG_PROP_SIGNATURE_CHECKSUMS );
82          List<String> checksumsAlgorithms = Arrays.asList( ConfigUtils.getString( session,
83                  DEFAULT_CHECKSUMS_ALGORITHMS, CONFIG_PROP_CHECKSUMS_ALGORITHMS ).split( "," ) );
84  
85          return forSignature
86                  ? new Maven2RepositoryLayout( checksumsAlgorithms )
87                  : new Maven2RepositoryLayoutEx( checksumsAlgorithms );
88      }
89  
90      private static class Maven2RepositoryLayout
91          implements RepositoryLayout
92      {
93  
94          private final List<String> checksumsAlgorithms;
95  
96          protected Maven2RepositoryLayout( List<String> checksumsAlgorithms )
97          {
98              this.checksumsAlgorithms = checksumsAlgorithms;
99          }
100 
101         private URI toUri( String path )
102         {
103             try
104             {
105                 return new URI( null, null, path, null );
106             }
107             catch ( URISyntaxException e )
108             {
109                 throw new IllegalStateException( e );
110             }
111         }
112 
113         public URI getLocation( Artifact artifact, boolean upload )
114         {
115             StringBuilder path = new StringBuilder( 128 );
116 
117             path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
118 
119             path.append( artifact.getArtifactId() ).append( '/' );
120 
121             path.append( artifact.getBaseVersion() ).append( '/' );
122 
123             path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
124 
125             if ( artifact.getClassifier().length() > 0 )
126             {
127                 path.append( '-' ).append( artifact.getClassifier() );
128             }
129 
130             if ( artifact.getExtension().length() > 0 )
131             {
132                 path.append( '.' ).append( artifact.getExtension() );
133             }
134 
135             return toUri( path.toString() );
136         }
137 
138         public URI getLocation( Metadata metadata, boolean upload )
139         {
140             StringBuilder path = new StringBuilder( 128 );
141 
142             if ( metadata.getGroupId().length() > 0 )
143             {
144                 path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
145 
146                 if ( metadata.getArtifactId().length() > 0 )
147                 {
148                     path.append( metadata.getArtifactId() ).append( '/' );
149 
150                     if ( metadata.getVersion().length() > 0 )
151                     {
152                         path.append( metadata.getVersion() ).append( '/' );
153                     }
154                 }
155             }
156 
157             path.append( metadata.getType() );
158 
159             return toUri( path.toString() );
160         }
161 
162         public List<Checksum> getChecksums( Artifact artifact, boolean upload, URI location )
163         {
164             return getChecksums( location );
165         }
166 
167         public List<Checksum> getChecksums( Metadata metadata, boolean upload, URI location )
168         {
169             return getChecksums( location );
170         }
171 
172         private List<Checksum> getChecksums( URI location )
173         {
174             List<Checksum> checksums = new ArrayList<>( checksumsAlgorithms.size() );
175             for ( String algorithm : checksumsAlgorithms )
176             {
177                 checksums.add( Checksum.forLocation( location, algorithm ) );
178             }
179             return checksums;
180         }
181 
182     }
183 
184     private static class Maven2RepositoryLayoutEx
185         extends Maven2RepositoryLayout
186     {
187 
188         protected Maven2RepositoryLayoutEx( List<String> checksumsAlgorithms )
189         {
190             super( checksumsAlgorithms );
191         }
192 
193         @Override
194         public List<Checksum> getChecksums( Artifact artifact, boolean upload, URI location )
195         {
196             if ( isSignature( artifact.getExtension() ) )
197             {
198                 return Collections.emptyList();
199             }
200             return super.getChecksums( artifact, upload, location );
201         }
202 
203         private boolean isSignature( String extension )
204         {
205             return extension.endsWith( ".asc" );
206         }
207 
208     }
209 
210 }