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