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